e1dd52d38a
restoreInitialState methods to FGGlobals, as well as the two-stage commit described above for loading saved files. fgInit now takes a snapshot of the initial state before handing off to the main loop, and the GUI reInit function restores that state explicitly before calling fgReInit. The FlightGear patches also modify fg_props.hxx to add optional useDefault arguments to all of the fgTie functions -- that lets you choose whether you want to pick up any default value in the property tree when you tie the property (the default is true).
203 lines
5.5 KiB
C++
203 lines
5.5 KiB
C++
#ifndef __FG_PROPS_HXX
|
||
#define __FG_PROPS_HXX 1
|
||
|
||
#include <simgear/debug/logstream.hxx>
|
||
#include <simgear/misc/props.hxx>
|
||
#include "globals.hxx"
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////
|
||
// Loading and saving properties.
|
||
////////////////////////////////////////////////////////////////////////
|
||
|
||
extern bool fgSaveFlight (ostream &output);
|
||
extern bool fgLoadFlight (istream &input);
|
||
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////
|
||
// Convenience functions for getting property values.
|
||
////////////////////////////////////////////////////////////////////////
|
||
|
||
/**
|
||
* Get an SGValue pointer that can be queried repeatedly.
|
||
*
|
||
* If the property value is going to be accessed within the loop,
|
||
* it is best to use this method for maximum efficiency.
|
||
*/
|
||
inline SGValue * fgGetValue (const string &name, bool create = false)
|
||
{
|
||
return globals->get_props()->getValue(name, create);
|
||
}
|
||
|
||
inline bool fgHasValue (const string &name)
|
||
{
|
||
return globals->get_props()->hasValue(name);
|
||
}
|
||
|
||
inline bool fgGetBool (const string &name, bool defaultValue = false)
|
||
{
|
||
return globals->get_props()->getBoolValue(name, defaultValue);
|
||
}
|
||
|
||
inline int fgGetInt (const string &name, int defaultValue = 0)
|
||
{
|
||
return globals->get_props()->getIntValue(name, defaultValue);
|
||
}
|
||
|
||
inline float fgGetFloat (const string &name, float defaultValue = 0.0)
|
||
{
|
||
return globals->get_props()->getFloatValue(name, defaultValue);
|
||
}
|
||
|
||
inline double fgGetDouble (const string &name, double defaultValue = 0.0)
|
||
{
|
||
return globals->get_props()->getDoubleValue(name, defaultValue);
|
||
}
|
||
|
||
inline string fgGetString (const string &name, string defaultValue = "")
|
||
{
|
||
return globals->get_props()->getStringValue(name, defaultValue);
|
||
}
|
||
|
||
inline bool fgSetBool (const string &name, bool val)
|
||
{
|
||
return globals->get_props()->setBoolValue(name, val);
|
||
}
|
||
|
||
inline bool fgSetInt (const string &name, int val)
|
||
{
|
||
return globals->get_props()->setIntValue(name, val);
|
||
}
|
||
|
||
inline bool fgSetFloat (const string &name, float val)
|
||
{
|
||
return globals->get_props()->setFloatValue(name, val);
|
||
}
|
||
|
||
inline bool fgSetDouble (const string &name, double val)
|
||
{
|
||
return globals->get_props()->setDoubleValue(name, val);
|
||
}
|
||
|
||
inline bool fgSetString (const string &name, const string &val)
|
||
{
|
||
return globals->get_props()->setStringValue(name, val);
|
||
}
|
||
|
||
|
||
|
||
////////////////////////////////////////////////////////////////////////
|
||
// Convenience functions for tying properties, with logging.
|
||
////////////////////////////////////////////////////////////////////////
|
||
|
||
inline void
|
||
fgUntie (const string &name)
|
||
{
|
||
if (!globals->get_props()->untie(name))
|
||
FG_LOG(FG_GENERAL, FG_WARN, "Failed to untie property " << name);
|
||
}
|
||
|
||
|
||
// Templates cause ambiguity here
|
||
inline void
|
||
fgTie (const string &name, bool *pointer, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValuePointer<bool>(pointer),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to a pointer");
|
||
}
|
||
|
||
inline void
|
||
fgTie (const string &name, int *pointer, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValuePointer<int>(pointer),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to a pointer");
|
||
}
|
||
|
||
inline void
|
||
fgTie (const string &name, float *pointer, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValuePointer<float>(pointer),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to a pointer");
|
||
}
|
||
|
||
inline void
|
||
fgTie (const string &name, double *pointer, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValuePointer<double>(pointer),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to a pointer");
|
||
}
|
||
|
||
inline void
|
||
fgTie (const string &name, string *pointer, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValuePointer<string>(pointer),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to a pointer");
|
||
}
|
||
|
||
template <class V>
|
||
inline void
|
||
fgTie (const string &name, V (*getter)(), void (*setter)(V) = 0,
|
||
bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name, SGRawValueFunctions<V>(getter, setter),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to functions");
|
||
}
|
||
|
||
template <class V>
|
||
inline void
|
||
fgTie (const string &name, int index, V (*getter)(int),
|
||
void (*setter)(int, V) = 0, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name,
|
||
SGRawValueFunctionsIndexed<V>(index,
|
||
getter,
|
||
setter),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to indexed functions");
|
||
}
|
||
|
||
template <class T, class V>
|
||
inline void
|
||
fgTie (const string &name, T * obj, V (T::*getter)() const,
|
||
void (T::*setter)(V) = 0, bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name,
|
||
SGRawValueMethods<T,V>(*obj, getter, setter),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to object methods");
|
||
}
|
||
|
||
template <class T, class V>
|
||
inline void
|
||
fgTie (const string &name, T * obj, int index,
|
||
V (T::*getter)(int) const, void (T::*setter)(int, V) = 0,
|
||
bool useDefault = true)
|
||
{
|
||
if (!globals->get_props()->tie(name,
|
||
SGRawValueMethodsIndexed<T,V>(*obj,
|
||
index,
|
||
getter,
|
||
setter),
|
||
useDefault))
|
||
FG_LOG(FG_GENERAL, FG_WARN,
|
||
"Failed to tie property " << name << " to indexed object methods");
|
||
}
|
||
|
||
|
||
#endif // __FG_PROPS_HXX
|
||
|