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 ()
658 _local_val.string_val = 0;
666 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
667 : _index(node._index),
669 _parent(0), // don't copy the parent
673 _listeners(0) // CHECK!!
675 _local_val.string_val = 0;
677 if (_type == props::NONE)
679 if (_type == props::ALIAS) {
680 _value.alias = node._value.alias;
685 if (_tied || _type == props::EXTENDED) {
686 _value.val = node._value.val->clone();
691 set_bool(node.get_bool());
694 set_int(node.get_int());
697 set_long(node.get_long());
700 set_float(node.get_float());
703 set_double(node.get_double());
706 case props::UNSPECIFIED:
707 set_string(node.get_string());
716 * Convenience constructor.
718 template<typename Itr>
719 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
721 SGPropertyNode * parent)
730 _local_val.string_val = 0;
732 if (!validateName(_name))
733 throw string("plain name expected instead of '") + _name + '\'';
736 SGPropertyNode::SGPropertyNode (const string& name,
738 SGPropertyNode * parent)
747 _local_val.string_val = 0;
749 if (!validateName(name))
750 throw string("plain name expected instead of '") + _name + '\'';
756 SGPropertyNode::~SGPropertyNode ()
758 // zero out all parent pointers, else they might be dangling
759 for (unsigned i = 0; i < _children.size(); ++i)
760 _children[i]->_parent = 0;
761 for (unsigned i = 0; i < _removedChildren.size(); ++i)
762 _removedChildren[i]->_parent = 0;
766 vector<SGPropertyChangeListener*>::iterator it;
767 for (it = _listeners->begin(); it != _listeners->end(); ++it)
768 (*it)->unregister_property(this);
775 * Alias to another node.
778 SGPropertyNode::alias (SGPropertyNode * target)
780 if (target == 0 || _type == props::ALIAS || _tied)
784 _value.alias = target;
785 _type = props::ALIAS;
791 * Alias to another node by path.
794 SGPropertyNode::alias (const char * path)
796 return alias(getNode(path, true));
804 SGPropertyNode::unalias ()
806 if (_type != props::ALIAS)
814 * Get the target of an alias.
817 SGPropertyNode::getAliasTarget ()
819 return (_type == props::ALIAS ? _value.alias : 0);
823 const SGPropertyNode *
824 SGPropertyNode::getAliasTarget () const
826 return (_type == props::ALIAS ? _value.alias : 0);
830 * create a non-const child by name after the last node with the same name.
833 SGPropertyNode::addChild (const char * name)
835 int pos = find_last_child(name, _children)+1;
837 SGPropertyNode_ptr node;
838 node = new SGPropertyNode(name, name + strlen(name), pos, this);
839 _children.push_back(node);
840 fireChildAdded(node);
846 * Get a non-const child by index.
849 SGPropertyNode::getChild (int position)
851 if (position >= 0 && position < nChildren())
852 return _children[position];
859 * Get a const child by index.
861 const SGPropertyNode *
862 SGPropertyNode::getChild (int position) const
864 if (position >= 0 && position < nChildren())
865 return _children[position];
872 * Get a non-const child by name and index, creating if necessary.
876 SGPropertyNode::getChild (const char * name, int index, bool create)
878 return getChildImpl(name, name + strlen(name), index, create);
882 SGPropertyNode::getChild (const string& name, int index, bool create)
884 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
889 node = new SGPropertyNode(name, index, this);
890 _children.push_back(node);
891 fireChildAdded(node);
899 * Get a const child by name and index.
901 const SGPropertyNode *
902 SGPropertyNode::getChild (const char * name, int index) const
904 int pos = find_child(name, name + strlen(name), index, _children);
906 return _children[pos];
913 * Get all children with the same name (but different indices).
916 SGPropertyNode::getChildren (const char * name) const
918 PropertyList children;
919 int max = _children.size();
921 for (int i = 0; i < max; i++)
922 if (compare_strings(_children[i]->getName(), name))
923 children.push_back(_children[i]);
925 sort(children.begin(), children.end(), CompareIndices());
931 * Remove child by position.
934 SGPropertyNode::removeChild (int pos, bool keep)
936 SGPropertyNode_ptr node;
937 if (pos < 0 || pos >= (int)_children.size())
940 PropertyList::iterator it = _children.begin();
942 node = _children[pos];
945 _removedChildren.push_back(node);
948 node->setAttribute(REMOVED, true);
950 fireChildRemoved(node);
956 * Remove a child node
959 SGPropertyNode::removeChild (const char * name, int index, bool keep)
961 SGPropertyNode_ptr ret;
962 int pos = find_child(name, name + strlen(name), index, _children);
964 ret = removeChild(pos, keep);
970 * Remove all children with the specified name.
973 SGPropertyNode::removeChildren (const char * name, bool keep)
975 PropertyList children;
977 for (int pos = _children.size() - 1; pos >= 0; pos--)
978 if (compare_strings(_children[pos]->getName(), name))
979 children.push_back(removeChild(pos, keep));
981 sort(children.begin(), children.end(), CompareIndices());
986 SGPropertyNode::getDisplayName (bool simplify) const
988 string display_name = _name;
989 if (_index != 0 || !simplify) {
991 sstr << '[' << _index << ']';
992 display_name += sstr.str();
998 SGPropertyNode::getPath (bool simplify) const
1000 typedef std::vector<SGConstPropertyNode_ptr> PList;
1002 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1003 pathList.push_back(node);
1005 for (PList::reverse_iterator itr = pathList.rbegin(),
1006 rend = pathList.rend();
1010 result += (*itr)->getDisplayName(simplify);
1016 SGPropertyNode::getType () const
1018 if (_type == props::ALIAS)
1019 return _value.alias->getType();
1020 else if (_type == props::EXTENDED)
1021 return _value.val->getType();
1028 SGPropertyNode::getBoolValue () const
1030 // Shortcut for common case
1031 if (_attr == (READ|WRITE) && _type == props::BOOL)
1034 if (getAttribute(TRACE_READ))
1036 if (!getAttribute(READ))
1037 return SGRawValue<bool>::DefaultValue();
1040 return _value.alias->getBoolValue();
1044 return get_int() == 0 ? false : true;
1046 return get_long() == 0L ? false : true;
1048 return get_float() == 0.0 ? false : true;
1050 return get_double() == 0.0L ? false : true;
1052 case props::UNSPECIFIED:
1053 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1056 return SGRawValue<bool>::DefaultValue();
1061 SGPropertyNode::getIntValue () const
1063 // Shortcut for common case
1064 if (_attr == (READ|WRITE) && _type == props::INT)
1067 if (getAttribute(TRACE_READ))
1069 if (!getAttribute(READ))
1070 return SGRawValue<int>::DefaultValue();
1073 return _value.alias->getIntValue();
1075 return int(get_bool());
1079 return int(get_long());
1081 return int(get_float());
1083 return int(get_double());
1085 case props::UNSPECIFIED:
1086 return atoi(get_string());
1089 return SGRawValue<int>::DefaultValue();
1094 SGPropertyNode::getLongValue () const
1096 // Shortcut for common case
1097 if (_attr == (READ|WRITE) && _type == props::LONG)
1100 if (getAttribute(TRACE_READ))
1102 if (!getAttribute(READ))
1103 return SGRawValue<long>::DefaultValue();
1106 return _value.alias->getLongValue();
1108 return long(get_bool());
1110 return long(get_int());
1114 return long(get_float());
1116 return long(get_double());
1118 case props::UNSPECIFIED:
1119 return strtol(get_string(), 0, 0);
1122 return SGRawValue<long>::DefaultValue();
1127 SGPropertyNode::getFloatValue () const
1129 // Shortcut for common case
1130 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1133 if (getAttribute(TRACE_READ))
1135 if (!getAttribute(READ))
1136 return SGRawValue<float>::DefaultValue();
1139 return _value.alias->getFloatValue();
1141 return float(get_bool());
1143 return float(get_int());
1145 return float(get_long());
1149 return float(get_double());
1151 case props::UNSPECIFIED:
1152 return atof(get_string());
1155 return SGRawValue<float>::DefaultValue();
1160 SGPropertyNode::getDoubleValue () const
1162 // Shortcut for common case
1163 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1164 return get_double();
1166 if (getAttribute(TRACE_READ))
1168 if (!getAttribute(READ))
1169 return SGRawValue<double>::DefaultValue();
1173 return _value.alias->getDoubleValue();
1175 return double(get_bool());
1177 return double(get_int());
1179 return double(get_long());
1181 return double(get_float());
1183 return get_double();
1185 case props::UNSPECIFIED:
1186 return strtod(get_string(), 0);
1189 return SGRawValue<double>::DefaultValue();
1194 SGPropertyNode::getStringValue () const
1196 // Shortcut for common case
1197 if (_attr == (READ|WRITE) && _type == props::STRING)
1198 return get_string();
1200 if (getAttribute(TRACE_READ))
1202 if (!getAttribute(READ))
1203 return SGRawValue<const char *>::DefaultValue();
1204 return make_string();
1208 SGPropertyNode::setBoolValue (bool value)
1210 // Shortcut for common case
1211 if (_attr == (READ|WRITE) && _type == props::BOOL)
1212 return set_bool(value);
1214 bool result = false;
1216 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1219 _type = props::BOOL;
1224 result = _value.alias->setBoolValue(value);
1227 result = set_bool(value);
1230 result = set_int(int(value));
1233 result = set_long(long(value));
1236 result = set_float(float(value));
1239 result = set_double(double(value));
1242 case props::UNSPECIFIED:
1243 result = set_string(value ? "true" : "false");
1250 if (getAttribute(TRACE_WRITE))
1256 SGPropertyNode::setIntValue (int value)
1258 // Shortcut for common case
1259 if (_attr == (READ|WRITE) && _type == props::INT)
1260 return set_int(value);
1262 bool result = false;
1264 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1267 _local_val.int_val = 0;
1272 result = _value.alias->setIntValue(value);
1275 result = set_bool(value == 0 ? false : true);
1278 result = set_int(value);
1281 result = set_long(long(value));
1284 result = set_float(float(value));
1287 result = set_double(double(value));
1290 case props::UNSPECIFIED: {
1292 sprintf(buf, "%d", value);
1293 result = set_string(buf);
1301 if (getAttribute(TRACE_WRITE))
1307 SGPropertyNode::setLongValue (long value)
1309 // Shortcut for common case
1310 if (_attr == (READ|WRITE) && _type == props::LONG)
1311 return set_long(value);
1313 bool result = false;
1315 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1317 _type = props::LONG;
1318 _local_val.long_val = 0L;
1323 result = _value.alias->setLongValue(value);
1326 result = set_bool(value == 0L ? false : true);
1329 result = set_int(int(value));
1332 result = set_long(value);
1335 result = set_float(float(value));
1338 result = set_double(double(value));
1341 case props::UNSPECIFIED: {
1343 sprintf(buf, "%ld", value);
1344 result = set_string(buf);
1352 if (getAttribute(TRACE_WRITE))
1358 SGPropertyNode::setFloatValue (float value)
1360 // Shortcut for common case
1361 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1362 return set_float(value);
1364 bool result = false;
1366 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1368 _type = props::FLOAT;
1369 _local_val.float_val = 0;
1374 result = _value.alias->setFloatValue(value);
1377 result = set_bool(value == 0.0 ? false : true);
1380 result = set_int(int(value));
1383 result = set_long(long(value));
1386 result = set_float(value);
1389 result = set_double(double(value));
1392 case props::UNSPECIFIED: {
1394 sprintf(buf, "%f", value);
1395 result = set_string(buf);
1403 if (getAttribute(TRACE_WRITE))
1409 SGPropertyNode::setDoubleValue (double value)
1411 // Shortcut for common case
1412 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1413 return set_double(value);
1415 bool result = false;
1417 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1419 _local_val.double_val = value;
1420 _type = props::DOUBLE;
1425 result = _value.alias->setDoubleValue(value);
1428 result = set_bool(value == 0.0L ? false : true);
1431 result = set_int(int(value));
1434 result = set_long(long(value));
1437 result = set_float(float(value));
1440 result = set_double(value);
1443 case props::UNSPECIFIED: {
1445 sprintf(buf, "%f", value);
1446 result = set_string(buf);
1454 if (getAttribute(TRACE_WRITE))
1460 SGPropertyNode::setStringValue (const char * value)
1462 // Shortcut for common case
1463 if (_attr == (READ|WRITE) && _type == props::STRING)
1464 return set_string(value);
1466 bool result = false;
1468 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1470 _type = props::STRING;
1475 result = _value.alias->setStringValue(value);
1478 result = set_bool((compare_strings(value, "true")
1479 || atoi(value)) ? true : false);
1482 result = set_int(atoi(value));
1485 result = set_long(strtol(value, 0, 0));
1488 result = set_float(atof(value));
1491 result = set_double(strtod(value, 0));
1494 case props::UNSPECIFIED:
1495 result = set_string(value);
1497 case props::EXTENDED:
1499 stringstream sstr(value);
1500 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1508 if (getAttribute(TRACE_WRITE))
1514 SGPropertyNode::setUnspecifiedValue (const char * value)
1516 bool result = false;
1518 if (_type == props::NONE) {
1520 _type = props::UNSPECIFIED;
1522 props::Type type = _type;
1523 if (type == props::EXTENDED)
1524 type = _value.val->getType();
1527 result = _value.alias->setUnspecifiedValue(value);
1530 result = set_bool((compare_strings(value, "true")
1531 || atoi(value)) ? true : false);
1534 result = set_int(atoi(value));
1537 result = set_long(strtol(value, 0, 0));
1540 result = set_float(atof(value));
1543 result = set_double(strtod(value, 0));
1546 case props::UNSPECIFIED:
1547 result = set_string(value);
1550 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1553 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1560 if (getAttribute(TRACE_WRITE))
1565 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1567 if (!getAttribute(READ))
1571 return _value.alias->printOn(stream);
1573 stream << (get_bool() ? "true" : "false");
1576 stream << get_int();
1579 stream << get_long();
1582 stream << get_float();
1585 stream << get_double();
1588 case props::UNSPECIFIED:
1589 stream << get_string();
1591 case props::EXTENDED:
1592 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1596 default: // avoid compiler warning
1603 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1606 if (_type == props::ALIAS || _tied)
1609 useDefault = useDefault && hasValue();
1610 std::string old_val;
1612 old_val = getStringValue();
1614 _type = props::STRING;
1616 _value.val = rawValue.clone();
1619 setStringValue(old_val.c_str());
1624 SGPropertyNode::untie ()
1631 bool val = getBoolValue();
1633 _type = props::BOOL;
1634 _local_val.bool_val = val;
1638 int val = getIntValue();
1641 _local_val.int_val = val;
1645 long val = getLongValue();
1647 _type = props::LONG;
1648 _local_val.long_val = val;
1651 case props::FLOAT: {
1652 float val = getFloatValue();
1654 _type = props::FLOAT;
1655 _local_val.float_val = val;
1658 case props::DOUBLE: {
1659 double val = getDoubleValue();
1661 _type = props::DOUBLE;
1662 _local_val.double_val = val;
1666 case props::UNSPECIFIED: {
1667 string val = getStringValue();
1669 _type = props::STRING;
1670 _local_val.string_val = copy_string(val.c_str());
1673 case props::EXTENDED: {
1674 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1675 _value.val = 0; // Prevent clearValue() from deleting
1677 _type = props::EXTENDED;
1678 _value.val = val->makeContainer();
1692 SGPropertyNode::getRootNode ()
1697 return _parent->getRootNode();
1700 const SGPropertyNode *
1701 SGPropertyNode::getRootNode () const
1706 return _parent->getRootNode();
1710 SGPropertyNode::getNode (const char * relative_path, bool create)
1712 using namespace boost;
1714 return find_node(this, make_iterator_range(relative_path, relative_path
1715 + strlen(relative_path)),
1720 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1722 using namespace boost;
1723 return find_node(this, make_iterator_range(relative_path, relative_path
1724 + strlen(relative_path)),
1728 const SGPropertyNode *
1729 SGPropertyNode::getNode (const char * relative_path) const
1731 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1734 const SGPropertyNode *
1735 SGPropertyNode::getNode (const char * relative_path, int index) const
1737 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1741 ////////////////////////////////////////////////////////////////////////
1742 // Convenience methods using relative paths.
1743 ////////////////////////////////////////////////////////////////////////
1747 * Test whether another node has a value attached.
1750 SGPropertyNode::hasValue (const char * relative_path) const
1752 const SGPropertyNode * node = getNode(relative_path);
1753 return (node == 0 ? false : node->hasValue());
1758 * Get the value type for another node.
1761 SGPropertyNode::getType (const char * relative_path) const
1763 const SGPropertyNode * node = getNode(relative_path);
1764 return (node == 0 ? props::UNSPECIFIED : node->getType());
1769 * Get a bool value for another node.
1772 SGPropertyNode::getBoolValue (const char * relative_path,
1773 bool defaultValue) const
1775 const SGPropertyNode * node = getNode(relative_path);
1776 return (node == 0 ? defaultValue : node->getBoolValue());
1781 * Get an int value for another node.
1784 SGPropertyNode::getIntValue (const char * relative_path,
1785 int defaultValue) const
1787 const SGPropertyNode * node = getNode(relative_path);
1788 return (node == 0 ? defaultValue : node->getIntValue());
1793 * Get a long value for another node.
1796 SGPropertyNode::getLongValue (const char * relative_path,
1797 long defaultValue) const
1799 const SGPropertyNode * node = getNode(relative_path);
1800 return (node == 0 ? defaultValue : node->getLongValue());
1805 * Get a float value for another node.
1808 SGPropertyNode::getFloatValue (const char * relative_path,
1809 float defaultValue) const
1811 const SGPropertyNode * node = getNode(relative_path);
1812 return (node == 0 ? defaultValue : node->getFloatValue());
1817 * Get a double value for another node.
1820 SGPropertyNode::getDoubleValue (const char * relative_path,
1821 double defaultValue) const
1823 const SGPropertyNode * node = getNode(relative_path);
1824 return (node == 0 ? defaultValue : node->getDoubleValue());
1829 * Get a string value for another node.
1832 SGPropertyNode::getStringValue (const char * relative_path,
1833 const char * defaultValue) const
1835 const SGPropertyNode * node = getNode(relative_path);
1836 return (node == 0 ? defaultValue : node->getStringValue());
1841 * Set a bool value for another node.
1844 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1846 return getNode(relative_path, true)->setBoolValue(value);
1851 * Set an int value for another node.
1854 SGPropertyNode::setIntValue (const char * relative_path, int value)
1856 return getNode(relative_path, true)->setIntValue(value);
1861 * Set a long value for another node.
1864 SGPropertyNode::setLongValue (const char * relative_path, long value)
1866 return getNode(relative_path, true)->setLongValue(value);
1871 * Set a float value for another node.
1874 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1876 return getNode(relative_path, true)->setFloatValue(value);
1881 * Set a double value for another node.
1884 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1886 return getNode(relative_path, true)->setDoubleValue(value);
1891 * Set a string value for another node.
1894 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1896 return getNode(relative_path, true)->setStringValue(value);
1901 * Set an unknown value for another node.
1904 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1907 return getNode(relative_path, true)->setUnspecifiedValue(value);
1912 * Test whether another node is tied.
1915 SGPropertyNode::isTied (const char * relative_path) const
1917 const SGPropertyNode * node = getNode(relative_path);
1918 return (node == 0 ? false : node->isTied());
1923 * Tie a node reached by a relative path, creating it if necessary.
1926 SGPropertyNode::tie (const char * relative_path,
1927 const SGRawValue<bool> &rawValue,
1930 return getNode(relative_path, true)->tie(rawValue, useDefault);
1935 * Tie a node reached by a relative path, creating it if necessary.
1938 SGPropertyNode::tie (const char * relative_path,
1939 const SGRawValue<int> &rawValue,
1942 return getNode(relative_path, true)->tie(rawValue, useDefault);
1947 * Tie a node reached by a relative path, creating it if necessary.
1950 SGPropertyNode::tie (const char * relative_path,
1951 const SGRawValue<long> &rawValue,
1954 return getNode(relative_path, true)->tie(rawValue, useDefault);
1959 * Tie a node reached by a relative path, creating it if necessary.
1962 SGPropertyNode::tie (const char * relative_path,
1963 const SGRawValue<float> &rawValue,
1966 return getNode(relative_path, true)->tie(rawValue, useDefault);
1971 * Tie a node reached by a relative path, creating it if necessary.
1974 SGPropertyNode::tie (const char * relative_path,
1975 const SGRawValue<double> &rawValue,
1978 return getNode(relative_path, true)->tie(rawValue, useDefault);
1983 * Tie a node reached by a relative path, creating it if necessary.
1986 SGPropertyNode::tie (const char * relative_path,
1987 const SGRawValue<const char *> &rawValue,
1990 return getNode(relative_path, true)->tie(rawValue, useDefault);
1995 * Attempt to untie another node reached by a relative path.
1998 SGPropertyNode::untie (const char * relative_path)
2000 SGPropertyNode * node = getNode(relative_path);
2001 return (node == 0 ? false : node->untie());
2005 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2008 if (_listeners == 0)
2009 _listeners = new vector<SGPropertyChangeListener*>;
2010 _listeners->push_back(listener);
2011 listener->register_property(this);
2013 listener->valueChanged(this);
2017 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2019 if (_listeners == 0)
2021 vector<SGPropertyChangeListener*>::iterator it =
2022 find(_listeners->begin(), _listeners->end(), listener);
2023 if (it != _listeners->end()) {
2024 _listeners->erase(it);
2025 listener->unregister_property(this);
2026 if (_listeners->empty()) {
2027 vector<SGPropertyChangeListener*>* tmp = _listeners;
2035 SGPropertyNode::fireValueChanged ()
2037 fireValueChanged(this);
2041 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2043 fireChildAdded(this, child);
2047 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2049 fireChildRemoved(this, child);
2053 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2055 if (_listeners != 0) {
2056 for (unsigned int i = 0; i < _listeners->size(); i++) {
2057 (*_listeners)[i]->valueChanged(node);
2061 _parent->fireValueChanged(node);
2065 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2066 SGPropertyNode * child)
2068 if (_listeners != 0) {
2069 for (unsigned int i = 0; i < _listeners->size(); i++) {
2070 (*_listeners)[i]->childAdded(parent, child);
2074 _parent->fireChildAdded(parent, child);
2078 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2079 SGPropertyNode * child)
2081 if (_listeners != 0) {
2082 for (unsigned int i = 0; i < _listeners->size(); i++) {
2083 (*_listeners)[i]->childRemoved(parent, child);
2087 _parent->fireChildRemoved(parent, child);
2091 ////////////////////////////////////////////////////////////////////////
2092 // Implementation of SGPropertyChangeListener.
2093 ////////////////////////////////////////////////////////////////////////
2095 SGPropertyChangeListener::~SGPropertyChangeListener ()
2097 for (int i = _properties.size() - 1; i >= 0; i--)
2098 _properties[i]->removeChangeListener(this);
2102 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2108 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2109 SGPropertyNode * child)
2115 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2116 SGPropertyNode * child)
2122 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2124 _properties.push_back(node);
2128 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2130 vector<SGPropertyNode *>::iterator it =
2131 find(_properties.begin(), _properties.end(), node);
2132 if (it != _properties.end())
2133 _properties.erase(it);
2137 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2140 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2141 for (int i = 0; i < 3; ++i) {
2152 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2154 for (int i = 0; i < 3; ++i) {
2155 stream >> result[i];
2161 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2164 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2165 for (int i = 0; i < 4; ++i) {
2176 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2178 for (int i = 0; i < 4; ++i) {
2179 stream >> result[i];
2186 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2188 props::Type ltype = lhs.getType();
2189 props::Type rtype = rhs.getType();
2196 return false; // XXX Should we look in aliases?
2198 return lhs.getValue<bool>() == rhs.getValue<bool>();
2200 return lhs.getValue<int>() == rhs.getValue<int>();
2202 return lhs.getValue<long>() == rhs.getValue<long>();
2204 return lhs.getValue<float>() == rhs.getValue<float>();
2206 return lhs.getValue<double>() == rhs.getValue<double>();
2208 case props::UNSPECIFIED:
2209 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2211 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2213 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2221 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2222 const SGPropertyNode& rhs)
2226 int lhsChildren = lhs.nChildren();
2227 int rhsChildren = rhs.nChildren();
2228 if (lhsChildren != rhsChildren)
2230 if (lhsChildren == 0)
2231 return compareNodeValue(lhs, rhs);
2232 for (size_t i = 0; i < lhs._children.size(); ++i) {
2233 const SGPropertyNode* lchild = lhs._children[i];
2234 const SGPropertyNode* rchild = rhs._children[i];
2235 // I'm guessing that the nodes will usually be in the same
2237 if (lchild->getIndex() != rchild->getIndex()
2238 || lchild->getNameString() != rchild->getNameString()) {
2240 for (PropertyList::const_iterator itr = rhs._children.begin(),
2241 end = rhs._children.end();
2244 if (lchild->getIndex() == (*itr)->getIndex()
2245 && lchild->getNameString() == (*itr)->getNameString()) {
2252 if (!compare(*lchild, *rchild))
2258 struct PropertyPlaceLess {
2259 typedef bool result_type;
2260 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2262 int comp = lhs->getNameString().compare(rhs->getNameString());
2264 return lhs->getIndex() < rhs->getIndex();
2270 size_t hash_value(const SGPropertyNode& node)
2272 using namespace boost;
2274 if (node.nChildren() == 0) {
2275 switch (node.getType()) {
2280 return hash_value(node.getValue<bool>());
2282 return hash_value(node.getValue<int>());
2284 return hash_value(node.getValue<long>());
2286 return hash_value(node.getValue<float>());
2288 return hash_value(node.getValue<double>());
2290 case props::UNSPECIFIED:
2292 const char *val = node.getStringValue();
2293 return hash_range(val, val + strlen(val));
2297 const SGVec3d val = node.getValue<SGVec3d>();
2298 return hash_range(&val[0], &val[3]);
2302 const SGVec4d val = node.getValue<SGVec4d>();
2303 return hash_range(&val[0], &val[4]);
2305 case props::ALIAS: // XXX Should we look in aliases?
2311 PropertyList children(node._children.begin(), node._children.end());
2312 sort(children.begin(), children.end(), PropertyPlaceLess());
2313 for (PropertyList::const_iterator itr = children.begin(),
2314 end = children.end();
2317 hash_combine(seed, (*itr)->_name);
2318 hash_combine(seed, (*itr)->_index);
2319 hash_combine(seed, hash_value(**itr));