1 // props.cxx -- implementation of FGFS global properties.
3 // Copyright (C) 2000 David Megginson - david@megginson.com
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/debug/logstream.hxx>
35 FGPropertyList current_properties;
37 static string empty_string;
41 ////////////////////////////////////////////////////////////////////////
42 // Implementation of FGValue.
43 ////////////////////////////////////////////////////////////////////////
47 * Construct a new value.
50 : _type(UNKNOWN), _tied(false)
60 if (!_tied && _type == STRING) {
61 delete _value.string_val;
62 _value.string_val = 0;
68 FGValue::getRawBool () const
71 if (_value.bool_func.getter != 0)
72 return (*(_value.bool_func.getter))();
76 return _value.bool_val;
82 FGValue::getRawInt () const
85 if (_value.int_func.getter != 0)
86 return (*(_value.int_func.getter))();
90 return _value.int_val;
96 FGValue::getRawFloat () const
99 if (_value.float_func.getter != 0)
100 return (*(_value.float_func.getter))();
104 return _value.float_val;
110 FGValue::getRawDouble () const
113 if (_value.double_func.getter != 0)
114 return (*(_value.double_func.getter))();
118 return _value.double_val;
124 FGValue::getRawString () const
127 if (_value.string_func.getter != 0)
128 return (*(_value.string_func.getter))();
132 return *_value.string_val;
138 FGValue::setRawBool (bool value)
141 if (_value.bool_func.setter != 0) {
142 (*_value.bool_func.setter)(value);
148 _value.bool_val = value;
155 FGValue::setRawInt (int value)
158 if (_value.int_func.setter != 0) {
159 (*_value.int_func.setter)(value);
165 _value.int_val = value;
172 FGValue::setRawFloat (float value)
175 if (_value.float_func.setter != 0) {
176 (*_value.float_func.setter)(value);
182 _value.float_val = value;
189 FGValue::setRawDouble (double value)
192 if (_value.double_func.setter != 0) {
193 (*_value.double_func.setter)(value);
199 _value.double_val = value;
206 FGValue::setRawString (const string &value)
209 if (_value.string_func.setter != 0) {
210 (*_value.string_func.setter)(value);
216 if (_value.string_val == 0)
217 _value.string_val = new string;
218 *(_value.string_val) = value;
225 * Attempt to get the boolean value of a property.
228 FGValue::getBoolValue () const
236 return (getRawInt() == 0 ? false : true);
238 return (getRawFloat() == 0.0 ? false : true);
240 return (getRawDouble() == 0.0 ? false : true);
242 return (getRawString() == "false" ? false : true);
250 * Attempt to get the integer value of a property.
253 FGValue::getIntValue () const
263 return (int)(getRawFloat());
265 return (int)(getRawDouble());
267 return atoi(getRawString().c_str());
275 * Attempt to get the float value of a property.
278 FGValue::getFloatValue () const
284 return (float)(getRawBool());
286 return (float)(getRawInt());
288 return getRawFloat();
290 return (float)(getRawDouble());
292 return (float)atof(getRawString().c_str());
300 * Attempt to get the double value of a property.
303 FGValue::getDoubleValue () const
309 return (double)(getRawBool());
311 return (double)(getRawInt());
313 return (double)(getRawFloat());
315 return getRawDouble();
317 return atof(getRawString().c_str());
325 * Attempt to get the string value of a property.
328 FGValue::getStringValue () const
338 return getRawString();
346 FGValue::setBoolValue (bool value)
348 if (_type == UNKNOWN || _type == BOOL) {
350 return setRawBool(value);
358 FGValue::setIntValue (int value)
360 if (_type == UNKNOWN || _type == INT) {
362 return setRawInt(value);
370 FGValue::setFloatValue (float value)
372 if (_type == UNKNOWN || _type == FLOAT) {
374 return setRawFloat(value);
382 FGValue::setDoubleValue (double value)
384 if (_type == UNKNOWN || _type == DOUBLE) {
386 return setRawDouble(value);
394 FGValue::setStringValue (const string &value)
396 if (_type == UNKNOWN || _type == STRING) {
398 return setRawString(value);
406 FGValue::tieBool (bool_getter getter, bool_setter setter = 0,
407 bool useDefault = true)
412 if (useDefault && setter && _type != UNKNOWN)
413 (*setter)(getBoolValue());
416 _value.bool_func.getter = getter;
417 _value.bool_func.setter = setter;
424 FGValue::tieInt (int_getter getter, int_setter setter = 0,
425 bool useDefault = true)
430 if (useDefault && setter && _type != UNKNOWN)
431 (*setter)(getIntValue());
434 _value.int_func.getter = getter;
435 _value.int_func.setter = setter;
442 FGValue::tieFloat (float_getter getter, float_setter setter = 0,
443 bool useDefault = true)
448 if (useDefault && setter && _type != UNKNOWN)
449 (*setter)(getFloatValue());
452 _value.float_func.getter = getter;
453 _value.float_func.setter = setter;
460 FGValue::tieDouble (double_getter getter, double_setter setter = 0,
461 bool useDefault = true)
466 if (useDefault && setter && _type != UNKNOWN)
467 (*setter)(getDoubleValue());
470 _value.double_func.getter = getter;
471 _value.double_func.setter = setter;
478 FGValue::tieString (string_getter getter, string_setter setter = 0,
479 bool useDefault = true)
484 if (useDefault && setter && _type != UNKNOWN)
485 (*setter)(getStringValue());
487 delete _value.string_val;
490 _value.string_func.getter = getter;
491 _value.string_func.setter = setter;
498 FGValue::untieBool ()
500 if (_tied && _type == BOOL) {
501 bool value = getRawBool();
502 _value.bool_val = value;
514 if (_tied && _type == INT) {
515 int value = getRawInt();
516 _value.int_val = value;
526 FGValue::untieFloat ()
528 if (_tied && _type == FLOAT) {
529 float value = getRawFloat();
530 _value.float_val = value;
540 FGValue::untieDouble ()
542 if (_tied && _type == DOUBLE) {
543 double value = getRawDouble();
544 _value.double_val = value;
554 FGValue::untieString ()
556 if (_tied && _type == STRING) {
557 const string &value = getRawString();
558 _value.string_val = new string(value);
568 ////////////////////////////////////////////////////////////////////////
569 // Implementation of FGPropertyList.
570 ////////////////////////////////////////////////////////////////////////
572 FGPropertyList::FGPropertyList ()
576 FGPropertyList::~FGPropertyList ()
582 FGPropertyList::getValue (const string &name, bool create = false)
584 const_iterator el = _props.find(name);
585 if (el == _props.end()) {
589 FG_LOG(FG_GENERAL, FG_INFO, "Creating new property '" << name << '\'');
592 return &(_props[name]);
597 FGPropertyList::getValue (const string &name) const
599 value_map::const_iterator el = _props.find(name);
600 return &(el->second);
605 FGPropertyList::getBoolValue (const string &name) const
607 const FGValue * val = getValue(name);
611 return val->getBoolValue();
616 FGPropertyList::getIntValue (const string &name) const
618 const FGValue * val = getValue(name);
622 return val->getIntValue();
627 FGPropertyList::getFloatValue (const string &name) const
629 const FGValue * val = getValue(name);
633 return val->getFloatValue();
638 FGPropertyList::getDoubleValue (const string &name) const
640 const FGValue * val = getValue(name);
644 return val->getDoubleValue();
649 FGPropertyList::getStringValue (const string &name) const
651 const FGValue * val = getValue(name);
655 return val->getStringValue();
660 FGPropertyList::setBoolValue (const string &name, bool value)
662 return getValue(name, true)->setBoolValue(value);
667 FGPropertyList::setIntValue (const string &name, int value)
669 return getValue(name, true)->setIntValue(value);
674 FGPropertyList::setFloatValue (const string &name, float value)
676 return getValue(name, true)->setFloatValue(value);
681 FGPropertyList::setDoubleValue (const string &name, double value)
683 return getValue(name, true)->setDoubleValue(value);
688 FGPropertyList::setStringValue (const string &name, const string &value)
690 return getValue(name, true)->setStringValue(value);
695 FGPropertyList::tieBool (const string &name,
698 bool useDefault = true)
700 FG_LOG(FG_GENERAL, FG_INFO, "Tying bool property '" << name << '\'');
701 return getValue(name, true)->tieBool(getter, setter, useDefault);
706 FGPropertyList::tieInt (const string &name,
709 bool useDefault = true)
711 FG_LOG(FG_GENERAL, FG_INFO, "Tying int property '" << name << '\'');
712 return getValue(name, true)->tieInt(getter, setter, useDefault);
717 FGPropertyList::tieFloat (const string &name,
720 bool useDefault = true)
722 FG_LOG(FG_GENERAL, FG_INFO, "Tying float property '" << name << '\'');
723 return getValue(name, true)->tieFloat(getter, setter, useDefault);
728 FGPropertyList::tieDouble (const string &name,
729 double_getter getter,
730 double_setter setter,
731 bool useDefault = true)
733 FG_LOG(FG_GENERAL, FG_INFO, "Tying double property '" << name << '\'');
734 return getValue(name, true)->tieDouble(getter, setter, useDefault);
739 FGPropertyList::tieString (const string &name,
740 string_getter getter,
741 string_setter setter,
742 bool useDefault = true)
744 FG_LOG(FG_GENERAL, FG_INFO, "Tying string property '" << name << '\'');
745 return getValue(name, true)->tieString(getter, setter, useDefault);
750 FGPropertyList::untieBool (const string &name)
752 FG_LOG(FG_GENERAL, FG_INFO, "Untying bool property '" << name << '\'');
753 return getValue(name, true)->untieBool();
758 FGPropertyList::untieInt (const string &name)
760 FG_LOG(FG_GENERAL, FG_INFO, "Untying int property '" << name << '\'');
761 return getValue(name, true)->untieInt();
766 FGPropertyList::untieFloat (const string &name)
768 FG_LOG(FG_GENERAL, FG_INFO, "Untying float property '" << name << '\'');
769 return getValue(name, true)->untieFloat();
774 FGPropertyList::untieDouble (const string &name)
776 FG_LOG(FG_GENERAL, FG_INFO, "Untying double property '" << name << '\'');
777 return getValue(name, true)->untieDouble();
782 FGPropertyList::untieString (const string &name)
784 FG_LOG(FG_GENERAL, FG_INFO, "Untying string property '" << name << '\'');
785 return getValue(name, true)->untieString();
790 FGPropertyList::dumpToLog () const
792 const_iterator it = FGPropertyList::begin();
793 FG_LOG(FG_GENERAL, FG_INFO, "Begin property list dump...");
794 while (it != end()) {
795 FG_LOG(FG_GENERAL, FG_INFO, "Property: " << it->first);
798 FG_LOG(FG_GENERAL, FG_INFO, "...End property list dump");