1 // FlightPlan.cxx - flight plan object
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 Curtis L. Olson
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "FlightPlan.hxx"
33 #include <boost/algorithm/string/case_conv.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <boost/foreach.hpp>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/misc/sg_path.hxx>
40 #include <simgear/magvar/magvar.hxx>
41 #include <simgear/timing/sg_time.hxx>
42 #include <simgear/misc/sgstream.hxx>
43 #include <simgear/misc/strutils.hxx>
44 #include <simgear/props/props_io.hxx>
47 #include <Main/globals.hxx>
48 #include "Main/fg_props.hxx"
49 #include <Navaids/procedure.hxx>
50 #include <Navaids/waypoint.hxx>
57 namespace flightgear {
59 typedef std::vector<FlightPlan::DelegateFactory*> FPDelegateFactoryVec;
60 static FPDelegateFactoryVec static_delegateFactories;
62 FlightPlan::FlightPlan() :
65 _departureRunway(NULL),
66 _destinationRunway(NULL),
72 _departureChanged = _arrivalChanged = _waypointsChanged = _currentWaypointChanged = false;
74 BOOST_FOREACH(DelegateFactory* factory, static_delegateFactories) {
75 Delegate* d = factory->createFlightPlanDelegate(this);
76 if (d) { // factory might not always create a delegate
77 d->_deleteWithPlan = true;
83 FlightPlan::~FlightPlan()
85 // delete all delegates which we own.
86 Delegate* d = _delegate;
90 if (cur->_deleteWithPlan) {
96 FlightPlan* FlightPlan::clone(const string& newIdent) const
98 FlightPlan* c = new FlightPlan();
99 c->_ident = newIdent.empty() ? _ident : newIdent;
102 // copy destination / departure data.
103 c->setDeparture(_departure);
104 c->setDeparture(_departureRunway);
107 c->setApproach(_approach);
108 } else if (_destinationRunway) {
109 c->setDestination(_destinationRunway);
110 } else if (_destination) {
111 c->setDestination(_destination);
118 c->_waypointsChanged = true;
119 for (int l=0; l < numLegs(); ++l) {
120 c->_legs.push_back(_legs[l]->cloneFor(c));
126 void FlightPlan::setIdent(const string& s)
131 string FlightPlan::ident() const
136 FlightPlan::Leg* FlightPlan::insertWayptAtIndex(Waypt* aWpt, int aIndex)
146 if ((aIndex == -1) || (aIndex > (int) _legs.size())) {
147 index = _legs.size();
150 insertWayptsAtIndex(wps, index);
151 return legAtIndex(index);
154 void FlightPlan::insertWayptsAtIndex(const WayptVec& wps, int aIndex)
161 if ((aIndex == -1) || (aIndex > (int) _legs.size())) {
162 index = _legs.size();
165 LegVec::iterator it = _legs.begin();
168 int endIndex = index + wps.size() - 1;
169 if (_currentIndex >= endIndex) {
170 _currentIndex += wps.size();
174 BOOST_FOREACH(WayptRef wp, wps) {
175 newLegs.push_back(new Leg(this, wp));
179 _waypointsChanged = true;
180 _legs.insert(it, newLegs.begin(), newLegs.end());
184 void FlightPlan::deleteIndex(int aIndex)
187 if (aIndex < 0) { // negative indices count the the end
188 index = _legs.size() + index;
191 if ((index < 0) || (index >= numLegs())) {
192 SG_LOG(SG_NAVAID, SG_WARN, "removeAtIndex with invalid index:" << aIndex);
197 _waypointsChanged = true;
199 LegVec::iterator it = _legs.begin();
205 if (_currentIndex == index) {
206 // current waypoint was removed
207 _currentWaypointChanged = true;
208 } else if (_currentIndex > index) {
209 --_currentIndex; // shift current index down if necessary
215 void FlightPlan::clear()
218 _waypointsChanged = true;
219 _currentWaypointChanged = true;
220 _arrivalChanged = true;
221 _departureChanged = true;
224 BOOST_FOREACH(Leg* l, _legs) {
235 RemoveWithFlag(WayptFlag f) : flag(f), delCount(0) { }
237 int numDeleted() const { return delCount; }
239 bool operator()(FlightPlan::Leg* leg) const
241 if (leg->waypoint()->flag(flag)) {
251 mutable int delCount;
254 int FlightPlan::clearWayptsWithFlag(WayptFlag flag)
257 // first pass, fix up currentIndex
258 for (int i=0; i<_currentIndex; ++i) {
260 if (l->waypoint()->flag(flag)) {
265 _currentIndex -= count;
267 // now delete and remove
268 RemoveWithFlag rf(flag);
269 LegVec::iterator it = std::remove_if(_legs.begin(), _legs.end(), rf);
270 if (it == _legs.end()) {
271 return 0; // nothing was cleared, don't fire the delegate
275 _waypointsChanged = true;
277 _currentWaypointChanged = true;
280 _legs.erase(it, _legs.end());
282 return rf.numDeleted();
285 void FlightPlan::setCurrentIndex(int index)
287 if ((index < -1) || (index >= numLegs())) {
288 throw sg_range_exception("invalid leg index", "FlightPlan::setCurrentIndex");
291 if (index == _currentIndex) {
296 _currentIndex = index;
297 _currentWaypointChanged = true;
301 int FlightPlan::findWayptIndex(const SGGeod& aPos) const
303 for (int i=0; i<numLegs(); ++i) {
304 if (_legs[i]->waypoint()->matches(aPos)) {
312 FlightPlan::Leg* FlightPlan::currentLeg() const
314 if ((_currentIndex < 0) || (_currentIndex >= numLegs()))
316 return legAtIndex(_currentIndex);
319 FlightPlan::Leg* FlightPlan::previousLeg() const
321 if (_currentIndex <= 0) {
325 return legAtIndex(_currentIndex - 1);
328 FlightPlan::Leg* FlightPlan::nextLeg() const
330 if ((_currentIndex < 0) || ((_currentIndex + 1) >= numLegs())) {
334 return legAtIndex(_currentIndex + 1);
337 FlightPlan::Leg* FlightPlan::legAtIndex(int index) const
339 if ((index < 0) || (index >= numLegs())) {
340 throw sg_range_exception("index out of range", "FlightPlan::legAtIndex");
346 int FlightPlan::findLegIndex(const Leg *l) const
348 for (unsigned int i=0; i<_legs.size(); ++i) {
357 void FlightPlan::setDeparture(FGAirport* apt)
359 if (apt == _departure) {
364 _departureChanged = true;
366 _departureRunway = NULL;
371 void FlightPlan::setDeparture(FGRunway* rwy)
373 if (_departureRunway == rwy) {
378 _departureChanged = true;
380 _departureRunway = rwy;
381 if (rwy->airport() != _departure) {
382 _departure = rwy->airport();
388 void FlightPlan::setSID(SID* sid, const std::string& transition)
395 _departureChanged = true;
397 _sidTransition = transition;
401 void FlightPlan::setSID(Transition* trans)
408 if (trans->parent()->type() != PROCEDURE_SID)
409 throw sg_exception("FlightPlan::setSID: transition does not belong to a SID");
411 setSID((SID*) trans->parent(), trans->ident());
414 Transition* FlightPlan::sidTransition() const
416 if (!_sid || _sidTransition.empty()) {
420 return _sid->findTransitionByName(_sidTransition);
423 void FlightPlan::setDestination(FGAirport* apt)
425 if (apt == _destination) {
430 _arrivalChanged = true;
432 _destinationRunway = NULL;
433 setSTAR((STAR*)NULL);
438 void FlightPlan::setDestination(FGRunway* rwy)
440 if (_destinationRunway == rwy) {
445 _arrivalChanged = true;
446 _destinationRunway = rwy;
447 if (_destination != rwy->airport()) {
448 _destination = rwy->airport();
449 setSTAR((STAR*)NULL);
455 void FlightPlan::setSTAR(STAR* star, const std::string& transition)
462 _arrivalChanged = true;
464 _starTransition = transition;
468 void FlightPlan::setSTAR(Transition* trans)
471 setSTAR((STAR*) NULL);
475 if (trans->parent()->type() != PROCEDURE_STAR)
476 throw sg_exception("FlightPlan::setSTAR: transition does not belong to a STAR");
478 setSTAR((STAR*) trans->parent(), trans->ident());
481 Transition* FlightPlan::starTransition() const
483 if (!_star || _starTransition.empty()) {
487 return _star->findTransitionByName(_starTransition);
490 void FlightPlan::setApproach(flightgear::Approach *app)
492 if (_approach == app) {
497 _arrivalChanged = true;
500 // keep runway + airport in sync
501 if (_destinationRunway != _approach->runway()) {
502 _destinationRunway = _approach->runway();
505 if (_destination != _destinationRunway->airport()) {
506 _destination = _destinationRunway->airport();
512 bool FlightPlan::save(const SGPath& path)
514 SG_LOG(SG_NAVAID, SG_INFO, "Saving route to " << path.str());
516 SGPropertyNode_ptr d(new SGPropertyNode);
517 d->setIntValue("version", 2);
520 d->setStringValue("departure/airport", _departure->ident());
522 d->setStringValue("departure/sid", _sid->ident());
525 if (_departureRunway) {
526 d->setStringValue("departure/runway", _departureRunway->ident());
531 d->setStringValue("destination/airport", _destination->ident());
533 d->setStringValue("destination/star", _star->ident());
537 d->setStringValue("destination/approach", _approach->ident());
540 //d->setStringValue("destination/transition", destination->getStringValue("transition"));
542 if (_destinationRunway) {
543 d->setStringValue("destination/runway", _destinationRunway->ident());
548 SGPropertyNode* routeNode = d->getChild("route", 0, true);
549 for (unsigned int i=0; i<_legs.size(); ++i) {
550 Waypt* wpt = _legs[i]->waypoint();
551 wpt->saveAsNode(routeNode->getChild("wp", i, true));
552 } // of waypoint iteration
553 writeProperties(path.str(), d, true /* write-all */);
555 } catch (sg_exception& e) {
556 SG_LOG(SG_NAVAID, SG_ALERT, "Failed to save flight-plan '" << path.str() << "'. " << e.getMessage());
561 bool FlightPlan::load(const SGPath& path)
565 SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << path.str()
566 << "'. The file does not exist.");
570 SGPropertyNode_ptr routeData(new SGPropertyNode);
571 SG_LOG(SG_NAVAID, SG_INFO, "going to read flight-plan from:" << path.str());
576 readProperties(path.str(), routeData);
577 } catch (sg_exception& ) {
578 // if XML parsing fails, the file might be simple textual list of waypoints
579 Status = loadPlainTextRoute(path);
581 _waypointsChanged = true;
584 if (routeData.valid())
587 int version = routeData->getIntValue("version", 1);
589 loadVersion1XMLRoute(routeData);
590 } else if (version == 2) {
591 loadVersion2XMLRoute(routeData);
593 throw sg_io_exception("unsupported XML route version");
596 } catch (sg_exception& e) {
597 SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load flight-plan '" << e.getOrigin()
598 << "'. " << e.getMessage());
607 void FlightPlan::loadXMLRouteHeader(SGPropertyNode_ptr routeData)
610 SGPropertyNode* dep = routeData->getChild("departure");
612 string depIdent = dep->getStringValue("airport");
613 setDeparture((FGAirport*) fgFindAirportID(depIdent));
615 string rwy(dep->getStringValue("runway"));
616 if (_departure->hasRunwayWithIdent(rwy)) {
617 setDeparture(_departure->getRunwayByIdent(rwy));
620 if (dep->hasChild("sid")) {
621 setSID(_departure->findSIDWithIdent(dep->getStringValue("sid")));
623 // departure->setStringValue("transition", dep->getStringValue("transition"));
628 SGPropertyNode* dst = routeData->getChild("destination");
630 setDestination((FGAirport*) fgFindAirportID(dst->getStringValue("airport")));
632 string rwy(dst->getStringValue("runway"));
633 if (_destination->hasRunwayWithIdent(rwy)) {
634 setDestination(_destination->getRunwayByIdent(rwy));
637 if (dst->hasChild("star")) {
638 setSTAR(_destination->findSTARWithIdent(dst->getStringValue("star")));
641 if (dst->hasChild("approach")) {
642 setApproach(_destination->findApproachWithIdent(dst->getStringValue("approach")));
646 // destination->setStringValue("transition", dst->getStringValue("transition"));
650 SGPropertyNode* alt = routeData->getChild("alternate");
652 //alternate->setStringValue(alt->getStringValue("airport"));
656 SGPropertyNode* crs = routeData->getChild("cruise");
658 // cruise->setDoubleValue("speed-kts", crs->getDoubleValue("speed-kts"));
659 // cruise->setDoubleValue("mach", crs->getDoubleValue("mach"));
660 // cruise->setDoubleValue("altitude-ft", crs->getDoubleValue("altitude-ft"));
661 } // of cruise data loading
665 void FlightPlan::loadVersion2XMLRoute(SGPropertyNode_ptr routeData)
667 loadXMLRouteHeader(routeData);
671 SGPropertyNode_ptr routeNode = routeData->getChild("route", 0);
672 for (int i=0; i<routeNode->nChildren(); ++i) {
673 SGPropertyNode_ptr wpNode = routeNode->getChild("wp", i);
674 Leg* l = new Leg(this, Waypt::createFromProperties(NULL, wpNode));
676 } // of route iteration
677 _waypointsChanged = true;
680 void FlightPlan::loadVersion1XMLRoute(SGPropertyNode_ptr routeData)
682 loadXMLRouteHeader(routeData);
686 SGPropertyNode_ptr routeNode = routeData->getChild("route", 0);
687 for (int i=0; i<routeNode->nChildren(); ++i) {
688 SGPropertyNode_ptr wpNode = routeNode->getChild("wp", i);
689 Leg* l = new Leg(this, parseVersion1XMLWaypt(wpNode));
691 } // of route iteration
692 _waypointsChanged = true;
695 WayptRef FlightPlan::parseVersion1XMLWaypt(SGPropertyNode* aWP)
698 if (!_legs.empty()) {
699 lastPos = _legs.back()->waypoint()->position();
700 } else if (_departure) {
701 lastPos = _departure->geod();
705 string ident(aWP->getStringValue("ident"));
706 if (aWP->hasChild("longitude-deg")) {
707 // explicit longitude/latitude
708 w = new BasicWaypt(SGGeod::fromDeg(aWP->getDoubleValue("longitude-deg"),
709 aWP->getDoubleValue("latitude-deg")), ident, NULL);
712 string nid = aWP->getStringValue("navid", ident.c_str());
713 FGPositionedRef p = FGPositioned::findClosestWithIdent(nid, lastPos);
719 SG_LOG(SG_GENERAL, SG_WARN, "unknown navaid in flightplan:" << nid);
720 pos = SGGeod::fromDeg(aWP->getDoubleValue("longitude-deg"),
721 aWP->getDoubleValue("latitude-deg"));
724 if (aWP->hasChild("offset-nm") && aWP->hasChild("offset-radial")) {
725 double radialDeg = aWP->getDoubleValue("offset-radial");
726 // convert magnetic radial to a true radial!
727 radialDeg += magvarDegAt(pos);
728 double offsetNm = aWP->getDoubleValue("offset-nm");
730 SGGeodesy::direct(pos, radialDeg, offsetNm * SG_NM_TO_METER, pos, az2);
733 w = new BasicWaypt(pos, ident, NULL);
736 double altFt = aWP->getDoubleValue("altitude-ft", -9999.9);
737 if (altFt > -9990.0) {
738 w->setAltitude(altFt, RESTRICT_AT);
744 bool FlightPlan::loadPlainTextRoute(const SGPath& path)
747 sg_gzifstream in(path.str().c_str());
749 throw sg_io_exception("Cannot open file for reading.");
755 getline(in, line, '\n');
756 // trim CR from end of line, if found
757 if (line[line.size() - 1] == '\r') {
758 line.erase(line.size() - 1, 1);
761 line = simgear::strutils::strip(line);
762 if (line.empty() || (line[0] == '#')) {
763 continue; // ignore empty/comment lines
766 WayptRef w = waypointFromString(line);
768 throw sg_io_exception("Failed to create waypoint from line '" + line + "'.");
771 _legs.push_back(new Leg(this, w));
772 } // of line iteration
773 } catch (sg_exception& e) {
774 SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load route from: '" << path.str() << "'. " << e.getMessage());
782 double FlightPlan::magvarDegAt(const SGGeod& pos) const
784 double jd = globals->get_time_params()->getJD();
785 return sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
788 WayptRef FlightPlan::waypointFromString(const string& tgt )
790 string target(boost::to_upper_copy(tgt));
795 RouteRestriction altSetting = RESTRICT_NONE;
797 size_t pos = target.find( '@' );
798 if ( pos != string::npos ) {
799 altFt = atof( target.c_str() + pos + 1 );
800 target = target.substr( 0, pos );
801 if ( !strcmp(fgGetString("/sim/startup/units"), "meter") )
802 altFt *= SG_METER_TO_FEET;
803 altSetting = RESTRICT_AT;
807 pos = target.find( ',' );
808 if ( pos != string::npos ) {
809 double lon = atof( target.substr(0, pos).c_str());
810 double lat = atof( target.c_str() + pos + 1);
812 char ew = (lon < 0.0) ? 'W' : 'E';
813 char ns = (lat < 0.0) ? 'S' : 'N';
814 snprintf(buf, 32, "%c%03d%c%03d", ew, (int) fabs(lon), ns, (int)fabs(lat));
816 wpt = new BasicWaypt(SGGeod::fromDeg(lon, lat), buf, NULL);
817 if (altSetting != RESTRICT_NONE) {
818 wpt->setAltitude(altFt, altSetting);
825 // route is empty, use current position
826 basePosition = globals->get_aircraft_position();
828 basePosition = _legs.back()->waypoint()->position();
831 string_list pieces(simgear::strutils::split(target, "/"));
832 FGPositionedRef p = FGPositioned::findClosestWithIdent(pieces.front(), basePosition);
834 SG_LOG( SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces.front());
838 double magvar = magvarDegAt(basePosition);
840 if (pieces.size() == 1) {
841 wpt = new NavaidWaypoint(p, NULL);
842 } else if (pieces.size() == 3) {
843 // navaid/radial/distance-nm notation
844 double radial = atof(pieces[1].c_str()),
845 distanceNm = atof(pieces[2].c_str());
847 wpt = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
848 } else if (pieces.size() == 2) {
849 FGAirport* apt = dynamic_cast<FGAirport*>(p.ptr());
851 SG_LOG(SG_NAVAID, SG_INFO, "Waypoint is not an airport:" << pieces.front());
855 if (!apt->hasRunwayWithIdent(pieces[1])) {
856 SG_LOG(SG_NAVAID, SG_INFO, "No runway: " << pieces[1] << " at " << pieces[0]);
860 FGRunway* runway = apt->getRunwayByIdent(pieces[1]);
861 wpt = new NavaidWaypoint(runway, NULL);
862 } else if (pieces.size() == 4) {
863 // navid/radial/navid/radial notation
864 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(pieces[2], basePosition);
866 SG_LOG( SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces[2]);
870 double r1 = atof(pieces[1].c_str()),
871 r2 = atof(pieces[3].c_str());
876 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
878 SG_LOG(SG_NAVAID, SG_INFO, "no valid intersection for:" << target);
882 std::string name = p->ident() + "-" + p2->ident();
883 wpt = new BasicWaypt(intersection, name, NULL);
887 SG_LOG(SG_NAVAID, SG_INFO, "Unable to parse waypoint:" << target);
891 if (altSetting != RESTRICT_NONE) {
892 wpt->setAltitude(altFt, altSetting);
897 FlightPlan::Leg::Leg(FlightPlan* owner, WayptRef wpt) :
899 _speedRestrict(RESTRICT_NONE),
900 _altRestrict(RESTRICT_NONE),
904 throw sg_exception("can't create FlightPlan::Leg without underlying waypoint");
906 _speed = _altitudeFt = 0;
909 FlightPlan::Leg* FlightPlan::Leg::cloneFor(FlightPlan* owner) const
911 Leg* c = new Leg(owner, _waypt);
914 c->_speedRestrict = _speedRestrict;
915 c->_altitudeFt = _altitudeFt;
916 c->_altRestrict = _altRestrict;
921 FlightPlan::Leg* FlightPlan::Leg::nextLeg() const
923 return _parent->legAtIndex(index() + 1);
926 unsigned int FlightPlan::Leg::index() const
928 return _parent->findLegIndex(this);
931 int FlightPlan::Leg::altitudeFt() const
933 if (_altRestrict != RESTRICT_NONE) {
937 return _waypt->altitudeFt();
940 int FlightPlan::Leg::speed() const
942 if (_speedRestrict != RESTRICT_NONE) {
946 return _waypt->speed();
949 int FlightPlan::Leg::speedKts() const
954 double FlightPlan::Leg::speedMach() const
956 if (!isMachRestrict(_speedRestrict)) {
960 return -(_speed / 100.0);
963 RouteRestriction FlightPlan::Leg::altitudeRestriction() const
965 if (_altRestrict != RESTRICT_NONE) {
969 return _waypt->altitudeRestriction();
972 RouteRestriction FlightPlan::Leg::speedRestriction() const
974 if (_speedRestrict != RESTRICT_NONE) {
975 return _speedRestrict;
978 return _waypt->speedRestriction();
981 void FlightPlan::Leg::setSpeed(RouteRestriction ty, double speed)
984 if (isMachRestrict(ty)) {
985 _speed = (speed * -100);
991 void FlightPlan::Leg::setAltitude(RouteRestriction ty, int altFt)
997 double FlightPlan::Leg::courseDeg() const
1002 double FlightPlan::Leg::distanceNm() const
1004 return _pathDistance;
1007 double FlightPlan::Leg::distanceAlongRoute() const
1009 return _distanceAlongPath;
1012 void FlightPlan::rebuildLegData()
1014 _totalDistance = 0.0;
1015 int lastLeg = static_cast<int>(_legs.size()) - 1;
1016 for (int l=0; l<lastLeg; ++l) {
1017 Leg* cur = _legs[l];
1018 Leg* next = _legs[l + 1];
1020 std::pair<double, double> crsDist =
1021 next->waypoint()->courseAndDistanceFrom(cur->waypoint()->position());
1022 _legs[l]->_courseDeg = crsDist.first;
1023 _legs[l]->_pathDistance = crsDist.second * SG_METER_TO_NM;
1024 _legs[l]->_distanceAlongPath = _totalDistance;
1025 _totalDistance += crsDist.second * SG_METER_TO_NM;
1026 } // of legs iteration
1028 // set some data on the final leg
1030 // keep the same course as the final leg, when passing the final
1032 _legs[lastLeg]->_courseDeg = _legs[lastLeg - 1]->_courseDeg;
1033 _legs[lastLeg]->_pathDistance = 0.0;
1034 _legs[lastLeg]->_distanceAlongPath = _totalDistance;
1038 void FlightPlan::lockDelegate()
1040 if (_delegateLock == 0) {
1041 assert(!_departureChanged && !_arrivalChanged &&
1042 !_waypointsChanged && !_currentWaypointChanged);
1048 void FlightPlan::unlockDelegate()
1050 assert(_delegateLock > 0);
1051 if (_delegateLock > 1) {
1056 if (_departureChanged) {
1057 _departureChanged = false;
1059 _delegate->runDepartureChanged();
1063 if (_arrivalChanged) {
1064 _arrivalChanged = false;
1066 _delegate->runArrivalChanged();
1070 if (_waypointsChanged) {
1071 _waypointsChanged = false;
1074 _delegate->runWaypointsChanged();
1078 if (_currentWaypointChanged) {
1079 _currentWaypointChanged = false;
1081 _delegate->runCurrentWaypointChanged();
1088 void FlightPlan::registerDelegateFactory(DelegateFactory* df)
1090 FPDelegateFactoryVec::iterator it = std::find(static_delegateFactories.begin(),
1091 static_delegateFactories.end(), df);
1092 if (it != static_delegateFactories.end()) {
1093 throw sg_exception("duplicate delegate factory registration");
1096 static_delegateFactories.push_back(df);
1099 void FlightPlan::unregisterDelegateFactory(DelegateFactory* df)
1101 FPDelegateFactoryVec::iterator it = std::find(static_delegateFactories.begin(),
1102 static_delegateFactories.end(), df);
1103 if (it == static_delegateFactories.end()) {
1107 static_delegateFactories.erase(it);
1110 void FlightPlan::addDelegate(Delegate* d)
1112 // wrap any existing delegate(s) in the new one
1113 d->_inner = _delegate;
1117 void FlightPlan::removeDelegate(Delegate* d)
1119 if (d == _delegate) {
1120 _delegate = _delegate->_inner;
1121 } else if (_delegate) {
1122 _delegate->removeInner(d);
1126 FlightPlan::Delegate::Delegate() :
1127 _deleteWithPlan(false),
1133 FlightPlan::Delegate::~Delegate()
1138 void FlightPlan::Delegate::removeInner(Delegate* d)
1145 // replace with grand-child
1147 } else { // recurse downwards
1148 _inner->removeInner(d);
1152 void FlightPlan::Delegate::runDepartureChanged()
1154 if (_inner) _inner->runDepartureChanged();
1158 void FlightPlan::Delegate::runArrivalChanged()
1160 if (_inner) _inner->runArrivalChanged();
1164 void FlightPlan::Delegate::runWaypointsChanged()
1166 if (_inner) _inner->runWaypointsChanged();
1170 void FlightPlan::Delegate::runCurrentWaypointChanged()
1172 if (_inner) _inner->runCurrentWaypointChanged();
1173 currentWaypointChanged();
1176 } // of namespace flightgear