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>
23 #include <boost/algorithm/string/find_iterator.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/functional/hash.hpp>
28 #include <boost/range.hpp>
30 #include <simgear/math/SGMath.hxx>
36 #include <simgear/compiler.h>
37 #include <simgear/debug/logstream.hxx>
39 #if ( _MSC_VER == 1200 )
40 // MSVC 6 is buggy, and needs something strange here
41 using std::vector<SGPropertyNode_ptr>;
42 using std::vector<SGPropertyChangeListener *>;
43 using std::vector<SGPropertyNode *>;
55 using std::stringstream;
57 using namespace simgear;
60 ////////////////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////////////////
65 * Comparator class for sorting by index.
70 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
71 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
85 ////////////////////////////////////////////////////////////////////////
86 // Local path normalization code.
87 ////////////////////////////////////////////////////////////////////////
90 * Parse the name for a path component.
92 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
95 template<typename Range>
97 parse_name (const SGPropertyNode *node, const Range &path)
99 typename Range::iterator i = path.begin();
100 typename Range::iterator max = path.end();
104 if (i != path.end() && *i == '.') {
107 if (i != max && *i != '/')
108 throw string("illegal character after . or ..");
109 } else if (isalpha(*i) || *i == '_') {
112 // The rules inside a name are a little
115 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
116 *i == '-' || *i == '.') {
118 } else if (*i == '[' || *i == '/') {
123 err.append("' found in propertyname after '"+node->getNameString()+"'");
124 err.append("\nname may contain only ._- and alphanumeric characters");
132 if (path.begin() == i) {
135 err.append("' found in propertyname after '"+node->getNameString()+"'");
136 err.append("\nname must begin with alpha or '_'");
140 return Range(path.begin(), i);
143 // Validate the name of a single node
144 inline bool validateName(const string& name)
146 using namespace boost;
149 if (!isalpha(name[0]) && name[0] != '_')
151 return all(make_iterator_range(name.begin(), name.end()),
152 is_alnum() || is_any_of("_-."));
156 ////////////////////////////////////////////////////////////////////////
157 // Other static utility functions.
158 ////////////////////////////////////////////////////////////////////////
162 copy_string (const char * s)
164 unsigned long int slen = strlen(s);
165 char * copy = new char[slen + 1];
167 // the source string length is known so no need to check for '\0'
168 // when copying every single character
169 memcpy(copy, s, slen);
170 *(copy + slen) = '\0';
175 compare_strings (const char * s1, const char * s2)
177 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
181 * Locate a child node by name and index.
183 template<typename Itr>
185 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
187 int nNodes = nodes.size();
188 boost::iterator_range<Itr> name(begin, end);
189 for (int i = 0; i < nNodes; i++) {
190 SGPropertyNode * node = nodes[i];
192 // searching for a mathing index is a lot less time consuming than
193 // comparing two strings so do that first.
194 if (node->getIndex() == index && boost::equals(node->getName(), name))
201 * Locate the child node with the highest index of the same name
204 find_last_child (const char * name, const PropertyList& nodes)
206 int nNodes = nodes.size();
209 for (int i = 0; i < nNodes; i++) {
210 SGPropertyNode * node = nodes[i];
211 if (compare_strings(node->getName(), name))
213 int idx = node->getIndex();
214 if (idx > index) index = idx;
220 template<typename Itr>
221 inline SGPropertyNode*
222 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
224 int pos = find_child(begin, end, index, _children);
226 return _children[pos];
228 SGPropertyNode_ptr node;
229 pos = find_child(begin, end, index, _removedChildren);
231 PropertyList::iterator it = _removedChildren.begin();
233 node = _removedChildren[pos];
234 _removedChildren.erase(it);
235 node->setAttribute(REMOVED, false);
236 _children.push_back(node);
237 fireChildAdded(node);
244 template<typename Itr>
246 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
248 SGPropertyNode* node = getExistingChild(begin, end, index, create);
253 node = new SGPropertyNode(begin, end, index, this);
254 _children.push_back(node);
255 fireChildAdded(node);
262 template<typename SplitItr>
264 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
267 typedef typename SplitItr::value_type Range;
268 // Run off the end of the list
273 // Success! This is the one we want.
277 // Empty name at this point is empty, not root.
279 return find_node_aux(current, ++itr, create, last_index);
280 Range name = parse_name(current, token);
281 if (equals(name, "."))
282 return find_node_aux(current, ++itr, create, last_index);
283 if (equals(name, "..")) {
284 SGPropertyNode * parent = current->getParent();
286 throw string("attempt to move past root with '..'");
287 return find_node_aux(parent, ++itr, create, last_index);
290 if (last_index >= 0) {
291 // If we are at the last token and last_index is valid, use
292 // last_index as the index value
294 while (!(++itr).eof()) {
308 if (name.end() != token.end()) {
309 if (*name.end() == '[') {
310 typename Range::iterator i = name.end() + 1, end = token.end();
311 for (;i != end; ++i) {
313 index = (index * 10) + (*i - '0');
318 if (i == token.end() || *i != ']')
319 throw string("unterminated index (looking for ']')");
321 throw string("illegal characters in token: ")
322 + string(name.begin(), name.end());
326 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
327 index, create), itr, create,
331 // Internal function for parsing property paths. last_index provides
332 // and index value for the last node name token, if supplied.
333 template<typename Range>
335 find_node (SGPropertyNode * current,
340 using namespace boost;
341 typedef split_iterator<typename range_result_iterator<Range>::type>
344 PathSplitIterator itr
345 = make_split_iterator(path, first_finder("/", is_equal()));
346 if (*path.begin() == '/')
347 return find_node_aux(current->getRootNode(), itr, create, last_index);
349 return find_node_aux(current, itr, create, last_index);
353 ////////////////////////////////////////////////////////////////////////
354 // Private methods from SGPropertyNode (may be inlined for speed).
355 ////////////////////////////////////////////////////////////////////////
358 SGPropertyNode::get_bool () const
361 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
363 return _local_val.bool_val;
367 SGPropertyNode::get_int () const
370 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
372 return _local_val.int_val;
376 SGPropertyNode::get_long () const
379 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
381 return _local_val.long_val;
385 SGPropertyNode::get_float () const
388 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
390 return _local_val.float_val;
394 SGPropertyNode::get_double () const
397 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
399 return _local_val.double_val;
403 SGPropertyNode::get_string () const
406 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
408 return _local_val.string_val;
412 SGPropertyNode::set_bool (bool val)
415 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
422 _local_val.bool_val = val;
429 SGPropertyNode::set_int (int val)
432 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
439 _local_val.int_val = val;
446 SGPropertyNode::set_long (long val)
449 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
456 _local_val.long_val = val;
463 SGPropertyNode::set_float (float val)
466 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
473 _local_val.float_val = val;
480 SGPropertyNode::set_double (double val)
483 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
490 _local_val.double_val = val;
497 SGPropertyNode::set_string (const char * val)
500 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
507 delete [] _local_val.string_val;
508 _local_val.string_val = copy_string(val);
515 SGPropertyNode::clearValue ()
517 if (_type == props::ALIAS) {
520 } else if (_type != props::NONE) {
523 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
526 _local_val.int_val = SGRawValue<int>::DefaultValue();
529 _local_val.long_val = SGRawValue<long>::DefaultValue();
532 _local_val.float_val = SGRawValue<float>::DefaultValue();
535 _local_val.double_val = SGRawValue<double>::DefaultValue();
538 case props::UNSPECIFIED:
540 delete [] _local_val.string_val;
542 _local_val.string_val = 0;
544 default: // avoid compiler warning
556 * Get the value as a string.
559 SGPropertyNode::make_string () const
561 if (!getAttribute(READ))
565 return _value.alias->getStringValue();
567 return get_bool() ? "true" : "false";
569 case props::UNSPECIFIED:
588 sstr << std::setprecision(10) << get_double();
590 case props::EXTENDED:
592 props::Type realType = _value.val->getType();
593 // Perhaps this should be done for all types?
594 if (realType == props::VEC3D || realType == props::VEC4D)
596 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
602 _buffer = sstr.str();
603 return _buffer.c_str();
607 * Trace a write access for a property.
610 SGPropertyNode::trace_write () const
613 cerr << "TRACE: Write node " << getPath () << ", value \""
614 << make_string() << '"' << endl;
616 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
617 << ", value \"" << make_string() << '"');
622 * Trace a read access for a property.
625 SGPropertyNode::trace_read () const
628 cerr << "TRACE: Write node " << getPath () << ", value \""
629 << make_string() << '"' << endl;
631 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
632 << ", value \"" << make_string() << '"');
637 ////////////////////////////////////////////////////////////////////////
638 // Public methods from SGPropertyNode.
639 ////////////////////////////////////////////////////////////////////////
642 * Last used attribute
643 * Update as needed when enum Attribute is changed
645 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
648 * Default constructor: always creates a root node.
650 SGPropertyNode::SGPropertyNode ()
659 _local_val.string_val = 0;
667 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
668 : _index(node._index),
670 _parent(0), // don't copy the parent
675 _listeners(0) // CHECK!!
677 _local_val.string_val = 0;
679 if (_type == props::NONE)
681 if (_type == props::ALIAS) {
682 _value.alias = node._value.alias;
687 if (_tied || _type == props::EXTENDED) {
688 _value.val = node._value.val->clone();
693 set_bool(node.get_bool());
696 set_int(node.get_int());
699 set_long(node.get_long());
702 set_float(node.get_float());
705 set_double(node.get_double());
708 case props::UNSPECIFIED:
709 set_string(node.get_string());
718 * Convenience constructor.
720 template<typename Itr>
721 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
723 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)
751 _local_val.string_val = 0;
753 if (!validateName(name))
754 throw string("plain name expected instead of '") + _name + '\'';
760 SGPropertyNode::~SGPropertyNode ()
762 // zero out all parent pointers, else they might be dangling
763 for (unsigned i = 0; i < _children.size(); ++i)
764 _children[i]->_parent = 0;
765 for (unsigned i = 0; i < _removedChildren.size(); ++i)
766 _removedChildren[i]->_parent = 0;
771 vector<SGPropertyChangeListener*>::iterator it;
772 for (it = _listeners->begin(); it != _listeners->end(); ++it)
773 (*it)->unregister_property(this);
780 * Alias to another node.
783 SGPropertyNode::alias (SGPropertyNode * target)
785 if (target == 0 || _type == props::ALIAS || _tied)
789 _value.alias = target;
790 _type = props::ALIAS;
796 * Alias to another node by path.
799 SGPropertyNode::alias (const char * path)
801 return alias(getNode(path, true));
809 SGPropertyNode::unalias ()
811 if (_type != props::ALIAS)
819 * Get the target of an alias.
822 SGPropertyNode::getAliasTarget ()
824 return (_type == props::ALIAS ? _value.alias : 0);
828 const SGPropertyNode *
829 SGPropertyNode::getAliasTarget () const
831 return (_type == props::ALIAS ? _value.alias : 0);
835 * create a non-const child by name after the last node with the same name.
838 SGPropertyNode::addChild (const char * name)
840 int pos = find_last_child(name, _children)+1;
842 SGPropertyNode_ptr node;
843 node = new SGPropertyNode(name, name + strlen(name), pos, this);
844 _children.push_back(node);
845 fireChildAdded(node);
851 * Get a non-const child by index.
854 SGPropertyNode::getChild (int position)
856 if (position >= 0 && position < nChildren())
857 return _children[position];
864 * Get a const child by index.
866 const SGPropertyNode *
867 SGPropertyNode::getChild (int position) const
869 if (position >= 0 && position < nChildren())
870 return _children[position];
877 * Get a non-const child by name and index, creating if necessary.
881 SGPropertyNode::getChild (const char * name, int index, bool create)
883 return getChildImpl(name, name + strlen(name), index, create);
887 SGPropertyNode::getChild (const string& name, int index, bool create)
889 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
894 node = new SGPropertyNode(name, index, this);
895 _children.push_back(node);
896 fireChildAdded(node);
904 * Get a const child by name and index.
906 const SGPropertyNode *
907 SGPropertyNode::getChild (const char * name, int index) const
909 int pos = find_child(name, name + strlen(name), index, _children);
911 return _children[pos];
918 * Get all children with the same name (but different indices).
921 SGPropertyNode::getChildren (const char * name) const
923 PropertyList children;
924 int max = _children.size();
926 for (int i = 0; i < max; i++)
927 if (compare_strings(_children[i]->getName(), name))
928 children.push_back(_children[i]);
930 sort(children.begin(), children.end(), CompareIndices());
936 * Remove this node and all children from nodes that link to them
937 * in their path cache.
940 SGPropertyNode::remove_from_path_caches ()
942 for (unsigned int i = 0; i < _children.size(); ++i)
943 _children[i]->remove_from_path_caches();
945 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
946 _linkedNodes[i]->erase(this);
947 _linkedNodes.clear();
952 * Remove child by position.
955 SGPropertyNode::removeChild (int pos, bool keep)
957 SGPropertyNode_ptr node;
958 if (pos < 0 || pos >= (int)_children.size())
961 PropertyList::iterator it = _children.begin();
963 node = _children[pos];
966 _removedChildren.push_back(node);
969 node->remove_from_path_caches();
970 node->setAttribute(REMOVED, true);
972 fireChildRemoved(node);
978 * Remove a child node
981 SGPropertyNode::removeChild (const char * name, int index, bool keep)
983 SGPropertyNode_ptr ret;
984 int pos = find_child(name, name + strlen(name), index, _children);
986 ret = removeChild(pos, keep);
992 * Remove all children with the specified name.
995 SGPropertyNode::removeChildren (const char * name, bool keep)
997 PropertyList children;
999 for (int pos = _children.size() - 1; pos >= 0; pos--)
1000 if (compare_strings(_children[pos]->getName(), name))
1001 children.push_back(removeChild(pos, keep));
1003 sort(children.begin(), children.end(), CompareIndices());
1009 * Remove a linked node.
1012 SGPropertyNode::remove_linked_node (hash_table * node)
1014 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
1015 if (_linkedNodes[i] == node) {
1016 vector<hash_table *>::iterator it = _linkedNodes.begin();
1018 _linkedNodes.erase(it);
1027 SGPropertyNode::getDisplayName (bool simplify) const
1029 string display_name = _name;
1030 if (_index != 0 || !simplify) {
1032 sstr << '[' << _index << ']';
1033 display_name += sstr.str();
1035 return display_name;
1040 SGPropertyNode::getPath (bool simplify) const
1042 // Calculate the complete path only once.
1043 if (_parent != 0 && _path.empty()) {
1044 _path = _parent->getPath(simplify);
1046 _path += getDisplayName(simplify);
1049 return _path.c_str();
1053 SGPropertyNode::getType () const
1055 if (_type == props::ALIAS)
1056 return _value.alias->getType();
1057 else if (_type == props::EXTENDED)
1058 return _value.val->getType();
1065 SGPropertyNode::getBoolValue () const
1067 // Shortcut for common case
1068 if (_attr == (READ|WRITE) && _type == props::BOOL)
1071 if (getAttribute(TRACE_READ))
1073 if (!getAttribute(READ))
1074 return SGRawValue<bool>::DefaultValue();
1077 return _value.alias->getBoolValue();
1081 return get_int() == 0 ? false : true;
1083 return get_long() == 0L ? false : true;
1085 return get_float() == 0.0 ? false : true;
1087 return get_double() == 0.0L ? false : true;
1089 case props::UNSPECIFIED:
1090 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1093 return SGRawValue<bool>::DefaultValue();
1098 SGPropertyNode::getIntValue () const
1100 // Shortcut for common case
1101 if (_attr == (READ|WRITE) && _type == props::INT)
1104 if (getAttribute(TRACE_READ))
1106 if (!getAttribute(READ))
1107 return SGRawValue<int>::DefaultValue();
1110 return _value.alias->getIntValue();
1112 return int(get_bool());
1116 return int(get_long());
1118 return int(get_float());
1120 return int(get_double());
1122 case props::UNSPECIFIED:
1123 return atoi(get_string());
1126 return SGRawValue<int>::DefaultValue();
1131 SGPropertyNode::getLongValue () const
1133 // Shortcut for common case
1134 if (_attr == (READ|WRITE) && _type == props::LONG)
1137 if (getAttribute(TRACE_READ))
1139 if (!getAttribute(READ))
1140 return SGRawValue<long>::DefaultValue();
1143 return _value.alias->getLongValue();
1145 return long(get_bool());
1147 return long(get_int());
1151 return long(get_float());
1153 return long(get_double());
1155 case props::UNSPECIFIED:
1156 return strtol(get_string(), 0, 0);
1159 return SGRawValue<long>::DefaultValue();
1164 SGPropertyNode::getFloatValue () const
1166 // Shortcut for common case
1167 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1170 if (getAttribute(TRACE_READ))
1172 if (!getAttribute(READ))
1173 return SGRawValue<float>::DefaultValue();
1176 return _value.alias->getFloatValue();
1178 return float(get_bool());
1180 return float(get_int());
1182 return float(get_long());
1186 return float(get_double());
1188 case props::UNSPECIFIED:
1189 return atof(get_string());
1192 return SGRawValue<float>::DefaultValue();
1197 SGPropertyNode::getDoubleValue () const
1199 // Shortcut for common case
1200 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1201 return get_double();
1203 if (getAttribute(TRACE_READ))
1205 if (!getAttribute(READ))
1206 return SGRawValue<double>::DefaultValue();
1210 return _value.alias->getDoubleValue();
1212 return double(get_bool());
1214 return double(get_int());
1216 return double(get_long());
1218 return double(get_float());
1220 return get_double();
1222 case props::UNSPECIFIED:
1223 return strtod(get_string(), 0);
1226 return SGRawValue<double>::DefaultValue();
1231 SGPropertyNode::getStringValue () const
1233 // Shortcut for common case
1234 if (_attr == (READ|WRITE) && _type == props::STRING)
1235 return get_string();
1237 if (getAttribute(TRACE_READ))
1239 if (!getAttribute(READ))
1240 return SGRawValue<const char *>::DefaultValue();
1241 return make_string();
1245 SGPropertyNode::setBoolValue (bool value)
1247 // Shortcut for common case
1248 if (_attr == (READ|WRITE) && _type == props::BOOL)
1249 return set_bool(value);
1251 bool result = false;
1253 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1256 _type = props::BOOL;
1261 result = _value.alias->setBoolValue(value);
1264 result = set_bool(value);
1267 result = set_int(int(value));
1270 result = set_long(long(value));
1273 result = set_float(float(value));
1276 result = set_double(double(value));
1279 case props::UNSPECIFIED:
1280 result = set_string(value ? "true" : "false");
1287 if (getAttribute(TRACE_WRITE))
1293 SGPropertyNode::setIntValue (int value)
1295 // Shortcut for common case
1296 if (_attr == (READ|WRITE) && _type == props::INT)
1297 return set_int(value);
1299 bool result = false;
1301 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1304 _local_val.int_val = 0;
1309 result = _value.alias->setIntValue(value);
1312 result = set_bool(value == 0 ? false : true);
1315 result = set_int(value);
1318 result = set_long(long(value));
1321 result = set_float(float(value));
1324 result = set_double(double(value));
1327 case props::UNSPECIFIED: {
1329 sprintf(buf, "%d", value);
1330 result = set_string(buf);
1338 if (getAttribute(TRACE_WRITE))
1344 SGPropertyNode::setLongValue (long value)
1346 // Shortcut for common case
1347 if (_attr == (READ|WRITE) && _type == props::LONG)
1348 return set_long(value);
1350 bool result = false;
1352 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1354 _type = props::LONG;
1355 _local_val.long_val = 0L;
1360 result = _value.alias->setLongValue(value);
1363 result = set_bool(value == 0L ? false : true);
1366 result = set_int(int(value));
1369 result = set_long(value);
1372 result = set_float(float(value));
1375 result = set_double(double(value));
1378 case props::UNSPECIFIED: {
1380 sprintf(buf, "%ld", value);
1381 result = set_string(buf);
1389 if (getAttribute(TRACE_WRITE))
1395 SGPropertyNode::setFloatValue (float value)
1397 // Shortcut for common case
1398 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1399 return set_float(value);
1401 bool result = false;
1403 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1405 _type = props::FLOAT;
1406 _local_val.float_val = 0;
1411 result = _value.alias->setFloatValue(value);
1414 result = set_bool(value == 0.0 ? false : true);
1417 result = set_int(int(value));
1420 result = set_long(long(value));
1423 result = set_float(value);
1426 result = set_double(double(value));
1429 case props::UNSPECIFIED: {
1431 sprintf(buf, "%f", value);
1432 result = set_string(buf);
1440 if (getAttribute(TRACE_WRITE))
1446 SGPropertyNode::setDoubleValue (double value)
1448 // Shortcut for common case
1449 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1450 return set_double(value);
1452 bool result = false;
1454 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1456 _local_val.double_val = value;
1457 _type = props::DOUBLE;
1462 result = _value.alias->setDoubleValue(value);
1465 result = set_bool(value == 0.0L ? false : true);
1468 result = set_int(int(value));
1471 result = set_long(long(value));
1474 result = set_float(float(value));
1477 result = set_double(value);
1480 case props::UNSPECIFIED: {
1482 sprintf(buf, "%f", value);
1483 result = set_string(buf);
1491 if (getAttribute(TRACE_WRITE))
1497 SGPropertyNode::setStringValue (const char * value)
1499 // Shortcut for common case
1500 if (_attr == (READ|WRITE) && _type == props::STRING)
1501 return set_string(value);
1503 bool result = false;
1505 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1507 _type = props::STRING;
1512 result = _value.alias->setStringValue(value);
1515 result = set_bool((compare_strings(value, "true")
1516 || atoi(value)) ? true : false);
1519 result = set_int(atoi(value));
1522 result = set_long(strtol(value, 0, 0));
1525 result = set_float(atof(value));
1528 result = set_double(strtod(value, 0));
1531 case props::UNSPECIFIED:
1532 result = set_string(value);
1534 case props::EXTENDED:
1536 stringstream sstr(value);
1537 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1545 if (getAttribute(TRACE_WRITE))
1551 SGPropertyNode::setUnspecifiedValue (const char * value)
1553 bool result = false;
1555 if (_type == props::NONE) {
1557 _type = props::UNSPECIFIED;
1559 props::Type type = _type;
1560 if (type == props::EXTENDED)
1561 type = _value.val->getType();
1564 result = _value.alias->setUnspecifiedValue(value);
1567 result = set_bool((compare_strings(value, "true")
1568 || atoi(value)) ? true : false);
1571 result = set_int(atoi(value));
1574 result = set_long(strtol(value, 0, 0));
1577 result = set_float(atof(value));
1580 result = set_double(strtod(value, 0));
1583 case props::UNSPECIFIED:
1584 result = set_string(value);
1587 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1590 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1597 if (getAttribute(TRACE_WRITE))
1602 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1604 if (!getAttribute(READ))
1608 return _value.alias->printOn(stream);
1610 stream << (get_bool() ? "true" : "false");
1613 stream << get_int();
1616 stream << get_long();
1619 stream << get_float();
1622 stream << get_double();
1625 case props::UNSPECIFIED:
1626 stream << get_string();
1628 case props::EXTENDED:
1629 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1633 default: // avoid compiler warning
1640 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1643 if (_type == props::ALIAS || _tied)
1646 useDefault = useDefault && hasValue();
1647 std::string old_val;
1649 old_val = getStringValue();
1651 _type = props::STRING;
1653 _value.val = rawValue.clone();
1656 setStringValue(old_val.c_str());
1661 SGPropertyNode::untie ()
1668 bool val = getBoolValue();
1670 _type = props::BOOL;
1671 _local_val.bool_val = val;
1675 int val = getIntValue();
1678 _local_val.int_val = val;
1682 long val = getLongValue();
1684 _type = props::LONG;
1685 _local_val.long_val = val;
1688 case props::FLOAT: {
1689 float val = getFloatValue();
1691 _type = props::FLOAT;
1692 _local_val.float_val = val;
1695 case props::DOUBLE: {
1696 double val = getDoubleValue();
1698 _type = props::DOUBLE;
1699 _local_val.double_val = val;
1703 case props::UNSPECIFIED: {
1704 string val = getStringValue();
1706 _type = props::STRING;
1707 _local_val.string_val = copy_string(val.c_str());
1710 case props::EXTENDED: {
1711 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1712 _value.val = 0; // Prevent clearValue() from deleting
1714 _type = props::EXTENDED;
1715 _value.val = val->makeContainer();
1729 SGPropertyNode::getRootNode ()
1734 return _parent->getRootNode();
1737 const SGPropertyNode *
1738 SGPropertyNode::getRootNode () const
1743 return _parent->getRootNode();
1747 SGPropertyNode::getNode (const char * relative_path, bool create)
1749 using namespace boost;
1750 if (_path_cache == 0)
1751 _path_cache = new hash_table;
1753 SGPropertyNode * result = _path_cache->get(relative_path);
1755 result = find_node(this,
1756 make_iterator_range(relative_path, relative_path
1757 + strlen(relative_path)),
1760 _path_cache->put(relative_path, result);
1767 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1769 using namespace boost;
1770 return find_node(this, make_iterator_range(relative_path, relative_path
1771 + strlen(relative_path)),
1775 const SGPropertyNode *
1776 SGPropertyNode::getNode (const char * relative_path) const
1778 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1781 const SGPropertyNode *
1782 SGPropertyNode::getNode (const char * relative_path, int index) const
1784 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1788 ////////////////////////////////////////////////////////////////////////
1789 // Convenience methods using relative paths.
1790 ////////////////////////////////////////////////////////////////////////
1794 * Test whether another node has a value attached.
1797 SGPropertyNode::hasValue (const char * relative_path) const
1799 const SGPropertyNode * node = getNode(relative_path);
1800 return (node == 0 ? false : node->hasValue());
1805 * Get the value type for another node.
1808 SGPropertyNode::getType (const char * relative_path) const
1810 const SGPropertyNode * node = getNode(relative_path);
1811 return (node == 0 ? props::UNSPECIFIED : node->getType());
1816 * Get a bool value for another node.
1819 SGPropertyNode::getBoolValue (const char * relative_path,
1820 bool defaultValue) const
1822 const SGPropertyNode * node = getNode(relative_path);
1823 return (node == 0 ? defaultValue : node->getBoolValue());
1828 * Get an int value for another node.
1831 SGPropertyNode::getIntValue (const char * relative_path,
1832 int defaultValue) const
1834 const SGPropertyNode * node = getNode(relative_path);
1835 return (node == 0 ? defaultValue : node->getIntValue());
1840 * Get a long value for another node.
1843 SGPropertyNode::getLongValue (const char * relative_path,
1844 long defaultValue) const
1846 const SGPropertyNode * node = getNode(relative_path);
1847 return (node == 0 ? defaultValue : node->getLongValue());
1852 * Get a float value for another node.
1855 SGPropertyNode::getFloatValue (const char * relative_path,
1856 float defaultValue) const
1858 const SGPropertyNode * node = getNode(relative_path);
1859 return (node == 0 ? defaultValue : node->getFloatValue());
1864 * Get a double value for another node.
1867 SGPropertyNode::getDoubleValue (const char * relative_path,
1868 double defaultValue) const
1870 const SGPropertyNode * node = getNode(relative_path);
1871 return (node == 0 ? defaultValue : node->getDoubleValue());
1876 * Get a string value for another node.
1879 SGPropertyNode::getStringValue (const char * relative_path,
1880 const char * defaultValue) const
1882 const SGPropertyNode * node = getNode(relative_path);
1883 return (node == 0 ? defaultValue : node->getStringValue());
1888 * Set a bool value for another node.
1891 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1893 return getNode(relative_path, true)->setBoolValue(value);
1898 * Set an int value for another node.
1901 SGPropertyNode::setIntValue (const char * relative_path, int value)
1903 return getNode(relative_path, true)->setIntValue(value);
1908 * Set a long value for another node.
1911 SGPropertyNode::setLongValue (const char * relative_path, long value)
1913 return getNode(relative_path, true)->setLongValue(value);
1918 * Set a float value for another node.
1921 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1923 return getNode(relative_path, true)->setFloatValue(value);
1928 * Set a double value for another node.
1931 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1933 return getNode(relative_path, true)->setDoubleValue(value);
1938 * Set a string value for another node.
1941 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1943 return getNode(relative_path, true)->setStringValue(value);
1948 * Set an unknown value for another node.
1951 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1954 return getNode(relative_path, true)->setUnspecifiedValue(value);
1959 * Test whether another node is tied.
1962 SGPropertyNode::isTied (const char * relative_path) const
1964 const SGPropertyNode * node = getNode(relative_path);
1965 return (node == 0 ? false : node->isTied());
1970 * Tie a node reached by a relative path, creating it if necessary.
1973 SGPropertyNode::tie (const char * relative_path,
1974 const SGRawValue<bool> &rawValue,
1977 return getNode(relative_path, true)->tie(rawValue, useDefault);
1982 * Tie a node reached by a relative path, creating it if necessary.
1985 SGPropertyNode::tie (const char * relative_path,
1986 const SGRawValue<int> &rawValue,
1989 return getNode(relative_path, true)->tie(rawValue, useDefault);
1994 * Tie a node reached by a relative path, creating it if necessary.
1997 SGPropertyNode::tie (const char * relative_path,
1998 const SGRawValue<long> &rawValue,
2001 return getNode(relative_path, true)->tie(rawValue, useDefault);
2006 * Tie a node reached by a relative path, creating it if necessary.
2009 SGPropertyNode::tie (const char * relative_path,
2010 const SGRawValue<float> &rawValue,
2013 return getNode(relative_path, true)->tie(rawValue, useDefault);
2018 * Tie a node reached by a relative path, creating it if necessary.
2021 SGPropertyNode::tie (const char * relative_path,
2022 const SGRawValue<double> &rawValue,
2025 return getNode(relative_path, true)->tie(rawValue, useDefault);
2030 * Tie a node reached by a relative path, creating it if necessary.
2033 SGPropertyNode::tie (const char * relative_path,
2034 const SGRawValue<const char *> &rawValue,
2037 return getNode(relative_path, true)->tie(rawValue, useDefault);
2042 * Attempt to untie another node reached by a relative path.
2045 SGPropertyNode::untie (const char * relative_path)
2047 SGPropertyNode * node = getNode(relative_path);
2048 return (node == 0 ? false : node->untie());
2052 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2055 if (_listeners == 0)
2056 _listeners = new vector<SGPropertyChangeListener*>;
2057 _listeners->push_back(listener);
2058 listener->register_property(this);
2060 listener->valueChanged(this);
2064 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2066 vector<SGPropertyChangeListener*>::iterator it =
2067 find(_listeners->begin(), _listeners->end(), listener);
2068 if (it != _listeners->end()) {
2069 _listeners->erase(it);
2070 listener->unregister_property(this);
2071 if (_listeners->empty()) {
2072 vector<SGPropertyChangeListener*>* tmp = _listeners;
2080 SGPropertyNode::fireValueChanged ()
2082 fireValueChanged(this);
2086 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2088 fireChildAdded(this, child);
2092 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2094 fireChildRemoved(this, child);
2098 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2100 if (_listeners != 0) {
2101 for (unsigned int i = 0; i < _listeners->size(); i++) {
2102 (*_listeners)[i]->valueChanged(node);
2106 _parent->fireValueChanged(node);
2110 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2111 SGPropertyNode * child)
2113 if (_listeners != 0) {
2114 for (unsigned int i = 0; i < _listeners->size(); i++) {
2115 (*_listeners)[i]->childAdded(parent, child);
2119 _parent->fireChildAdded(parent, child);
2123 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2124 SGPropertyNode * child)
2126 if (_listeners != 0) {
2127 for (unsigned int i = 0; i < _listeners->size(); i++) {
2128 (*_listeners)[i]->childRemoved(parent, child);
2132 _parent->fireChildRemoved(parent, child);
2137 ////////////////////////////////////////////////////////////////////////
2138 // Simplified hash table for caching paths.
2139 ////////////////////////////////////////////////////////////////////////
2141 #define HASH_TABLE_SIZE 199
2143 SGPropertyNode::hash_table::entry::entry ()
2148 SGPropertyNode::hash_table::entry::~entry ()
2150 // Don't delete the value; we don't own
2155 SGPropertyNode::hash_table::entry::set_key (const char * key)
2161 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2166 SGPropertyNode::hash_table::bucket::bucket ()
2172 SGPropertyNode::hash_table::bucket::~bucket ()
2174 for (int i = 0; i < _length; i++)
2179 SGPropertyNode::hash_table::entry *
2180 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2183 for (i = 0; i < _length; i++) {
2184 if (!strcmp(_entries[i]->get_key(), key))
2188 entry ** new_entries = new entry*[_length+1];
2189 for (i = 0; i < _length; i++) {
2190 new_entries[i] = _entries[i];
2193 _entries = new_entries;
2194 _entries[_length] = new entry;
2195 _entries[_length]->set_key(key);
2197 return _entries[_length - 1];
2204 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2206 for (int i = 0; i < _length; i++) {
2207 if (_entries[i]->get_value() == node) {
2209 for (++i; i < _length; i++) {
2210 _entries[i-1] = _entries[i];
2220 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2222 for (int i = 0; i < _length; i++) {
2223 SGPropertyNode * node = _entries[i]->get_value();
2225 node->remove_linked_node(owner);
2229 SGPropertyNode::hash_table::hash_table ()
2235 SGPropertyNode::hash_table::~hash_table ()
2237 for (unsigned int i = 0; i < _data_length; i++) {
2239 _data[i]->clear(this);
2247 SGPropertyNode::hash_table::get (const char * key)
2249 if (_data_length == 0)
2251 unsigned int index = hashcode(key) % _data_length;
2252 if (_data[index] == 0)
2254 entry * e = _data[index]->get_entry(key);
2258 return e->get_value();
2262 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2264 if (_data_length == 0) {
2265 _data = new bucket*[HASH_TABLE_SIZE];
2266 _data_length = HASH_TABLE_SIZE;
2267 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2270 unsigned int index = hashcode(key) % _data_length;
2271 if (_data[index] == 0) {
2272 _data[index] = new bucket;
2274 entry * e = _data[index]->get_entry(key, true);
2275 e->set_value(value);
2276 value->add_linked_node(this);
2280 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2282 for (unsigned int i = 0; i < _data_length; i++)
2283 if (_data[i] && _data[i]->erase(node))
2290 SGPropertyNode::hash_table::hashcode (const char * key)
2292 unsigned int hash = 0;
2294 hash = 31 * hash + *key;
2302 ////////////////////////////////////////////////////////////////////////
2303 // Implementation of SGPropertyChangeListener.
2304 ////////////////////////////////////////////////////////////////////////
2306 SGPropertyChangeListener::~SGPropertyChangeListener ()
2308 for (int i = _properties.size() - 1; i >= 0; i--)
2309 _properties[i]->removeChangeListener(this);
2313 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2319 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2320 SGPropertyNode * child)
2326 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2327 SGPropertyNode * child)
2333 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2335 _properties.push_back(node);
2339 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2341 vector<SGPropertyNode *>::iterator it =
2342 find(_properties.begin(), _properties.end(), node);
2343 if (it != _properties.end())
2344 _properties.erase(it);
2348 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2351 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2352 for (int i = 0; i < 3; ++i) {
2363 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2365 for (int i = 0; i < 3; ++i) {
2366 stream >> result[i];
2372 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2375 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2376 for (int i = 0; i < 4; ++i) {
2387 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2389 for (int i = 0; i < 4; ++i) {
2390 stream >> result[i];
2397 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2399 props::Type ltype = lhs.getType();
2400 props::Type rtype = rhs.getType();
2407 return false; // XXX Should we look in aliases?
2409 return lhs.getValue<bool>() == rhs.getValue<bool>();
2411 return lhs.getValue<int>() == rhs.getValue<int>();
2413 return lhs.getValue<long>() == rhs.getValue<long>();
2415 return lhs.getValue<float>() == rhs.getValue<float>();
2417 return lhs.getValue<double>() == rhs.getValue<double>();
2419 case props::UNSPECIFIED:
2420 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2422 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2424 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2432 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2433 const SGPropertyNode& rhs)
2437 int lhsChildren = lhs.nChildren();
2438 int rhsChildren = rhs.nChildren();
2439 if (lhsChildren != rhsChildren)
2441 if (lhsChildren == 0)
2442 return compareNodeValue(lhs, rhs);
2443 for (size_t i = 0; i < lhs._children.size(); ++i) {
2444 const SGPropertyNode* lchild = lhs._children[i];
2445 const SGPropertyNode* rchild = rhs._children[i];
2446 // I'm guessing that the nodes will usually be in the same
2448 if (lchild->getIndex() != rchild->getIndex()
2449 || lchild->getNameString() != rchild->getNameString()) {
2451 for (PropertyList::const_iterator itr = rhs._children.begin(),
2452 end = rhs._children.end();
2455 if (lchild->getIndex() == (*itr)->getIndex()
2456 && lchild->getNameString() == (*itr)->getNameString()) {
2463 if (!compare(*lchild, *rchild))
2469 struct PropertyPlaceLess {
2470 typedef bool result_type;
2471 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2473 int comp = lhs->getNameString().compare(rhs->getNameString());
2475 return lhs->getIndex() < rhs->getIndex();
2481 size_t hash_value(const SGPropertyNode& node)
2483 using namespace boost;
2485 if (node.nChildren() == 0) {
2486 switch (node.getType()) {
2491 return hash_value(node.getValue<bool>());
2493 return hash_value(node.getValue<int>());
2495 return hash_value(node.getValue<long>());
2497 return hash_value(node.getValue<float>());
2499 return hash_value(node.getValue<double>());
2501 case props::UNSPECIFIED:
2503 const char *val = node.getStringValue();
2504 return hash_range(val, val + strlen(val));
2508 const SGVec3d val = node.getValue<SGVec3d>();
2509 return hash_range(&val[0], &val[3]);
2513 const SGVec4d val = node.getValue<SGVec4d>();
2514 return hash_range(&val[0], &val[4]);
2516 case props::ALIAS: // XXX Should we look in aliases?
2522 PropertyList children(node._children.begin(), node._children.end());
2523 sort(children.begin(), children.end(), PropertyPlaceLess());
2524 for (PropertyList::const_iterator itr = children.begin(),
2525 end = children.end();
2528 hash_combine(seed, (*itr)->_name);
2529 hash_combine(seed, (*itr)->_index);
2530 hash_combine(seed, hash_value(**itr));