]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/FlightPlan.hxx
Fix a FlightPlan lifetime issue.
[flightgear.git] / src / Navaids / FlightPlan.hxx
1 /**
2  * FlightPlan.hxx - defines a full flight-plan object, including
3  * departure, cruise, arrival information and waypoints
4  */
5  
6 // Written by James Turner, started 2012.
7 //
8 // Copyright (C) 2012 James Turner
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
24 #ifndef FG_FLIGHTPLAN_HXX
25 #define FG_FLIGHTPLAN_HXX
26
27 #include <Navaids/route.hxx>
28 #include <Airports/airport.hxx>
29
30 namespace flightgear
31 {
32
33 class Transition;
34 class FlightPlan;
35     
36 typedef SGSharedPtr<FlightPlan> FlightPlanRef;
37     
38 class FlightPlan : public RouteBase
39 {
40 public:
41   FlightPlan();
42   virtual ~FlightPlan();
43   
44   virtual std::string ident() const;
45   void setIdent(const std::string& s);
46   
47   FlightPlan* clone(const std::string& newIdent = std::string()) const;
48   
49   /**
50    * flight-plan leg encapsulation
51    */
52   class Leg
53   {
54   public:
55     FlightPlan* owner() const
56     { return _parent; }
57     
58     Waypt* waypoint() const
59     { return _waypt; }
60     
61     // reutrn the next leg after this one
62     Leg* nextLeg() const;
63     
64     unsigned int index() const;
65     
66     int altitudeFt() const;             
67     int speed() const;
68     
69     int speedKts() const;
70     double speedMach() const;
71     
72     RouteRestriction altitudeRestriction() const;    
73     RouteRestriction speedRestriction() const;
74     
75     void setSpeed(RouteRestriction ty, double speed);
76     void setAltitude(RouteRestriction ty, int altFt);
77     
78     double courseDeg() const;
79     double distanceNm() const;
80     double distanceAlongRoute() const;
81   private:
82     friend class FlightPlan;
83     
84     Leg(FlightPlan* owner, WayptRef wpt);
85     
86     Leg* cloneFor(FlightPlan* owner) const;
87     
88     FlightPlan* _parent;
89     RouteRestriction _speedRestrict, _altRestrict;
90     int _speed;
91     int _altitudeFt;
92     WayptRef _waypt;
93     /// length of this leg following the flown path
94     mutable double _pathDistance;
95     mutable double _courseDeg;
96     /// total distance of this leg from departure point
97     mutable double _distanceAlongPath; 
98   };
99   
100   class Delegate
101   {
102   public:
103     virtual ~Delegate();
104         
105     virtual void departureChanged() { }
106     virtual void arrivalChanged() { }
107     virtual void waypointsChanged() { }
108     virtual void cleared() { }
109     virtual void currentWaypointChanged() { }
110     virtual void endOfFlightPlan() { }
111   protected:
112     Delegate();
113     
114   private:
115     void removeInner(Delegate* d);
116     
117     void runDepartureChanged();
118     void runArrivalChanged();
119     void runWaypointsChanged();
120     void runCurrentWaypointChanged();
121     void runCleared();
122     void runFinished();
123       
124     friend class FlightPlan;
125     
126     bool _deleteWithPlan;
127     Delegate* _inner;
128   };
129   
130   Leg* insertWayptAtIndex(Waypt* aWpt, int aIndex);
131   void insertWayptsAtIndex(const WayptVec& wps, int aIndex);
132   
133   void deleteIndex(int index);
134   void clear();
135   int clearWayptsWithFlag(WayptFlag flag);
136   
137   int currentIndex() const
138   { return _currentIndex; }
139   
140   void setCurrentIndex(int index);
141   
142   void finish();
143     
144   Leg* currentLeg() const;
145   Leg* nextLeg() const;
146   Leg* previousLeg() const;
147   
148   int numLegs() const
149   { return _legs.size(); }
150   
151   Leg* legAtIndex(int index) const;
152   int findLegIndex(const Leg* l) const;
153   
154   int findWayptIndex(const SGGeod& aPos) const;
155   
156   bool load(const SGPath& p);
157   bool save(const SGPath& p);
158   
159   FGAirportRef departureAirport() const
160   { return _departure; }
161   
162   FGAirportRef destinationAirport() const
163   { return _destination; }
164   
165   FGRunway* departureRunway() const
166   { return _departureRunway; }
167   
168   FGRunway* destinationRunway() const
169   { return _destinationRunway; }
170   
171   Approach* approach() const
172   { return _approach; }
173   
174   void setDeparture(FGAirport* apt);
175   void setDeparture(FGRunway* rwy);
176   
177   SID* sid() const
178   { return _sid; }
179   
180   Transition* sidTransition() const;
181   
182   void setSID(SID* sid, const std::string& transition = std::string());
183   
184   void setSID(Transition* sidWithTrans);
185   
186   void setDestination(FGAirport* apt);
187   void setDestination(FGRunway* rwy);
188   
189   /**
190     * note setting an approach will implicitly update the destination
191     * airport and runway to match
192     */
193   void setApproach(Approach* app);
194   
195   STAR* star() const
196   { return _star; }
197   
198   Transition* starTransition() const;
199   
200   void setSTAR(STAR* star, const std::string& transition = std::string());
201   
202   void setSTAR(Transition* starWithTrans);
203   
204   double totalDistanceNm() const
205   { return _totalDistance; }
206   
207   /**
208    * given a waypoint index, and an offset in NM, find the geodetic
209    * position on the route path. I.e the point 10nm before or after
210    * a particular waypoint.
211    */
212   SGGeod pointAlongRoute(int aIndex, double aOffsetNm) const;
213     
214   /**
215    * Create a WayPoint from a string in the following format:
216    *  - simple identifier
217    *  - decimal-lon,decimal-lat
218    *  - airport-id/runway-id
219    *  - navaid/radial-deg/offset-nm
220    */
221   WayptRef waypointFromString(const std::string& target);
222   
223   /**
224    * abstract interface for creating delegates automatically when a
225    * flight-plan is created or loaded
226    */
227   class DelegateFactory
228   {
229   public:
230     virtual Delegate* createFlightPlanDelegate(FlightPlan* fp) = 0;
231   };
232   
233   static void registerDelegateFactory(DelegateFactory* df);
234   static void unregisterDelegateFactory(DelegateFactory* df);
235   
236   void addDelegate(Delegate* d);
237   void removeDelegate(Delegate* d);
238 private:
239   void lockDelegate();
240   void unlockDelegate();
241   
242   int _delegateLock;
243   bool _arrivalChanged, 
244     _departureChanged, 
245     _waypointsChanged, 
246     _currentWaypointChanged;
247   
248   bool loadPlainTextRoute(const SGPath& path);
249   
250   void loadVersion1XMLRoute(SGPropertyNode_ptr routeData);
251   void loadVersion2XMLRoute(SGPropertyNode_ptr routeData);
252   void loadXMLRouteHeader(SGPropertyNode_ptr routeData);
253   WayptRef parseVersion1XMLWaypt(SGPropertyNode* aWP);
254   
255   double magvarDegAt(const SGGeod& pos) const;
256   
257   std::string _ident;
258   int _currentIndex;
259   
260   FGAirportRef _departure, _destination;
261   FGRunway* _departureRunway, *_destinationRunway;
262   SGSharedPtr<SID> _sid;
263   SGSharedPtr<STAR> _star;
264   SGSharedPtr<Approach> _approach;
265   std::string _sidTransition, _starTransition;
266   
267   double _totalDistance;
268   void rebuildLegData();
269   
270   typedef std::vector<Leg*> LegVec;
271   LegVec _legs;
272   
273   Delegate* _delegate;
274 };
275   
276 } // of namespace flightgear
277
278 #endif // of FG_FLIGHTPLAN_HXX