1 // props.cxx - implementation of a property list.
2 // Started Fall 2000 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
5 // See props.html for documentation [replace with URL when available].
10 # include <simgear_config.h>
14 #include "PropertyInterpolationMgr.hxx"
15 #include "vectorPropTemplates.hxx"
27 #include <boost/algorithm/string/find_iterator.hpp>
28 #include <boost/algorithm/string/predicate.hpp>
29 #include <boost/algorithm/string/classification.hpp>
30 #include <boost/bind.hpp>
31 #include <boost/functional/hash.hpp>
32 #include <boost/range.hpp>
38 #include <simgear/compiler.h>
39 #include <simgear/debug/logstream.hxx>
41 #if ( _MSC_VER == 1200 )
42 // MSVC 6 is buggy, and needs something strange here
43 using std::vector<SGPropertyNode_ptr>;
44 using std::vector<SGPropertyChangeListener *>;
45 using std::vector<SGPropertyNode *>;
57 using std::stringstream;
59 using namespace simgear;
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * Comparator class for sorting by index.
71 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
72 return (n1->getIndex() < n2->getIndex());
77 ////////////////////////////////////////////////////////////////////////
78 // Convenience macros for value access.
79 ////////////////////////////////////////////////////////////////////////
81 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
82 #define TEST_WRITE if (!getAttribute(WRITE)) return false
84 ////////////////////////////////////////////////////////////////////////
85 // Local path normalization code.
86 ////////////////////////////////////////////////////////////////////////
89 * Parse the name for a path component.
91 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
94 template<typename Range>
96 parse_name (const SGPropertyNode *node, const Range &path)
98 typename Range::iterator i = path.begin();
99 typename Range::iterator max = path.end();
103 if (i != path.end() && *i == '.') {
106 if (i != max && *i != '/')
107 throw string("illegal character after . or ..");
108 } else if (isalpha(*i) || *i == '_') {
111 // The rules inside a name are a little
114 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
115 *i == '-' || *i == '.') {
117 } else if (*i == '[' || *i == '/') {
122 err.append("' found in propertyname after '"+node->getNameString()+"'");
123 err.append("\nname may contain only ._- and alphanumeric characters");
131 if (path.begin() == i) {
134 err.append("' found in propertyname after '"+node->getNameString()+"'");
135 err.append("\nname must begin with alpha or '_'");
139 return Range(path.begin(), i);
142 // Validate the name of a single node
143 inline bool validateName(const string& name)
145 using namespace boost;
148 if (!isalpha(name[0]) && name[0] != '_')
150 return all(make_iterator_range(name.begin(), name.end()),
151 is_alnum() || is_any_of("_-."));
154 ////////////////////////////////////////////////////////////////////////
155 // Other static utility functions.
156 ////////////////////////////////////////////////////////////////////////
160 copy_string (const char * s)
162 size_t slen = strlen(s);
163 char * copy = new char[slen + 1];
165 // the source string length is known so no need to check for '\0'
166 // when copying every single character
167 memcpy(copy, s, slen);
168 *(copy + slen) = '\0';
173 compare_strings (const char * s1, const char * s2)
175 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
179 * Locate a child node by name and index.
181 template<typename Itr>
183 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
185 size_t nNodes = nodes.size();
186 boost::iterator_range<Itr> name(begin, end);
187 for (size_t i = 0; i < nNodes; i++) {
188 SGPropertyNode * node = nodes[i];
190 // searching for a matching index is a lot less time consuming than
191 // comparing two strings so do that first.
192 if (node->getIndex() == index && boost::equals(node->getName(), name))
193 return static_cast<int>(i);
199 * Locate the child node with the highest index of the same name
202 find_last_child (const char * name, const PropertyList& nodes)
204 size_t nNodes = nodes.size();
207 for (size_t i = 0; i < nNodes; i++) {
208 SGPropertyNode * node = nodes[i];
209 if (compare_strings(node->getName(), name))
211 int idx = node->getIndex();
212 if (idx > index) index = idx;
219 * Get first unused index for child nodes with the given name
222 first_unused_index( const char * name,
223 const PropertyList& nodes,
226 const char* nameEnd = name + strlen(name);
228 for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
230 if( find_child(name, nameEnd, index, nodes) < 0 )
234 SG_LOG(SG_GENERAL, SG_ALERT, "Too many nodes: " << name);
238 template<typename Itr>
239 inline SGPropertyNode*
240 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
242 int pos = find_child(begin, end, index, _children);
244 return _children[pos];
246 SGPropertyNode_ptr node;
247 pos = find_child(begin, end, index, _removedChildren);
249 PropertyList::iterator it = _removedChildren.begin();
251 node = _removedChildren[pos];
252 _removedChildren.erase(it);
253 node->setAttribute(REMOVED, false);
254 _children.push_back(node);
255 fireChildAdded(node);
262 template<typename Itr>
264 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
266 SGPropertyNode* node = getExistingChild(begin, end, index, create);
271 node = new SGPropertyNode(begin, end, index, this);
272 _children.push_back(node);
273 fireChildAdded(node);
280 template<typename SplitItr>
282 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
285 typedef typename SplitItr::value_type Range;
286 // Run off the end of the list
291 // Success! This is the one we want.
295 // Empty name at this point is empty, not root.
297 return find_node_aux(current, ++itr, create, last_index);
298 Range name = parse_name(current, token);
299 if (equals(name, "."))
300 return find_node_aux(current, ++itr, create, last_index);
301 if (equals(name, "..")) {
302 SGPropertyNode * parent = current->getParent();
304 throw string("attempt to move past root with '..'");
305 return find_node_aux(parent, ++itr, create, last_index);
308 if (last_index >= 0) {
309 // If we are at the last token and last_index is valid, use
310 // last_index as the index value
312 while (!(++itr).eof()) {
326 if (name.end() != token.end()) {
327 if (*name.end() == '[') {
328 typename Range::iterator i = name.end() + 1, end = token.end();
329 for (;i != end; ++i) {
331 index = (index * 10) + (*i - '0');
336 if (i == token.end() || *i != ']')
337 throw string("unterminated index (looking for ']')");
339 throw string("illegal characters in token: ")
340 + string(name.begin(), name.end());
344 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
345 index, create), itr, create,
349 // Internal function for parsing property paths. last_index provides
350 // and index value for the last node name token, if supplied.
351 template<typename Range>
353 find_node (SGPropertyNode * current,
358 using namespace boost;
359 typedef split_iterator<typename range_result_iterator<Range>::type>
362 PathSplitIterator itr
363 = make_split_iterator(path, first_finder("/", is_equal()));
364 if (*path.begin() == '/')
365 return find_node_aux(current->getRootNode(), itr, create, last_index);
367 return find_node_aux(current, itr, create, last_index);
370 ////////////////////////////////////////////////////////////////////////
371 // Private methods from SGPropertyNode (may be inlined for speed).
372 ////////////////////////////////////////////////////////////////////////
375 SGPropertyNode::get_bool () const
378 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
380 return _local_val.bool_val;
384 SGPropertyNode::get_int () const
387 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
389 return _local_val.int_val;
393 SGPropertyNode::get_long () const
396 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
398 return _local_val.long_val;
402 SGPropertyNode::get_float () const
405 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
407 return _local_val.float_val;
411 SGPropertyNode::get_double () const
414 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
416 return _local_val.double_val;
420 SGPropertyNode::get_string () const
423 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
425 return _local_val.string_val;
429 SGPropertyNode::set_bool (bool val)
432 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
439 _local_val.bool_val = val;
446 SGPropertyNode::set_int (int val)
449 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
456 _local_val.int_val = val;
463 SGPropertyNode::set_long (long val)
466 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
473 _local_val.long_val = val;
480 SGPropertyNode::set_float (float val)
483 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
490 _local_val.float_val = val;
497 SGPropertyNode::set_double (double val)
500 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
507 _local_val.double_val = val;
514 SGPropertyNode::set_string (const char * val)
517 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
524 delete [] _local_val.string_val;
525 _local_val.string_val = copy_string(val);
532 SGPropertyNode::clearValue ()
534 if (_type == props::ALIAS) {
537 } else if (_type != props::NONE) {
540 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
543 _local_val.int_val = SGRawValue<int>::DefaultValue();
546 _local_val.long_val = SGRawValue<long>::DefaultValue();
549 _local_val.float_val = SGRawValue<float>::DefaultValue();
552 _local_val.double_val = SGRawValue<double>::DefaultValue();
555 case props::UNSPECIFIED:
557 delete [] _local_val.string_val;
559 _local_val.string_val = 0;
561 default: // avoid compiler warning
573 * Get the value as a string.
576 SGPropertyNode::make_string () const
578 if (!getAttribute(READ))
582 return _value.alias->getStringValue();
584 return get_bool() ? "true" : "false";
586 case props::UNSPECIFIED:
605 sstr << std::setprecision(10) << get_double();
607 case props::EXTENDED:
609 props::Type realType = _value.val->getType();
610 // Perhaps this should be done for all types?
611 if (realType == props::VEC3D || realType == props::VEC4D)
613 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
619 _buffer = sstr.str();
620 return _buffer.c_str();
624 * Trace a write access for a property.
627 SGPropertyNode::trace_write () const
630 cerr << "TRACE: Write node " << getPath () << ", value \""
631 << make_string() << '"' << endl;
633 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
634 << ", value \"" << make_string() << '"');
639 * Trace a read access for a property.
642 SGPropertyNode::trace_read () const
645 cerr << "TRACE: Write node " << getPath () << ", value \""
646 << make_string() << '"' << endl;
648 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
649 << ", value \"" << make_string() << '"');
653 ////////////////////////////////////////////////////////////////////////
654 // Public methods from SGPropertyNode.
655 ////////////////////////////////////////////////////////////////////////
658 * Last used attribute
659 * Update as needed when enum Attribute is changed
661 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
664 * Default constructor: always creates a root node.
666 SGPropertyNode::SGPropertyNode ()
674 _local_val.string_val = 0;
682 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
683 : SGReferenced(node),
686 _parent(0), // don't copy the parent
690 _listeners(0) // CHECK!!
692 _local_val.string_val = 0;
694 if (_type == props::NONE)
696 if (_type == props::ALIAS) {
697 _value.alias = node._value.alias;
702 if (_tied || _type == props::EXTENDED) {
703 _value.val = node._value.val->clone();
708 set_bool(node.get_bool());
711 set_int(node.get_int());
714 set_long(node.get_long());
717 set_float(node.get_float());
720 set_double(node.get_double());
723 case props::UNSPECIFIED:
724 set_string(node.get_string());
733 * Convenience constructor.
735 template<typename Itr>
736 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
738 SGPropertyNode * parent)
747 _local_val.string_val = 0;
749 if (!validateName(_name))
750 throw string("plain name expected instead of '") + _name + '\'';
753 SGPropertyNode::SGPropertyNode (const string& name,
755 SGPropertyNode * parent)
764 _local_val.string_val = 0;
766 if (!validateName(name))
767 throw string("plain name expected instead of '") + _name + '\'';
773 SGPropertyNode::~SGPropertyNode ()
775 // zero out all parent pointers, else they might be dangling
776 for (unsigned i = 0; i < _children.size(); ++i)
777 _children[i]->_parent = 0;
778 for (unsigned i = 0; i < _removedChildren.size(); ++i)
779 _removedChildren[i]->_parent = 0;
783 vector<SGPropertyChangeListener*>::iterator it;
784 for (it = _listeners->begin(); it != _listeners->end(); ++it)
785 (*it)->unregister_property(this);
792 * Alias to another node.
795 SGPropertyNode::alias (SGPropertyNode * target)
797 if (target && (_type != props::ALIAS) && (!_tied))
801 _value.alias = target;
802 _type = props::ALIAS;
810 SG_LOG(SG_GENERAL, SG_ALERT,
811 "Failed to create alias for " << getPath() << ". "
812 "The target property does not exist.");
815 if (_type == props::ALIAS)
817 if (_value.alias == target)
818 return true; // ok, identical alias requested
819 SG_LOG(SG_GENERAL, SG_ALERT,
820 "Failed to create alias at " << target->getPath() << ". "
821 "Source "<< getPath() << " is already aliasing another property.");
826 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
827 "Source " << getPath() << " is a tied property.");
836 * Alias to another node by path.
839 SGPropertyNode::alias (const char * path)
841 return alias(getNode(path, true));
849 SGPropertyNode::unalias ()
851 if (_type != props::ALIAS)
859 * Get the target of an alias.
862 SGPropertyNode::getAliasTarget ()
864 return (_type == props::ALIAS ? _value.alias : 0);
868 const SGPropertyNode *
869 SGPropertyNode::getAliasTarget () const
871 return (_type == props::ALIAS ? _value.alias : 0);
875 * create a non-const child by name after the last node with the same name.
878 SGPropertyNode::addChild(const char * name, int min_index, bool append)
881 ? std::max(find_last_child(name, _children) + 1, min_index)
882 : first_unused_index(name, _children, min_index);
884 SGPropertyNode_ptr node;
885 node = new SGPropertyNode(name, name + strlen(name), pos, this);
886 _children.push_back(node);
887 fireChildAdded(node);
892 * Create multiple children with unused indices
894 simgear::PropertyList
895 SGPropertyNode::addChildren( const std::string& name,
900 simgear::PropertyList nodes;
901 std::set<int> used_indices;
905 // First grab all used indices. This saves us of testing every index if it
906 // is used for every element to be created
907 for( size_t i = 0; i < nodes.size(); i++ )
909 const SGPropertyNode* node = nodes[i];
911 if( node->getNameString() == name && node->getIndex() >= min_index )
912 used_indices.insert(node->getIndex());
917 // If we don't want to fill the holes just find last node
918 min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index);
921 for( int index = min_index;
922 index < std::numeric_limits<int>::max() && nodes.size() < count;
925 if( used_indices.find(index) == used_indices.end() )
927 SGPropertyNode_ptr node;
928 node = new SGPropertyNode(name, index, this);
929 _children.push_back(node);
930 fireChildAdded(node);
931 nodes.push_back(node);
939 * Get a non-const child by index.
942 SGPropertyNode::getChild (int position)
944 if (position >= 0 && position < nChildren())
945 return _children[position];
952 * Get a const child by index.
954 const SGPropertyNode *
955 SGPropertyNode::getChild (int position) const
957 if (position >= 0 && position < nChildren())
958 return _children[position];
965 * Get a non-const child by name and index, creating if necessary.
969 SGPropertyNode::getChild (const char * name, int index, bool create)
971 return getChildImpl(name, name + strlen(name), index, create);
975 SGPropertyNode::getChild (const string& name, int index, bool create)
977 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
982 node = new SGPropertyNode(name, index, this);
983 _children.push_back(node);
984 fireChildAdded(node);
992 * Get a const child by name and index.
994 const SGPropertyNode *
995 SGPropertyNode::getChild (const char * name, int index) const
997 int pos = find_child(name, name + strlen(name), index, _children);
999 return _children[pos];
1006 * Get all children with the same name (but different indices).
1009 SGPropertyNode::getChildren (const char * name) const
1011 PropertyList children;
1012 size_t max = _children.size();
1014 for (size_t i = 0; i < max; i++)
1015 if (compare_strings(_children[i]->getName(), name))
1016 children.push_back(_children[i]);
1018 sort(children.begin(), children.end(), CompareIndices());
1024 * Remove child by position.
1027 SGPropertyNode::removeChild (int pos, bool keep)
1029 SGPropertyNode_ptr node;
1030 if (pos < 0 || pos >= (int)_children.size())
1033 PropertyList::iterator it = _children.begin();
1035 node = _children[pos];
1036 _children.erase(it);
1038 _removedChildren.push_back(node);
1041 node->setAttribute(REMOVED, true);
1043 fireChildRemoved(node);
1049 * Remove a child node
1052 SGPropertyNode::removeChild (const char * name, int index, bool keep)
1054 SGPropertyNode_ptr ret;
1055 int pos = find_child(name, name + strlen(name), index, _children);
1057 ret = removeChild(pos, keep);
1063 * Remove all children with the specified name.
1066 SGPropertyNode::removeChildren (const char * name, bool keep)
1068 PropertyList children;
1070 for (int pos = static_cast<int>(_children.size() - 1); pos >= 0; pos--)
1071 if (compare_strings(_children[pos]->getName(), name))
1072 children.push_back(removeChild(pos, keep));
1074 sort(children.begin(), children.end(), CompareIndices());
1079 SGPropertyNode::getDisplayName (bool simplify) const
1081 string display_name = _name;
1082 if (_index != 0 || !simplify) {
1084 sstr << '[' << _index << ']';
1085 display_name += sstr.str();
1087 return display_name;
1091 SGPropertyNode::getPath (bool simplify) const
1093 typedef std::vector<SGConstPropertyNode_ptr> PList;
1095 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1096 pathList.push_back(node);
1098 for (PList::reverse_iterator itr = pathList.rbegin(),
1099 rend = pathList.rend();
1103 result += (*itr)->getDisplayName(simplify);
1109 SGPropertyNode::getType () const
1111 if (_type == props::ALIAS)
1112 return _value.alias->getType();
1113 else if (_type == props::EXTENDED)
1114 return _value.val->getType();
1121 SGPropertyNode::getBoolValue () const
1123 // Shortcut for common case
1124 if (_attr == (READ|WRITE) && _type == props::BOOL)
1127 if (getAttribute(TRACE_READ))
1129 if (!getAttribute(READ))
1130 return SGRawValue<bool>::DefaultValue();
1133 return _value.alias->getBoolValue();
1137 return get_int() == 0 ? false : true;
1139 return get_long() == 0L ? false : true;
1141 return get_float() == 0.0 ? false : true;
1143 return get_double() == 0.0L ? false : true;
1145 case props::UNSPECIFIED:
1146 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1149 return SGRawValue<bool>::DefaultValue();
1154 SGPropertyNode::getIntValue () const
1156 // Shortcut for common case
1157 if (_attr == (READ|WRITE) && _type == props::INT)
1160 if (getAttribute(TRACE_READ))
1162 if (!getAttribute(READ))
1163 return SGRawValue<int>::DefaultValue();
1166 return _value.alias->getIntValue();
1168 return int(get_bool());
1172 return int(get_long());
1174 return int(get_float());
1176 return int(get_double());
1178 case props::UNSPECIFIED:
1179 return atoi(get_string());
1182 return SGRawValue<int>::DefaultValue();
1187 SGPropertyNode::getLongValue () const
1189 // Shortcut for common case
1190 if (_attr == (READ|WRITE) && _type == props::LONG)
1193 if (getAttribute(TRACE_READ))
1195 if (!getAttribute(READ))
1196 return SGRawValue<long>::DefaultValue();
1199 return _value.alias->getLongValue();
1201 return long(get_bool());
1203 return long(get_int());
1207 return long(get_float());
1209 return long(get_double());
1211 case props::UNSPECIFIED:
1212 return strtol(get_string(), 0, 0);
1215 return SGRawValue<long>::DefaultValue();
1220 SGPropertyNode::getFloatValue () const
1222 // Shortcut for common case
1223 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1226 if (getAttribute(TRACE_READ))
1228 if (!getAttribute(READ))
1229 return SGRawValue<float>::DefaultValue();
1232 return _value.alias->getFloatValue();
1234 return float(get_bool());
1236 return float(get_int());
1238 return float(get_long());
1242 return float(get_double());
1244 case props::UNSPECIFIED:
1245 return atof(get_string());
1248 return SGRawValue<float>::DefaultValue();
1253 SGPropertyNode::getDoubleValue () const
1255 // Shortcut for common case
1256 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1257 return get_double();
1259 if (getAttribute(TRACE_READ))
1261 if (!getAttribute(READ))
1262 return SGRawValue<double>::DefaultValue();
1266 return _value.alias->getDoubleValue();
1268 return double(get_bool());
1270 return double(get_int());
1272 return double(get_long());
1274 return double(get_float());
1276 return get_double();
1278 case props::UNSPECIFIED:
1279 return strtod(get_string(), 0);
1282 return SGRawValue<double>::DefaultValue();
1287 SGPropertyNode::getStringValue () const
1289 // Shortcut for common case
1290 if (_attr == (READ|WRITE) && _type == props::STRING)
1291 return get_string();
1293 if (getAttribute(TRACE_READ))
1295 if (!getAttribute(READ))
1296 return SGRawValue<const char *>::DefaultValue();
1297 return make_string();
1301 SGPropertyNode::setBoolValue (bool value)
1303 // Shortcut for common case
1304 if (_attr == (READ|WRITE) && _type == props::BOOL)
1305 return set_bool(value);
1307 bool result = false;
1309 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1312 _type = props::BOOL;
1317 result = _value.alias->setBoolValue(value);
1320 result = set_bool(value);
1323 result = set_int(int(value));
1326 result = set_long(long(value));
1329 result = set_float(float(value));
1332 result = set_double(double(value));
1335 case props::UNSPECIFIED:
1336 result = set_string(value ? "true" : "false");
1343 if (getAttribute(TRACE_WRITE))
1349 SGPropertyNode::setIntValue (int value)
1351 // Shortcut for common case
1352 if (_attr == (READ|WRITE) && _type == props::INT)
1353 return set_int(value);
1355 bool result = false;
1357 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1360 _local_val.int_val = 0;
1365 result = _value.alias->setIntValue(value);
1368 result = set_bool(value == 0 ? false : true);
1371 result = set_int(value);
1374 result = set_long(long(value));
1377 result = set_float(float(value));
1380 result = set_double(double(value));
1383 case props::UNSPECIFIED: {
1385 sprintf(buf, "%d", value);
1386 result = set_string(buf);
1394 if (getAttribute(TRACE_WRITE))
1400 SGPropertyNode::setLongValue (long value)
1402 // Shortcut for common case
1403 if (_attr == (READ|WRITE) && _type == props::LONG)
1404 return set_long(value);
1406 bool result = false;
1408 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1410 _type = props::LONG;
1411 _local_val.long_val = 0L;
1416 result = _value.alias->setLongValue(value);
1419 result = set_bool(value == 0L ? false : true);
1422 result = set_int(int(value));
1425 result = set_long(value);
1428 result = set_float(float(value));
1431 result = set_double(double(value));
1434 case props::UNSPECIFIED: {
1436 sprintf(buf, "%ld", value);
1437 result = set_string(buf);
1445 if (getAttribute(TRACE_WRITE))
1451 SGPropertyNode::setFloatValue (float value)
1453 // Shortcut for common case
1454 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1455 return set_float(value);
1457 bool result = false;
1459 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1461 _type = props::FLOAT;
1462 _local_val.float_val = 0;
1467 result = _value.alias->setFloatValue(value);
1470 result = set_bool(value == 0.0 ? false : true);
1473 result = set_int(int(value));
1476 result = set_long(long(value));
1479 result = set_float(value);
1482 result = set_double(double(value));
1485 case props::UNSPECIFIED: {
1487 sprintf(buf, "%f", value);
1488 result = set_string(buf);
1496 if (getAttribute(TRACE_WRITE))
1502 SGPropertyNode::setDoubleValue (double value)
1504 // Shortcut for common case
1505 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1506 return set_double(value);
1508 bool result = false;
1510 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1512 _local_val.double_val = value;
1513 _type = props::DOUBLE;
1518 result = _value.alias->setDoubleValue(value);
1521 result = set_bool(value == 0.0L ? false : true);
1524 result = set_int(int(value));
1527 result = set_long(long(value));
1530 result = set_float(float(value));
1533 result = set_double(value);
1536 case props::UNSPECIFIED: {
1538 sprintf(buf, "%f", value);
1539 result = set_string(buf);
1547 if (getAttribute(TRACE_WRITE))
1553 SGPropertyNode::setStringValue (const char * value)
1555 // Shortcut for common case
1556 if (_attr == (READ|WRITE) && _type == props::STRING)
1557 return set_string(value);
1559 bool result = false;
1561 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1563 _type = props::STRING;
1568 result = _value.alias->setStringValue(value);
1571 result = set_bool((compare_strings(value, "true")
1572 || atoi(value)) ? true : false);
1575 result = set_int(atoi(value));
1578 result = set_long(strtol(value, 0, 0));
1581 result = set_float(atof(value));
1584 result = set_double(strtod(value, 0));
1587 case props::UNSPECIFIED:
1588 result = set_string(value);
1590 case props::EXTENDED:
1592 stringstream sstr(value);
1593 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1601 if (getAttribute(TRACE_WRITE))
1607 SGPropertyNode::setUnspecifiedValue (const char * value)
1609 bool result = false;
1611 if (_type == props::NONE) {
1613 _type = props::UNSPECIFIED;
1615 props::Type type = _type;
1616 if (type == props::EXTENDED)
1617 type = _value.val->getType();
1620 result = _value.alias->setUnspecifiedValue(value);
1623 result = set_bool((compare_strings(value, "true")
1624 || atoi(value)) ? true : false);
1627 result = set_int(atoi(value));
1630 result = set_long(strtol(value, 0, 0));
1633 result = set_float(atof(value));
1636 result = set_double(strtod(value, 0));
1639 case props::UNSPECIFIED:
1640 result = set_string(value);
1643 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1646 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1653 if (getAttribute(TRACE_WRITE))
1658 //------------------------------------------------------------------------------
1659 bool SGPropertyNode::interpolate( const std::string& type,
1660 const SGPropertyNode& target,
1662 const std::string& easing )
1664 if( !_interpolation_mgr )
1666 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1668 // no interpolation possible -> set to target immediately
1669 setUnspecifiedValue( target.getStringValue() );
1673 return _interpolation_mgr->interpolate(this, type, target, duration, easing);
1676 //------------------------------------------------------------------------------
1677 bool SGPropertyNode::interpolate( const std::string& type,
1678 const PropertyList& values,
1679 const double_list& deltas,
1680 const std::string& easing )
1682 if( !_interpolation_mgr )
1684 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1686 // no interpolation possible -> set to last value immediately
1687 if( !values.empty() )
1688 setUnspecifiedValue(values.back()->getStringValue());
1692 return _interpolation_mgr->interpolate(this, type, values, deltas, easing);
1695 //------------------------------------------------------------------------------
1696 void SGPropertyNode::setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr)
1698 _interpolation_mgr = mgr;
1701 simgear::PropertyInterpolationMgr* SGPropertyNode::_interpolation_mgr = 0;
1703 //------------------------------------------------------------------------------
1704 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1706 if (!getAttribute(READ))
1710 return _value.alias->printOn(stream);
1712 stream << (get_bool() ? "true" : "false");
1715 stream << get_int();
1718 stream << get_long();
1721 stream << get_float();
1724 stream << get_double();
1727 case props::UNSPECIFIED:
1728 stream << get_string();
1730 case props::EXTENDED:
1731 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1735 default: // avoid compiler warning
1742 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1745 if (_type == props::ALIAS || _tied)
1748 useDefault = useDefault && hasValue();
1749 std::string old_val;
1751 old_val = getStringValue();
1753 _type = props::STRING;
1755 _value.val = rawValue.clone();
1758 int save_attributes = getAttributes();
1759 setAttribute( WRITE, true );
1760 setStringValue(old_val.c_str());
1761 setAttributes( save_attributes );
1767 SGPropertyNode::untie ()
1774 bool val = getBoolValue();
1776 _type = props::BOOL;
1777 _local_val.bool_val = val;
1781 int val = getIntValue();
1784 _local_val.int_val = val;
1788 long val = getLongValue();
1790 _type = props::LONG;
1791 _local_val.long_val = val;
1794 case props::FLOAT: {
1795 float val = getFloatValue();
1797 _type = props::FLOAT;
1798 _local_val.float_val = val;
1801 case props::DOUBLE: {
1802 double val = getDoubleValue();
1804 _type = props::DOUBLE;
1805 _local_val.double_val = val;
1809 case props::UNSPECIFIED: {
1810 string val = getStringValue();
1812 _type = props::STRING;
1813 _local_val.string_val = copy_string(val.c_str());
1816 case props::EXTENDED: {
1817 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1818 _value.val = 0; // Prevent clearValue() from deleting
1820 _type = props::EXTENDED;
1821 _value.val = val->makeContainer();
1835 SGPropertyNode::getRootNode ()
1840 return _parent->getRootNode();
1843 const SGPropertyNode *
1844 SGPropertyNode::getRootNode () const
1849 return _parent->getRootNode();
1853 SGPropertyNode::getNode (const char * relative_path, bool create)
1855 using namespace boost;
1857 return find_node(this, make_iterator_range(relative_path, relative_path
1858 + strlen(relative_path)),
1863 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1865 using namespace boost;
1866 return find_node(this, make_iterator_range(relative_path, relative_path
1867 + strlen(relative_path)),
1871 const SGPropertyNode *
1872 SGPropertyNode::getNode (const char * relative_path) const
1874 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1877 const SGPropertyNode *
1878 SGPropertyNode::getNode (const char * relative_path, int index) const
1880 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1883 ////////////////////////////////////////////////////////////////////////
1884 // Convenience methods using relative paths.
1885 ////////////////////////////////////////////////////////////////////////
1889 * Test whether another node has a value attached.
1892 SGPropertyNode::hasValue (const char * relative_path) const
1894 const SGPropertyNode * node = getNode(relative_path);
1895 return (node == 0 ? false : node->hasValue());
1900 * Get the value type for another node.
1903 SGPropertyNode::getType (const char * relative_path) const
1905 const SGPropertyNode * node = getNode(relative_path);
1906 return (node == 0 ? props::UNSPECIFIED : node->getType());
1911 * Get a bool value for another node.
1914 SGPropertyNode::getBoolValue (const char * relative_path,
1915 bool defaultValue) const
1917 const SGPropertyNode * node = getNode(relative_path);
1918 return (node == 0 ? defaultValue : node->getBoolValue());
1923 * Get an int value for another node.
1926 SGPropertyNode::getIntValue (const char * relative_path,
1927 int defaultValue) const
1929 const SGPropertyNode * node = getNode(relative_path);
1930 return (node == 0 ? defaultValue : node->getIntValue());
1935 * Get a long value for another node.
1938 SGPropertyNode::getLongValue (const char * relative_path,
1939 long defaultValue) const
1941 const SGPropertyNode * node = getNode(relative_path);
1942 return (node == 0 ? defaultValue : node->getLongValue());
1947 * Get a float value for another node.
1950 SGPropertyNode::getFloatValue (const char * relative_path,
1951 float defaultValue) const
1953 const SGPropertyNode * node = getNode(relative_path);
1954 return (node == 0 ? defaultValue : node->getFloatValue());
1959 * Get a double value for another node.
1962 SGPropertyNode::getDoubleValue (const char * relative_path,
1963 double defaultValue) const
1965 const SGPropertyNode * node = getNode(relative_path);
1966 return (node == 0 ? defaultValue : node->getDoubleValue());
1971 * Get a string value for another node.
1974 SGPropertyNode::getStringValue (const char * relative_path,
1975 const char * defaultValue) const
1977 const SGPropertyNode * node = getNode(relative_path);
1978 return (node == 0 ? defaultValue : node->getStringValue());
1983 * Set a bool value for another node.
1986 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1988 return getNode(relative_path, true)->setBoolValue(value);
1993 * Set an int value for another node.
1996 SGPropertyNode::setIntValue (const char * relative_path, int value)
1998 return getNode(relative_path, true)->setIntValue(value);
2003 * Set a long value for another node.
2006 SGPropertyNode::setLongValue (const char * relative_path, long value)
2008 return getNode(relative_path, true)->setLongValue(value);
2013 * Set a float value for another node.
2016 SGPropertyNode::setFloatValue (const char * relative_path, float value)
2018 return getNode(relative_path, true)->setFloatValue(value);
2023 * Set a double value for another node.
2026 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
2028 return getNode(relative_path, true)->setDoubleValue(value);
2033 * Set a string value for another node.
2036 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
2038 return getNode(relative_path, true)->setStringValue(value);
2043 * Set an unknown value for another node.
2046 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
2049 return getNode(relative_path, true)->setUnspecifiedValue(value);
2054 * Test whether another node is tied.
2057 SGPropertyNode::isTied (const char * relative_path) const
2059 const SGPropertyNode * node = getNode(relative_path);
2060 return (node == 0 ? false : node->isTied());
2065 * Tie a node reached by a relative path, creating it if necessary.
2068 SGPropertyNode::tie (const char * relative_path,
2069 const SGRawValue<bool> &rawValue,
2072 return getNode(relative_path, true)->tie(rawValue, useDefault);
2077 * Tie a node reached by a relative path, creating it if necessary.
2080 SGPropertyNode::tie (const char * relative_path,
2081 const SGRawValue<int> &rawValue,
2084 return getNode(relative_path, true)->tie(rawValue, useDefault);
2089 * Tie a node reached by a relative path, creating it if necessary.
2092 SGPropertyNode::tie (const char * relative_path,
2093 const SGRawValue<long> &rawValue,
2096 return getNode(relative_path, true)->tie(rawValue, useDefault);
2101 * Tie a node reached by a relative path, creating it if necessary.
2104 SGPropertyNode::tie (const char * relative_path,
2105 const SGRawValue<float> &rawValue,
2108 return getNode(relative_path, true)->tie(rawValue, useDefault);
2113 * Tie a node reached by a relative path, creating it if necessary.
2116 SGPropertyNode::tie (const char * relative_path,
2117 const SGRawValue<double> &rawValue,
2120 return getNode(relative_path, true)->tie(rawValue, useDefault);
2125 * Tie a node reached by a relative path, creating it if necessary.
2128 SGPropertyNode::tie (const char * relative_path,
2129 const SGRawValue<const char *> &rawValue,
2132 return getNode(relative_path, true)->tie(rawValue, useDefault);
2137 * Attempt to untie another node reached by a relative path.
2140 SGPropertyNode::untie (const char * relative_path)
2142 SGPropertyNode * node = getNode(relative_path);
2143 return (node == 0 ? false : node->untie());
2147 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2150 if (_listeners == 0)
2151 _listeners = new vector<SGPropertyChangeListener*>;
2152 _listeners->push_back(listener);
2153 listener->register_property(this);
2155 listener->valueChanged(this);
2159 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2161 if (_listeners == 0)
2163 vector<SGPropertyChangeListener*>::iterator it =
2164 find(_listeners->begin(), _listeners->end(), listener);
2165 if (it != _listeners->end()) {
2166 _listeners->erase(it);
2167 listener->unregister_property(this);
2168 if (_listeners->empty()) {
2169 vector<SGPropertyChangeListener*>* tmp = _listeners;
2177 SGPropertyNode::fireValueChanged ()
2179 fireValueChanged(this);
2183 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2185 fireChildAdded(this, child);
2189 SGPropertyNode::fireCreatedRecursive(bool fire_self)
2193 _parent->fireChildAdded(this);
2195 if( _children.empty() && getType() != simgear::props::NONE )
2196 return fireValueChanged();
2199 for(size_t i = 0; i < _children.size(); ++i)
2200 _children[i]->fireCreatedRecursive(true);
2204 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2206 fireChildRemoved(this, child);
2210 SGPropertyNode::fireChildrenRemovedRecursive()
2212 for(size_t i = 0; i < _children.size(); ++i)
2214 SGPropertyNode* child = _children[i];
2215 fireChildRemoved(this, child);
2216 child->fireChildrenRemovedRecursive();
2221 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2223 if (_listeners != 0) {
2224 for (unsigned int i = 0; i < _listeners->size(); i++) {
2225 (*_listeners)[i]->valueChanged(node);
2229 _parent->fireValueChanged(node);
2233 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2234 SGPropertyNode * child)
2236 if (_listeners != 0) {
2237 for (unsigned int i = 0; i < _listeners->size(); i++) {
2238 (*_listeners)[i]->childAdded(parent, child);
2242 _parent->fireChildAdded(parent, child);
2246 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2247 SGPropertyNode * child)
2249 if (_listeners != 0) {
2250 for (unsigned int i = 0; i < _listeners->size(); i++) {
2251 (*_listeners)[i]->childRemoved(parent, child);
2255 _parent->fireChildRemoved(parent, child);
2258 ////////////////////////////////////////////////////////////////////////
2259 // Implementation of SGPropertyChangeListener.
2260 ////////////////////////////////////////////////////////////////////////
2262 SGPropertyChangeListener::~SGPropertyChangeListener ()
2264 for (int i = static_cast<int>(_properties.size() - 1); i >= 0; i--)
2265 _properties[i]->removeChangeListener(this);
2269 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2275 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2276 SGPropertyNode * child)
2282 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2283 SGPropertyNode * child)
2289 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2291 _properties.push_back(node);
2295 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2297 vector<SGPropertyNode *>::iterator it =
2298 find(_properties.begin(), _properties.end(), node);
2299 if (it != _properties.end())
2300 _properties.erase(it);
2304 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2307 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2308 for (int i = 0; i < 3; ++i) {
2319 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2321 for (int i = 0; i < 3; ++i) {
2322 stream >> result[i];
2328 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2331 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2332 for (int i = 0; i < 4; ++i) {
2343 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2345 for (int i = 0; i < 4; ++i) {
2346 stream >> result[i];
2353 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2355 props::Type ltype = lhs.getType();
2356 props::Type rtype = rhs.getType();
2363 return false; // XXX Should we look in aliases?
2365 return lhs.getValue<bool>() == rhs.getValue<bool>();
2367 return lhs.getValue<int>() == rhs.getValue<int>();
2369 return lhs.getValue<long>() == rhs.getValue<long>();
2371 return lhs.getValue<float>() == rhs.getValue<float>();
2373 return lhs.getValue<double>() == rhs.getValue<double>();
2375 case props::UNSPECIFIED:
2376 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2378 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2380 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2388 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2389 const SGPropertyNode& rhs)
2393 int lhsChildren = lhs.nChildren();
2394 int rhsChildren = rhs.nChildren();
2395 if (lhsChildren != rhsChildren)
2397 if (lhsChildren == 0)
2398 return compareNodeValue(lhs, rhs);
2399 for (size_t i = 0; i < lhs._children.size(); ++i) {
2400 const SGPropertyNode* lchild = lhs._children[i];
2401 const SGPropertyNode* rchild = rhs._children[i];
2402 // I'm guessing that the nodes will usually be in the same
2404 if (lchild->getIndex() != rchild->getIndex()
2405 || lchild->getNameString() != rchild->getNameString()) {
2407 for (PropertyList::const_iterator itr = rhs._children.begin(),
2408 end = rhs._children.end();
2411 if (lchild->getIndex() == (*itr)->getIndex()
2412 && lchild->getNameString() == (*itr)->getNameString()) {
2419 if (!compare(*lchild, *rchild))
2425 struct PropertyPlaceLess {
2426 typedef bool result_type;
2427 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2429 int comp = lhs->getNameString().compare(rhs->getNameString());
2431 return lhs->getIndex() < rhs->getIndex();
2437 size_t hash_value(const SGPropertyNode& node)
2439 using namespace boost;
2441 if (node.nChildren() == 0) {
2442 switch (node.getType()) {
2447 return hash_value(node.getValue<bool>());
2449 return hash_value(node.getValue<int>());
2451 return hash_value(node.getValue<long>());
2453 return hash_value(node.getValue<float>());
2455 return hash_value(node.getValue<double>());
2457 case props::UNSPECIFIED:
2459 const char *val = node.getStringValue();
2460 return hash_range(val, val + strlen(val));
2464 const SGVec3d val = node.getValue<SGVec3d>();
2465 return hash_range(&val[0], &val[3]);
2469 const SGVec4d val = node.getValue<SGVec4d>();
2470 return hash_range(&val[0], &val[4]);
2472 case props::ALIAS: // XXX Should we look in aliases?
2478 PropertyList children(node._children.begin(), node._children.end());
2479 sort(children.begin(), children.end(), PropertyPlaceLess());
2480 for (PropertyList::const_iterator itr = children.begin(),
2481 end = children.end();
2484 hash_combine(seed, (*itr)->_name);
2485 hash_combine(seed, (*itr)->_index);
2486 hash_combine(seed, hash_value(**itr));