FlightGear next
fg_props.hxx
Go to the documentation of this file.
1// fg_props.hxx - Declarations and inline methods for property handling.
2// Written by David Megginson, started 2000.
3//
4// This file is in the Public Domain, and comes with no warranty.
5
6#ifndef __FG_PROPS_HXX
7#define __FG_PROPS_HXX 1
8
9#include <iosfwd>
10#include <algorithm>
11
12#include <simgear/structure/subsystem_mgr.hxx>
13#include <simgear/props/tiedpropertylist.hxx>
14
15#include <Main/globals.hxx>
16
18// Property management.
20
21class FGProperties : public SGSubsystem
22{
23public:
24 FGProperties ();
25 virtual ~FGProperties ();
26
27 // Subsystem API.
28 void bind() override;
29 void init() override;
30 void unbind() override;
31 void update(double dt) override;
32
33 // Subsystem identification.
34 static const char* staticSubsystemClassId() { return "properties"; }
35
36private:
37 bool getFreeze() const;
38 void setFreeze(bool freeze);
39
40 simgear::TiedPropertyList _tiedProperties;
41
42 static const char* getLatitudeString ();
43 static const char* getLongitudeString ();
44
45 static SGConstPropertyNode_ptr _longDeg, _latDeg, _lonLatformat;
46
47 SGPropertyNode_ptr _offset;
48 SGPropertyNode_ptr _uyear, _umonth, _uday, _uhour, _umin, _usec, _uwday, _udsec;
49 SGPropertyNode_ptr _ryear, _rmonth, _rday, _rhour, _rmin, _rsec, _rwday, _rdsec;
50
51 SGPropertyNode_ptr _headingMagnetic, _trackMagnetic;
52 SGPropertyNode_ptr _magVar;
53 SGPropertyNode_ptr _trueHeading, _trueTrack;
54
55 struct tm _lastUtc;
56 struct tm _lastRealTime;
57
58 bool _simFreeze = false;
59 SGPropertyNode_ptr _timeGmtNode, _timeGmtStringNode;
60 SGPropertyNode_ptr _simFreezeNode;
61};
62
63
75extern bool fgSaveFlight (std::ostream &output, bool write_all = false);
76
77
87extern bool fgLoadFlight (std::istream &input);
88
89
101extern bool fgLoadProps (const std::string& path, SGPropertyNode * props,
102 bool in_fg_root = true, int default_mode = 0);
103
104void setLoggingClasses (const char * c);
105void setLoggingPriority (const char * p);
106
107
109// Convenience functions for getting property values.
111
119extern SGPropertyNode * fgGetNode (const char * path, bool create = false);
120
128inline SGPropertyNode * fgGetNode (const std::string & path, bool create = false)
129{
130 return fgGetNode(path.c_str(), create );
131}
132
133
148extern SGPropertyNode * fgGetNode (const char * path,
149 int index, bool create = false);
150
165inline SGPropertyNode * fgGetNode (const std::string & path,
166 int index, bool create = false)
167{
168 return fgGetNode(path.c_str(), index, create );
169}
170
171
178extern bool fgHasNode (const char * path);
179
186inline bool fgHasNode (const std::string & path)
187{
188 return fgHasNode( path.c_str() );
189}
190
191
200extern void fgAddChangeListener (SGPropertyChangeListener * listener,
201 const char * path);
202
211inline void fgAddChangeListener (SGPropertyChangeListener * listener,
212 const std::string & path)
213{
214 fgAddChangeListener( listener, path.c_str() );
215}
216
217
226extern void fgAddChangeListener (SGPropertyChangeListener * listener,
227 const char * path, int index);
228
237inline void fgAddChangeListener (SGPropertyChangeListener * listener,
238 const std::string & path, int index)
239{
240 fgAddChangeListener( listener, path.c_str(), index );
241}
242
243
258extern bool fgGetBool (const char * name, bool defaultValue = false);
259
274inline bool fgGetBool (const std::string & name, bool defaultValue = false)
275{
276 return fgGetBool( name.c_str(), defaultValue );
277}
278
279
294extern int fgGetInt (const char * name, int defaultValue = 0);
295
310inline int fgGetInt (const std::string & name, int defaultValue = 0)
311{
312 return fgGetInt( name.c_str(), defaultValue );
313}
314
315
330extern long fgGetLong (const char * name, long defaultValue = 0L);
331
346inline long fgGetLong (const std::string & name, long defaultValue = 0L)
347{
348 return fgGetLong( name.c_str(), defaultValue );
349}
350
351
366extern float fgGetFloat (const char * name, float defaultValue = 0.0);
367
382inline float fgGetFloat (const std::string & name, float defaultValue = 0.0)
383{
384 return fgGetFloat( name.c_str(), defaultValue );
385}
386
387
402extern double fgGetDouble (const char * name, double defaultValue = 0.0);
403
418inline double fgGetDouble (const std::string & name, double defaultValue = 0.0)
419{
420 return fgGetDouble( name.c_str(), defaultValue );
421}
422
423
438extern std::string fgGetString (const char * name,
439 const char * defaultValue = "");
440
455inline std::string fgGetString (const std::string & name,
456 const std::string & defaultValue = std::string(""))
457{
458 return fgGetString( name.c_str(), defaultValue.c_str() );
459}
460
461
475extern bool fgSetBool (const char * name, bool val);
476
490inline bool fgSetBool (const std::string & name, bool val)
491{
492 return fgSetBool( name.c_str(), val );
493}
494
495
509extern bool fgSetInt (const char * name, int val);
510
524inline bool fgSetInt (const std::string & name, int val)
525{
526 return fgSetInt( name.c_str(), val );
527}
528
542extern bool fgSetLong (const char * name, long val);
543
557inline bool fgSetLong (const std::string & name, long val)
558{
559 return fgSetLong( name.c_str(), val );
560}
561
562
576extern bool fgSetFloat (const char * name, float val);
577
591inline bool fgSetFloat (const std::string & name, float val)
592{
593 return fgSetFloat( name.c_str(), val );
594}
595
596
610extern bool fgSetDouble (const char * name, double val);
611
625inline bool fgSetDouble (const std::string & name, double val)
626{
627 return fgSetDouble( name.c_str(), val );
628}
629
630
644extern bool fgSetString (const char * name, const char * val);
645
659inline bool fgSetString (const std::string & name, const std::string & val)
660{
661 return fgSetString( name.c_str(), val.c_str() );
662}
663
664
666// Convenience functions for setting property attributes.
668
669
682extern void fgSetArchivable (const char * name, bool state = true);
683
684
697extern void fgSetReadable (const char * name, bool state = true);
698
699
712extern void fgSetWritable (const char * name, bool state = true);
713
714
716// Convenience functions for tying properties, with logging.
718
719
726extern void fgUntie (const char * name);
727
731void fgUntieIfDefined(const std::string& name);
732
749template <class V>
750inline void
751fgTie (const char * name, V (*getter)(), void (*setter)(V) = 0,
752 bool useDefault = true)
753{
754 if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
755 useDefault))
756 SG_LOG(SG_GENERAL, SG_DEV_WARN,
757 "Failed to tie property " << name << " to functions");
758}
759
760
779template <class V>
780inline void
781fgTie (const char * name, int index, V (*getter)(int),
782 void (*setter)(int, V) = 0, bool useDefault = true)
783{
784 if (!globals->get_props()->tie(name,
785 SGRawValueFunctionsIndexed<V>(index,
786 getter,
787 setter),
788 useDefault))
789 SG_LOG(SG_GENERAL, SG_DEV_WARN,
790 "Failed to tie property " << name << " to indexed functions");
791}
792
793
813template <class T, class V>
814inline void
815fgTie (const char * name, T * obj, V (T::*getter)() const,
816 void (T::*setter)(V) = 0, bool useDefault = true)
817{
818 if (!globals->get_props()->tie(name,
819 SGRawValueMethods<T,V>(*obj, getter, setter),
820 useDefault))
821 SG_LOG(SG_GENERAL, SG_DEV_WARN,
822 "Failed to tie property " << name << " to object methods");
823}
824
825
845template <class T, class V>
846inline void
847fgTie (const char * name, T * obj, int index,
848 V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
849 bool useDefault = true)
850{
851 if (!globals->get_props()->tie(name,
852 SGRawValueMethodsIndexed<T,V>(*obj,
853 index,
854 getter,
855 setter),
856 useDefault))
857 SG_LOG(SG_GENERAL, SG_DEV_WARN,
858 "Failed to tie property " << name << " to indexed object methods");
859}
860
861#endif // __FG_PROPS_HXX
862
#define p(x)
void unbind() override
Definition fg_props.cxx:352
void init() override
Definition fg_props.cxx:281
void update(double dt) override
Definition fg_props.cxx:367
virtual ~FGProperties()
Definition fg_props.cxx:276
static const char * staticSubsystemClassId()
Definition fg_props.hxx:34
void bind() override
Definition fg_props.cxx:298
const char * name
void fgAddChangeListener(SGPropertyChangeListener *listener, const char *path)
Add a listener to a node.
Definition fg_props.cxx:513
bool fgHasNode(const char *path)
Test whether a given node exists.
Definition fg_props.cxx:507
double fgGetDouble(const char *name, double defaultValue=0.0)
Get a double value for a property.
Definition proptest.cpp:30
std::string fgGetString(const char *name, const char *defaultValue="")
Get a string value for a property.
Definition fg_props.cxx:556
void fgSetReadable(const char *name, bool state=true)
Set the state of the read attribute for a property.
Definition fg_props.cxx:610
void fgUntie(const char *name)
Untie a property from an external data source.
Definition fg_props.cxx:634
void fgTie(const char *name, V(*getter)(), void(*setter)(V)=0, bool useDefault=true)
Tie a property to a pair of simple functions.
Definition fg_props.hxx:751
int fgGetInt(const char *name, int defaultValue=0)
Get an int value for a property.
Definition fg_props.cxx:532
bool fgSaveFlight(std::ostream &output, bool write_all=false)
Save a flight to disk.
Definition fg_props.cxx:424
bool fgSetDouble(const char *name, double val)
Set a double value for a property.
Definition proptest.cpp:31
bool fgSetBool(const char *name, bool val)
Set a bool value for a property.
Definition proptest.cpp:24
float fgGetFloat(const char *name, float defaultValue=0.0)
Get a float value for a property.
Definition proptest.cpp:29
SGPropertyNode * fgGetNode(const char *path, bool create=false)
Get a property node.
Definition proptest.cpp:27
void setLoggingPriority(const char *p)
Set the logging priority.
Definition fg_props.cxx:100
void fgUntieIfDefined(const std::string &name)
@brfief variant of the above which doesn't warn if the property does not exist
Definition fg_props.cxx:651
bool fgLoadProps(const std::string &path, SGPropertyNode *props, bool in_fg_root=true, int default_mode=0)
Load properties from a file.
Definition fg_props.cxx:469
bool fgGetBool(const char *name, bool defaultValue=false)
Get a bool value for a property.
Definition proptest.cpp:25
bool fgLoadFlight(std::istream &input)
Load a flight from disk.
Definition fg_props.cxx:448
bool fgSetLong(const char *name, long val)
Set a long value for a property.
Definition fg_props.cxx:574
bool fgSetInt(const char *name, int val)
Set an int value for a property.
Definition fg_props.cxx:568
bool fgSetString(const char *name, const char *val)
Set a string value for a property.
Definition proptest.cpp:26
long fgGetLong(const char *name, long defaultValue=0L)
Get a long value for a property.
Definition fg_props.cxx:538
bool fgSetFloat(const char *name, float val)
Set a float value for a property.
Definition proptest.cpp:23
void setLoggingClasses(const char *c)
Set the logging classes.
Definition fg_props.cxx:65
void fgSetArchivable(const char *name, bool state=true)
Set the state of the archive attribute for a property.
Definition fg_props.cxx:598
void fgSetWritable(const char *name, bool state=true)
Set the state of the write attribute for a property.
Definition fg_props.cxx:622
FGGlobals * globals
Definition globals.cxx:142