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 && (_type != props::ALIAS) && (!_tied))
785 _value.alias = target;
786 _type = props::ALIAS;
794 SG_LOG(SG_GENERAL, SG_ALERT,
795 "Failed to create alias for " << getPath() << ". "
796 "The target property does not exist.");
799 if (_type == props::ALIAS)
801 SG_LOG(SG_GENERAL, SG_ALERT,
802 "Failed to create alias at " << target->getPath() << ". "
803 "Source "<< getPath() << " is also an alias. Unsupported recursion.");
808 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
809 "Source " << getPath() << " is a tied property.");
818 * Alias to another node by path.
821 SGPropertyNode::alias (const char * path)
823 return alias(getNode(path, true));
831 SGPropertyNode::unalias ()
833 if (_type != props::ALIAS)
841 * Get the target of an alias.
844 SGPropertyNode::getAliasTarget ()
846 return (_type == props::ALIAS ? _value.alias : 0);
850 const SGPropertyNode *
851 SGPropertyNode::getAliasTarget () const
853 return (_type == props::ALIAS ? _value.alias : 0);
857 * create a non-const child by name after the last node with the same name.
860 SGPropertyNode::addChild (const char * name)
862 int pos = find_last_child(name, _children)+1;
864 SGPropertyNode_ptr node;
865 node = new SGPropertyNode(name, name + strlen(name), pos, this);
866 _children.push_back(node);
867 fireChildAdded(node);
873 * Get a non-const child by index.
876 SGPropertyNode::getChild (int position)
878 if (position >= 0 && position < nChildren())
879 return _children[position];
886 * Get a const child by index.
888 const SGPropertyNode *
889 SGPropertyNode::getChild (int position) const
891 if (position >= 0 && position < nChildren())
892 return _children[position];
899 * Get a non-const child by name and index, creating if necessary.
903 SGPropertyNode::getChild (const char * name, int index, bool create)
905 return getChildImpl(name, name + strlen(name), index, create);
909 SGPropertyNode::getChild (const string& name, int index, bool create)
911 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
916 node = new SGPropertyNode(name, index, this);
917 _children.push_back(node);
918 fireChildAdded(node);
926 * Get a const child by name and index.
928 const SGPropertyNode *
929 SGPropertyNode::getChild (const char * name, int index) const
931 int pos = find_child(name, name + strlen(name), index, _children);
933 return _children[pos];
940 * Get all children with the same name (but different indices).
943 SGPropertyNode::getChildren (const char * name) const
945 PropertyList children;
946 int max = _children.size();
948 for (int i = 0; i < max; i++)
949 if (compare_strings(_children[i]->getName(), name))
950 children.push_back(_children[i]);
952 sort(children.begin(), children.end(), CompareIndices());
958 * Remove child by position.
961 SGPropertyNode::removeChild (int pos, bool keep)
963 SGPropertyNode_ptr node;
964 if (pos < 0 || pos >= (int)_children.size())
967 PropertyList::iterator it = _children.begin();
969 node = _children[pos];
972 _removedChildren.push_back(node);
975 node->setAttribute(REMOVED, true);
977 fireChildRemoved(node);
983 * Remove a child node
986 SGPropertyNode::removeChild (const char * name, int index, bool keep)
988 SGPropertyNode_ptr ret;
989 int pos = find_child(name, name + strlen(name), index, _children);
991 ret = removeChild(pos, keep);
997 * Remove all children with the specified name.
1000 SGPropertyNode::removeChildren (const char * name, bool keep)
1002 PropertyList children;
1004 for (int pos = _children.size() - 1; pos >= 0; pos--)
1005 if (compare_strings(_children[pos]->getName(), name))
1006 children.push_back(removeChild(pos, keep));
1008 sort(children.begin(), children.end(), CompareIndices());
1013 SGPropertyNode::getDisplayName (bool simplify) const
1015 string display_name = _name;
1016 if (_index != 0 || !simplify) {
1018 sstr << '[' << _index << ']';
1019 display_name += sstr.str();
1021 return display_name;
1025 SGPropertyNode::getPath (bool simplify) const
1027 typedef std::vector<SGConstPropertyNode_ptr> PList;
1029 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1030 pathList.push_back(node);
1032 for (PList::reverse_iterator itr = pathList.rbegin(),
1033 rend = pathList.rend();
1037 result += (*itr)->getDisplayName(simplify);
1043 SGPropertyNode::getType () const
1045 if (_type == props::ALIAS)
1046 return _value.alias->getType();
1047 else if (_type == props::EXTENDED)
1048 return _value.val->getType();
1055 SGPropertyNode::getBoolValue () const
1057 // Shortcut for common case
1058 if (_attr == (READ|WRITE) && _type == props::BOOL)
1061 if (getAttribute(TRACE_READ))
1063 if (!getAttribute(READ))
1064 return SGRawValue<bool>::DefaultValue();
1067 return _value.alias->getBoolValue();
1071 return get_int() == 0 ? false : true;
1073 return get_long() == 0L ? false : true;
1075 return get_float() == 0.0 ? false : true;
1077 return get_double() == 0.0L ? false : true;
1079 case props::UNSPECIFIED:
1080 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1083 return SGRawValue<bool>::DefaultValue();
1088 SGPropertyNode::getIntValue () const
1090 // Shortcut for common case
1091 if (_attr == (READ|WRITE) && _type == props::INT)
1094 if (getAttribute(TRACE_READ))
1096 if (!getAttribute(READ))
1097 return SGRawValue<int>::DefaultValue();
1100 return _value.alias->getIntValue();
1102 return int(get_bool());
1106 return int(get_long());
1108 return int(get_float());
1110 return int(get_double());
1112 case props::UNSPECIFIED:
1113 return atoi(get_string());
1116 return SGRawValue<int>::DefaultValue();
1121 SGPropertyNode::getLongValue () const
1123 // Shortcut for common case
1124 if (_attr == (READ|WRITE) && _type == props::LONG)
1127 if (getAttribute(TRACE_READ))
1129 if (!getAttribute(READ))
1130 return SGRawValue<long>::DefaultValue();
1133 return _value.alias->getLongValue();
1135 return long(get_bool());
1137 return long(get_int());
1141 return long(get_float());
1143 return long(get_double());
1145 case props::UNSPECIFIED:
1146 return strtol(get_string(), 0, 0);
1149 return SGRawValue<long>::DefaultValue();
1154 SGPropertyNode::getFloatValue () const
1156 // Shortcut for common case
1157 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1160 if (getAttribute(TRACE_READ))
1162 if (!getAttribute(READ))
1163 return SGRawValue<float>::DefaultValue();
1166 return _value.alias->getFloatValue();
1168 return float(get_bool());
1170 return float(get_int());
1172 return float(get_long());
1176 return float(get_double());
1178 case props::UNSPECIFIED:
1179 return atof(get_string());
1182 return SGRawValue<float>::DefaultValue();
1187 SGPropertyNode::getDoubleValue () const
1189 // Shortcut for common case
1190 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1191 return get_double();
1193 if (getAttribute(TRACE_READ))
1195 if (!getAttribute(READ))
1196 return SGRawValue<double>::DefaultValue();
1200 return _value.alias->getDoubleValue();
1202 return double(get_bool());
1204 return double(get_int());
1206 return double(get_long());
1208 return double(get_float());
1210 return get_double();
1212 case props::UNSPECIFIED:
1213 return strtod(get_string(), 0);
1216 return SGRawValue<double>::DefaultValue();
1221 SGPropertyNode::getStringValue () const
1223 // Shortcut for common case
1224 if (_attr == (READ|WRITE) && _type == props::STRING)
1225 return get_string();
1227 if (getAttribute(TRACE_READ))
1229 if (!getAttribute(READ))
1230 return SGRawValue<const char *>::DefaultValue();
1231 return make_string();
1235 SGPropertyNode::setBoolValue (bool value)
1237 // Shortcut for common case
1238 if (_attr == (READ|WRITE) && _type == props::BOOL)
1239 return set_bool(value);
1241 bool result = false;
1243 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1246 _type = props::BOOL;
1251 result = _value.alias->setBoolValue(value);
1254 result = set_bool(value);
1257 result = set_int(int(value));
1260 result = set_long(long(value));
1263 result = set_float(float(value));
1266 result = set_double(double(value));
1269 case props::UNSPECIFIED:
1270 result = set_string(value ? "true" : "false");
1277 if (getAttribute(TRACE_WRITE))
1283 SGPropertyNode::setIntValue (int value)
1285 // Shortcut for common case
1286 if (_attr == (READ|WRITE) && _type == props::INT)
1287 return set_int(value);
1289 bool result = false;
1291 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1294 _local_val.int_val = 0;
1299 result = _value.alias->setIntValue(value);
1302 result = set_bool(value == 0 ? false : true);
1305 result = set_int(value);
1308 result = set_long(long(value));
1311 result = set_float(float(value));
1314 result = set_double(double(value));
1317 case props::UNSPECIFIED: {
1319 sprintf(buf, "%d", value);
1320 result = set_string(buf);
1328 if (getAttribute(TRACE_WRITE))
1334 SGPropertyNode::setLongValue (long value)
1336 // Shortcut for common case
1337 if (_attr == (READ|WRITE) && _type == props::LONG)
1338 return set_long(value);
1340 bool result = false;
1342 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1344 _type = props::LONG;
1345 _local_val.long_val = 0L;
1350 result = _value.alias->setLongValue(value);
1353 result = set_bool(value == 0L ? false : true);
1356 result = set_int(int(value));
1359 result = set_long(value);
1362 result = set_float(float(value));
1365 result = set_double(double(value));
1368 case props::UNSPECIFIED: {
1370 sprintf(buf, "%ld", value);
1371 result = set_string(buf);
1379 if (getAttribute(TRACE_WRITE))
1385 SGPropertyNode::setFloatValue (float value)
1387 // Shortcut for common case
1388 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1389 return set_float(value);
1391 bool result = false;
1393 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1395 _type = props::FLOAT;
1396 _local_val.float_val = 0;
1401 result = _value.alias->setFloatValue(value);
1404 result = set_bool(value == 0.0 ? false : true);
1407 result = set_int(int(value));
1410 result = set_long(long(value));
1413 result = set_float(value);
1416 result = set_double(double(value));
1419 case props::UNSPECIFIED: {
1421 sprintf(buf, "%f", value);
1422 result = set_string(buf);
1430 if (getAttribute(TRACE_WRITE))
1436 SGPropertyNode::setDoubleValue (double value)
1438 // Shortcut for common case
1439 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1440 return set_double(value);
1442 bool result = false;
1444 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1446 _local_val.double_val = value;
1447 _type = props::DOUBLE;
1452 result = _value.alias->setDoubleValue(value);
1455 result = set_bool(value == 0.0L ? false : true);
1458 result = set_int(int(value));
1461 result = set_long(long(value));
1464 result = set_float(float(value));
1467 result = set_double(value);
1470 case props::UNSPECIFIED: {
1472 sprintf(buf, "%f", value);
1473 result = set_string(buf);
1481 if (getAttribute(TRACE_WRITE))
1487 SGPropertyNode::setStringValue (const char * value)
1489 // Shortcut for common case
1490 if (_attr == (READ|WRITE) && _type == props::STRING)
1491 return set_string(value);
1493 bool result = false;
1495 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1497 _type = props::STRING;
1502 result = _value.alias->setStringValue(value);
1505 result = set_bool((compare_strings(value, "true")
1506 || atoi(value)) ? true : false);
1509 result = set_int(atoi(value));
1512 result = set_long(strtol(value, 0, 0));
1515 result = set_float(atof(value));
1518 result = set_double(strtod(value, 0));
1521 case props::UNSPECIFIED:
1522 result = set_string(value);
1524 case props::EXTENDED:
1526 stringstream sstr(value);
1527 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1535 if (getAttribute(TRACE_WRITE))
1541 SGPropertyNode::setUnspecifiedValue (const char * value)
1543 bool result = false;
1545 if (_type == props::NONE) {
1547 _type = props::UNSPECIFIED;
1549 props::Type type = _type;
1550 if (type == props::EXTENDED)
1551 type = _value.val->getType();
1554 result = _value.alias->setUnspecifiedValue(value);
1557 result = set_bool((compare_strings(value, "true")
1558 || atoi(value)) ? true : false);
1561 result = set_int(atoi(value));
1564 result = set_long(strtol(value, 0, 0));
1567 result = set_float(atof(value));
1570 result = set_double(strtod(value, 0));
1573 case props::UNSPECIFIED:
1574 result = set_string(value);
1577 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1580 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1587 if (getAttribute(TRACE_WRITE))
1592 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1594 if (!getAttribute(READ))
1598 return _value.alias->printOn(stream);
1600 stream << (get_bool() ? "true" : "false");
1603 stream << get_int();
1606 stream << get_long();
1609 stream << get_float();
1612 stream << get_double();
1615 case props::UNSPECIFIED:
1616 stream << get_string();
1618 case props::EXTENDED:
1619 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1623 default: // avoid compiler warning
1630 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1633 if (_type == props::ALIAS || _tied)
1636 useDefault = useDefault && hasValue();
1637 std::string old_val;
1639 old_val = getStringValue();
1641 _type = props::STRING;
1643 _value.val = rawValue.clone();
1646 int save_attributes = getAttributes();
1647 setAttribute( WRITE, true );
1648 setStringValue(old_val.c_str());
1649 setAttributes( save_attributes );
1655 SGPropertyNode::untie ()
1662 bool val = getBoolValue();
1664 _type = props::BOOL;
1665 _local_val.bool_val = val;
1669 int val = getIntValue();
1672 _local_val.int_val = val;
1676 long val = getLongValue();
1678 _type = props::LONG;
1679 _local_val.long_val = val;
1682 case props::FLOAT: {
1683 float val = getFloatValue();
1685 _type = props::FLOAT;
1686 _local_val.float_val = val;
1689 case props::DOUBLE: {
1690 double val = getDoubleValue();
1692 _type = props::DOUBLE;
1693 _local_val.double_val = val;
1697 case props::UNSPECIFIED: {
1698 string val = getStringValue();
1700 _type = props::STRING;
1701 _local_val.string_val = copy_string(val.c_str());
1704 case props::EXTENDED: {
1705 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1706 _value.val = 0; // Prevent clearValue() from deleting
1708 _type = props::EXTENDED;
1709 _value.val = val->makeContainer();
1723 SGPropertyNode::getRootNode ()
1728 return _parent->getRootNode();
1731 const SGPropertyNode *
1732 SGPropertyNode::getRootNode () const
1737 return _parent->getRootNode();
1741 SGPropertyNode::getNode (const char * relative_path, bool create)
1743 using namespace boost;
1745 return find_node(this, make_iterator_range(relative_path, relative_path
1746 + strlen(relative_path)),
1751 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1753 using namespace boost;
1754 return find_node(this, make_iterator_range(relative_path, relative_path
1755 + strlen(relative_path)),
1759 const SGPropertyNode *
1760 SGPropertyNode::getNode (const char * relative_path) const
1762 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1765 const SGPropertyNode *
1766 SGPropertyNode::getNode (const char * relative_path, int index) const
1768 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1772 ////////////////////////////////////////////////////////////////////////
1773 // Convenience methods using relative paths.
1774 ////////////////////////////////////////////////////////////////////////
1778 * Test whether another node has a value attached.
1781 SGPropertyNode::hasValue (const char * relative_path) const
1783 const SGPropertyNode * node = getNode(relative_path);
1784 return (node == 0 ? false : node->hasValue());
1789 * Get the value type for another node.
1792 SGPropertyNode::getType (const char * relative_path) const
1794 const SGPropertyNode * node = getNode(relative_path);
1795 return (node == 0 ? props::UNSPECIFIED : node->getType());
1800 * Get a bool value for another node.
1803 SGPropertyNode::getBoolValue (const char * relative_path,
1804 bool defaultValue) const
1806 const SGPropertyNode * node = getNode(relative_path);
1807 return (node == 0 ? defaultValue : node->getBoolValue());
1812 * Get an int value for another node.
1815 SGPropertyNode::getIntValue (const char * relative_path,
1816 int defaultValue) const
1818 const SGPropertyNode * node = getNode(relative_path);
1819 return (node == 0 ? defaultValue : node->getIntValue());
1824 * Get a long value for another node.
1827 SGPropertyNode::getLongValue (const char * relative_path,
1828 long defaultValue) const
1830 const SGPropertyNode * node = getNode(relative_path);
1831 return (node == 0 ? defaultValue : node->getLongValue());
1836 * Get a float value for another node.
1839 SGPropertyNode::getFloatValue (const char * relative_path,
1840 float defaultValue) const
1842 const SGPropertyNode * node = getNode(relative_path);
1843 return (node == 0 ? defaultValue : node->getFloatValue());
1848 * Get a double value for another node.
1851 SGPropertyNode::getDoubleValue (const char * relative_path,
1852 double defaultValue) const
1854 const SGPropertyNode * node = getNode(relative_path);
1855 return (node == 0 ? defaultValue : node->getDoubleValue());
1860 * Get a string value for another node.
1863 SGPropertyNode::getStringValue (const char * relative_path,
1864 const char * defaultValue) const
1866 const SGPropertyNode * node = getNode(relative_path);
1867 return (node == 0 ? defaultValue : node->getStringValue());
1872 * Set a bool value for another node.
1875 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1877 return getNode(relative_path, true)->setBoolValue(value);
1882 * Set an int value for another node.
1885 SGPropertyNode::setIntValue (const char * relative_path, int value)
1887 return getNode(relative_path, true)->setIntValue(value);
1892 * Set a long value for another node.
1895 SGPropertyNode::setLongValue (const char * relative_path, long value)
1897 return getNode(relative_path, true)->setLongValue(value);
1902 * Set a float value for another node.
1905 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1907 return getNode(relative_path, true)->setFloatValue(value);
1912 * Set a double value for another node.
1915 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1917 return getNode(relative_path, true)->setDoubleValue(value);
1922 * Set a string value for another node.
1925 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1927 return getNode(relative_path, true)->setStringValue(value);
1932 * Set an unknown value for another node.
1935 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1938 return getNode(relative_path, true)->setUnspecifiedValue(value);
1943 * Test whether another node is tied.
1946 SGPropertyNode::isTied (const char * relative_path) const
1948 const SGPropertyNode * node = getNode(relative_path);
1949 return (node == 0 ? false : node->isTied());
1954 * Tie a node reached by a relative path, creating it if necessary.
1957 SGPropertyNode::tie (const char * relative_path,
1958 const SGRawValue<bool> &rawValue,
1961 return getNode(relative_path, true)->tie(rawValue, useDefault);
1966 * Tie a node reached by a relative path, creating it if necessary.
1969 SGPropertyNode::tie (const char * relative_path,
1970 const SGRawValue<int> &rawValue,
1973 return getNode(relative_path, true)->tie(rawValue, useDefault);
1978 * Tie a node reached by a relative path, creating it if necessary.
1981 SGPropertyNode::tie (const char * relative_path,
1982 const SGRawValue<long> &rawValue,
1985 return getNode(relative_path, true)->tie(rawValue, useDefault);
1990 * Tie a node reached by a relative path, creating it if necessary.
1993 SGPropertyNode::tie (const char * relative_path,
1994 const SGRawValue<float> &rawValue,
1997 return getNode(relative_path, true)->tie(rawValue, useDefault);
2002 * Tie a node reached by a relative path, creating it if necessary.
2005 SGPropertyNode::tie (const char * relative_path,
2006 const SGRawValue<double> &rawValue,
2009 return getNode(relative_path, true)->tie(rawValue, useDefault);
2014 * Tie a node reached by a relative path, creating it if necessary.
2017 SGPropertyNode::tie (const char * relative_path,
2018 const SGRawValue<const char *> &rawValue,
2021 return getNode(relative_path, true)->tie(rawValue, useDefault);
2026 * Attempt to untie another node reached by a relative path.
2029 SGPropertyNode::untie (const char * relative_path)
2031 SGPropertyNode * node = getNode(relative_path);
2032 return (node == 0 ? false : node->untie());
2036 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2039 if (_listeners == 0)
2040 _listeners = new vector<SGPropertyChangeListener*>;
2041 _listeners->push_back(listener);
2042 listener->register_property(this);
2044 listener->valueChanged(this);
2048 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2050 if (_listeners == 0)
2052 vector<SGPropertyChangeListener*>::iterator it =
2053 find(_listeners->begin(), _listeners->end(), listener);
2054 if (it != _listeners->end()) {
2055 _listeners->erase(it);
2056 listener->unregister_property(this);
2057 if (_listeners->empty()) {
2058 vector<SGPropertyChangeListener*>* tmp = _listeners;
2066 SGPropertyNode::fireValueChanged ()
2068 fireValueChanged(this);
2072 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2074 fireChildAdded(this, child);
2078 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2080 fireChildRemoved(this, child);
2084 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2086 if (_listeners != 0) {
2087 for (unsigned int i = 0; i < _listeners->size(); i++) {
2088 (*_listeners)[i]->valueChanged(node);
2092 _parent->fireValueChanged(node);
2096 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2097 SGPropertyNode * child)
2099 if (_listeners != 0) {
2100 for (unsigned int i = 0; i < _listeners->size(); i++) {
2101 (*_listeners)[i]->childAdded(parent, child);
2105 _parent->fireChildAdded(parent, child);
2109 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2110 SGPropertyNode * child)
2112 if (_listeners != 0) {
2113 for (unsigned int i = 0; i < _listeners->size(); i++) {
2114 (*_listeners)[i]->childRemoved(parent, child);
2118 _parent->fireChildRemoved(parent, child);
2122 ////////////////////////////////////////////////////////////////////////
2123 // Implementation of SGPropertyChangeListener.
2124 ////////////////////////////////////////////////////////////////////////
2126 SGPropertyChangeListener::~SGPropertyChangeListener ()
2128 for (int i = _properties.size() - 1; i >= 0; i--)
2129 _properties[i]->removeChangeListener(this);
2133 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2139 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2140 SGPropertyNode * child)
2146 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2147 SGPropertyNode * child)
2153 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2155 _properties.push_back(node);
2159 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2161 vector<SGPropertyNode *>::iterator it =
2162 find(_properties.begin(), _properties.end(), node);
2163 if (it != _properties.end())
2164 _properties.erase(it);
2168 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2171 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2172 for (int i = 0; i < 3; ++i) {
2183 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2185 for (int i = 0; i < 3; ++i) {
2186 stream >> result[i];
2192 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2195 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2196 for (int i = 0; i < 4; ++i) {
2207 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2209 for (int i = 0; i < 4; ++i) {
2210 stream >> result[i];
2217 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2219 props::Type ltype = lhs.getType();
2220 props::Type rtype = rhs.getType();
2227 return false; // XXX Should we look in aliases?
2229 return lhs.getValue<bool>() == rhs.getValue<bool>();
2231 return lhs.getValue<int>() == rhs.getValue<int>();
2233 return lhs.getValue<long>() == rhs.getValue<long>();
2235 return lhs.getValue<float>() == rhs.getValue<float>();
2237 return lhs.getValue<double>() == rhs.getValue<double>();
2239 case props::UNSPECIFIED:
2240 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2242 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2244 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2252 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2253 const SGPropertyNode& rhs)
2257 int lhsChildren = lhs.nChildren();
2258 int rhsChildren = rhs.nChildren();
2259 if (lhsChildren != rhsChildren)
2261 if (lhsChildren == 0)
2262 return compareNodeValue(lhs, rhs);
2263 for (size_t i = 0; i < lhs._children.size(); ++i) {
2264 const SGPropertyNode* lchild = lhs._children[i];
2265 const SGPropertyNode* rchild = rhs._children[i];
2266 // I'm guessing that the nodes will usually be in the same
2268 if (lchild->getIndex() != rchild->getIndex()
2269 || lchild->getNameString() != rchild->getNameString()) {
2271 for (PropertyList::const_iterator itr = rhs._children.begin(),
2272 end = rhs._children.end();
2275 if (lchild->getIndex() == (*itr)->getIndex()
2276 && lchild->getNameString() == (*itr)->getNameString()) {
2283 if (!compare(*lchild, *rchild))
2289 struct PropertyPlaceLess {
2290 typedef bool result_type;
2291 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2293 int comp = lhs->getNameString().compare(rhs->getNameString());
2295 return lhs->getIndex() < rhs->getIndex();
2301 size_t hash_value(const SGPropertyNode& node)
2303 using namespace boost;
2305 if (node.nChildren() == 0) {
2306 switch (node.getType()) {
2311 return hash_value(node.getValue<bool>());
2313 return hash_value(node.getValue<int>());
2315 return hash_value(node.getValue<long>());
2317 return hash_value(node.getValue<float>());
2319 return hash_value(node.getValue<double>());
2321 case props::UNSPECIFIED:
2323 const char *val = node.getStringValue();
2324 return hash_range(val, val + strlen(val));
2328 const SGVec3d val = node.getValue<SGVec3d>();
2329 return hash_range(&val[0], &val[3]);
2333 const SGVec4d val = node.getValue<SGVec4d>();
2334 return hash_range(&val[0], &val[4]);
2336 case props::ALIAS: // XXX Should we look in aliases?
2342 PropertyList children(node._children.begin(), node._children.end());
2343 sort(children.begin(), children.end(), PropertyPlaceLess());
2344 for (PropertyList::const_iterator itr = children.begin(),
2345 end = children.end();
2348 hash_combine(seed, (*itr)->_name);
2349 hash_combine(seed, (*itr)->_index);
2350 hash_combine(seed, hash_value(**itr));