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 = PRESERVE;
648 * Default constructor: always creates a root node.
650 SGPropertyNode::SGPropertyNode ()
658 _local_val.string_val = 0;
666 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
667 : SGReferenced(node),
670 _parent(0), // don't copy the parent
674 _listeners(0) // CHECK!!
676 _local_val.string_val = 0;
678 if (_type == props::NONE)
680 if (_type == props::ALIAS) {
681 _value.alias = node._value.alias;
686 if (_tied || _type == props::EXTENDED) {
687 _value.val = node._value.val->clone();
692 set_bool(node.get_bool());
695 set_int(node.get_int());
698 set_long(node.get_long());
701 set_float(node.get_float());
704 set_double(node.get_double());
707 case props::UNSPECIFIED:
708 set_string(node.get_string());
717 * Convenience constructor.
719 template<typename Itr>
720 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
722 SGPropertyNode * parent)
731 _local_val.string_val = 0;
733 if (!validateName(_name))
734 throw string("plain name expected instead of '") + _name + '\'';
737 SGPropertyNode::SGPropertyNode (const string& name,
739 SGPropertyNode * parent)
748 _local_val.string_val = 0;
750 if (!validateName(name))
751 throw string("plain name expected instead of '") + _name + '\'';
757 SGPropertyNode::~SGPropertyNode ()
759 // zero out all parent pointers, else they might be dangling
760 for (unsigned i = 0; i < _children.size(); ++i)
761 _children[i]->_parent = 0;
762 for (unsigned i = 0; i < _removedChildren.size(); ++i)
763 _removedChildren[i]->_parent = 0;
767 vector<SGPropertyChangeListener*>::iterator it;
768 for (it = _listeners->begin(); it != _listeners->end(); ++it)
769 (*it)->unregister_property(this);
776 * Alias to another node.
779 SGPropertyNode::alias (SGPropertyNode * target)
781 if (target == 0 || _type == props::ALIAS || _tied)
785 _value.alias = target;
786 _type = props::ALIAS;
792 * Alias to another node by path.
795 SGPropertyNode::alias (const char * path)
797 return alias(getNode(path, true));
805 SGPropertyNode::unalias ()
807 if (_type != props::ALIAS)
815 * Get the target of an alias.
818 SGPropertyNode::getAliasTarget ()
820 return (_type == props::ALIAS ? _value.alias : 0);
824 const SGPropertyNode *
825 SGPropertyNode::getAliasTarget () const
827 return (_type == props::ALIAS ? _value.alias : 0);
831 * create a non-const child by name after the last node with the same name.
834 SGPropertyNode::addChild (const char * name)
836 int pos = find_last_child(name, _children)+1;
838 SGPropertyNode_ptr node;
839 node = new SGPropertyNode(name, name + strlen(name), pos, this);
840 _children.push_back(node);
841 fireChildAdded(node);
847 * Get a non-const child by index.
850 SGPropertyNode::getChild (int position)
852 if (position >= 0 && position < nChildren())
853 return _children[position];
860 * Get a const child by index.
862 const SGPropertyNode *
863 SGPropertyNode::getChild (int position) const
865 if (position >= 0 && position < nChildren())
866 return _children[position];
873 * Get a non-const child by name and index, creating if necessary.
877 SGPropertyNode::getChild (const char * name, int index, bool create)
879 return getChildImpl(name, name + strlen(name), index, create);
883 SGPropertyNode::getChild (const string& name, int index, bool create)
885 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
890 node = new SGPropertyNode(name, index, this);
891 _children.push_back(node);
892 fireChildAdded(node);
900 * Get a const child by name and index.
902 const SGPropertyNode *
903 SGPropertyNode::getChild (const char * name, int index) const
905 int pos = find_child(name, name + strlen(name), index, _children);
907 return _children[pos];
914 * Get all children with the same name (but different indices).
917 SGPropertyNode::getChildren (const char * name) const
919 PropertyList children;
920 int max = _children.size();
922 for (int i = 0; i < max; i++)
923 if (compare_strings(_children[i]->getName(), name))
924 children.push_back(_children[i]);
926 sort(children.begin(), children.end(), CompareIndices());
932 * Remove child by position.
935 SGPropertyNode::removeChild (int pos, bool keep)
937 SGPropertyNode_ptr node;
938 if (pos < 0 || pos >= (int)_children.size())
941 PropertyList::iterator it = _children.begin();
943 node = _children[pos];
946 _removedChildren.push_back(node);
949 node->setAttribute(REMOVED, true);
951 fireChildRemoved(node);
957 * Remove a child node
960 SGPropertyNode::removeChild (const char * name, int index, bool keep)
962 SGPropertyNode_ptr ret;
963 int pos = find_child(name, name + strlen(name), index, _children);
965 ret = removeChild(pos, keep);
971 * Remove all children with the specified name.
974 SGPropertyNode::removeChildren (const char * name, bool keep)
976 PropertyList children;
978 for (int pos = _children.size() - 1; pos >= 0; pos--)
979 if (compare_strings(_children[pos]->getName(), name))
980 children.push_back(removeChild(pos, keep));
982 sort(children.begin(), children.end(), CompareIndices());
987 SGPropertyNode::getDisplayName (bool simplify) const
989 string display_name = _name;
990 if (_index != 0 || !simplify) {
992 sstr << '[' << _index << ']';
993 display_name += sstr.str();
999 SGPropertyNode::getPath (bool simplify) const
1001 typedef std::vector<SGConstPropertyNode_ptr> PList;
1003 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1004 pathList.push_back(node);
1006 for (PList::reverse_iterator itr = pathList.rbegin(),
1007 rend = pathList.rend();
1011 result += (*itr)->getDisplayName(simplify);
1017 SGPropertyNode::getType () const
1019 if (_type == props::ALIAS)
1020 return _value.alias->getType();
1021 else if (_type == props::EXTENDED)
1022 return _value.val->getType();
1029 SGPropertyNode::getBoolValue () const
1031 // Shortcut for common case
1032 if (_attr == (READ|WRITE) && _type == props::BOOL)
1035 if (getAttribute(TRACE_READ))
1037 if (!getAttribute(READ))
1038 return SGRawValue<bool>::DefaultValue();
1041 return _value.alias->getBoolValue();
1045 return get_int() == 0 ? false : true;
1047 return get_long() == 0L ? false : true;
1049 return get_float() == 0.0 ? false : true;
1051 return get_double() == 0.0L ? false : true;
1053 case props::UNSPECIFIED:
1054 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1057 return SGRawValue<bool>::DefaultValue();
1062 SGPropertyNode::getIntValue () const
1064 // Shortcut for common case
1065 if (_attr == (READ|WRITE) && _type == props::INT)
1068 if (getAttribute(TRACE_READ))
1070 if (!getAttribute(READ))
1071 return SGRawValue<int>::DefaultValue();
1074 return _value.alias->getIntValue();
1076 return int(get_bool());
1080 return int(get_long());
1082 return int(get_float());
1084 return int(get_double());
1086 case props::UNSPECIFIED:
1087 return atoi(get_string());
1090 return SGRawValue<int>::DefaultValue();
1095 SGPropertyNode::getLongValue () const
1097 // Shortcut for common case
1098 if (_attr == (READ|WRITE) && _type == props::LONG)
1101 if (getAttribute(TRACE_READ))
1103 if (!getAttribute(READ))
1104 return SGRawValue<long>::DefaultValue();
1107 return _value.alias->getLongValue();
1109 return long(get_bool());
1111 return long(get_int());
1115 return long(get_float());
1117 return long(get_double());
1119 case props::UNSPECIFIED:
1120 return strtol(get_string(), 0, 0);
1123 return SGRawValue<long>::DefaultValue();
1128 SGPropertyNode::getFloatValue () const
1130 // Shortcut for common case
1131 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1134 if (getAttribute(TRACE_READ))
1136 if (!getAttribute(READ))
1137 return SGRawValue<float>::DefaultValue();
1140 return _value.alias->getFloatValue();
1142 return float(get_bool());
1144 return float(get_int());
1146 return float(get_long());
1150 return float(get_double());
1152 case props::UNSPECIFIED:
1153 return atof(get_string());
1156 return SGRawValue<float>::DefaultValue();
1161 SGPropertyNode::getDoubleValue () const
1163 // Shortcut for common case
1164 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1165 return get_double();
1167 if (getAttribute(TRACE_READ))
1169 if (!getAttribute(READ))
1170 return SGRawValue<double>::DefaultValue();
1174 return _value.alias->getDoubleValue();
1176 return double(get_bool());
1178 return double(get_int());
1180 return double(get_long());
1182 return double(get_float());
1184 return get_double();
1186 case props::UNSPECIFIED:
1187 return strtod(get_string(), 0);
1190 return SGRawValue<double>::DefaultValue();
1195 SGPropertyNode::getStringValue () const
1197 // Shortcut for common case
1198 if (_attr == (READ|WRITE) && _type == props::STRING)
1199 return get_string();
1201 if (getAttribute(TRACE_READ))
1203 if (!getAttribute(READ))
1204 return SGRawValue<const char *>::DefaultValue();
1205 return make_string();
1209 SGPropertyNode::setBoolValue (bool value)
1211 // Shortcut for common case
1212 if (_attr == (READ|WRITE) && _type == props::BOOL)
1213 return set_bool(value);
1215 bool result = false;
1217 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1220 _type = props::BOOL;
1225 result = _value.alias->setBoolValue(value);
1228 result = set_bool(value);
1231 result = set_int(int(value));
1234 result = set_long(long(value));
1237 result = set_float(float(value));
1240 result = set_double(double(value));
1243 case props::UNSPECIFIED:
1244 result = set_string(value ? "true" : "false");
1251 if (getAttribute(TRACE_WRITE))
1257 SGPropertyNode::setIntValue (int value)
1259 // Shortcut for common case
1260 if (_attr == (READ|WRITE) && _type == props::INT)
1261 return set_int(value);
1263 bool result = false;
1265 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1268 _local_val.int_val = 0;
1273 result = _value.alias->setIntValue(value);
1276 result = set_bool(value == 0 ? false : true);
1279 result = set_int(value);
1282 result = set_long(long(value));
1285 result = set_float(float(value));
1288 result = set_double(double(value));
1291 case props::UNSPECIFIED: {
1293 sprintf(buf, "%d", value);
1294 result = set_string(buf);
1302 if (getAttribute(TRACE_WRITE))
1308 SGPropertyNode::setLongValue (long value)
1310 // Shortcut for common case
1311 if (_attr == (READ|WRITE) && _type == props::LONG)
1312 return set_long(value);
1314 bool result = false;
1316 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1318 _type = props::LONG;
1319 _local_val.long_val = 0L;
1324 result = _value.alias->setLongValue(value);
1327 result = set_bool(value == 0L ? false : true);
1330 result = set_int(int(value));
1333 result = set_long(value);
1336 result = set_float(float(value));
1339 result = set_double(double(value));
1342 case props::UNSPECIFIED: {
1344 sprintf(buf, "%ld", value);
1345 result = set_string(buf);
1353 if (getAttribute(TRACE_WRITE))
1359 SGPropertyNode::setFloatValue (float value)
1361 // Shortcut for common case
1362 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1363 return set_float(value);
1365 bool result = false;
1367 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1369 _type = props::FLOAT;
1370 _local_val.float_val = 0;
1375 result = _value.alias->setFloatValue(value);
1378 result = set_bool(value == 0.0 ? false : true);
1381 result = set_int(int(value));
1384 result = set_long(long(value));
1387 result = set_float(value);
1390 result = set_double(double(value));
1393 case props::UNSPECIFIED: {
1395 sprintf(buf, "%f", value);
1396 result = set_string(buf);
1404 if (getAttribute(TRACE_WRITE))
1410 SGPropertyNode::setDoubleValue (double value)
1412 // Shortcut for common case
1413 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1414 return set_double(value);
1416 bool result = false;
1418 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1420 _local_val.double_val = value;
1421 _type = props::DOUBLE;
1426 result = _value.alias->setDoubleValue(value);
1429 result = set_bool(value == 0.0L ? false : true);
1432 result = set_int(int(value));
1435 result = set_long(long(value));
1438 result = set_float(float(value));
1441 result = set_double(value);
1444 case props::UNSPECIFIED: {
1446 sprintf(buf, "%f", value);
1447 result = set_string(buf);
1455 if (getAttribute(TRACE_WRITE))
1461 SGPropertyNode::setStringValue (const char * value)
1463 // Shortcut for common case
1464 if (_attr == (READ|WRITE) && _type == props::STRING)
1465 return set_string(value);
1467 bool result = false;
1469 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1471 _type = props::STRING;
1476 result = _value.alias->setStringValue(value);
1479 result = set_bool((compare_strings(value, "true")
1480 || atoi(value)) ? true : false);
1483 result = set_int(atoi(value));
1486 result = set_long(strtol(value, 0, 0));
1489 result = set_float(atof(value));
1492 result = set_double(strtod(value, 0));
1495 case props::UNSPECIFIED:
1496 result = set_string(value);
1498 case props::EXTENDED:
1500 stringstream sstr(value);
1501 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1509 if (getAttribute(TRACE_WRITE))
1515 SGPropertyNode::setUnspecifiedValue (const char * value)
1517 bool result = false;
1519 if (_type == props::NONE) {
1521 _type = props::UNSPECIFIED;
1523 props::Type type = _type;
1524 if (type == props::EXTENDED)
1525 type = _value.val->getType();
1528 result = _value.alias->setUnspecifiedValue(value);
1531 result = set_bool((compare_strings(value, "true")
1532 || atoi(value)) ? true : false);
1535 result = set_int(atoi(value));
1538 result = set_long(strtol(value, 0, 0));
1541 result = set_float(atof(value));
1544 result = set_double(strtod(value, 0));
1547 case props::UNSPECIFIED:
1548 result = set_string(value);
1551 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1554 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1561 if (getAttribute(TRACE_WRITE))
1566 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1568 if (!getAttribute(READ))
1572 return _value.alias->printOn(stream);
1574 stream << (get_bool() ? "true" : "false");
1577 stream << get_int();
1580 stream << get_long();
1583 stream << get_float();
1586 stream << get_double();
1589 case props::UNSPECIFIED:
1590 stream << get_string();
1592 case props::EXTENDED:
1593 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1597 default: // avoid compiler warning
1604 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1607 if (_type == props::ALIAS || _tied)
1610 useDefault = useDefault && hasValue();
1611 std::string old_val;
1613 old_val = getStringValue();
1615 _type = props::STRING;
1617 _value.val = rawValue.clone();
1620 int save_attributes = getAttributes();
1621 setAttribute( WRITE, true );
1622 setStringValue(old_val.c_str());
1623 setAttributes( save_attributes );
1629 SGPropertyNode::untie ()
1636 bool val = getBoolValue();
1638 _type = props::BOOL;
1639 _local_val.bool_val = val;
1643 int val = getIntValue();
1646 _local_val.int_val = val;
1650 long val = getLongValue();
1652 _type = props::LONG;
1653 _local_val.long_val = val;
1656 case props::FLOAT: {
1657 float val = getFloatValue();
1659 _type = props::FLOAT;
1660 _local_val.float_val = val;
1663 case props::DOUBLE: {
1664 double val = getDoubleValue();
1666 _type = props::DOUBLE;
1667 _local_val.double_val = val;
1671 case props::UNSPECIFIED: {
1672 string val = getStringValue();
1674 _type = props::STRING;
1675 _local_val.string_val = copy_string(val.c_str());
1678 case props::EXTENDED: {
1679 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1680 _value.val = 0; // Prevent clearValue() from deleting
1682 _type = props::EXTENDED;
1683 _value.val = val->makeContainer();
1697 SGPropertyNode::getRootNode ()
1702 return _parent->getRootNode();
1705 const SGPropertyNode *
1706 SGPropertyNode::getRootNode () const
1711 return _parent->getRootNode();
1715 SGPropertyNode::getNode (const char * relative_path, bool create)
1717 using namespace boost;
1719 return find_node(this, make_iterator_range(relative_path, relative_path
1720 + strlen(relative_path)),
1725 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1727 using namespace boost;
1728 return find_node(this, make_iterator_range(relative_path, relative_path
1729 + strlen(relative_path)),
1733 const SGPropertyNode *
1734 SGPropertyNode::getNode (const char * relative_path) const
1736 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1739 const SGPropertyNode *
1740 SGPropertyNode::getNode (const char * relative_path, int index) const
1742 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1746 ////////////////////////////////////////////////////////////////////////
1747 // Convenience methods using relative paths.
1748 ////////////////////////////////////////////////////////////////////////
1752 * Test whether another node has a value attached.
1755 SGPropertyNode::hasValue (const char * relative_path) const
1757 const SGPropertyNode * node = getNode(relative_path);
1758 return (node == 0 ? false : node->hasValue());
1763 * Get the value type for another node.
1766 SGPropertyNode::getType (const char * relative_path) const
1768 const SGPropertyNode * node = getNode(relative_path);
1769 return (node == 0 ? props::UNSPECIFIED : node->getType());
1774 * Get a bool value for another node.
1777 SGPropertyNode::getBoolValue (const char * relative_path,
1778 bool defaultValue) const
1780 const SGPropertyNode * node = getNode(relative_path);
1781 return (node == 0 ? defaultValue : node->getBoolValue());
1786 * Get an int value for another node.
1789 SGPropertyNode::getIntValue (const char * relative_path,
1790 int defaultValue) const
1792 const SGPropertyNode * node = getNode(relative_path);
1793 return (node == 0 ? defaultValue : node->getIntValue());
1798 * Get a long value for another node.
1801 SGPropertyNode::getLongValue (const char * relative_path,
1802 long defaultValue) const
1804 const SGPropertyNode * node = getNode(relative_path);
1805 return (node == 0 ? defaultValue : node->getLongValue());
1810 * Get a float value for another node.
1813 SGPropertyNode::getFloatValue (const char * relative_path,
1814 float defaultValue) const
1816 const SGPropertyNode * node = getNode(relative_path);
1817 return (node == 0 ? defaultValue : node->getFloatValue());
1822 * Get a double value for another node.
1825 SGPropertyNode::getDoubleValue (const char * relative_path,
1826 double defaultValue) const
1828 const SGPropertyNode * node = getNode(relative_path);
1829 return (node == 0 ? defaultValue : node->getDoubleValue());
1834 * Get a string value for another node.
1837 SGPropertyNode::getStringValue (const char * relative_path,
1838 const char * defaultValue) const
1840 const SGPropertyNode * node = getNode(relative_path);
1841 return (node == 0 ? defaultValue : node->getStringValue());
1846 * Set a bool value for another node.
1849 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1851 return getNode(relative_path, true)->setBoolValue(value);
1856 * Set an int value for another node.
1859 SGPropertyNode::setIntValue (const char * relative_path, int value)
1861 return getNode(relative_path, true)->setIntValue(value);
1866 * Set a long value for another node.
1869 SGPropertyNode::setLongValue (const char * relative_path, long value)
1871 return getNode(relative_path, true)->setLongValue(value);
1876 * Set a float value for another node.
1879 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1881 return getNode(relative_path, true)->setFloatValue(value);
1886 * Set a double value for another node.
1889 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1891 return getNode(relative_path, true)->setDoubleValue(value);
1896 * Set a string value for another node.
1899 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1901 return getNode(relative_path, true)->setStringValue(value);
1906 * Set an unknown value for another node.
1909 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1912 return getNode(relative_path, true)->setUnspecifiedValue(value);
1917 * Test whether another node is tied.
1920 SGPropertyNode::isTied (const char * relative_path) const
1922 const SGPropertyNode * node = getNode(relative_path);
1923 return (node == 0 ? false : node->isTied());
1928 * Tie a node reached by a relative path, creating it if necessary.
1931 SGPropertyNode::tie (const char * relative_path,
1932 const SGRawValue<bool> &rawValue,
1935 return getNode(relative_path, true)->tie(rawValue, useDefault);
1940 * Tie a node reached by a relative path, creating it if necessary.
1943 SGPropertyNode::tie (const char * relative_path,
1944 const SGRawValue<int> &rawValue,
1947 return getNode(relative_path, true)->tie(rawValue, useDefault);
1952 * Tie a node reached by a relative path, creating it if necessary.
1955 SGPropertyNode::tie (const char * relative_path,
1956 const SGRawValue<long> &rawValue,
1959 return getNode(relative_path, true)->tie(rawValue, useDefault);
1964 * Tie a node reached by a relative path, creating it if necessary.
1967 SGPropertyNode::tie (const char * relative_path,
1968 const SGRawValue<float> &rawValue,
1971 return getNode(relative_path, true)->tie(rawValue, useDefault);
1976 * Tie a node reached by a relative path, creating it if necessary.
1979 SGPropertyNode::tie (const char * relative_path,
1980 const SGRawValue<double> &rawValue,
1983 return getNode(relative_path, true)->tie(rawValue, useDefault);
1988 * Tie a node reached by a relative path, creating it if necessary.
1991 SGPropertyNode::tie (const char * relative_path,
1992 const SGRawValue<const char *> &rawValue,
1995 return getNode(relative_path, true)->tie(rawValue, useDefault);
2000 * Attempt to untie another node reached by a relative path.
2003 SGPropertyNode::untie (const char * relative_path)
2005 SGPropertyNode * node = getNode(relative_path);
2006 return (node == 0 ? false : node->untie());
2010 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2013 if (_listeners == 0)
2014 _listeners = new vector<SGPropertyChangeListener*>;
2015 _listeners->push_back(listener);
2016 listener->register_property(this);
2018 listener->valueChanged(this);
2022 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2024 if (_listeners == 0)
2026 vector<SGPropertyChangeListener*>::iterator it =
2027 find(_listeners->begin(), _listeners->end(), listener);
2028 if (it != _listeners->end()) {
2029 _listeners->erase(it);
2030 listener->unregister_property(this);
2031 if (_listeners->empty()) {
2032 vector<SGPropertyChangeListener*>* tmp = _listeners;
2040 SGPropertyNode::fireValueChanged ()
2042 fireValueChanged(this);
2046 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2048 fireChildAdded(this, child);
2052 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2054 fireChildRemoved(this, child);
2058 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2060 if (_listeners != 0) {
2061 for (unsigned int i = 0; i < _listeners->size(); i++) {
2062 (*_listeners)[i]->valueChanged(node);
2066 _parent->fireValueChanged(node);
2070 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2071 SGPropertyNode * child)
2073 if (_listeners != 0) {
2074 for (unsigned int i = 0; i < _listeners->size(); i++) {
2075 (*_listeners)[i]->childAdded(parent, child);
2079 _parent->fireChildAdded(parent, child);
2083 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2084 SGPropertyNode * child)
2086 if (_listeners != 0) {
2087 for (unsigned int i = 0; i < _listeners->size(); i++) {
2088 (*_listeners)[i]->childRemoved(parent, child);
2092 _parent->fireChildRemoved(parent, child);
2096 ////////////////////////////////////////////////////////////////////////
2097 // Implementation of SGPropertyChangeListener.
2098 ////////////////////////////////////////////////////////////////////////
2100 SGPropertyChangeListener::~SGPropertyChangeListener ()
2102 for (int i = _properties.size() - 1; i >= 0; i--)
2103 _properties[i]->removeChangeListener(this);
2107 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2113 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2114 SGPropertyNode * child)
2120 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2121 SGPropertyNode * child)
2127 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2129 _properties.push_back(node);
2133 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2135 vector<SGPropertyNode *>::iterator it =
2136 find(_properties.begin(), _properties.end(), node);
2137 if (it != _properties.end())
2138 _properties.erase(it);
2142 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2145 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2146 for (int i = 0; i < 3; ++i) {
2157 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2159 for (int i = 0; i < 3; ++i) {
2160 stream >> result[i];
2166 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2169 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2170 for (int i = 0; i < 4; ++i) {
2181 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2183 for (int i = 0; i < 4; ++i) {
2184 stream >> result[i];
2191 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2193 props::Type ltype = lhs.getType();
2194 props::Type rtype = rhs.getType();
2201 return false; // XXX Should we look in aliases?
2203 return lhs.getValue<bool>() == rhs.getValue<bool>();
2205 return lhs.getValue<int>() == rhs.getValue<int>();
2207 return lhs.getValue<long>() == rhs.getValue<long>();
2209 return lhs.getValue<float>() == rhs.getValue<float>();
2211 return lhs.getValue<double>() == rhs.getValue<double>();
2213 case props::UNSPECIFIED:
2214 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2216 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2218 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2226 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2227 const SGPropertyNode& rhs)
2231 int lhsChildren = lhs.nChildren();
2232 int rhsChildren = rhs.nChildren();
2233 if (lhsChildren != rhsChildren)
2235 if (lhsChildren == 0)
2236 return compareNodeValue(lhs, rhs);
2237 for (size_t i = 0; i < lhs._children.size(); ++i) {
2238 const SGPropertyNode* lchild = lhs._children[i];
2239 const SGPropertyNode* rchild = rhs._children[i];
2240 // I'm guessing that the nodes will usually be in the same
2242 if (lchild->getIndex() != rchild->getIndex()
2243 || lchild->getNameString() != rchild->getNameString()) {
2245 for (PropertyList::const_iterator itr = rhs._children.begin(),
2246 end = rhs._children.end();
2249 if (lchild->getIndex() == (*itr)->getIndex()
2250 && lchild->getNameString() == (*itr)->getNameString()) {
2257 if (!compare(*lchild, *rchild))
2263 struct PropertyPlaceLess {
2264 typedef bool result_type;
2265 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2267 int comp = lhs->getNameString().compare(rhs->getNameString());
2269 return lhs->getIndex() < rhs->getIndex();
2275 size_t hash_value(const SGPropertyNode& node)
2277 using namespace boost;
2279 if (node.nChildren() == 0) {
2280 switch (node.getType()) {
2285 return hash_value(node.getValue<bool>());
2287 return hash_value(node.getValue<int>());
2289 return hash_value(node.getValue<long>());
2291 return hash_value(node.getValue<float>());
2293 return hash_value(node.getValue<double>());
2295 case props::UNSPECIFIED:
2297 const char *val = node.getStringValue();
2298 return hash_range(val, val + strlen(val));
2302 const SGVec3d val = node.getValue<SGVec3d>();
2303 return hash_range(&val[0], &val[3]);
2307 const SGVec4d val = node.getValue<SGVec4d>();
2308 return hash_range(&val[0], &val[4]);
2310 case props::ALIAS: // XXX Should we look in aliases?
2316 PropertyList children(node._children.begin(), node._children.end());
2317 sort(children.begin(), children.end(), PropertyPlaceLess());
2318 for (PropertyList::const_iterator itr = children.begin(),
2319 end = children.end();
2322 hash_combine(seed, (*itr)->_name);
2323 hash_combine(seed, (*itr)->_index);
2324 hash_combine(seed, hash_value(**itr));