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 if (dep->hasChild("runway")) {
616 setDeparture(_departure->getRunwayByIdent(dep->getStringValue("runway")));
619 if (dep->hasChild("sid")) {
620 setSID(_departure->findSIDWithIdent(dep->getStringValue("sid")));
622 // departure->setStringValue("transition", dep->getStringValue("transition"));
627 SGPropertyNode* dst = routeData->getChild("destination");
629 setDestination((FGAirport*) fgFindAirportID(dst->getStringValue("airport")));
631 if (dst->hasChild("runway")) {
632 setDestination(_destination->getRunwayByIdent(dst->getStringValue("runway")));
635 if (dst->hasChild("star")) {
636 setSTAR(_destination->findSTARWithIdent(dst->getStringValue("star")));
639 if (dst->hasChild("approach")) {
640 setApproach(_destination->findApproachWithIdent(dst->getStringValue("approach")));
644 // destination->setStringValue("transition", dst->getStringValue("transition"));
648 SGPropertyNode* alt = routeData->getChild("alternate");
650 //alternate->setStringValue(alt->getStringValue("airport"));
654 SGPropertyNode* crs = routeData->getChild("cruise");
656 // cruise->setDoubleValue("speed-kts", crs->getDoubleValue("speed-kts"));
657 // cruise->setDoubleValue("mach", crs->getDoubleValue("mach"));
658 // cruise->setDoubleValue("altitude-ft", crs->getDoubleValue("altitude-ft"));
659 } // of cruise data loading
663 void FlightPlan::loadVersion2XMLRoute(SGPropertyNode_ptr routeData)
665 loadXMLRouteHeader(routeData);
669 SGPropertyNode_ptr routeNode = routeData->getChild("route", 0);
670 for (int i=0; i<routeNode->nChildren(); ++i) {
671 SGPropertyNode_ptr wpNode = routeNode->getChild("wp", i);
672 Leg* l = new Leg(this, Waypt::createFromProperties(NULL, wpNode));
674 } // of route iteration
675 _waypointsChanged = true;
678 void FlightPlan::loadVersion1XMLRoute(SGPropertyNode_ptr routeData)
680 loadXMLRouteHeader(routeData);
684 SGPropertyNode_ptr routeNode = routeData->getChild("route", 0);
685 for (int i=0; i<routeNode->nChildren(); ++i) {
686 SGPropertyNode_ptr wpNode = routeNode->getChild("wp", i);
687 Leg* l = new Leg(this, parseVersion1XMLWaypt(wpNode));
689 } // of route iteration
690 _waypointsChanged = true;
693 WayptRef FlightPlan::parseVersion1XMLWaypt(SGPropertyNode* aWP)
696 if (!_legs.empty()) {
697 lastPos = _legs.back()->waypoint()->position();
698 } else if (_departure) {
699 lastPos = _departure->geod();
703 string ident(aWP->getStringValue("ident"));
704 if (aWP->hasChild("longitude-deg")) {
705 // explicit longitude/latitude
706 w = new BasicWaypt(SGGeod::fromDeg(aWP->getDoubleValue("longitude-deg"),
707 aWP->getDoubleValue("latitude-deg")), ident, NULL);
710 string nid = aWP->getStringValue("navid", ident.c_str());
711 FGPositionedRef p = FGPositioned::findClosestWithIdent(nid, lastPos);
713 throw sg_io_exception("bad route file, unknown navid:" + nid);
716 SGGeod pos(p->geod());
717 if (aWP->hasChild("offset-nm") && aWP->hasChild("offset-radial")) {
718 double radialDeg = aWP->getDoubleValue("offset-radial");
719 // convert magnetic radial to a true radial!
720 radialDeg += magvarDegAt(pos);
721 double offsetNm = aWP->getDoubleValue("offset-nm");
723 SGGeodesy::direct(p->geod(), radialDeg, offsetNm * SG_NM_TO_METER, pos, az2);
726 w = new BasicWaypt(pos, ident, NULL);
729 double altFt = aWP->getDoubleValue("altitude-ft", -9999.9);
730 if (altFt > -9990.0) {
731 w->setAltitude(altFt, RESTRICT_AT);
737 bool FlightPlan::loadPlainTextRoute(const SGPath& path)
740 sg_gzifstream in(path.str().c_str());
742 throw sg_io_exception("Cannot open file for reading.");
748 getline(in, line, '\n');
749 // trim CR from end of line, if found
750 if (line[line.size() - 1] == '\r') {
751 line.erase(line.size() - 1, 1);
754 line = simgear::strutils::strip(line);
755 if (line.empty() || (line[0] == '#')) {
756 continue; // ignore empty/comment lines
759 WayptRef w = waypointFromString(line);
761 throw sg_io_exception("Failed to create waypoint from line '" + line + "'.");
764 _legs.push_back(new Leg(this, w));
765 } // of line iteration
766 } catch (sg_exception& e) {
767 SG_LOG(SG_NAVAID, SG_ALERT, "Failed to load route from: '" << path.str() << "'. " << e.getMessage());
775 double FlightPlan::magvarDegAt(const SGGeod& pos) const
777 double jd = globals->get_time_params()->getJD();
778 return sgGetMagVar(pos, jd) * SG_RADIANS_TO_DEGREES;
781 WayptRef FlightPlan::waypointFromString(const string& tgt )
783 string target(boost::to_upper_copy(tgt));
788 RouteRestriction altSetting = RESTRICT_NONE;
790 size_t pos = target.find( '@' );
791 if ( pos != string::npos ) {
792 altFt = atof( target.c_str() + pos + 1 );
793 target = target.substr( 0, pos );
794 if ( !strcmp(fgGetString("/sim/startup/units"), "meter") )
795 altFt *= SG_METER_TO_FEET;
796 altSetting = RESTRICT_AT;
800 pos = target.find( ',' );
801 if ( pos != string::npos ) {
802 double lon = atof( target.substr(0, pos).c_str());
803 double lat = atof( target.c_str() + pos + 1);
805 char ew = (lon < 0.0) ? 'W' : 'E';
806 char ns = (lat < 0.0) ? 'S' : 'N';
807 snprintf(buf, 32, "%c%03d%c%03d", ew, (int) fabs(lon), ns, (int)fabs(lat));
809 wpt = new BasicWaypt(SGGeod::fromDeg(lon, lat), buf, NULL);
810 if (altSetting != RESTRICT_NONE) {
811 wpt->setAltitude(altFt, altSetting);
818 // route is empty, use current position
819 basePosition = globals->get_aircraft_position();
821 basePosition = _legs.back()->waypoint()->position();
824 string_list pieces(simgear::strutils::split(target, "/"));
825 FGPositionedRef p = FGPositioned::findClosestWithIdent(pieces.front(), basePosition);
827 SG_LOG( SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces.front());
831 double magvar = magvarDegAt(basePosition);
833 if (pieces.size() == 1) {
834 wpt = new NavaidWaypoint(p, NULL);
835 } else if (pieces.size() == 3) {
836 // navaid/radial/distance-nm notation
837 double radial = atof(pieces[1].c_str()),
838 distanceNm = atof(pieces[2].c_str());
840 wpt = new OffsetNavaidWaypoint(p, NULL, radial, distanceNm);
841 } else if (pieces.size() == 2) {
842 FGAirport* apt = dynamic_cast<FGAirport*>(p.ptr());
844 SG_LOG(SG_NAVAID, SG_INFO, "Waypoint is not an airport:" << pieces.front());
848 if (!apt->hasRunwayWithIdent(pieces[1])) {
849 SG_LOG(SG_NAVAID, SG_INFO, "No runway: " << pieces[1] << " at " << pieces[0]);
853 FGRunway* runway = apt->getRunwayByIdent(pieces[1]);
854 wpt = new NavaidWaypoint(runway, NULL);
855 } else if (pieces.size() == 4) {
856 // navid/radial/navid/radial notation
857 FGPositionedRef p2 = FGPositioned::findClosestWithIdent(pieces[2], basePosition);
859 SG_LOG( SG_NAVAID, SG_INFO, "Unable to find FGPositioned with ident:" << pieces[2]);
863 double r1 = atof(pieces[1].c_str()),
864 r2 = atof(pieces[3].c_str());
869 bool ok = SGGeodesy::radialIntersection(p->geod(), r1, p2->geod(), r2, intersection);
871 SG_LOG(SG_NAVAID, SG_INFO, "no valid intersection for:" << target);
875 std::string name = p->ident() + "-" + p2->ident();
876 wpt = new BasicWaypt(intersection, name, NULL);
880 SG_LOG(SG_NAVAID, SG_INFO, "Unable to parse waypoint:" << target);
884 if (altSetting != RESTRICT_NONE) {
885 wpt->setAltitude(altFt, altSetting);
890 FlightPlan::Leg::Leg(FlightPlan* owner, WayptRef wpt) :
892 _speedRestrict(RESTRICT_NONE),
893 _altRestrict(RESTRICT_NONE),
897 throw sg_exception("can't create FlightPlan::Leg without underlying waypoint");
899 _speed = _altitudeFt = 0;
902 FlightPlan::Leg* FlightPlan::Leg::cloneFor(FlightPlan* owner) const
904 Leg* c = new Leg(owner, _waypt);
907 c->_speedRestrict = _speedRestrict;
908 c->_altitudeFt = _altitudeFt;
909 c->_altRestrict = _altRestrict;
914 FlightPlan::Leg* FlightPlan::Leg::nextLeg() const
916 return _parent->legAtIndex(index() + 1);
919 unsigned int FlightPlan::Leg::index() const
921 return _parent->findLegIndex(this);
924 int FlightPlan::Leg::altitudeFt() const
926 if (_altRestrict != RESTRICT_NONE) {
930 return _waypt->altitudeFt();
933 int FlightPlan::Leg::speed() const
935 if (_speedRestrict != RESTRICT_NONE) {
939 return _waypt->speed();
942 int FlightPlan::Leg::speedKts() const
947 double FlightPlan::Leg::speedMach() const
949 if (!isMachRestrict(_speedRestrict)) {
953 return -(_speed / 100.0);
956 RouteRestriction FlightPlan::Leg::altitudeRestriction() const
958 if (_altRestrict != RESTRICT_NONE) {
962 return _waypt->altitudeRestriction();
965 RouteRestriction FlightPlan::Leg::speedRestriction() const
967 if (_speedRestrict != RESTRICT_NONE) {
968 return _speedRestrict;
971 return _waypt->speedRestriction();
974 void FlightPlan::Leg::setSpeed(RouteRestriction ty, double speed)
977 if (isMachRestrict(ty)) {
978 _speed = (speed * -100);
984 void FlightPlan::Leg::setAltitude(RouteRestriction ty, int altFt)
990 double FlightPlan::Leg::courseDeg() const
995 double FlightPlan::Leg::distanceNm() const
997 return _pathDistance;
1000 double FlightPlan::Leg::distanceAlongRoute() const
1002 return _distanceAlongPath;
1005 void FlightPlan::rebuildLegData()
1007 _totalDistance = 0.0;
1008 int lastLeg = static_cast<int>(_legs.size()) - 1;
1009 for (int l=0; l<lastLeg; ++l) {
1010 Leg* cur = _legs[l];
1011 Leg* next = _legs[l + 1];
1013 std::pair<double, double> crsDist =
1014 next->waypoint()->courseAndDistanceFrom(cur->waypoint()->position());
1015 _legs[l]->_courseDeg = crsDist.first;
1016 _legs[l]->_pathDistance = crsDist.second * SG_METER_TO_NM;
1017 _legs[l]->_distanceAlongPath = _totalDistance;
1018 _totalDistance += crsDist.second * SG_METER_TO_NM;
1019 } // of legs iteration
1021 // set some data on the final leg
1023 // keep the same course as the final leg, when passing the final
1025 _legs[lastLeg]->_courseDeg = _legs[lastLeg - 1]->_courseDeg;
1026 _legs[lastLeg]->_pathDistance = 0.0;
1027 _legs[lastLeg]->_distanceAlongPath = _totalDistance;
1031 void FlightPlan::lockDelegate()
1033 if (_delegateLock == 0) {
1034 assert(!_departureChanged && !_arrivalChanged &&
1035 !_waypointsChanged && !_currentWaypointChanged);
1041 void FlightPlan::unlockDelegate()
1043 assert(_delegateLock > 0);
1044 if (_delegateLock > 1) {
1049 if (_departureChanged) {
1050 _departureChanged = false;
1052 _delegate->runDepartureChanged();
1056 if (_arrivalChanged) {
1057 _arrivalChanged = false;
1059 _delegate->runArrivalChanged();
1063 if (_waypointsChanged) {
1064 _waypointsChanged = false;
1067 _delegate->runWaypointsChanged();
1071 if (_currentWaypointChanged) {
1072 _currentWaypointChanged = false;
1074 _delegate->runCurrentWaypointChanged();
1081 void FlightPlan::registerDelegateFactory(DelegateFactory* df)
1083 FPDelegateFactoryVec::iterator it = std::find(static_delegateFactories.begin(),
1084 static_delegateFactories.end(), df);
1085 if (it != static_delegateFactories.end()) {
1086 throw sg_exception("duplicate delegate factory registration");
1089 static_delegateFactories.push_back(df);
1092 void FlightPlan::unregisterDelegateFactory(DelegateFactory* df)
1094 FPDelegateFactoryVec::iterator it = std::find(static_delegateFactories.begin(),
1095 static_delegateFactories.end(), df);
1096 if (it == static_delegateFactories.end()) {
1100 static_delegateFactories.erase(it);
1103 void FlightPlan::addDelegate(Delegate* d)
1105 // wrap any existing delegate(s) in the new one
1106 d->_inner = _delegate;
1110 void FlightPlan::removeDelegate(Delegate* d)
1112 if (d == _delegate) {
1113 _delegate = _delegate->_inner;
1114 } else if (_delegate) {
1115 _delegate->removeInner(d);
1119 FlightPlan::Delegate::Delegate() :
1120 _deleteWithPlan(false),
1126 FlightPlan::Delegate::~Delegate()
1131 void FlightPlan::Delegate::removeInner(Delegate* d)
1138 // replace with grand-child
1140 } else { // recurse downwards
1141 _inner->removeInner(d);
1145 void FlightPlan::Delegate::runDepartureChanged()
1147 if (_inner) _inner->runDepartureChanged();
1151 void FlightPlan::Delegate::runArrivalChanged()
1153 if (_inner) _inner->runArrivalChanged();
1157 void FlightPlan::Delegate::runWaypointsChanged()
1159 if (_inner) _inner->runWaypointsChanged();
1163 void FlightPlan::Delegate::runCurrentWaypointChanged()
1165 if (_inner) _inner->runCurrentWaypointChanged();
1166 currentWaypointChanged();
1169 } // of namespace flightgear