1 // props.hxx -- declaration of SimGear Property Manager.
3 // Written by David Megginson - david@megginson.com
5 // This module is in the PUBLIC DOMAIN.
7 // This program is distributed in the hope that it will be useful, but
8 // WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See props.html for documentation [replace with URL when available].
30 #pragma warn A sloppy coder has defined UNKNOWN as a macro!
35 #pragma warn A sloppy coder has defined BOOL as a macro!
40 #pragma warn A sloppy coder has defined INT as a macro!
45 #pragma warn A sloppy coder has defined FLOAT as a macro!
50 #pragma warn A sloppy coder has defined DOUBLE as a macro!
55 #pragma warn A sloppy coder has defined STRING as a macro!
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * Abstract representation of a FlightGear value.
68 * This value is designed to be fairly robust -- it can exist without
69 * a specified value, it can be any of several types, and it can
70 * be tied to an external variable without disrupting any existing
71 * pointers or references to the value. Some basic type conversions
72 * are also handled automatically.
74 * Values also have attributes that control whether they can be read
75 * from, written to, or archived (i.e. saved to disk).
82 typedef bool (*bool_getter)();
83 typedef int (*int_getter)();
84 typedef float (*float_getter)();
85 typedef double (*double_getter)();
86 typedef const string &(*string_getter)();
89 typedef void (*bool_setter)(bool);
90 typedef void (*int_setter)(int);
91 typedef void (*float_setter)(float);
92 typedef void (*double_setter)(double);
93 typedef void (*string_setter)(const string &);
96 UNKNOWN, // no value assigned yet
99 FLOAT, // floating point
100 DOUBLE, // double precision
108 virtual Type getType () const { return _type; }
109 virtual bool isTied () const { return _tied; }
112 virtual bool getBoolValue () const;
113 virtual int getIntValue () const;
114 virtual float getFloatValue () const;
115 virtual double getDoubleValue () const;
116 virtual const string & getStringValue () const;
119 virtual bool setBoolValue (bool value);
120 virtual bool setIntValue (int value);
121 virtual bool setFloatValue (float value);
122 virtual bool setDoubleValue (double value);
123 virtual bool setStringValue (const string &value);
124 virtual bool setUnknownValue (const string &value);
126 // Tie to external variables.
127 virtual bool tieBool (bool_getter getter,
128 bool_setter setter = 0,
129 bool useDefault = true);
130 virtual bool tieInt (int_getter getter,
131 int_setter setter = 0,
132 bool useDefault = true);
133 virtual bool tieFloat (float_getter getter,
134 float_setter setter = 0,
135 bool useDefault = true);
136 virtual bool tieDouble (double_getter getter,
137 double_setter setter = 0,
138 bool useDefault = true);
139 virtual bool tieString (string_getter getter,
140 string_setter setter = 0,
141 bool useDefault = true);
143 // Untie from external variables.
144 virtual bool untie ();
148 bool getRawBool () const;
149 int getRawInt () const;
150 float getRawFloat () const;
151 double getRawDouble () const;
152 const string &getRawString () const;
154 bool setRawBool (bool value);
155 bool setRawInt (int value);
156 bool setRawFloat (float value);
157 bool setRawDouble (double value);
158 bool setRawString (const string & value);
165 mutable string string_val;
167 // The value is one of the following...
193 double_setter setter;
194 double_getter getter;
198 string_setter setter;
199 string_getter getter;
208 ////////////////////////////////////////////////////////////////////////
209 // Top-level manager.
210 ////////////////////////////////////////////////////////////////////////
214 * A list of FlightGear properties.
216 * This list associates names (conventional written as paths,
217 * i.e. "/foo/bar/hack") with SGValue classes. Once an SGValue
218 * object is associated with the name, the association is
219 * permanent -- it is safe to keep a pointer or reference.
220 * however, that the type of a value may change if it is tied
223 * When iterating through the list, the value type is
225 * pair<string,SGValue>
227 * To get the name from a const_iterator, use
231 * and to get the value from a const_iterator, use
238 typedef map<string, SGValue> value_map;
240 typedef SGValue::bool_getter bool_getter;
241 typedef SGValue::int_getter int_getter;
242 typedef SGValue::float_getter float_getter;
243 typedef SGValue::double_getter double_getter;
244 typedef SGValue::string_getter string_getter;
246 typedef SGValue::bool_setter bool_setter;
247 typedef SGValue::int_setter int_setter;
248 typedef SGValue::float_setter float_setter;
249 typedef SGValue::double_setter double_setter;
250 typedef SGValue::string_setter string_setter;
252 typedef value_map::value_type value_type;
253 typedef value_map::size_type size_type;
254 typedef value_map::const_iterator const_iterator;
257 virtual ~SGPropertyList ();
259 virtual size_type size () const { return _props.size(); }
261 virtual const_iterator begin () const { return _props.begin(); }
262 virtual const_iterator end () const { return _props.end(); }
264 virtual SGValue * getValue (const string &name, bool create = false);
265 virtual const SGValue * getValue (const string &name) const;
267 virtual bool getBoolValue (const string &name,
268 bool defaultValue = false) const;
269 virtual int getIntValue (const string &name,
270 int defaultValue = 0) const;
271 virtual float getFloatValue (const string &name,
272 float defaultValue = 0.0) const;
273 virtual double getDoubleValue (const string &name,
274 double defaultValue = 0.0L) const;
275 virtual const string & getStringValue (const string &name,
276 const string &defaultValue = "")
279 virtual bool setBoolValue (const string &name, bool value);
280 virtual bool setIntValue (const string &name, int value);
281 virtual bool setFloatValue (const string &name, float value);
282 virtual bool setDoubleValue (const string &name, double value);
283 virtual bool setStringValue (const string &name, const string &value);
284 virtual bool setUnknownValue (const string &name, const string &value);
286 virtual bool tieBool (const string &name,
288 bool_setter setter = 0,
289 bool useDefault = true);
290 virtual bool tieInt (const string &name,
292 int_setter setter = 0,
293 bool useDefault = true);
294 virtual bool tieFloat (const string &name,
296 float_setter setter = 0,
297 bool useDefault = true);
298 virtual bool tieDouble (const string &name,
299 double_getter getter,
300 double_setter setter = 0,
301 bool useDefault = true);
302 virtual bool tieString (const string &name,
303 string_getter getter,
304 string_setter setter = 0,
305 bool useDefault = true);
307 virtual bool untie (const string &name);
315 ////////////////////////////////////////////////////////////////////////
316 // Tree/node/directory view.
317 ////////////////////////////////////////////////////////////////////////
321 * Tree view of a property list.
323 * This class provides a virtual tree view of a property list, without
324 * actually constructing a tree -- the view always stays in sync with
325 * the property list itself.
327 * This class is designed to be used for setup and configuration; it is
328 * optimized for ease of use rather than performance, and shouldn't be
329 * used inside a tight loop.
331 * Every node is actually just a path together with a pointer to
332 * the real property list and a few convenient operations; to the
333 * user, however, it looks like a node in a tree or a file system,
334 * with the regular operations such as getChild and getParent.
336 * Note that a node may be both a branch and a leaf -- that is, it
337 * may have a value itself and it may have children. Here is a simple
338 * example that prints the names of all of the different nodes inside
341 * SGPropertyNode controls("/controls", current_property_list);
342 * SGPropertyNode child;
343 * int size = controls.size();
344 * for (int i = 0; i < size; i++) {
345 * if (controls.getChild(child, i))
346 * cout << child.getName() << endl;
348 * cerr << "Failed to read child " << i << endl;
354 // Constructor and destructor
355 SGPropertyNode (const string &path = "", SGPropertyList * props = 0);
356 virtual ~SGPropertyNode ();
358 // Accessor and setter for the internal
360 virtual const string &getPath () const { return _path; }
361 virtual void setPath (const string &path);
363 // Accessor and setter for the real
365 virtual SGPropertyList * getPropertyList () { return _props; }
366 virtual void setPropertyList (SGPropertyList * props) {
370 // Accessors for derived information.
371 virtual int size () const;
372 virtual const string &getName () const;
373 virtual SGPropertyNode &getParent () const;
374 virtual SGPropertyNode &getChild (int n) const;
375 virtual SGPropertyNode &getSubNode (const string &subpath) const;
377 // Get values directly.
378 virtual SGValue * getValue (const string &subpath = "");
379 virtual bool getBoolValue (const string &subpath = "",
380 bool defaultValue = false) const;
381 virtual int getIntValue (const string &subpath = "",
382 int defaultValue = 0) const;
383 virtual float getFloatValue (const string &subpath = "",
384 float defaultValue = 0.0) const;
385 virtual double getDoubleValue (const string &subpath = "",
386 double defaultValue = 0.0L) const;
387 virtual const string &
388 getStringValue (const string &subpath = "",
389 const string &defaultValue = "") const;
393 SGPropertyList * _props;
394 // for pointer persistence...
395 // NOT THREAD SAFE!!!
396 // (each thread must have its own node
398 mutable string _name;
399 mutable SGPropertyNode * _node;
404 ////////////////////////////////////////////////////////////////////////
406 ////////////////////////////////////////////////////////////////////////
408 extern bool readPropertyList (istream &input, SGPropertyList * props);
409 extern bool readPropertyList (const string &file, SGPropertyList * props);
410 extern bool writePropertyList (ostream &output, const SGPropertyList * props);
411 extern bool writePropertyList (const string &file,
412 const SGPropertyList * props);
416 ////////////////////////////////////////////////////////////////////////
417 // Global property manager.
418 ////////////////////////////////////////////////////////////////////////
420 extern SGPropertyList current_properties;
423 #endif // __PROPS_HXX