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>
22 #include <boost/algorithm/string/find_iterator.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/algorithm/string/classification.hpp>
25 #include <boost/range.hpp>
27 #include <simgear/math/SGMath.hxx>
33 #include <simgear/compiler.h>
34 #include <simgear/debug/logstream.hxx>
36 #if ( _MSC_VER == 1200 )
37 // MSVC 6 is buggy, and needs something strange here
38 using std::vector<SGPropertyNode_ptr>;
39 using std::vector<SGPropertyChangeListener *>;
40 using std::vector<SGPropertyNode *>;
52 using std::stringstream;
54 using namespace simgear;
57 ////////////////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////////////////
62 * Comparator class for sorting by index.
67 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
68 return (n1->getIndex() < n2->getIndex());
74 ////////////////////////////////////////////////////////////////////////
75 // Convenience macros for value access.
76 ////////////////////////////////////////////////////////////////////////
78 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
79 #define TEST_WRITE if (!getAttribute(WRITE)) return false
82 ////////////////////////////////////////////////////////////////////////
83 // Local path normalization code.
84 ////////////////////////////////////////////////////////////////////////
87 * Parse the name for a path component.
89 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
92 template<typename Range>
94 parse_name (const Range &path)
96 typename Range::iterator i = path.begin();
97 typename Range::iterator max = path.end();
101 if (i != path.end() && *i == '.') {
104 if (i != max && *i != '/')
105 throw string("illegal character after . or ..");
106 } else if (isalpha(*i) || *i == '_') {
109 // The rules inside a name are a little
112 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
113 *i == '-' || *i == '.') {
115 } else if (*i == '[' || *i == '/') {
118 throw string("name may contain only ._- and alphanumeric characters");
125 if (path.begin() == i)
126 throw string("name must begin with alpha or '_'");
128 return Range(path.begin(), i);
131 // Validate the name of a single node
132 inline bool validateName(const string& name)
134 using namespace boost;
137 if (!isalpha(name[0]) && name[0] != '_')
139 return all(make_iterator_range(name.begin(), name.end()),
140 is_alnum() || is_any_of("_-."));
144 ////////////////////////////////////////////////////////////////////////
145 // Other static utility functions.
146 ////////////////////////////////////////////////////////////////////////
150 copy_string (const char * s)
152 unsigned long int slen = strlen(s);
153 char * copy = new char[slen + 1];
155 // the source string length is known so no need to check for '\0'
156 // when copying every single character
157 memcpy(copy, s, slen);
158 *(copy + slen) = '\0';
163 compare_strings (const char * s1, const char * s2)
165 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
169 * Locate a child node by name and index.
171 template<typename Itr>
173 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
175 int nNodes = nodes.size();
176 boost::iterator_range<Itr> name(begin, end);
177 for (int i = 0; i < nNodes; i++) {
178 SGPropertyNode * node = nodes[i];
180 // searching for a mathing index is a lot less time consuming than
181 // comparing two strings so do that first.
182 if (node->getIndex() == index && boost::equals(node->getName(), name))
189 * Locate the child node with the highest index of the same name
192 find_last_child (const char * name, const PropertyList& nodes)
194 int nNodes = nodes.size();
197 for (int i = 0; i < nNodes; i++) {
198 SGPropertyNode * node = nodes[i];
199 if (compare_strings(node->getName(), name))
201 int idx = node->getIndex();
202 if (idx > index) index = idx;
208 template<typename Itr>
209 inline SGPropertyNode*
210 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
212 int pos = find_child(begin, end, index, _children);
214 return _children[pos];
216 SGPropertyNode_ptr node;
217 pos = find_child(begin, end, index, _removedChildren);
219 PropertyList::iterator it = _removedChildren.begin();
221 node = _removedChildren[pos];
222 _removedChildren.erase(it);
223 node->setAttribute(REMOVED, false);
224 _children.push_back(node);
225 fireChildAdded(node);
232 template<typename Itr>
234 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
236 SGPropertyNode* node = getExistingChild(begin, end, index, create);
241 node = new SGPropertyNode(begin, end, index, this);
242 _children.push_back(node);
243 fireChildAdded(node);
250 template<typename SplitItr>
252 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
255 typedef typename SplitItr::value_type Range;
256 // Run off the end of the list
261 // Success! This is the one we want.
265 // Empty name at this point is empty, not root.
267 return find_node_aux(current, ++itr, create, last_index);
268 Range name = parse_name(token);
269 if (equals(name, "."))
270 return find_node_aux(current, ++itr, create, last_index);
271 if (equals(name, "..")) {
272 SGPropertyNode * parent = current->getParent();
274 throw string("attempt to move past root with '..'");
275 return find_node_aux(parent, ++itr, create, last_index);
278 if (last_index >= 0) {
279 // If we are at the last token and last_index is valid, use
280 // last_index as the index value
282 while (!(++itr).eof()) {
296 if (name.end() != token.end()) {
297 if (*name.end() == '[') {
298 typename Range::iterator i = name.end() + 1, end = token.end();
299 for (;i != end; ++i) {
301 index = (index * 10) + (*i - '0');
306 if (i == token.end() || *i != ']')
307 throw string("unterminated index (looking for ']')");
309 throw string("illegal characters in token: ")
310 + string(name.begin(), name.end());
314 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
315 index, create), itr, create,
319 // Internal function for parsing property paths. last_index provides
320 // and index value for the last node name token, if supplied.
321 template<typename Range>
323 find_node (SGPropertyNode * current,
328 using namespace boost;
329 typedef split_iterator<typename range_result_iterator<Range>::type>
332 PathSplitIterator itr
333 = make_split_iterator(path, first_finder("/", is_equal()));
334 if (*path.begin() == '/')
335 return find_node_aux(current->getRootNode(), itr, create, last_index);
337 return find_node_aux(current, itr, create, last_index);
341 ////////////////////////////////////////////////////////////////////////
342 // Private methods from SGPropertyNode (may be inlined for speed).
343 ////////////////////////////////////////////////////////////////////////
346 SGPropertyNode::get_bool () const
349 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
351 return _local_val.bool_val;
355 SGPropertyNode::get_int () const
358 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
360 return _local_val.int_val;
364 SGPropertyNode::get_long () const
367 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
369 return _local_val.long_val;
373 SGPropertyNode::get_float () const
376 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
378 return _local_val.float_val;
382 SGPropertyNode::get_double () const
385 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
387 return _local_val.double_val;
391 SGPropertyNode::get_string () const
394 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
396 return _local_val.string_val;
400 SGPropertyNode::set_bool (bool val)
403 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
410 _local_val.bool_val = val;
417 SGPropertyNode::set_int (int val)
420 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
427 _local_val.int_val = val;
434 SGPropertyNode::set_long (long val)
437 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
444 _local_val.long_val = val;
451 SGPropertyNode::set_float (float val)
454 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
461 _local_val.float_val = val;
468 SGPropertyNode::set_double (double val)
471 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
478 _local_val.double_val = val;
485 SGPropertyNode::set_string (const char * val)
488 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
495 delete [] _local_val.string_val;
496 _local_val.string_val = copy_string(val);
503 SGPropertyNode::clearValue ()
505 if (_type == props::ALIAS) {
508 } else if (_type != props::NONE) {
511 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
514 _local_val.int_val = SGRawValue<int>::DefaultValue();
517 _local_val.long_val = SGRawValue<long>::DefaultValue();
520 _local_val.float_val = SGRawValue<float>::DefaultValue();
523 _local_val.double_val = SGRawValue<double>::DefaultValue();
526 case props::UNSPECIFIED:
528 delete [] _local_val.string_val;
530 _local_val.string_val = 0;
532 default: // avoid compiler warning
544 * Get the value as a string.
547 SGPropertyNode::make_string () const
549 if (!getAttribute(READ))
553 return _value.alias->getStringValue();
555 return get_bool() ? "true" : "false";
557 case props::UNSPECIFIED:
576 sstr << std::setprecision(10) << get_double();
578 case props::EXTENDED:
580 props::Type realType = _value.val->getType();
581 // Perhaps this should be done for all types?
582 if (realType == props::VEC3D || realType == props::VEC4D)
584 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
590 _buffer = sstr.str();
591 return _buffer.c_str();
595 * Trace a write access for a property.
598 SGPropertyNode::trace_write () const
601 cerr << "TRACE: Write node " << getPath () << ", value \""
602 << make_string() << '"' << endl;
604 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
605 << ", value \"" << make_string() << '"');
610 * Trace a read access for a property.
613 SGPropertyNode::trace_read () const
616 cerr << "TRACE: Write node " << getPath () << ", value \""
617 << make_string() << '"' << endl;
619 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
620 << ", value \"" << make_string() << '"');
625 ////////////////////////////////////////////////////////////////////////
626 // Public methods from SGPropertyNode.
627 ////////////////////////////////////////////////////////////////////////
630 * Last used attribute
631 * Update as needed when enum Attribute is changed
633 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
636 * Default constructor: always creates a root node.
638 SGPropertyNode::SGPropertyNode ()
647 _local_val.string_val = 0;
655 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
656 : _index(node._index),
658 _parent(0), // don't copy the parent
663 _listeners(0) // CHECK!!
665 _local_val.string_val = 0;
667 if (_type == props::NONE)
669 if (_type == props::ALIAS) {
670 _value.alias = node._value.alias;
675 if (_tied || _type == props::EXTENDED) {
676 _value.val = node._value.val->clone();
681 set_bool(node.get_bool());
684 set_int(node.get_int());
687 set_long(node.get_long());
690 set_float(node.get_float());
693 set_double(node.get_double());
696 case props::UNSPECIFIED:
697 set_string(node.get_string());
706 * Convenience constructor.
708 template<typename Itr>
709 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
711 SGPropertyNode * parent)
721 _local_val.string_val = 0;
723 if (!validateName(_name))
724 throw string("plain name expected instead of '") + _name + '\'';
727 SGPropertyNode::SGPropertyNode (const string& name,
729 SGPropertyNode * parent)
739 _local_val.string_val = 0;
741 if (!validateName(name))
742 throw string("plain name expected instead of '") + _name + '\'';
748 SGPropertyNode::~SGPropertyNode ()
750 // zero out all parent pointers, else they might be dangling
751 for (unsigned i = 0; i < _children.size(); ++i)
752 _children[i]->_parent = 0;
753 for (unsigned i = 0; i < _removedChildren.size(); ++i)
754 _removedChildren[i]->_parent = 0;
759 vector<SGPropertyChangeListener*>::iterator it;
760 for (it = _listeners->begin(); it != _listeners->end(); ++it)
761 (*it)->unregister_property(this);
768 * Alias to another node.
771 SGPropertyNode::alias (SGPropertyNode * target)
773 if (target == 0 || _type == props::ALIAS || _tied)
777 _value.alias = target;
778 _type = props::ALIAS;
784 * Alias to another node by path.
787 SGPropertyNode::alias (const char * path)
789 return alias(getNode(path, true));
797 SGPropertyNode::unalias ()
799 if (_type != props::ALIAS)
807 * Get the target of an alias.
810 SGPropertyNode::getAliasTarget ()
812 return (_type == props::ALIAS ? _value.alias : 0);
816 const SGPropertyNode *
817 SGPropertyNode::getAliasTarget () const
819 return (_type == props::ALIAS ? _value.alias : 0);
823 * create a non-const child by name after the last node with the same name.
826 SGPropertyNode::addChild (const char * name)
828 int pos = find_last_child(name, _children)+1;
830 SGPropertyNode_ptr node;
831 node = new SGPropertyNode(name, name + strlen(name), pos, this);
832 _children.push_back(node);
833 fireChildAdded(node);
839 * Get a non-const child by index.
842 SGPropertyNode::getChild (int position)
844 if (position >= 0 && position < nChildren())
845 return _children[position];
852 * Get a const child by index.
854 const SGPropertyNode *
855 SGPropertyNode::getChild (int position) const
857 if (position >= 0 && position < nChildren())
858 return _children[position];
865 * Get a non-const child by name and index, creating if necessary.
869 SGPropertyNode::getChild (const char * name, int index, bool create)
871 return getChildImpl(name, name + strlen(name), index, create);
875 SGPropertyNode::getChild (const string& name, int index, bool create)
877 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
882 node = new SGPropertyNode(name, index, this);
883 _children.push_back(node);
884 fireChildAdded(node);
892 * Get a const child by name and index.
894 const SGPropertyNode *
895 SGPropertyNode::getChild (const char * name, int index) const
897 int pos = find_child(name, name + strlen(name), index, _children);
899 return _children[pos];
906 * Get all children with the same name (but different indices).
909 SGPropertyNode::getChildren (const char * name) const
911 PropertyList children;
912 int max = _children.size();
914 for (int i = 0; i < max; i++)
915 if (compare_strings(_children[i]->getName(), name))
916 children.push_back(_children[i]);
918 sort(children.begin(), children.end(), CompareIndices());
924 * Remove this node and all children from nodes that link to them
925 * in their path cache.
928 SGPropertyNode::remove_from_path_caches ()
930 for (unsigned int i = 0; i < _children.size(); ++i)
931 _children[i]->remove_from_path_caches();
933 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
934 _linkedNodes[i]->erase(this);
935 _linkedNodes.clear();
940 * Remove child by position.
943 SGPropertyNode::removeChild (int pos, bool keep)
945 SGPropertyNode_ptr node;
946 if (pos < 0 || pos >= (int)_children.size())
949 PropertyList::iterator it = _children.begin();
951 node = _children[pos];
954 _removedChildren.push_back(node);
957 node->remove_from_path_caches();
958 node->setAttribute(REMOVED, true);
960 fireChildRemoved(node);
966 * Remove a child node
969 SGPropertyNode::removeChild (const char * name, int index, bool keep)
971 SGPropertyNode_ptr ret;
972 int pos = find_child(name, name + strlen(name), index, _children);
974 ret = removeChild(pos, keep);
980 * Remove all children with the specified name.
983 SGPropertyNode::removeChildren (const char * name, bool keep)
985 PropertyList children;
987 for (int pos = _children.size() - 1; pos >= 0; pos--)
988 if (compare_strings(_children[pos]->getName(), name))
989 children.push_back(removeChild(pos, keep));
991 sort(children.begin(), children.end(), CompareIndices());
997 * Remove a linked node.
1000 SGPropertyNode::remove_linked_node (hash_table * node)
1002 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
1003 if (_linkedNodes[i] == node) {
1004 vector<hash_table *>::iterator it = _linkedNodes.begin();
1006 _linkedNodes.erase(it);
1015 SGPropertyNode::getDisplayName (bool simplify) const
1017 string display_name = _name;
1018 if (_index != 0 || !simplify) {
1020 sstr << '[' << _index << ']';
1021 display_name += sstr.str();
1023 return display_name;
1028 SGPropertyNode::getPath (bool simplify) const
1030 // Calculate the complete path only once.
1031 if (_parent != 0 && _path.empty()) {
1032 _path = _parent->getPath(simplify);
1034 _path += getDisplayName(simplify);
1037 return _path.c_str();
1041 SGPropertyNode::getType () const
1043 if (_type == props::ALIAS)
1044 return _value.alias->getType();
1045 else if (_type == props::EXTENDED)
1046 return _value.val->getType();
1053 SGPropertyNode::getBoolValue () const
1055 // Shortcut for common case
1056 if (_attr == (READ|WRITE) && _type == props::BOOL)
1059 if (getAttribute(TRACE_READ))
1061 if (!getAttribute(READ))
1062 return SGRawValue<bool>::DefaultValue();
1065 return _value.alias->getBoolValue();
1069 return get_int() == 0 ? false : true;
1071 return get_long() == 0L ? false : true;
1073 return get_float() == 0.0 ? false : true;
1075 return get_double() == 0.0L ? false : true;
1077 case props::UNSPECIFIED:
1078 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1081 return SGRawValue<bool>::DefaultValue();
1086 SGPropertyNode::getIntValue () const
1088 // Shortcut for common case
1089 if (_attr == (READ|WRITE) && _type == props::INT)
1092 if (getAttribute(TRACE_READ))
1094 if (!getAttribute(READ))
1095 return SGRawValue<int>::DefaultValue();
1098 return _value.alias->getIntValue();
1100 return int(get_bool());
1104 return int(get_long());
1106 return int(get_float());
1108 return int(get_double());
1110 case props::UNSPECIFIED:
1111 return atoi(get_string());
1114 return SGRawValue<int>::DefaultValue();
1119 SGPropertyNode::getLongValue () const
1121 // Shortcut for common case
1122 if (_attr == (READ|WRITE) && _type == props::LONG)
1125 if (getAttribute(TRACE_READ))
1127 if (!getAttribute(READ))
1128 return SGRawValue<long>::DefaultValue();
1131 return _value.alias->getLongValue();
1133 return long(get_bool());
1135 return long(get_int());
1139 return long(get_float());
1141 return long(get_double());
1143 case props::UNSPECIFIED:
1144 return strtol(get_string(), 0, 0);
1147 return SGRawValue<long>::DefaultValue();
1152 SGPropertyNode::getFloatValue () const
1154 // Shortcut for common case
1155 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1158 if (getAttribute(TRACE_READ))
1160 if (!getAttribute(READ))
1161 return SGRawValue<float>::DefaultValue();
1164 return _value.alias->getFloatValue();
1166 return float(get_bool());
1168 return float(get_int());
1170 return float(get_long());
1174 return float(get_double());
1176 case props::UNSPECIFIED:
1177 return atof(get_string());
1180 return SGRawValue<float>::DefaultValue();
1185 SGPropertyNode::getDoubleValue () const
1187 // Shortcut for common case
1188 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1189 return get_double();
1191 if (getAttribute(TRACE_READ))
1193 if (!getAttribute(READ))
1194 return SGRawValue<double>::DefaultValue();
1198 return _value.alias->getDoubleValue();
1200 return double(get_bool());
1202 return double(get_int());
1204 return double(get_long());
1206 return double(get_float());
1208 return get_double();
1210 case props::UNSPECIFIED:
1211 return strtod(get_string(), 0);
1214 return SGRawValue<double>::DefaultValue();
1219 SGPropertyNode::getStringValue () const
1221 // Shortcut for common case
1222 if (_attr == (READ|WRITE) && _type == props::STRING)
1223 return get_string();
1225 if (getAttribute(TRACE_READ))
1227 if (!getAttribute(READ))
1228 return SGRawValue<const char *>::DefaultValue();
1229 return make_string();
1233 SGPropertyNode::setBoolValue (bool value)
1235 // Shortcut for common case
1236 if (_attr == (READ|WRITE) && _type == props::BOOL)
1237 return set_bool(value);
1239 bool result = false;
1241 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1244 _type = props::BOOL;
1249 result = _value.alias->setBoolValue(value);
1252 result = set_bool(value);
1255 result = set_int(int(value));
1258 result = set_long(long(value));
1261 result = set_float(float(value));
1264 result = set_double(double(value));
1267 case props::UNSPECIFIED:
1268 result = set_string(value ? "true" : "false");
1275 if (getAttribute(TRACE_WRITE))
1281 SGPropertyNode::setIntValue (int value)
1283 // Shortcut for common case
1284 if (_attr == (READ|WRITE) && _type == props::INT)
1285 return set_int(value);
1287 bool result = false;
1289 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1292 _local_val.int_val = 0;
1297 result = _value.alias->setIntValue(value);
1300 result = set_bool(value == 0 ? false : true);
1303 result = set_int(value);
1306 result = set_long(long(value));
1309 result = set_float(float(value));
1312 result = set_double(double(value));
1315 case props::UNSPECIFIED: {
1317 sprintf(buf, "%d", value);
1318 result = set_string(buf);
1326 if (getAttribute(TRACE_WRITE))
1332 SGPropertyNode::setLongValue (long value)
1334 // Shortcut for common case
1335 if (_attr == (READ|WRITE) && _type == props::LONG)
1336 return set_long(value);
1338 bool result = false;
1340 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1342 _type = props::LONG;
1343 _local_val.long_val = 0L;
1348 result = _value.alias->setLongValue(value);
1351 result = set_bool(value == 0L ? false : true);
1354 result = set_int(int(value));
1357 result = set_long(value);
1360 result = set_float(float(value));
1363 result = set_double(double(value));
1366 case props::UNSPECIFIED: {
1368 sprintf(buf, "%ld", value);
1369 result = set_string(buf);
1377 if (getAttribute(TRACE_WRITE))
1383 SGPropertyNode::setFloatValue (float value)
1385 // Shortcut for common case
1386 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1387 return set_float(value);
1389 bool result = false;
1391 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1393 _type = props::FLOAT;
1394 _local_val.float_val = 0;
1399 result = _value.alias->setFloatValue(value);
1402 result = set_bool(value == 0.0 ? false : true);
1405 result = set_int(int(value));
1408 result = set_long(long(value));
1411 result = set_float(value);
1414 result = set_double(double(value));
1417 case props::UNSPECIFIED: {
1419 sprintf(buf, "%f", value);
1420 result = set_string(buf);
1428 if (getAttribute(TRACE_WRITE))
1434 SGPropertyNode::setDoubleValue (double value)
1436 // Shortcut for common case
1437 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1438 return set_double(value);
1440 bool result = false;
1442 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1444 _local_val.double_val = value;
1445 _type = props::DOUBLE;
1450 result = _value.alias->setDoubleValue(value);
1453 result = set_bool(value == 0.0L ? false : true);
1456 result = set_int(int(value));
1459 result = set_long(long(value));
1462 result = set_float(float(value));
1465 result = set_double(value);
1468 case props::UNSPECIFIED: {
1470 sprintf(buf, "%f", value);
1471 result = set_string(buf);
1479 if (getAttribute(TRACE_WRITE))
1485 SGPropertyNode::setStringValue (const char * value)
1487 // Shortcut for common case
1488 if (_attr == (READ|WRITE) && _type == props::STRING)
1489 return set_string(value);
1491 bool result = false;
1493 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1495 _type = props::STRING;
1500 result = _value.alias->setStringValue(value);
1503 result = set_bool((compare_strings(value, "true")
1504 || atoi(value)) ? true : false);
1507 result = set_int(atoi(value));
1510 result = set_long(strtol(value, 0, 0));
1513 result = set_float(atof(value));
1516 result = set_double(strtod(value, 0));
1519 case props::UNSPECIFIED:
1520 result = set_string(value);
1522 case props::EXTENDED:
1524 stringstream sstr(value);
1525 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1533 if (getAttribute(TRACE_WRITE))
1539 SGPropertyNode::setUnspecifiedValue (const char * value)
1541 bool result = false;
1543 if (_type == props::NONE) {
1545 _type = props::UNSPECIFIED;
1547 props::Type type = _type;
1548 if (type == props::EXTENDED)
1549 type = _value.val->getType();
1552 result = _value.alias->setUnspecifiedValue(value);
1555 result = set_bool((compare_strings(value, "true")
1556 || atoi(value)) ? true : false);
1559 result = set_int(atoi(value));
1562 result = set_long(strtol(value, 0, 0));
1565 result = set_float(atof(value));
1568 result = set_double(strtod(value, 0));
1571 case props::UNSPECIFIED:
1572 result = set_string(value);
1575 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1578 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1585 if (getAttribute(TRACE_WRITE))
1590 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1592 if (!getAttribute(READ))
1596 return _value.alias->printOn(stream);
1598 stream << (get_bool() ? "true" : "false");
1601 stream << get_int();
1604 stream << get_long();
1607 stream << get_float();
1610 stream << get_double();
1613 case props::UNSPECIFIED:
1614 stream << get_string();
1616 case props::EXTENDED:
1617 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1621 default: // avoid compiler warning
1628 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1631 if (_type == props::ALIAS || _tied)
1634 useDefault = useDefault && hasValue();
1635 std::string old_val;
1637 old_val = getStringValue();
1639 _type = props::STRING;
1641 _value.val = rawValue.clone();
1644 setStringValue(old_val.c_str());
1649 SGPropertyNode::untie ()
1656 bool val = getBoolValue();
1658 _type = props::BOOL;
1659 _local_val.bool_val = val;
1663 int val = getIntValue();
1666 _local_val.int_val = val;
1670 long val = getLongValue();
1672 _type = props::LONG;
1673 _local_val.long_val = val;
1676 case props::FLOAT: {
1677 float val = getFloatValue();
1679 _type = props::FLOAT;
1680 _local_val.float_val = val;
1683 case props::DOUBLE: {
1684 double val = getDoubleValue();
1686 _type = props::DOUBLE;
1687 _local_val.double_val = val;
1691 case props::UNSPECIFIED: {
1692 string val = getStringValue();
1694 _type = props::STRING;
1695 _local_val.string_val = copy_string(val.c_str());
1698 case props::EXTENDED: {
1699 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1700 _value.val = 0; // Prevent clearValue() from deleting
1702 _type = props::EXTENDED;
1703 _value.val = val->makeContainer();
1717 SGPropertyNode::getRootNode ()
1722 return _parent->getRootNode();
1725 const SGPropertyNode *
1726 SGPropertyNode::getRootNode () const
1731 return _parent->getRootNode();
1735 SGPropertyNode::getNode (const char * relative_path, bool create)
1737 using namespace boost;
1738 if (_path_cache == 0)
1739 _path_cache = new hash_table;
1741 SGPropertyNode * result = _path_cache->get(relative_path);
1743 result = find_node(this,
1744 make_iterator_range(relative_path, relative_path
1745 + strlen(relative_path)),
1748 _path_cache->put(relative_path, result);
1755 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1757 using namespace boost;
1758 return find_node(this, make_iterator_range(relative_path, relative_path
1759 + strlen(relative_path)),
1763 const SGPropertyNode *
1764 SGPropertyNode::getNode (const char * relative_path) const
1766 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1769 const SGPropertyNode *
1770 SGPropertyNode::getNode (const char * relative_path, int index) const
1772 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1776 ////////////////////////////////////////////////////////////////////////
1777 // Convenience methods using relative paths.
1778 ////////////////////////////////////////////////////////////////////////
1782 * Test whether another node has a value attached.
1785 SGPropertyNode::hasValue (const char * relative_path) const
1787 const SGPropertyNode * node = getNode(relative_path);
1788 return (node == 0 ? false : node->hasValue());
1793 * Get the value type for another node.
1796 SGPropertyNode::getType (const char * relative_path) const
1798 const SGPropertyNode * node = getNode(relative_path);
1799 return (node == 0 ? props::UNSPECIFIED : node->getType());
1804 * Get a bool value for another node.
1807 SGPropertyNode::getBoolValue (const char * relative_path,
1808 bool defaultValue) const
1810 const SGPropertyNode * node = getNode(relative_path);
1811 return (node == 0 ? defaultValue : node->getBoolValue());
1816 * Get an int value for another node.
1819 SGPropertyNode::getIntValue (const char * relative_path,
1820 int defaultValue) const
1822 const SGPropertyNode * node = getNode(relative_path);
1823 return (node == 0 ? defaultValue : node->getIntValue());
1828 * Get a long value for another node.
1831 SGPropertyNode::getLongValue (const char * relative_path,
1832 long defaultValue) const
1834 const SGPropertyNode * node = getNode(relative_path);
1835 return (node == 0 ? defaultValue : node->getLongValue());
1840 * Get a float value for another node.
1843 SGPropertyNode::getFloatValue (const char * relative_path,
1844 float defaultValue) const
1846 const SGPropertyNode * node = getNode(relative_path);
1847 return (node == 0 ? defaultValue : node->getFloatValue());
1852 * Get a double value for another node.
1855 SGPropertyNode::getDoubleValue (const char * relative_path,
1856 double defaultValue) const
1858 const SGPropertyNode * node = getNode(relative_path);
1859 return (node == 0 ? defaultValue : node->getDoubleValue());
1864 * Get a string value for another node.
1867 SGPropertyNode::getStringValue (const char * relative_path,
1868 const char * defaultValue) const
1870 const SGPropertyNode * node = getNode(relative_path);
1871 return (node == 0 ? defaultValue : node->getStringValue());
1876 * Set a bool value for another node.
1879 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1881 return getNode(relative_path, true)->setBoolValue(value);
1886 * Set an int value for another node.
1889 SGPropertyNode::setIntValue (const char * relative_path, int value)
1891 return getNode(relative_path, true)->setIntValue(value);
1896 * Set a long value for another node.
1899 SGPropertyNode::setLongValue (const char * relative_path, long value)
1901 return getNode(relative_path, true)->setLongValue(value);
1906 * Set a float value for another node.
1909 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1911 return getNode(relative_path, true)->setFloatValue(value);
1916 * Set a double value for another node.
1919 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1921 return getNode(relative_path, true)->setDoubleValue(value);
1926 * Set a string value for another node.
1929 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1931 return getNode(relative_path, true)->setStringValue(value);
1936 * Set an unknown value for another node.
1939 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1942 return getNode(relative_path, true)->setUnspecifiedValue(value);
1947 * Test whether another node is tied.
1950 SGPropertyNode::isTied (const char * relative_path) const
1952 const SGPropertyNode * node = getNode(relative_path);
1953 return (node == 0 ? false : node->isTied());
1958 * Tie a node reached by a relative path, creating it if necessary.
1961 SGPropertyNode::tie (const char * relative_path,
1962 const SGRawValue<bool> &rawValue,
1965 return getNode(relative_path, true)->tie(rawValue, useDefault);
1970 * Tie a node reached by a relative path, creating it if necessary.
1973 SGPropertyNode::tie (const char * relative_path,
1974 const SGRawValue<int> &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<long> &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<float> &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<double> &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<const char *> &rawValue,
2025 return getNode(relative_path, true)->tie(rawValue, useDefault);
2030 * Attempt to untie another node reached by a relative path.
2033 SGPropertyNode::untie (const char * relative_path)
2035 SGPropertyNode * node = getNode(relative_path);
2036 return (node == 0 ? false : node->untie());
2040 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2043 if (_listeners == 0)
2044 _listeners = new vector<SGPropertyChangeListener*>;
2045 _listeners->push_back(listener);
2046 listener->register_property(this);
2048 listener->valueChanged(this);
2052 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2054 vector<SGPropertyChangeListener*>::iterator it =
2055 find(_listeners->begin(), _listeners->end(), listener);
2056 if (it != _listeners->end()) {
2057 _listeners->erase(it);
2058 listener->unregister_property(this);
2059 if (_listeners->empty()) {
2060 vector<SGPropertyChangeListener*>* tmp = _listeners;
2068 SGPropertyNode::fireValueChanged ()
2070 fireValueChanged(this);
2074 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2076 fireChildAdded(this, child);
2080 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2082 fireChildRemoved(this, child);
2086 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2088 if (_listeners != 0) {
2089 for (unsigned int i = 0; i < _listeners->size(); i++) {
2090 (*_listeners)[i]->valueChanged(node);
2094 _parent->fireValueChanged(node);
2098 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2099 SGPropertyNode * child)
2101 if (_listeners != 0) {
2102 for (unsigned int i = 0; i < _listeners->size(); i++) {
2103 (*_listeners)[i]->childAdded(parent, child);
2107 _parent->fireChildAdded(parent, child);
2111 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2112 SGPropertyNode * child)
2114 if (_listeners != 0) {
2115 for (unsigned int i = 0; i < _listeners->size(); i++) {
2116 (*_listeners)[i]->childRemoved(parent, child);
2120 _parent->fireChildRemoved(parent, child);
2125 ////////////////////////////////////////////////////////////////////////
2126 // Simplified hash table for caching paths.
2127 ////////////////////////////////////////////////////////////////////////
2129 #define HASH_TABLE_SIZE 199
2131 SGPropertyNode::hash_table::entry::entry ()
2136 SGPropertyNode::hash_table::entry::~entry ()
2138 // Don't delete the value; we don't own
2143 SGPropertyNode::hash_table::entry::set_key (const char * key)
2149 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2154 SGPropertyNode::hash_table::bucket::bucket ()
2160 SGPropertyNode::hash_table::bucket::~bucket ()
2162 for (int i = 0; i < _length; i++)
2167 SGPropertyNode::hash_table::entry *
2168 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2171 for (i = 0; i < _length; i++) {
2172 if (!strcmp(_entries[i]->get_key(), key))
2176 entry ** new_entries = new entry*[_length+1];
2177 for (i = 0; i < _length; i++) {
2178 new_entries[i] = _entries[i];
2181 _entries = new_entries;
2182 _entries[_length] = new entry;
2183 _entries[_length]->set_key(key);
2185 return _entries[_length - 1];
2192 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2194 for (int i = 0; i < _length; i++) {
2195 if (_entries[i]->get_value() == node) {
2197 for (++i; i < _length; i++) {
2198 _entries[i-1] = _entries[i];
2208 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2210 for (int i = 0; i < _length; i++) {
2211 SGPropertyNode * node = _entries[i]->get_value();
2213 node->remove_linked_node(owner);
2217 SGPropertyNode::hash_table::hash_table ()
2223 SGPropertyNode::hash_table::~hash_table ()
2225 for (unsigned int i = 0; i < _data_length; i++) {
2227 _data[i]->clear(this);
2235 SGPropertyNode::hash_table::get (const char * key)
2237 if (_data_length == 0)
2239 unsigned int index = hashcode(key) % _data_length;
2240 if (_data[index] == 0)
2242 entry * e = _data[index]->get_entry(key);
2246 return e->get_value();
2250 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2252 if (_data_length == 0) {
2253 _data = new bucket*[HASH_TABLE_SIZE];
2254 _data_length = HASH_TABLE_SIZE;
2255 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2258 unsigned int index = hashcode(key) % _data_length;
2259 if (_data[index] == 0) {
2260 _data[index] = new bucket;
2262 entry * e = _data[index]->get_entry(key, true);
2263 e->set_value(value);
2264 value->add_linked_node(this);
2268 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2270 for (unsigned int i = 0; i < _data_length; i++)
2271 if (_data[i] && _data[i]->erase(node))
2278 SGPropertyNode::hash_table::hashcode (const char * key)
2280 unsigned int hash = 0;
2282 hash = 31 * hash + *key;
2290 ////////////////////////////////////////////////////////////////////////
2291 // Implementation of SGPropertyChangeListener.
2292 ////////////////////////////////////////////////////////////////////////
2294 SGPropertyChangeListener::~SGPropertyChangeListener ()
2296 for (int i = _properties.size() - 1; i >= 0; i--)
2297 _properties[i]->removeChangeListener(this);
2301 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2307 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2308 SGPropertyNode * child)
2314 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2315 SGPropertyNode * child)
2321 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2323 _properties.push_back(node);
2327 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2329 vector<SGPropertyNode *>::iterator it =
2330 find(_properties.begin(), _properties.end(), node);
2331 if (it != _properties.end())
2332 _properties.erase(it);
2336 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2339 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2340 for (int i = 0; i < 3; ++i) {
2351 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2353 for (int i = 0; i < 3; ++i) {
2354 stream >> result[i];
2360 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2363 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2364 for (int i = 0; i < 4; ++i) {
2375 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2377 for (int i = 0; i < 4; ++i) {
2378 stream >> result[i];