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)
242 int pos = find_child(begin, end, index, _children);
244 return _children[pos];
248 template<typename Itr>
250 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
252 SGPropertyNode* node = getExistingChild(begin, end, index);
257 node = new SGPropertyNode(begin, end, index, this);
258 _children.push_back(node);
259 fireChildAdded(node);
266 template<typename SplitItr>
268 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
271 typedef typename SplitItr::value_type Range;
272 // Run off the end of the list
277 // Success! This is the one we want.
281 // Empty name at this point is empty, not root.
283 return find_node_aux(current, ++itr, create, last_index);
284 Range name = parse_name(current, token);
285 if (equals(name, "."))
286 return find_node_aux(current, ++itr, create, last_index);
287 if (equals(name, "..")) {
288 SGPropertyNode * parent = current->getParent();
290 throw string("attempt to move past root with '..'");
291 return find_node_aux(parent, ++itr, create, last_index);
294 if (last_index >= 0) {
295 // If we are at the last token and last_index is valid, use
296 // last_index as the index value
298 while (!(++itr).eof()) {
312 if (name.end() != token.end()) {
313 if (*name.end() == '[') {
314 typename Range::iterator i = name.end() + 1, end = token.end();
315 for (;i != end; ++i) {
317 index = (index * 10) + (*i - '0');
322 if (i == token.end() || *i != ']')
323 throw string("unterminated index (looking for ']')");
325 throw string("illegal characters in token: ")
326 + string(name.begin(), name.end());
330 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
331 index, create), itr, create,
335 // Internal function for parsing property paths. last_index provides
336 // and index value for the last node name token, if supplied.
337 template<typename Range>
339 find_node (SGPropertyNode * current,
344 using namespace boost;
345 typedef split_iterator<typename range_result_iterator<Range>::type>
348 PathSplitIterator itr
349 = make_split_iterator(path, first_finder("/", is_equal()));
350 if (*path.begin() == '/')
351 return find_node_aux(current->getRootNode(), itr, create, last_index);
353 return find_node_aux(current, itr, create, last_index);
356 ////////////////////////////////////////////////////////////////////////
357 // Private methods from SGPropertyNode (may be inlined for speed).
358 ////////////////////////////////////////////////////////////////////////
361 SGPropertyNode::get_bool () const
364 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
366 return _local_val.bool_val;
370 SGPropertyNode::get_int () const
373 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
375 return _local_val.int_val;
379 SGPropertyNode::get_long () const
382 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
384 return _local_val.long_val;
388 SGPropertyNode::get_float () const
391 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
393 return _local_val.float_val;
397 SGPropertyNode::get_double () const
400 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
402 return _local_val.double_val;
406 SGPropertyNode::get_string () const
409 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
411 return _local_val.string_val;
415 SGPropertyNode::set_bool (bool val)
418 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
425 _local_val.bool_val = val;
432 SGPropertyNode::set_int (int val)
435 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
442 _local_val.int_val = val;
449 SGPropertyNode::set_long (long val)
452 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
459 _local_val.long_val = val;
466 SGPropertyNode::set_float (float val)
469 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
476 _local_val.float_val = val;
483 SGPropertyNode::set_double (double val)
486 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
493 _local_val.double_val = val;
500 SGPropertyNode::set_string (const char * val)
503 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
510 delete [] _local_val.string_val;
511 _local_val.string_val = copy_string(val);
518 SGPropertyNode::clearValue ()
520 if (_type == props::ALIAS) {
523 } else if (_type != props::NONE) {
526 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
529 _local_val.int_val = SGRawValue<int>::DefaultValue();
532 _local_val.long_val = SGRawValue<long>::DefaultValue();
535 _local_val.float_val = SGRawValue<float>::DefaultValue();
538 _local_val.double_val = SGRawValue<double>::DefaultValue();
541 case props::UNSPECIFIED:
543 delete [] _local_val.string_val;
545 _local_val.string_val = 0;
547 default: // avoid compiler warning
559 * Get the value as a string.
562 SGPropertyNode::make_string () const
564 if (!getAttribute(READ))
568 return _value.alias->getStringValue();
570 return get_bool() ? "true" : "false";
572 case props::UNSPECIFIED:
591 sstr << std::setprecision(10) << get_double();
593 case props::EXTENDED:
595 props::Type realType = _value.val->getType();
596 // Perhaps this should be done for all types?
597 if (realType == props::VEC3D || realType == props::VEC4D)
599 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
605 _buffer = sstr.str();
606 return _buffer.c_str();
610 * Trace a write access for a property.
613 SGPropertyNode::trace_write () const
616 cerr << "TRACE: Write node " << getPath () << ", value \""
617 << make_string() << '"' << endl;
619 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
620 << ", value \"" << make_string() << '"');
625 * Trace a read access for a property.
628 SGPropertyNode::trace_read () const
631 cerr << "TRACE: Write node " << getPath () << ", value \""
632 << make_string() << '"' << endl;
634 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
635 << ", value \"" << make_string() << '"');
639 ////////////////////////////////////////////////////////////////////////
640 // Public methods from SGPropertyNode.
641 ////////////////////////////////////////////////////////////////////////
644 * Last used attribute
645 * Update as needed when enum Attribute is changed
647 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
650 * Default constructor: always creates a root node.
652 SGPropertyNode::SGPropertyNode ()
660 _local_val.string_val = 0;
668 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
669 : SGReferenced(node),
672 _parent(0), // don't copy the parent
676 _listeners(0) // CHECK!!
678 _local_val.string_val = 0;
680 if (_type == props::NONE)
682 if (_type == props::ALIAS) {
683 _value.alias = node._value.alias;
688 if (_tied || _type == props::EXTENDED) {
689 _value.val = node._value.val->clone();
694 set_bool(node.get_bool());
697 set_int(node.get_int());
700 set_long(node.get_long());
703 set_float(node.get_float());
706 set_double(node.get_double());
709 case props::UNSPECIFIED:
710 set_string(node.get_string());
719 * Convenience constructor.
721 template<typename Itr>
722 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
724 SGPropertyNode * parent)
733 _local_val.string_val = 0;
735 if (!validateName(_name))
736 throw string("plain name expected instead of '") + _name + '\'';
739 SGPropertyNode::SGPropertyNode (const string& name,
741 SGPropertyNode * parent)
750 _local_val.string_val = 0;
752 if (!validateName(name))
753 throw string("plain name expected instead of '") + _name + '\'';
759 SGPropertyNode::~SGPropertyNode ()
761 // zero out all parent pointers, else they might be dangling
762 for (unsigned i = 0; i < _children.size(); ++i)
763 _children[i]->_parent = 0;
767 vector<SGPropertyChangeListener*>::iterator it;
768 for (it = _listeners->begin(); it != _listeners->end(); ++it)
769 (*it)->unregister_property(this);
776 * Alias to another node.
779 SGPropertyNode::alias (SGPropertyNode * target)
781 if (target && (_type != props::ALIAS) && (!_tied))
785 _value.alias = target;
786 _type = props::ALIAS;
794 SG_LOG(SG_GENERAL, SG_ALERT,
795 "Failed to create alias for " << getPath() << ". "
796 "The target property does not exist.");
799 if (_type == props::ALIAS)
801 if (_value.alias == target)
802 return true; // ok, identical alias requested
803 SG_LOG(SG_GENERAL, SG_ALERT,
804 "Failed to create alias at " << target->getPath() << ". "
805 "Source "<< getPath() << " is already aliasing another property.");
810 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
811 "Source " << getPath() << " is a tied property.");
820 * Alias to another node by path.
823 SGPropertyNode::alias (const char * path)
825 return alias(getNode(path, true));
833 SGPropertyNode::unalias ()
835 if (_type != props::ALIAS)
843 * Get the target of an alias.
846 SGPropertyNode::getAliasTarget ()
848 return (_type == props::ALIAS ? _value.alias : 0);
852 const SGPropertyNode *
853 SGPropertyNode::getAliasTarget () const
855 return (_type == props::ALIAS ? _value.alias : 0);
859 * create a non-const child by name after the last node with the same name.
862 SGPropertyNode::addChild(const char * name, int min_index, bool append)
865 ? std::max(find_last_child(name, _children) + 1, min_index)
866 : first_unused_index(name, _children, min_index);
868 SGPropertyNode_ptr node;
869 node = new SGPropertyNode(name, name + strlen(name), pos, this);
870 _children.push_back(node);
871 fireChildAdded(node);
876 * Create multiple children with unused indices
878 simgear::PropertyList
879 SGPropertyNode::addChildren( const std::string& name,
884 simgear::PropertyList nodes;
885 std::set<int> used_indices;
889 // First grab all used indices. This saves us of testing every index if it
890 // is used for every element to be created
891 for( size_t i = 0; i < nodes.size(); i++ )
893 const SGPropertyNode* node = nodes[i];
895 if( node->getNameString() == name && node->getIndex() >= min_index )
896 used_indices.insert(node->getIndex());
901 // If we don't want to fill the holes just find last node
902 min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index);
905 for( int index = min_index;
906 index < std::numeric_limits<int>::max() && nodes.size() < count;
909 if( used_indices.find(index) == used_indices.end() )
911 SGPropertyNode_ptr node;
912 node = new SGPropertyNode(name, index, this);
913 _children.push_back(node);
914 fireChildAdded(node);
915 nodes.push_back(node);
923 * Get a non-const child by index.
926 SGPropertyNode::getChild (int position)
928 if (position >= 0 && position < nChildren())
929 return _children[position];
936 * Get a const child by index.
938 const SGPropertyNode *
939 SGPropertyNode::getChild (int position) const
941 if (position >= 0 && position < nChildren())
942 return _children[position];
949 * Get a non-const child by name and index, creating if necessary.
953 SGPropertyNode::getChild (const char * name, int index, bool create)
955 return getChildImpl(name, name + strlen(name), index, create);
959 SGPropertyNode::getChild (const string& name, int index, bool create)
961 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index);
965 node = new SGPropertyNode(name, index, this);
966 _children.push_back(node);
967 fireChildAdded(node);
975 * Get a const child by name and index.
977 const SGPropertyNode *
978 SGPropertyNode::getChild (const char * name, int index) const
980 int pos = find_child(name, name + strlen(name), index, _children);
982 return _children[pos];
989 * Get all children with the same name (but different indices).
992 SGPropertyNode::getChildren (const char * name) const
994 PropertyList children;
995 size_t max = _children.size();
997 for (size_t i = 0; i < max; i++)
998 if (compare_strings(_children[i]->getName(), name))
999 children.push_back(_children[i]);
1001 sort(children.begin(), children.end(), CompareIndices());
1007 * Remove child by position.
1010 SGPropertyNode::removeChild(int pos)
1012 SGPropertyNode_ptr node;
1013 if (pos < 0 || pos >= (int)_children.size())
1016 PropertyList::iterator it = _children.begin();
1018 node = _children[pos];
1019 _children.erase(it);
1021 node->setAttribute(REMOVED, true);
1023 fireChildRemoved(node);
1029 * Remove a child node
1032 SGPropertyNode::removeChild(const char * name, int index)
1034 SGPropertyNode_ptr ret;
1035 int pos = find_child(name, name + strlen(name), index, _children);
1037 ret = removeChild(pos);
1043 * Remove all children with the specified name.
1046 SGPropertyNode::removeChildren(const char * name)
1048 PropertyList children;
1050 for (int pos = static_cast<int>(_children.size() - 1); pos >= 0; pos--)
1051 if (compare_strings(_children[pos]->getName(), name))
1052 children.push_back(removeChild(pos));
1054 sort(children.begin(), children.end(), CompareIndices());
1059 SGPropertyNode::removeAllChildren()
1061 for(unsigned i = 0; i < _children.size(); ++i)
1063 SGPropertyNode_ptr& node = _children[i];
1065 node->setAttribute(REMOVED, true);
1067 fireChildRemoved(node);
1074 SGPropertyNode::getDisplayName (bool simplify) const
1076 string display_name = _name;
1077 if (_index != 0 || !simplify) {
1079 sstr << '[' << _index << ']';
1080 display_name += sstr.str();
1082 return display_name;
1086 SGPropertyNode::getPath (bool simplify) const
1088 typedef std::vector<SGConstPropertyNode_ptr> PList;
1090 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1091 pathList.push_back(node);
1093 for (PList::reverse_iterator itr = pathList.rbegin(),
1094 rend = pathList.rend();
1098 result += (*itr)->getDisplayName(simplify);
1104 SGPropertyNode::getType () const
1106 if (_type == props::ALIAS)
1107 return _value.alias->getType();
1108 else if (_type == props::EXTENDED)
1109 return _value.val->getType();
1116 SGPropertyNode::getBoolValue () const
1118 // Shortcut for common case
1119 if (_attr == (READ|WRITE) && _type == props::BOOL)
1122 if (getAttribute(TRACE_READ))
1124 if (!getAttribute(READ))
1125 return SGRawValue<bool>::DefaultValue();
1128 return _value.alias->getBoolValue();
1132 return get_int() == 0 ? false : true;
1134 return get_long() == 0L ? false : true;
1136 return get_float() == 0.0 ? false : true;
1138 return get_double() == 0.0L ? false : true;
1140 case props::UNSPECIFIED:
1141 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1144 return SGRawValue<bool>::DefaultValue();
1149 SGPropertyNode::getIntValue () const
1151 // Shortcut for common case
1152 if (_attr == (READ|WRITE) && _type == props::INT)
1155 if (getAttribute(TRACE_READ))
1157 if (!getAttribute(READ))
1158 return SGRawValue<int>::DefaultValue();
1161 return _value.alias->getIntValue();
1163 return int(get_bool());
1167 return int(get_long());
1169 return int(get_float());
1171 return int(get_double());
1173 case props::UNSPECIFIED:
1174 return atoi(get_string());
1177 return SGRawValue<int>::DefaultValue();
1182 SGPropertyNode::getLongValue () const
1184 // Shortcut for common case
1185 if (_attr == (READ|WRITE) && _type == props::LONG)
1188 if (getAttribute(TRACE_READ))
1190 if (!getAttribute(READ))
1191 return SGRawValue<long>::DefaultValue();
1194 return _value.alias->getLongValue();
1196 return long(get_bool());
1198 return long(get_int());
1202 return long(get_float());
1204 return long(get_double());
1206 case props::UNSPECIFIED:
1207 return strtol(get_string(), 0, 0);
1210 return SGRawValue<long>::DefaultValue();
1215 SGPropertyNode::getFloatValue () const
1217 // Shortcut for common case
1218 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1221 if (getAttribute(TRACE_READ))
1223 if (!getAttribute(READ))
1224 return SGRawValue<float>::DefaultValue();
1227 return _value.alias->getFloatValue();
1229 return float(get_bool());
1231 return float(get_int());
1233 return float(get_long());
1237 return float(get_double());
1239 case props::UNSPECIFIED:
1240 return atof(get_string());
1243 return SGRawValue<float>::DefaultValue();
1248 SGPropertyNode::getDoubleValue () const
1250 // Shortcut for common case
1251 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1252 return get_double();
1254 if (getAttribute(TRACE_READ))
1256 if (!getAttribute(READ))
1257 return SGRawValue<double>::DefaultValue();
1261 return _value.alias->getDoubleValue();
1263 return double(get_bool());
1265 return double(get_int());
1267 return double(get_long());
1269 return double(get_float());
1271 return get_double();
1273 case props::UNSPECIFIED:
1274 return strtod(get_string(), 0);
1277 return SGRawValue<double>::DefaultValue();
1282 SGPropertyNode::getStringValue () const
1284 // Shortcut for common case
1285 if (_attr == (READ|WRITE) && _type == props::STRING)
1286 return get_string();
1288 if (getAttribute(TRACE_READ))
1290 if (!getAttribute(READ))
1291 return SGRawValue<const char *>::DefaultValue();
1292 return make_string();
1296 SGPropertyNode::setBoolValue (bool value)
1298 // Shortcut for common case
1299 if (_attr == (READ|WRITE) && _type == props::BOOL)
1300 return set_bool(value);
1302 bool result = false;
1304 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1307 _type = props::BOOL;
1312 result = _value.alias->setBoolValue(value);
1315 result = set_bool(value);
1318 result = set_int(int(value));
1321 result = set_long(long(value));
1324 result = set_float(float(value));
1327 result = set_double(double(value));
1330 case props::UNSPECIFIED:
1331 result = set_string(value ? "true" : "false");
1338 if (getAttribute(TRACE_WRITE))
1344 SGPropertyNode::setIntValue (int value)
1346 // Shortcut for common case
1347 if (_attr == (READ|WRITE) && _type == props::INT)
1348 return set_int(value);
1350 bool result = false;
1352 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1355 _local_val.int_val = 0;
1360 result = _value.alias->setIntValue(value);
1363 result = set_bool(value == 0 ? false : true);
1366 result = set_int(value);
1369 result = set_long(long(value));
1372 result = set_float(float(value));
1375 result = set_double(double(value));
1378 case props::UNSPECIFIED: {
1380 sprintf(buf, "%d", value);
1381 result = set_string(buf);
1389 if (getAttribute(TRACE_WRITE))
1395 SGPropertyNode::setLongValue (long value)
1397 // Shortcut for common case
1398 if (_attr == (READ|WRITE) && _type == props::LONG)
1399 return set_long(value);
1401 bool result = false;
1403 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1405 _type = props::LONG;
1406 _local_val.long_val = 0L;
1411 result = _value.alias->setLongValue(value);
1414 result = set_bool(value == 0L ? false : true);
1417 result = set_int(int(value));
1420 result = set_long(value);
1423 result = set_float(float(value));
1426 result = set_double(double(value));
1429 case props::UNSPECIFIED: {
1431 sprintf(buf, "%ld", value);
1432 result = set_string(buf);
1440 if (getAttribute(TRACE_WRITE))
1446 SGPropertyNode::setFloatValue (float value)
1448 // Shortcut for common case
1449 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1450 return set_float(value);
1452 bool result = false;
1454 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1456 _type = props::FLOAT;
1457 _local_val.float_val = 0;
1462 result = _value.alias->setFloatValue(value);
1465 result = set_bool(value == 0.0 ? false : true);
1468 result = set_int(int(value));
1471 result = set_long(long(value));
1474 result = set_float(value);
1477 result = set_double(double(value));
1480 case props::UNSPECIFIED: {
1482 sprintf(buf, "%f", value);
1483 result = set_string(buf);
1491 if (getAttribute(TRACE_WRITE))
1497 SGPropertyNode::setDoubleValue (double value)
1499 // Shortcut for common case
1500 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1501 return set_double(value);
1503 bool result = false;
1505 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1507 _local_val.double_val = value;
1508 _type = props::DOUBLE;
1513 result = _value.alias->setDoubleValue(value);
1516 result = set_bool(value == 0.0L ? false : true);
1519 result = set_int(int(value));
1522 result = set_long(long(value));
1525 result = set_float(float(value));
1528 result = set_double(value);
1531 case props::UNSPECIFIED: {
1533 sprintf(buf, "%f", value);
1534 result = set_string(buf);
1542 if (getAttribute(TRACE_WRITE))
1548 SGPropertyNode::setStringValue (const char * value)
1550 // Shortcut for common case
1551 if (_attr == (READ|WRITE) && _type == props::STRING)
1552 return set_string(value);
1554 bool result = false;
1556 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1558 _type = props::STRING;
1563 result = _value.alias->setStringValue(value);
1566 result = set_bool((compare_strings(value, "true")
1567 || atoi(value)) ? true : false);
1570 result = set_int(atoi(value));
1573 result = set_long(strtol(value, 0, 0));
1576 result = set_float(atof(value));
1579 result = set_double(strtod(value, 0));
1582 case props::UNSPECIFIED:
1583 result = set_string(value);
1585 case props::EXTENDED:
1587 stringstream sstr(value);
1588 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1596 if (getAttribute(TRACE_WRITE))
1602 SGPropertyNode::setUnspecifiedValue (const char * value)
1604 bool result = false;
1606 if (_type == props::NONE) {
1608 _type = props::UNSPECIFIED;
1610 props::Type type = _type;
1611 if (type == props::EXTENDED)
1612 type = _value.val->getType();
1615 result = _value.alias->setUnspecifiedValue(value);
1618 result = set_bool((compare_strings(value, "true")
1619 || atoi(value)) ? true : false);
1622 result = set_int(atoi(value));
1625 result = set_long(strtol(value, 0, 0));
1628 result = set_float(atof(value));
1631 result = set_double(strtod(value, 0));
1634 case props::UNSPECIFIED:
1635 result = set_string(value);
1638 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1641 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1648 if (getAttribute(TRACE_WRITE))
1653 //------------------------------------------------------------------------------
1654 bool SGPropertyNode::interpolate( const std::string& type,
1655 const SGPropertyNode& target,
1657 const std::string& easing )
1659 if( !_interpolation_mgr )
1661 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1663 // no interpolation possible -> set to target immediately
1664 setUnspecifiedValue( target.getStringValue() );
1668 return _interpolation_mgr->interpolate(this, type, target, duration, easing);
1671 //------------------------------------------------------------------------------
1672 bool SGPropertyNode::interpolate( const std::string& type,
1673 const PropertyList& values,
1674 const double_list& deltas,
1675 const std::string& easing )
1677 if( !_interpolation_mgr )
1679 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1681 // no interpolation possible -> set to last value immediately
1682 if( !values.empty() )
1683 setUnspecifiedValue(values.back()->getStringValue());
1687 return _interpolation_mgr->interpolate(this, type, values, deltas, easing);
1690 //------------------------------------------------------------------------------
1691 void SGPropertyNode::setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr)
1693 _interpolation_mgr = mgr;
1696 simgear::PropertyInterpolationMgr* SGPropertyNode::_interpolation_mgr = 0;
1698 //------------------------------------------------------------------------------
1699 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1701 if (!getAttribute(READ))
1705 return _value.alias->printOn(stream);
1707 stream << (get_bool() ? "true" : "false");
1710 stream << get_int();
1713 stream << get_long();
1716 stream << get_float();
1719 stream << get_double();
1722 case props::UNSPECIFIED:
1723 stream << get_string();
1725 case props::EXTENDED:
1726 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1730 default: // avoid compiler warning
1737 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1740 if (_type == props::ALIAS || _tied)
1743 useDefault = useDefault && hasValue();
1744 std::string old_val;
1746 old_val = getStringValue();
1748 _type = props::STRING;
1750 _value.val = rawValue.clone();
1753 int save_attributes = getAttributes();
1754 setAttribute( WRITE, true );
1755 setStringValue(old_val.c_str());
1756 setAttributes( save_attributes );
1762 SGPropertyNode::untie ()
1769 bool val = getBoolValue();
1771 _type = props::BOOL;
1772 _local_val.bool_val = val;
1776 int val = getIntValue();
1779 _local_val.int_val = val;
1783 long val = getLongValue();
1785 _type = props::LONG;
1786 _local_val.long_val = val;
1789 case props::FLOAT: {
1790 float val = getFloatValue();
1792 _type = props::FLOAT;
1793 _local_val.float_val = val;
1796 case props::DOUBLE: {
1797 double val = getDoubleValue();
1799 _type = props::DOUBLE;
1800 _local_val.double_val = val;
1804 case props::UNSPECIFIED: {
1805 string val = getStringValue();
1807 _type = props::STRING;
1808 _local_val.string_val = copy_string(val.c_str());
1811 case props::EXTENDED: {
1812 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1813 _value.val = 0; // Prevent clearValue() from deleting
1815 _type = props::EXTENDED;
1816 _value.val = val->makeContainer();
1830 SGPropertyNode::getRootNode ()
1835 return _parent->getRootNode();
1838 const SGPropertyNode *
1839 SGPropertyNode::getRootNode () const
1844 return _parent->getRootNode();
1848 SGPropertyNode::getNode (const char * relative_path, bool create)
1850 using namespace boost;
1852 return find_node(this, make_iterator_range(relative_path, relative_path
1853 + strlen(relative_path)),
1858 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1860 using namespace boost;
1861 return find_node(this, make_iterator_range(relative_path, relative_path
1862 + strlen(relative_path)),
1866 const SGPropertyNode *
1867 SGPropertyNode::getNode (const char * relative_path) const
1869 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1872 const SGPropertyNode *
1873 SGPropertyNode::getNode (const char * relative_path, int index) const
1875 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1878 ////////////////////////////////////////////////////////////////////////
1879 // Convenience methods using relative paths.
1880 ////////////////////////////////////////////////////////////////////////
1884 * Test whether another node has a value attached.
1887 SGPropertyNode::hasValue (const char * relative_path) const
1889 const SGPropertyNode * node = getNode(relative_path);
1890 return (node == 0 ? false : node->hasValue());
1895 * Get the value type for another node.
1898 SGPropertyNode::getType (const char * relative_path) const
1900 const SGPropertyNode * node = getNode(relative_path);
1901 return (node == 0 ? props::UNSPECIFIED : node->getType());
1906 * Get a bool value for another node.
1909 SGPropertyNode::getBoolValue (const char * relative_path,
1910 bool defaultValue) const
1912 const SGPropertyNode * node = getNode(relative_path);
1913 return (node == 0 ? defaultValue : node->getBoolValue());
1918 * Get an int value for another node.
1921 SGPropertyNode::getIntValue (const char * relative_path,
1922 int defaultValue) const
1924 const SGPropertyNode * node = getNode(relative_path);
1925 return (node == 0 ? defaultValue : node->getIntValue());
1930 * Get a long value for another node.
1933 SGPropertyNode::getLongValue (const char * relative_path,
1934 long defaultValue) const
1936 const SGPropertyNode * node = getNode(relative_path);
1937 return (node == 0 ? defaultValue : node->getLongValue());
1942 * Get a float value for another node.
1945 SGPropertyNode::getFloatValue (const char * relative_path,
1946 float defaultValue) const
1948 const SGPropertyNode * node = getNode(relative_path);
1949 return (node == 0 ? defaultValue : node->getFloatValue());
1954 * Get a double value for another node.
1957 SGPropertyNode::getDoubleValue (const char * relative_path,
1958 double defaultValue) const
1960 const SGPropertyNode * node = getNode(relative_path);
1961 return (node == 0 ? defaultValue : node->getDoubleValue());
1966 * Get a string value for another node.
1969 SGPropertyNode::getStringValue (const char * relative_path,
1970 const char * defaultValue) const
1972 const SGPropertyNode * node = getNode(relative_path);
1973 return (node == 0 ? defaultValue : node->getStringValue());
1978 * Set a bool value for another node.
1981 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1983 return getNode(relative_path, true)->setBoolValue(value);
1988 * Set an int value for another node.
1991 SGPropertyNode::setIntValue (const char * relative_path, int value)
1993 return getNode(relative_path, true)->setIntValue(value);
1998 * Set a long value for another node.
2001 SGPropertyNode::setLongValue (const char * relative_path, long value)
2003 return getNode(relative_path, true)->setLongValue(value);
2008 * Set a float value for another node.
2011 SGPropertyNode::setFloatValue (const char * relative_path, float value)
2013 return getNode(relative_path, true)->setFloatValue(value);
2018 * Set a double value for another node.
2021 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
2023 return getNode(relative_path, true)->setDoubleValue(value);
2028 * Set a string value for another node.
2031 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
2033 return getNode(relative_path, true)->setStringValue(value);
2038 * Set an unknown value for another node.
2041 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
2044 return getNode(relative_path, true)->setUnspecifiedValue(value);
2049 * Test whether another node is tied.
2052 SGPropertyNode::isTied (const char * relative_path) const
2054 const SGPropertyNode * node = getNode(relative_path);
2055 return (node == 0 ? false : node->isTied());
2060 * Tie a node reached by a relative path, creating it if necessary.
2063 SGPropertyNode::tie (const char * relative_path,
2064 const SGRawValue<bool> &rawValue,
2067 return getNode(relative_path, true)->tie(rawValue, useDefault);
2072 * Tie a node reached by a relative path, creating it if necessary.
2075 SGPropertyNode::tie (const char * relative_path,
2076 const SGRawValue<int> &rawValue,
2079 return getNode(relative_path, true)->tie(rawValue, useDefault);
2084 * Tie a node reached by a relative path, creating it if necessary.
2087 SGPropertyNode::tie (const char * relative_path,
2088 const SGRawValue<long> &rawValue,
2091 return getNode(relative_path, true)->tie(rawValue, useDefault);
2096 * Tie a node reached by a relative path, creating it if necessary.
2099 SGPropertyNode::tie (const char * relative_path,
2100 const SGRawValue<float> &rawValue,
2103 return getNode(relative_path, true)->tie(rawValue, useDefault);
2108 * Tie a node reached by a relative path, creating it if necessary.
2111 SGPropertyNode::tie (const char * relative_path,
2112 const SGRawValue<double> &rawValue,
2115 return getNode(relative_path, true)->tie(rawValue, useDefault);
2120 * Tie a node reached by a relative path, creating it if necessary.
2123 SGPropertyNode::tie (const char * relative_path,
2124 const SGRawValue<const char *> &rawValue,
2127 return getNode(relative_path, true)->tie(rawValue, useDefault);
2132 * Attempt to untie another node reached by a relative path.
2135 SGPropertyNode::untie (const char * relative_path)
2137 SGPropertyNode * node = getNode(relative_path);
2138 return (node == 0 ? false : node->untie());
2142 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2145 if (_listeners == 0)
2146 _listeners = new vector<SGPropertyChangeListener*>;
2147 _listeners->push_back(listener);
2148 listener->register_property(this);
2150 listener->valueChanged(this);
2154 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2156 if (_listeners == 0)
2158 vector<SGPropertyChangeListener*>::iterator it =
2159 find(_listeners->begin(), _listeners->end(), listener);
2160 if (it != _listeners->end()) {
2161 _listeners->erase(it);
2162 listener->unregister_property(this);
2163 if (_listeners->empty()) {
2164 vector<SGPropertyChangeListener*>* tmp = _listeners;
2172 SGPropertyNode::fireValueChanged ()
2174 fireValueChanged(this);
2178 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2180 fireChildAdded(this, child);
2184 SGPropertyNode::fireCreatedRecursive(bool fire_self)
2188 _parent->fireChildAdded(this);
2190 if( _children.empty() && getType() != simgear::props::NONE )
2191 return fireValueChanged();
2194 for(size_t i = 0; i < _children.size(); ++i)
2195 _children[i]->fireCreatedRecursive(true);
2199 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2201 fireChildRemoved(this, child);
2205 SGPropertyNode::fireChildrenRemovedRecursive()
2207 for(size_t i = 0; i < _children.size(); ++i)
2209 SGPropertyNode* child = _children[i];
2210 fireChildRemoved(this, child);
2211 child->fireChildrenRemovedRecursive();
2216 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2218 if (_listeners != 0) {
2219 for (unsigned int i = 0; i < _listeners->size(); i++) {
2220 (*_listeners)[i]->valueChanged(node);
2224 _parent->fireValueChanged(node);
2228 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2229 SGPropertyNode * child)
2231 if (_listeners != 0) {
2232 for (unsigned int i = 0; i < _listeners->size(); i++) {
2233 (*_listeners)[i]->childAdded(parent, child);
2237 _parent->fireChildAdded(parent, child);
2241 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2242 SGPropertyNode * child)
2244 if (_listeners != 0) {
2245 for (unsigned int i = 0; i < _listeners->size(); i++) {
2246 (*_listeners)[i]->childRemoved(parent, child);
2250 _parent->fireChildRemoved(parent, child);
2253 ////////////////////////////////////////////////////////////////////////
2254 // Implementation of SGPropertyChangeListener.
2255 ////////////////////////////////////////////////////////////////////////
2257 SGPropertyChangeListener::~SGPropertyChangeListener ()
2259 for (int i = static_cast<int>(_properties.size() - 1); i >= 0; i--)
2260 _properties[i]->removeChangeListener(this);
2264 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2270 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2271 SGPropertyNode * child)
2277 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2278 SGPropertyNode * child)
2284 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2286 _properties.push_back(node);
2290 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2292 vector<SGPropertyNode *>::iterator it =
2293 find(_properties.begin(), _properties.end(), node);
2294 if (it != _properties.end())
2295 _properties.erase(it);
2299 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2302 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2303 for (int i = 0; i < 3; ++i) {
2314 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2316 for (int i = 0; i < 3; ++i) {
2317 stream >> result[i];
2323 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2326 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2327 for (int i = 0; i < 4; ++i) {
2338 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2340 for (int i = 0; i < 4; ++i) {
2341 stream >> result[i];
2348 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2350 props::Type ltype = lhs.getType();
2351 props::Type rtype = rhs.getType();
2358 return false; // XXX Should we look in aliases?
2360 return lhs.getValue<bool>() == rhs.getValue<bool>();
2362 return lhs.getValue<int>() == rhs.getValue<int>();
2364 return lhs.getValue<long>() == rhs.getValue<long>();
2366 return lhs.getValue<float>() == rhs.getValue<float>();
2368 return lhs.getValue<double>() == rhs.getValue<double>();
2370 case props::UNSPECIFIED:
2371 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2373 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2375 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2383 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2384 const SGPropertyNode& rhs)
2388 int lhsChildren = lhs.nChildren();
2389 int rhsChildren = rhs.nChildren();
2390 if (lhsChildren != rhsChildren)
2392 if (lhsChildren == 0)
2393 return compareNodeValue(lhs, rhs);
2394 for (size_t i = 0; i < lhs._children.size(); ++i) {
2395 const SGPropertyNode* lchild = lhs._children[i];
2396 const SGPropertyNode* rchild = rhs._children[i];
2397 // I'm guessing that the nodes will usually be in the same
2399 if (lchild->getIndex() != rchild->getIndex()
2400 || lchild->getNameString() != rchild->getNameString()) {
2402 for (PropertyList::const_iterator itr = rhs._children.begin(),
2403 end = rhs._children.end();
2406 if (lchild->getIndex() == (*itr)->getIndex()
2407 && lchild->getNameString() == (*itr)->getNameString()) {
2414 if (!compare(*lchild, *rchild))
2420 struct PropertyPlaceLess {
2421 typedef bool result_type;
2422 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2424 int comp = lhs->getNameString().compare(rhs->getNameString());
2426 return lhs->getIndex() < rhs->getIndex();
2432 size_t hash_value(const SGPropertyNode& node)
2434 using namespace boost;
2436 if (node.nChildren() == 0) {
2437 switch (node.getType()) {
2442 return hash_value(node.getValue<bool>());
2444 return hash_value(node.getValue<int>());
2446 return hash_value(node.getValue<long>());
2448 return hash_value(node.getValue<float>());
2450 return hash_value(node.getValue<double>());
2452 case props::UNSPECIFIED:
2454 const char *val = node.getStringValue();
2455 return hash_range(val, val + strlen(val));
2459 const SGVec3d val = node.getValue<SGVec3d>();
2460 return hash_range(&val[0], &val[3]);
2464 const SGVec4d val = node.getValue<SGVec4d>();
2465 return hash_range(&val[0], &val[4]);
2467 case props::ALIAS: // XXX Should we look in aliases?
2473 PropertyList children(node._children.begin(), node._children.end());
2474 sort(children.begin(), children.end(), PropertyPlaceLess());
2475 for (PropertyList::const_iterator itr = children.begin(),
2476 end = children.end();
2479 hash_combine(seed, (*itr)->_name);
2480 hash_combine(seed, (*itr)->_index);
2481 hash_combine(seed, hash_value(**itr));