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 typedef std::vector<SGConstPropertyNode_ptr> PList;
1044 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1045 pathList.push_back(node);
1047 for (PList::reverse_iterator itr = pathList.rbegin(),
1048 rend = pathList.rend();
1052 result += (*itr)->getDisplayName(simplify);
1058 SGPropertyNode::getType () const
1060 if (_type == props::ALIAS)
1061 return _value.alias->getType();
1062 else if (_type == props::EXTENDED)
1063 return _value.val->getType();
1070 SGPropertyNode::getBoolValue () const
1072 // Shortcut for common case
1073 if (_attr == (READ|WRITE) && _type == props::BOOL)
1076 if (getAttribute(TRACE_READ))
1078 if (!getAttribute(READ))
1079 return SGRawValue<bool>::DefaultValue();
1082 return _value.alias->getBoolValue();
1086 return get_int() == 0 ? false : true;
1088 return get_long() == 0L ? false : true;
1090 return get_float() == 0.0 ? false : true;
1092 return get_double() == 0.0L ? false : true;
1094 case props::UNSPECIFIED:
1095 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1098 return SGRawValue<bool>::DefaultValue();
1103 SGPropertyNode::getIntValue () const
1105 // Shortcut for common case
1106 if (_attr == (READ|WRITE) && _type == props::INT)
1109 if (getAttribute(TRACE_READ))
1111 if (!getAttribute(READ))
1112 return SGRawValue<int>::DefaultValue();
1115 return _value.alias->getIntValue();
1117 return int(get_bool());
1121 return int(get_long());
1123 return int(get_float());
1125 return int(get_double());
1127 case props::UNSPECIFIED:
1128 return atoi(get_string());
1131 return SGRawValue<int>::DefaultValue();
1136 SGPropertyNode::getLongValue () const
1138 // Shortcut for common case
1139 if (_attr == (READ|WRITE) && _type == props::LONG)
1142 if (getAttribute(TRACE_READ))
1144 if (!getAttribute(READ))
1145 return SGRawValue<long>::DefaultValue();
1148 return _value.alias->getLongValue();
1150 return long(get_bool());
1152 return long(get_int());
1156 return long(get_float());
1158 return long(get_double());
1160 case props::UNSPECIFIED:
1161 return strtol(get_string(), 0, 0);
1164 return SGRawValue<long>::DefaultValue();
1169 SGPropertyNode::getFloatValue () const
1171 // Shortcut for common case
1172 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1175 if (getAttribute(TRACE_READ))
1177 if (!getAttribute(READ))
1178 return SGRawValue<float>::DefaultValue();
1181 return _value.alias->getFloatValue();
1183 return float(get_bool());
1185 return float(get_int());
1187 return float(get_long());
1191 return float(get_double());
1193 case props::UNSPECIFIED:
1194 return atof(get_string());
1197 return SGRawValue<float>::DefaultValue();
1202 SGPropertyNode::getDoubleValue () const
1204 // Shortcut for common case
1205 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1206 return get_double();
1208 if (getAttribute(TRACE_READ))
1210 if (!getAttribute(READ))
1211 return SGRawValue<double>::DefaultValue();
1215 return _value.alias->getDoubleValue();
1217 return double(get_bool());
1219 return double(get_int());
1221 return double(get_long());
1223 return double(get_float());
1225 return get_double();
1227 case props::UNSPECIFIED:
1228 return strtod(get_string(), 0);
1231 return SGRawValue<double>::DefaultValue();
1236 SGPropertyNode::getStringValue () const
1238 // Shortcut for common case
1239 if (_attr == (READ|WRITE) && _type == props::STRING)
1240 return get_string();
1242 if (getAttribute(TRACE_READ))
1244 if (!getAttribute(READ))
1245 return SGRawValue<const char *>::DefaultValue();
1246 return make_string();
1250 SGPropertyNode::setBoolValue (bool value)
1252 // Shortcut for common case
1253 if (_attr == (READ|WRITE) && _type == props::BOOL)
1254 return set_bool(value);
1256 bool result = false;
1258 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1261 _type = props::BOOL;
1266 result = _value.alias->setBoolValue(value);
1269 result = set_bool(value);
1272 result = set_int(int(value));
1275 result = set_long(long(value));
1278 result = set_float(float(value));
1281 result = set_double(double(value));
1284 case props::UNSPECIFIED:
1285 result = set_string(value ? "true" : "false");
1292 if (getAttribute(TRACE_WRITE))
1298 SGPropertyNode::setIntValue (int value)
1300 // Shortcut for common case
1301 if (_attr == (READ|WRITE) && _type == props::INT)
1302 return set_int(value);
1304 bool result = false;
1306 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1309 _local_val.int_val = 0;
1314 result = _value.alias->setIntValue(value);
1317 result = set_bool(value == 0 ? false : true);
1320 result = set_int(value);
1323 result = set_long(long(value));
1326 result = set_float(float(value));
1329 result = set_double(double(value));
1332 case props::UNSPECIFIED: {
1334 sprintf(buf, "%d", value);
1335 result = set_string(buf);
1343 if (getAttribute(TRACE_WRITE))
1349 SGPropertyNode::setLongValue (long value)
1351 // Shortcut for common case
1352 if (_attr == (READ|WRITE) && _type == props::LONG)
1353 return set_long(value);
1355 bool result = false;
1357 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1359 _type = props::LONG;
1360 _local_val.long_val = 0L;
1365 result = _value.alias->setLongValue(value);
1368 result = set_bool(value == 0L ? false : true);
1371 result = set_int(int(value));
1374 result = set_long(value);
1377 result = set_float(float(value));
1380 result = set_double(double(value));
1383 case props::UNSPECIFIED: {
1385 sprintf(buf, "%ld", value);
1386 result = set_string(buf);
1394 if (getAttribute(TRACE_WRITE))
1400 SGPropertyNode::setFloatValue (float value)
1402 // Shortcut for common case
1403 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1404 return set_float(value);
1406 bool result = false;
1408 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1410 _type = props::FLOAT;
1411 _local_val.float_val = 0;
1416 result = _value.alias->setFloatValue(value);
1419 result = set_bool(value == 0.0 ? false : true);
1422 result = set_int(int(value));
1425 result = set_long(long(value));
1428 result = set_float(value);
1431 result = set_double(double(value));
1434 case props::UNSPECIFIED: {
1436 sprintf(buf, "%f", value);
1437 result = set_string(buf);
1445 if (getAttribute(TRACE_WRITE))
1451 SGPropertyNode::setDoubleValue (double value)
1453 // Shortcut for common case
1454 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1455 return set_double(value);
1457 bool result = false;
1459 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1461 _local_val.double_val = value;
1462 _type = props::DOUBLE;
1467 result = _value.alias->setDoubleValue(value);
1470 result = set_bool(value == 0.0L ? false : true);
1473 result = set_int(int(value));
1476 result = set_long(long(value));
1479 result = set_float(float(value));
1482 result = set_double(value);
1485 case props::UNSPECIFIED: {
1487 sprintf(buf, "%f", value);
1488 result = set_string(buf);
1496 if (getAttribute(TRACE_WRITE))
1502 SGPropertyNode::setStringValue (const char * value)
1504 // Shortcut for common case
1505 if (_attr == (READ|WRITE) && _type == props::STRING)
1506 return set_string(value);
1508 bool result = false;
1510 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1512 _type = props::STRING;
1517 result = _value.alias->setStringValue(value);
1520 result = set_bool((compare_strings(value, "true")
1521 || atoi(value)) ? true : false);
1524 result = set_int(atoi(value));
1527 result = set_long(strtol(value, 0, 0));
1530 result = set_float(atof(value));
1533 result = set_double(strtod(value, 0));
1536 case props::UNSPECIFIED:
1537 result = set_string(value);
1539 case props::EXTENDED:
1541 stringstream sstr(value);
1542 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1550 if (getAttribute(TRACE_WRITE))
1556 SGPropertyNode::setUnspecifiedValue (const char * value)
1558 bool result = false;
1560 if (_type == props::NONE) {
1562 _type = props::UNSPECIFIED;
1564 props::Type type = _type;
1565 if (type == props::EXTENDED)
1566 type = _value.val->getType();
1569 result = _value.alias->setUnspecifiedValue(value);
1572 result = set_bool((compare_strings(value, "true")
1573 || atoi(value)) ? true : false);
1576 result = set_int(atoi(value));
1579 result = set_long(strtol(value, 0, 0));
1582 result = set_float(atof(value));
1585 result = set_double(strtod(value, 0));
1588 case props::UNSPECIFIED:
1589 result = set_string(value);
1592 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1595 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1602 if (getAttribute(TRACE_WRITE))
1607 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1609 if (!getAttribute(READ))
1613 return _value.alias->printOn(stream);
1615 stream << (get_bool() ? "true" : "false");
1618 stream << get_int();
1621 stream << get_long();
1624 stream << get_float();
1627 stream << get_double();
1630 case props::UNSPECIFIED:
1631 stream << get_string();
1633 case props::EXTENDED:
1634 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1638 default: // avoid compiler warning
1645 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1648 if (_type == props::ALIAS || _tied)
1651 useDefault = useDefault && hasValue();
1652 std::string old_val;
1654 old_val = getStringValue();
1656 _type = props::STRING;
1658 _value.val = rawValue.clone();
1661 setStringValue(old_val.c_str());
1666 SGPropertyNode::untie ()
1673 bool val = getBoolValue();
1675 _type = props::BOOL;
1676 _local_val.bool_val = val;
1680 int val = getIntValue();
1683 _local_val.int_val = val;
1687 long val = getLongValue();
1689 _type = props::LONG;
1690 _local_val.long_val = val;
1693 case props::FLOAT: {
1694 float val = getFloatValue();
1696 _type = props::FLOAT;
1697 _local_val.float_val = val;
1700 case props::DOUBLE: {
1701 double val = getDoubleValue();
1703 _type = props::DOUBLE;
1704 _local_val.double_val = val;
1708 case props::UNSPECIFIED: {
1709 string val = getStringValue();
1711 _type = props::STRING;
1712 _local_val.string_val = copy_string(val.c_str());
1715 case props::EXTENDED: {
1716 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1717 _value.val = 0; // Prevent clearValue() from deleting
1719 _type = props::EXTENDED;
1720 _value.val = val->makeContainer();
1734 SGPropertyNode::getRootNode ()
1739 return _parent->getRootNode();
1742 const SGPropertyNode *
1743 SGPropertyNode::getRootNode () const
1748 return _parent->getRootNode();
1752 SGPropertyNode::getNode (const char * relative_path, bool create)
1754 using namespace boost;
1755 if (_path_cache == 0)
1756 _path_cache = new hash_table;
1758 SGPropertyNode * result = _path_cache->get(relative_path);
1760 result = find_node(this,
1761 make_iterator_range(relative_path, relative_path
1762 + strlen(relative_path)),
1765 _path_cache->put(relative_path, result);
1772 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1774 using namespace boost;
1775 return find_node(this, make_iterator_range(relative_path, relative_path
1776 + strlen(relative_path)),
1780 const SGPropertyNode *
1781 SGPropertyNode::getNode (const char * relative_path) const
1783 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1786 const SGPropertyNode *
1787 SGPropertyNode::getNode (const char * relative_path, int index) const
1789 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1793 ////////////////////////////////////////////////////////////////////////
1794 // Convenience methods using relative paths.
1795 ////////////////////////////////////////////////////////////////////////
1799 * Test whether another node has a value attached.
1802 SGPropertyNode::hasValue (const char * relative_path) const
1804 const SGPropertyNode * node = getNode(relative_path);
1805 return (node == 0 ? false : node->hasValue());
1810 * Get the value type for another node.
1813 SGPropertyNode::getType (const char * relative_path) const
1815 const SGPropertyNode * node = getNode(relative_path);
1816 return (node == 0 ? props::UNSPECIFIED : node->getType());
1821 * Get a bool value for another node.
1824 SGPropertyNode::getBoolValue (const char * relative_path,
1825 bool defaultValue) const
1827 const SGPropertyNode * node = getNode(relative_path);
1828 return (node == 0 ? defaultValue : node->getBoolValue());
1833 * Get an int value for another node.
1836 SGPropertyNode::getIntValue (const char * relative_path,
1837 int defaultValue) const
1839 const SGPropertyNode * node = getNode(relative_path);
1840 return (node == 0 ? defaultValue : node->getIntValue());
1845 * Get a long value for another node.
1848 SGPropertyNode::getLongValue (const char * relative_path,
1849 long defaultValue) const
1851 const SGPropertyNode * node = getNode(relative_path);
1852 return (node == 0 ? defaultValue : node->getLongValue());
1857 * Get a float value for another node.
1860 SGPropertyNode::getFloatValue (const char * relative_path,
1861 float defaultValue) const
1863 const SGPropertyNode * node = getNode(relative_path);
1864 return (node == 0 ? defaultValue : node->getFloatValue());
1869 * Get a double value for another node.
1872 SGPropertyNode::getDoubleValue (const char * relative_path,
1873 double defaultValue) const
1875 const SGPropertyNode * node = getNode(relative_path);
1876 return (node == 0 ? defaultValue : node->getDoubleValue());
1881 * Get a string value for another node.
1884 SGPropertyNode::getStringValue (const char * relative_path,
1885 const char * defaultValue) const
1887 const SGPropertyNode * node = getNode(relative_path);
1888 return (node == 0 ? defaultValue : node->getStringValue());
1893 * Set a bool value for another node.
1896 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1898 return getNode(relative_path, true)->setBoolValue(value);
1903 * Set an int value for another node.
1906 SGPropertyNode::setIntValue (const char * relative_path, int value)
1908 return getNode(relative_path, true)->setIntValue(value);
1913 * Set a long value for another node.
1916 SGPropertyNode::setLongValue (const char * relative_path, long value)
1918 return getNode(relative_path, true)->setLongValue(value);
1923 * Set a float value for another node.
1926 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1928 return getNode(relative_path, true)->setFloatValue(value);
1933 * Set a double value for another node.
1936 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1938 return getNode(relative_path, true)->setDoubleValue(value);
1943 * Set a string value for another node.
1946 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1948 return getNode(relative_path, true)->setStringValue(value);
1953 * Set an unknown value for another node.
1956 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1959 return getNode(relative_path, true)->setUnspecifiedValue(value);
1964 * Test whether another node is tied.
1967 SGPropertyNode::isTied (const char * relative_path) const
1969 const SGPropertyNode * node = getNode(relative_path);
1970 return (node == 0 ? false : node->isTied());
1975 * Tie a node reached by a relative path, creating it if necessary.
1978 SGPropertyNode::tie (const char * relative_path,
1979 const SGRawValue<bool> &rawValue,
1982 return getNode(relative_path, true)->tie(rawValue, useDefault);
1987 * Tie a node reached by a relative path, creating it if necessary.
1990 SGPropertyNode::tie (const char * relative_path,
1991 const SGRawValue<int> &rawValue,
1994 return getNode(relative_path, true)->tie(rawValue, useDefault);
1999 * Tie a node reached by a relative path, creating it if necessary.
2002 SGPropertyNode::tie (const char * relative_path,
2003 const SGRawValue<long> &rawValue,
2006 return getNode(relative_path, true)->tie(rawValue, useDefault);
2011 * Tie a node reached by a relative path, creating it if necessary.
2014 SGPropertyNode::tie (const char * relative_path,
2015 const SGRawValue<float> &rawValue,
2018 return getNode(relative_path, true)->tie(rawValue, useDefault);
2023 * Tie a node reached by a relative path, creating it if necessary.
2026 SGPropertyNode::tie (const char * relative_path,
2027 const SGRawValue<double> &rawValue,
2030 return getNode(relative_path, true)->tie(rawValue, useDefault);
2035 * Tie a node reached by a relative path, creating it if necessary.
2038 SGPropertyNode::tie (const char * relative_path,
2039 const SGRawValue<const char *> &rawValue,
2042 return getNode(relative_path, true)->tie(rawValue, useDefault);
2047 * Attempt to untie another node reached by a relative path.
2050 SGPropertyNode::untie (const char * relative_path)
2052 SGPropertyNode * node = getNode(relative_path);
2053 return (node == 0 ? false : node->untie());
2057 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2060 if (_listeners == 0)
2061 _listeners = new vector<SGPropertyChangeListener*>;
2062 _listeners->push_back(listener);
2063 listener->register_property(this);
2065 listener->valueChanged(this);
2069 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2071 vector<SGPropertyChangeListener*>::iterator it =
2072 find(_listeners->begin(), _listeners->end(), listener);
2073 if (it != _listeners->end()) {
2074 _listeners->erase(it);
2075 listener->unregister_property(this);
2076 if (_listeners->empty()) {
2077 vector<SGPropertyChangeListener*>* tmp = _listeners;
2085 SGPropertyNode::fireValueChanged ()
2087 fireValueChanged(this);
2091 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2093 fireChildAdded(this, child);
2097 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2099 fireChildRemoved(this, child);
2103 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2105 if (_listeners != 0) {
2106 for (unsigned int i = 0; i < _listeners->size(); i++) {
2107 (*_listeners)[i]->valueChanged(node);
2111 _parent->fireValueChanged(node);
2115 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2116 SGPropertyNode * child)
2118 if (_listeners != 0) {
2119 for (unsigned int i = 0; i < _listeners->size(); i++) {
2120 (*_listeners)[i]->childAdded(parent, child);
2124 _parent->fireChildAdded(parent, child);
2128 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2129 SGPropertyNode * child)
2131 if (_listeners != 0) {
2132 for (unsigned int i = 0; i < _listeners->size(); i++) {
2133 (*_listeners)[i]->childRemoved(parent, child);
2137 _parent->fireChildRemoved(parent, child);
2142 ////////////////////////////////////////////////////////////////////////
2143 // Simplified hash table for caching paths.
2144 ////////////////////////////////////////////////////////////////////////
2146 #define HASH_TABLE_SIZE 199
2148 SGPropertyNode::hash_table::entry::entry ()
2153 SGPropertyNode::hash_table::entry::~entry ()
2155 // Don't delete the value; we don't own
2160 SGPropertyNode::hash_table::entry::set_key (const char * key)
2166 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2171 SGPropertyNode::hash_table::bucket::bucket ()
2177 SGPropertyNode::hash_table::bucket::~bucket ()
2179 for (int i = 0; i < _length; i++)
2184 SGPropertyNode::hash_table::entry *
2185 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2188 for (i = 0; i < _length; i++) {
2189 if (!strcmp(_entries[i]->get_key(), key))
2193 entry ** new_entries = new entry*[_length+1];
2194 for (i = 0; i < _length; i++) {
2195 new_entries[i] = _entries[i];
2198 _entries = new_entries;
2199 _entries[_length] = new entry;
2200 _entries[_length]->set_key(key);
2202 return _entries[_length - 1];
2209 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2211 for (int i = 0; i < _length; i++) {
2212 if (_entries[i]->get_value() == node) {
2214 for (++i; i < _length; i++) {
2215 _entries[i-1] = _entries[i];
2225 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2227 for (int i = 0; i < _length; i++) {
2228 SGPropertyNode * node = _entries[i]->get_value();
2230 node->remove_linked_node(owner);
2234 SGPropertyNode::hash_table::hash_table ()
2240 SGPropertyNode::hash_table::~hash_table ()
2242 for (unsigned int i = 0; i < _data_length; i++) {
2244 _data[i]->clear(this);
2252 SGPropertyNode::hash_table::get (const char * key)
2254 if (_data_length == 0)
2256 unsigned int index = hashcode(key) % _data_length;
2257 if (_data[index] == 0)
2259 entry * e = _data[index]->get_entry(key);
2263 return e->get_value();
2267 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2269 if (_data_length == 0) {
2270 _data = new bucket*[HASH_TABLE_SIZE];
2271 _data_length = HASH_TABLE_SIZE;
2272 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2275 unsigned int index = hashcode(key) % _data_length;
2276 if (_data[index] == 0) {
2277 _data[index] = new bucket;
2279 entry * e = _data[index]->get_entry(key, true);
2280 e->set_value(value);
2281 value->add_linked_node(this);
2285 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2287 for (unsigned int i = 0; i < _data_length; i++)
2288 if (_data[i] && _data[i]->erase(node))
2295 SGPropertyNode::hash_table::hashcode (const char * key)
2297 unsigned int hash = 0;
2299 hash = 31 * hash + *key;
2307 ////////////////////////////////////////////////////////////////////////
2308 // Implementation of SGPropertyChangeListener.
2309 ////////////////////////////////////////////////////////////////////////
2311 SGPropertyChangeListener::~SGPropertyChangeListener ()
2313 for (int i = _properties.size() - 1; i >= 0; i--)
2314 _properties[i]->removeChangeListener(this);
2318 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2324 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2325 SGPropertyNode * child)
2331 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2332 SGPropertyNode * child)
2338 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2340 _properties.push_back(node);
2344 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2346 vector<SGPropertyNode *>::iterator it =
2347 find(_properties.begin(), _properties.end(), node);
2348 if (it != _properties.end())
2349 _properties.erase(it);
2353 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2356 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2357 for (int i = 0; i < 3; ++i) {
2368 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2370 for (int i = 0; i < 3; ++i) {
2371 stream >> result[i];
2377 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2380 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2381 for (int i = 0; i < 4; ++i) {
2392 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2394 for (int i = 0; i < 4; ++i) {
2395 stream >> result[i];
2402 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2404 props::Type ltype = lhs.getType();
2405 props::Type rtype = rhs.getType();
2412 return false; // XXX Should we look in aliases?
2414 return lhs.getValue<bool>() == rhs.getValue<bool>();
2416 return lhs.getValue<int>() == rhs.getValue<int>();
2418 return lhs.getValue<long>() == rhs.getValue<long>();
2420 return lhs.getValue<float>() == rhs.getValue<float>();
2422 return lhs.getValue<double>() == rhs.getValue<double>();
2424 case props::UNSPECIFIED:
2425 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2427 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2429 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2437 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2438 const SGPropertyNode& rhs)
2442 int lhsChildren = lhs.nChildren();
2443 int rhsChildren = rhs.nChildren();
2444 if (lhsChildren != rhsChildren)
2446 if (lhsChildren == 0)
2447 return compareNodeValue(lhs, rhs);
2448 for (size_t i = 0; i < lhs._children.size(); ++i) {
2449 const SGPropertyNode* lchild = lhs._children[i];
2450 const SGPropertyNode* rchild = rhs._children[i];
2451 // I'm guessing that the nodes will usually be in the same
2453 if (lchild->getIndex() != rchild->getIndex()
2454 || lchild->getNameString() != rchild->getNameString()) {
2456 for (PropertyList::const_iterator itr = rhs._children.begin(),
2457 end = rhs._children.end();
2460 if (lchild->getIndex() == (*itr)->getIndex()
2461 && lchild->getNameString() == (*itr)->getNameString()) {
2468 if (!compare(*lchild, *rchild))
2474 struct PropertyPlaceLess {
2475 typedef bool result_type;
2476 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2478 int comp = lhs->getNameString().compare(rhs->getNameString());
2480 return lhs->getIndex() < rhs->getIndex();
2486 size_t hash_value(const SGPropertyNode& node)
2488 using namespace boost;
2490 if (node.nChildren() == 0) {
2491 switch (node.getType()) {
2496 return hash_value(node.getValue<bool>());
2498 return hash_value(node.getValue<int>());
2500 return hash_value(node.getValue<long>());
2502 return hash_value(node.getValue<float>());
2504 return hash_value(node.getValue<double>());
2506 case props::UNSPECIFIED:
2508 const char *val = node.getStringValue();
2509 return hash_range(val, val + strlen(val));
2513 const SGVec3d val = node.getValue<SGVec3d>();
2514 return hash_range(&val[0], &val[3]);
2518 const SGVec4d val = node.getValue<SGVec4d>();
2519 return hash_range(&val[0], &val[4]);
2521 case props::ALIAS: // XXX Should we look in aliases?
2527 PropertyList children(node._children.begin(), node._children.end());
2528 sort(children.begin(), children.end(), PropertyPlaceLess());
2529 for (PropertyList::const_iterator itr = children.begin(),
2530 end = children.end();
2533 hash_combine(seed, (*itr)->_name);
2534 hash_combine(seed, (*itr)->_index);
2535 hash_combine(seed, hash_value(**itr));