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());
1005 //------------------------------------------------------------------------------
1006 bool SGPropertyNode::removeChild(SGPropertyNode* node)
1008 if( node->_parent != this )
1011 PropertyList::iterator it =
1012 std::find(_children.begin(), _children.end(), node);
1013 if( it == _children.end() )
1020 //------------------------------------------------------------------------------
1021 SGPropertyNode_ptr SGPropertyNode::removeChild(int pos)
1023 if (pos < 0 || pos >= (int)_children.size())
1024 return SGPropertyNode_ptr();
1026 return eraseChild(_children.begin() + pos);
1031 * Remove a child node
1034 SGPropertyNode::removeChild(const char * name, int index)
1036 SGPropertyNode_ptr ret;
1037 int pos = find_child(name, name + strlen(name), index, _children);
1039 ret = removeChild(pos);
1045 * Remove all children with the specified name.
1048 SGPropertyNode::removeChildren(const char * name)
1050 PropertyList children;
1052 for (int pos = static_cast<int>(_children.size() - 1); pos >= 0; pos--)
1053 if (compare_strings(_children[pos]->getName(), name))
1054 children.push_back(removeChild(pos));
1056 sort(children.begin(), children.end(), CompareIndices());
1061 SGPropertyNode::removeAllChildren()
1063 for(unsigned i = 0; i < _children.size(); ++i)
1065 SGPropertyNode_ptr& node = _children[i];
1067 node->setAttribute(REMOVED, true);
1069 fireChildRemoved(node);
1076 SGPropertyNode::getDisplayName (bool simplify) const
1078 string display_name = _name;
1079 if (_index != 0 || !simplify) {
1081 sstr << '[' << _index << ']';
1082 display_name += sstr.str();
1084 return display_name;
1088 SGPropertyNode::getPath (bool simplify) const
1090 typedef std::vector<SGConstPropertyNode_ptr> PList;
1092 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1093 pathList.push_back(node);
1095 for (PList::reverse_iterator itr = pathList.rbegin(),
1096 rend = pathList.rend();
1100 result += (*itr)->getDisplayName(simplify);
1106 SGPropertyNode::getType () const
1108 if (_type == props::ALIAS)
1109 return _value.alias->getType();
1110 else if (_type == props::EXTENDED)
1111 return _value.val->getType();
1118 SGPropertyNode::getBoolValue () const
1120 // Shortcut for common case
1121 if (_attr == (READ|WRITE) && _type == props::BOOL)
1124 if (getAttribute(TRACE_READ))
1126 if (!getAttribute(READ))
1127 return SGRawValue<bool>::DefaultValue();
1130 return _value.alias->getBoolValue();
1134 return get_int() == 0 ? false : true;
1136 return get_long() == 0L ? false : true;
1138 return get_float() == 0.0 ? false : true;
1140 return get_double() == 0.0L ? false : true;
1142 case props::UNSPECIFIED:
1143 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1146 return SGRawValue<bool>::DefaultValue();
1151 SGPropertyNode::getIntValue () const
1153 // Shortcut for common case
1154 if (_attr == (READ|WRITE) && _type == props::INT)
1157 if (getAttribute(TRACE_READ))
1159 if (!getAttribute(READ))
1160 return SGRawValue<int>::DefaultValue();
1163 return _value.alias->getIntValue();
1165 return int(get_bool());
1169 return int(get_long());
1171 return int(get_float());
1173 return int(get_double());
1175 case props::UNSPECIFIED:
1176 return atoi(get_string());
1179 return SGRawValue<int>::DefaultValue();
1184 SGPropertyNode::getLongValue () const
1186 // Shortcut for common case
1187 if (_attr == (READ|WRITE) && _type == props::LONG)
1190 if (getAttribute(TRACE_READ))
1192 if (!getAttribute(READ))
1193 return SGRawValue<long>::DefaultValue();
1196 return _value.alias->getLongValue();
1198 return long(get_bool());
1200 return long(get_int());
1204 return long(get_float());
1206 return long(get_double());
1208 case props::UNSPECIFIED:
1209 return strtol(get_string(), 0, 0);
1212 return SGRawValue<long>::DefaultValue();
1217 SGPropertyNode::getFloatValue () const
1219 // Shortcut for common case
1220 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1223 if (getAttribute(TRACE_READ))
1225 if (!getAttribute(READ))
1226 return SGRawValue<float>::DefaultValue();
1229 return _value.alias->getFloatValue();
1231 return float(get_bool());
1233 return float(get_int());
1235 return float(get_long());
1239 return float(get_double());
1241 case props::UNSPECIFIED:
1242 return atof(get_string());
1245 return SGRawValue<float>::DefaultValue();
1250 SGPropertyNode::getDoubleValue () const
1252 // Shortcut for common case
1253 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1254 return get_double();
1256 if (getAttribute(TRACE_READ))
1258 if (!getAttribute(READ))
1259 return SGRawValue<double>::DefaultValue();
1263 return _value.alias->getDoubleValue();
1265 return double(get_bool());
1267 return double(get_int());
1269 return double(get_long());
1271 return double(get_float());
1273 return get_double();
1275 case props::UNSPECIFIED:
1276 return strtod(get_string(), 0);
1279 return SGRawValue<double>::DefaultValue();
1284 SGPropertyNode::getStringValue () const
1286 // Shortcut for common case
1287 if (_attr == (READ|WRITE) && _type == props::STRING)
1288 return get_string();
1290 if (getAttribute(TRACE_READ))
1292 if (!getAttribute(READ))
1293 return SGRawValue<const char *>::DefaultValue();
1294 return make_string();
1298 SGPropertyNode::setBoolValue (bool value)
1300 // Shortcut for common case
1301 if (_attr == (READ|WRITE) && _type == props::BOOL)
1302 return set_bool(value);
1304 bool result = false;
1306 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1309 _type = props::BOOL;
1314 result = _value.alias->setBoolValue(value);
1317 result = set_bool(value);
1320 result = set_int(int(value));
1323 result = set_long(long(value));
1326 result = set_float(float(value));
1329 result = set_double(double(value));
1332 case props::UNSPECIFIED:
1333 result = set_string(value ? "true" : "false");
1340 if (getAttribute(TRACE_WRITE))
1346 SGPropertyNode::setIntValue (int value)
1348 // Shortcut for common case
1349 if (_attr == (READ|WRITE) && _type == props::INT)
1350 return set_int(value);
1352 bool result = false;
1354 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1357 _local_val.int_val = 0;
1362 result = _value.alias->setIntValue(value);
1365 result = set_bool(value == 0 ? false : true);
1368 result = set_int(value);
1371 result = set_long(long(value));
1374 result = set_float(float(value));
1377 result = set_double(double(value));
1380 case props::UNSPECIFIED: {
1382 sprintf(buf, "%d", value);
1383 result = set_string(buf);
1391 if (getAttribute(TRACE_WRITE))
1397 SGPropertyNode::setLongValue (long value)
1399 // Shortcut for common case
1400 if (_attr == (READ|WRITE) && _type == props::LONG)
1401 return set_long(value);
1403 bool result = false;
1405 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1407 _type = props::LONG;
1408 _local_val.long_val = 0L;
1413 result = _value.alias->setLongValue(value);
1416 result = set_bool(value == 0L ? false : true);
1419 result = set_int(int(value));
1422 result = set_long(value);
1425 result = set_float(float(value));
1428 result = set_double(double(value));
1431 case props::UNSPECIFIED: {
1433 sprintf(buf, "%ld", value);
1434 result = set_string(buf);
1442 if (getAttribute(TRACE_WRITE))
1448 SGPropertyNode::setFloatValue (float value)
1450 // Shortcut for common case
1451 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1452 return set_float(value);
1454 bool result = false;
1456 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1458 _type = props::FLOAT;
1459 _local_val.float_val = 0;
1464 result = _value.alias->setFloatValue(value);
1467 result = set_bool(value == 0.0 ? false : true);
1470 result = set_int(int(value));
1473 result = set_long(long(value));
1476 result = set_float(value);
1479 result = set_double(double(value));
1482 case props::UNSPECIFIED: {
1484 sprintf(buf, "%f", value);
1485 result = set_string(buf);
1493 if (getAttribute(TRACE_WRITE))
1499 SGPropertyNode::setDoubleValue (double value)
1501 // Shortcut for common case
1502 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1503 return set_double(value);
1505 bool result = false;
1507 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1509 _local_val.double_val = value;
1510 _type = props::DOUBLE;
1515 result = _value.alias->setDoubleValue(value);
1518 result = set_bool(value == 0.0L ? false : true);
1521 result = set_int(int(value));
1524 result = set_long(long(value));
1527 result = set_float(float(value));
1530 result = set_double(value);
1533 case props::UNSPECIFIED: {
1535 sprintf(buf, "%f", value);
1536 result = set_string(buf);
1544 if (getAttribute(TRACE_WRITE))
1550 SGPropertyNode::setStringValue (const char * value)
1552 // Shortcut for common case
1553 if (_attr == (READ|WRITE) && _type == props::STRING)
1554 return set_string(value);
1556 bool result = false;
1558 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1560 _type = props::STRING;
1565 result = _value.alias->setStringValue(value);
1568 result = set_bool((compare_strings(value, "true")
1569 || atoi(value)) ? true : false);
1572 result = set_int(atoi(value));
1575 result = set_long(strtol(value, 0, 0));
1578 result = set_float(atof(value));
1581 result = set_double(strtod(value, 0));
1584 case props::UNSPECIFIED:
1585 result = set_string(value);
1587 case props::EXTENDED:
1589 stringstream sstr(value);
1590 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1598 if (getAttribute(TRACE_WRITE))
1604 SGPropertyNode::setUnspecifiedValue (const char * value)
1606 bool result = false;
1608 if (_type == props::NONE) {
1610 _type = props::UNSPECIFIED;
1612 props::Type type = _type;
1613 if (type == props::EXTENDED)
1614 type = _value.val->getType();
1617 result = _value.alias->setUnspecifiedValue(value);
1620 result = set_bool((compare_strings(value, "true")
1621 || atoi(value)) ? true : false);
1624 result = set_int(atoi(value));
1627 result = set_long(strtol(value, 0, 0));
1630 result = set_float(atof(value));
1633 result = set_double(strtod(value, 0));
1636 case props::UNSPECIFIED:
1637 result = set_string(value);
1640 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1643 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1650 if (getAttribute(TRACE_WRITE))
1655 //------------------------------------------------------------------------------
1656 bool SGPropertyNode::interpolate( const std::string& type,
1657 const SGPropertyNode& target,
1659 const std::string& easing )
1661 if( !_interpolation_mgr )
1663 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1665 // no interpolation possible -> set to target immediately
1666 setUnspecifiedValue( target.getStringValue() );
1670 return _interpolation_mgr->interpolate(this, type, target, duration, easing);
1673 //------------------------------------------------------------------------------
1674 bool SGPropertyNode::interpolate( const std::string& type,
1675 const PropertyList& values,
1676 const double_list& deltas,
1677 const std::string& easing )
1679 if( !_interpolation_mgr )
1681 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1683 // no interpolation possible -> set to last value immediately
1684 if( !values.empty() )
1685 setUnspecifiedValue(values.back()->getStringValue());
1689 return _interpolation_mgr->interpolate(this, type, values, deltas, easing);
1692 //------------------------------------------------------------------------------
1693 void SGPropertyNode::setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr)
1695 _interpolation_mgr = mgr;
1698 //------------------------------------------------------------------------------
1699 simgear::PropertyInterpolationMgr* SGPropertyNode::getInterpolationMgr()
1701 return _interpolation_mgr;
1704 simgear::PropertyInterpolationMgr* SGPropertyNode::_interpolation_mgr = 0;
1706 //------------------------------------------------------------------------------
1707 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1709 if (!getAttribute(READ))
1713 return _value.alias->printOn(stream);
1715 stream << (get_bool() ? "true" : "false");
1718 stream << get_int();
1721 stream << get_long();
1724 stream << get_float();
1727 stream << get_double();
1730 case props::UNSPECIFIED:
1731 stream << get_string();
1733 case props::EXTENDED:
1734 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1738 default: // avoid compiler warning
1745 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1748 if (_type == props::ALIAS || _tied)
1751 useDefault = useDefault && hasValue();
1752 std::string old_val;
1754 old_val = getStringValue();
1756 _type = props::STRING;
1758 _value.val = rawValue.clone();
1761 int save_attributes = getAttributes();
1762 setAttribute( WRITE, true );
1763 setStringValue(old_val.c_str());
1764 setAttributes( save_attributes );
1770 SGPropertyNode::untie ()
1777 bool val = getBoolValue();
1779 _type = props::BOOL;
1780 _local_val.bool_val = val;
1784 int val = getIntValue();
1787 _local_val.int_val = val;
1791 long val = getLongValue();
1793 _type = props::LONG;
1794 _local_val.long_val = val;
1797 case props::FLOAT: {
1798 float val = getFloatValue();
1800 _type = props::FLOAT;
1801 _local_val.float_val = val;
1804 case props::DOUBLE: {
1805 double val = getDoubleValue();
1807 _type = props::DOUBLE;
1808 _local_val.double_val = val;
1812 case props::UNSPECIFIED: {
1813 string val = getStringValue();
1815 _type = props::STRING;
1816 _local_val.string_val = copy_string(val.c_str());
1819 case props::EXTENDED: {
1820 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1821 _value.val = 0; // Prevent clearValue() from deleting
1823 _type = props::EXTENDED;
1824 _value.val = val->makeContainer();
1838 SGPropertyNode::getRootNode ()
1843 return _parent->getRootNode();
1846 const SGPropertyNode *
1847 SGPropertyNode::getRootNode () const
1852 return _parent->getRootNode();
1856 SGPropertyNode::getNode (const char * relative_path, bool create)
1858 using namespace boost;
1860 return find_node(this, make_iterator_range(relative_path, relative_path
1861 + strlen(relative_path)),
1866 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1868 using namespace boost;
1869 return find_node(this, make_iterator_range(relative_path, relative_path
1870 + strlen(relative_path)),
1874 const SGPropertyNode *
1875 SGPropertyNode::getNode (const char * relative_path) const
1877 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1880 const SGPropertyNode *
1881 SGPropertyNode::getNode (const char * relative_path, int index) const
1883 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1886 ////////////////////////////////////////////////////////////////////////
1887 // Convenience methods using relative paths.
1888 ////////////////////////////////////////////////////////////////////////
1892 * Test whether another node has a value attached.
1895 SGPropertyNode::hasValue (const char * relative_path) const
1897 const SGPropertyNode * node = getNode(relative_path);
1898 return (node == 0 ? false : node->hasValue());
1903 * Get the value type for another node.
1906 SGPropertyNode::getType (const char * relative_path) const
1908 const SGPropertyNode * node = getNode(relative_path);
1909 return (node == 0 ? props::UNSPECIFIED : node->getType());
1914 * Get a bool value for another node.
1917 SGPropertyNode::getBoolValue (const char * relative_path,
1918 bool defaultValue) const
1920 const SGPropertyNode * node = getNode(relative_path);
1921 return (node == 0 ? defaultValue : node->getBoolValue());
1926 * Get an int value for another node.
1929 SGPropertyNode::getIntValue (const char * relative_path,
1930 int defaultValue) const
1932 const SGPropertyNode * node = getNode(relative_path);
1933 return (node == 0 ? defaultValue : node->getIntValue());
1938 * Get a long value for another node.
1941 SGPropertyNode::getLongValue (const char * relative_path,
1942 long defaultValue) const
1944 const SGPropertyNode * node = getNode(relative_path);
1945 return (node == 0 ? defaultValue : node->getLongValue());
1950 * Get a float value for another node.
1953 SGPropertyNode::getFloatValue (const char * relative_path,
1954 float defaultValue) const
1956 const SGPropertyNode * node = getNode(relative_path);
1957 return (node == 0 ? defaultValue : node->getFloatValue());
1962 * Get a double value for another node.
1965 SGPropertyNode::getDoubleValue (const char * relative_path,
1966 double defaultValue) const
1968 const SGPropertyNode * node = getNode(relative_path);
1969 return (node == 0 ? defaultValue : node->getDoubleValue());
1974 * Get a string value for another node.
1977 SGPropertyNode::getStringValue (const char * relative_path,
1978 const char * defaultValue) const
1980 const SGPropertyNode * node = getNode(relative_path);
1981 return (node == 0 ? defaultValue : node->getStringValue());
1986 * Set a bool value for another node.
1989 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1991 return getNode(relative_path, true)->setBoolValue(value);
1996 * Set an int value for another node.
1999 SGPropertyNode::setIntValue (const char * relative_path, int value)
2001 return getNode(relative_path, true)->setIntValue(value);
2006 * Set a long value for another node.
2009 SGPropertyNode::setLongValue (const char * relative_path, long value)
2011 return getNode(relative_path, true)->setLongValue(value);
2016 * Set a float value for another node.
2019 SGPropertyNode::setFloatValue (const char * relative_path, float value)
2021 return getNode(relative_path, true)->setFloatValue(value);
2026 * Set a double value for another node.
2029 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
2031 return getNode(relative_path, true)->setDoubleValue(value);
2036 * Set a string value for another node.
2039 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
2041 return getNode(relative_path, true)->setStringValue(value);
2046 * Set an unknown value for another node.
2049 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
2052 return getNode(relative_path, true)->setUnspecifiedValue(value);
2057 * Test whether another node is tied.
2060 SGPropertyNode::isTied (const char * relative_path) const
2062 const SGPropertyNode * node = getNode(relative_path);
2063 return (node == 0 ? false : node->isTied());
2068 * Tie a node reached by a relative path, creating it if necessary.
2071 SGPropertyNode::tie (const char * relative_path,
2072 const SGRawValue<bool> &rawValue,
2075 return getNode(relative_path, true)->tie(rawValue, useDefault);
2080 * Tie a node reached by a relative path, creating it if necessary.
2083 SGPropertyNode::tie (const char * relative_path,
2084 const SGRawValue<int> &rawValue,
2087 return getNode(relative_path, true)->tie(rawValue, useDefault);
2092 * Tie a node reached by a relative path, creating it if necessary.
2095 SGPropertyNode::tie (const char * relative_path,
2096 const SGRawValue<long> &rawValue,
2099 return getNode(relative_path, true)->tie(rawValue, useDefault);
2104 * Tie a node reached by a relative path, creating it if necessary.
2107 SGPropertyNode::tie (const char * relative_path,
2108 const SGRawValue<float> &rawValue,
2111 return getNode(relative_path, true)->tie(rawValue, useDefault);
2116 * Tie a node reached by a relative path, creating it if necessary.
2119 SGPropertyNode::tie (const char * relative_path,
2120 const SGRawValue<double> &rawValue,
2123 return getNode(relative_path, true)->tie(rawValue, useDefault);
2128 * Tie a node reached by a relative path, creating it if necessary.
2131 SGPropertyNode::tie (const char * relative_path,
2132 const SGRawValue<const char *> &rawValue,
2135 return getNode(relative_path, true)->tie(rawValue, useDefault);
2140 * Attempt to untie another node reached by a relative path.
2143 SGPropertyNode::untie (const char * relative_path)
2145 SGPropertyNode * node = getNode(relative_path);
2146 return (node == 0 ? false : node->untie());
2150 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2153 if (_listeners == 0)
2154 _listeners = new vector<SGPropertyChangeListener*>;
2155 _listeners->push_back(listener);
2156 listener->register_property(this);
2158 listener->valueChanged(this);
2162 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2164 if (_listeners == 0)
2166 vector<SGPropertyChangeListener*>::iterator it =
2167 find(_listeners->begin(), _listeners->end(), listener);
2168 if (it != _listeners->end()) {
2169 _listeners->erase(it);
2170 listener->unregister_property(this);
2171 if (_listeners->empty()) {
2172 vector<SGPropertyChangeListener*>* tmp = _listeners;
2180 SGPropertyNode::fireValueChanged ()
2182 fireValueChanged(this);
2186 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2188 fireChildAdded(this, child);
2192 SGPropertyNode::fireCreatedRecursive(bool fire_self)
2196 _parent->fireChildAdded(this);
2198 if( _children.empty() && getType() != simgear::props::NONE )
2199 return fireValueChanged();
2202 for(size_t i = 0; i < _children.size(); ++i)
2203 _children[i]->fireCreatedRecursive(true);
2207 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2209 fireChildRemoved(this, child);
2213 SGPropertyNode::fireChildrenRemovedRecursive()
2215 for(size_t i = 0; i < _children.size(); ++i)
2217 SGPropertyNode* child = _children[i];
2218 fireChildRemoved(this, child);
2219 child->fireChildrenRemovedRecursive();
2224 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2226 if (_listeners != 0) {
2227 for (unsigned int i = 0; i < _listeners->size(); i++) {
2228 (*_listeners)[i]->valueChanged(node);
2232 _parent->fireValueChanged(node);
2236 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2237 SGPropertyNode * child)
2239 if (_listeners != 0) {
2240 for (unsigned int i = 0; i < _listeners->size(); i++) {
2241 (*_listeners)[i]->childAdded(parent, child);
2245 _parent->fireChildAdded(parent, child);
2249 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2250 SGPropertyNode * child)
2252 if (_listeners != 0) {
2253 for (unsigned int i = 0; i < _listeners->size(); i++) {
2254 (*_listeners)[i]->childRemoved(parent, child);
2258 _parent->fireChildRemoved(parent, child);
2261 //------------------------------------------------------------------------------
2263 SGPropertyNode::eraseChild(simgear::PropertyList::iterator child)
2265 SGPropertyNode_ptr node = *child;
2266 node->setAttribute(REMOVED, true);
2268 fireChildRemoved(node);
2270 _children.erase(child);
2274 ////////////////////////////////////////////////////////////////////////
2275 // Implementation of SGPropertyChangeListener.
2276 ////////////////////////////////////////////////////////////////////////
2278 SGPropertyChangeListener::~SGPropertyChangeListener ()
2280 for (int i = static_cast<int>(_properties.size() - 1); i >= 0; i--)
2281 _properties[i]->removeChangeListener(this);
2285 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2291 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2292 SGPropertyNode * child)
2298 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2299 SGPropertyNode * child)
2305 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2307 _properties.push_back(node);
2311 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2313 vector<SGPropertyNode *>::iterator it =
2314 find(_properties.begin(), _properties.end(), node);
2315 if (it != _properties.end())
2316 _properties.erase(it);
2320 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2323 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2324 for (int i = 0; i < 3; ++i) {
2335 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2337 for (int i = 0; i < 3; ++i) {
2338 stream >> result[i];
2344 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2347 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2348 for (int i = 0; i < 4; ++i) {
2359 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2361 for (int i = 0; i < 4; ++i) {
2362 stream >> result[i];
2369 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2371 props::Type ltype = lhs.getType();
2372 props::Type rtype = rhs.getType();
2379 return false; // XXX Should we look in aliases?
2381 return lhs.getValue<bool>() == rhs.getValue<bool>();
2383 return lhs.getValue<int>() == rhs.getValue<int>();
2385 return lhs.getValue<long>() == rhs.getValue<long>();
2387 return lhs.getValue<float>() == rhs.getValue<float>();
2389 return lhs.getValue<double>() == rhs.getValue<double>();
2391 case props::UNSPECIFIED:
2392 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2394 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2396 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2404 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2405 const SGPropertyNode& rhs)
2409 int lhsChildren = lhs.nChildren();
2410 int rhsChildren = rhs.nChildren();
2411 if (lhsChildren != rhsChildren)
2413 if (lhsChildren == 0)
2414 return compareNodeValue(lhs, rhs);
2415 for (size_t i = 0; i < lhs._children.size(); ++i) {
2416 const SGPropertyNode* lchild = lhs._children[i];
2417 const SGPropertyNode* rchild = rhs._children[i];
2418 // I'm guessing that the nodes will usually be in the same
2420 if (lchild->getIndex() != rchild->getIndex()
2421 || lchild->getNameString() != rchild->getNameString()) {
2423 for (PropertyList::const_iterator itr = rhs._children.begin(),
2424 end = rhs._children.end();
2427 if (lchild->getIndex() == (*itr)->getIndex()
2428 && lchild->getNameString() == (*itr)->getNameString()) {
2435 if (!compare(*lchild, *rchild))
2441 struct PropertyPlaceLess {
2442 typedef bool result_type;
2443 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2445 int comp = lhs->getNameString().compare(rhs->getNameString());
2447 return lhs->getIndex() < rhs->getIndex();
2453 size_t hash_value(const SGPropertyNode& node)
2455 using namespace boost;
2457 if (node.nChildren() == 0) {
2458 switch (node.getType()) {
2463 return hash_value(node.getValue<bool>());
2465 return hash_value(node.getValue<int>());
2467 return hash_value(node.getValue<long>());
2469 return hash_value(node.getValue<float>());
2471 return hash_value(node.getValue<double>());
2473 case props::UNSPECIFIED:
2475 const char *val = node.getStringValue();
2476 return hash_range(val, val + strlen(val));
2480 const SGVec3d val = node.getValue<SGVec3d>();
2481 return hash_range(&val[0], &val[3]);
2485 const SGVec4d val = node.getValue<SGVec4d>();
2486 return hash_range(&val[0], &val[4]);
2488 case props::ALIAS: // XXX Should we look in aliases?
2494 PropertyList children(node._children.begin(), node._children.end());
2495 sort(children.begin(), children.end(), PropertyPlaceLess());
2496 for (PropertyList::const_iterator itr = children.begin(),
2497 end = children.end();
2500 hash_combine(seed, (*itr)->_name);
2501 hash_combine(seed, (*itr)->_index);
2502 hash_combine(seed, hash_value(**itr));