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>
14 #include "vectorPropTemplates.hxx"
24 #include <boost/algorithm/string/find_iterator.hpp>
25 #include <boost/algorithm/string/predicate.hpp>
26 #include <boost/algorithm/string/classification.hpp>
27 #include <boost/bind.hpp>
28 #include <boost/functional/hash.hpp>
29 #include <boost/range.hpp>
31 #include <simgear/math/SGMath.hxx>
37 #include <simgear/compiler.h>
38 #include <simgear/debug/logstream.hxx>
40 #if ( _MSC_VER == 1200 )
41 // MSVC 6 is buggy, and needs something strange here
42 using std::vector<SGPropertyNode_ptr>;
43 using std::vector<SGPropertyChangeListener *>;
44 using std::vector<SGPropertyNode *>;
56 using std::stringstream;
58 using namespace simgear;
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * Comparator class for sorting by index.
71 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
72 return (n1->getIndex() < n2->getIndex());
78 ////////////////////////////////////////////////////////////////////////
79 // Convenience macros for value access.
80 ////////////////////////////////////////////////////////////////////////
82 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
83 #define TEST_WRITE if (!getAttribute(WRITE)) return false
86 ////////////////////////////////////////////////////////////////////////
87 // Local path normalization code.
88 ////////////////////////////////////////////////////////////////////////
91 * Parse the name for a path component.
93 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
96 template<typename Range>
98 parse_name (const SGPropertyNode *node, const Range &path)
100 typename Range::iterator i = path.begin();
101 typename Range::iterator max = path.end();
105 if (i != path.end() && *i == '.') {
108 if (i != max && *i != '/')
109 throw string("illegal character after . or ..");
110 } else if (isalpha(*i) || *i == '_') {
113 // The rules inside a name are a little
116 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
117 *i == '-' || *i == '.') {
119 } else if (*i == '[' || *i == '/') {
124 err.append("' found in propertyname after '"+node->getNameString()+"'");
125 err.append("\nname may contain only ._- and alphanumeric characters");
133 if (path.begin() == i) {
136 err.append("' found in propertyname after '"+node->getNameString()+"'");
137 err.append("\nname must begin with alpha or '_'");
141 return Range(path.begin(), i);
144 // Validate the name of a single node
145 inline bool validateName(const string& name)
147 using namespace boost;
150 if (!isalpha(name[0]) && name[0] != '_')
152 return all(make_iterator_range(name.begin(), name.end()),
153 is_alnum() || is_any_of("_-."));
157 ////////////////////////////////////////////////////////////////////////
158 // Other static utility functions.
159 ////////////////////////////////////////////////////////////////////////
163 copy_string (const char * s)
165 unsigned long int slen = strlen(s);
166 char * copy = new char[slen + 1];
168 // the source string length is known so no need to check for '\0'
169 // when copying every single character
170 memcpy(copy, s, slen);
171 *(copy + slen) = '\0';
176 compare_strings (const char * s1, const char * s2)
178 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
182 * Locate a child node by name and index.
184 template<typename Itr>
186 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
188 int nNodes = nodes.size();
189 boost::iterator_range<Itr> name(begin, end);
190 for (int i = 0; i < nNodes; i++) {
191 SGPropertyNode * node = nodes[i];
193 // searching for a mathing index is a lot less time consuming than
194 // comparing two strings so do that first.
195 if (node->getIndex() == index && boost::equals(node->getName(), name))
202 * Locate the child node with the highest index of the same name
205 find_last_child (const char * name, const PropertyList& nodes)
207 int nNodes = nodes.size();
210 for (int i = 0; i < nNodes; i++) {
211 SGPropertyNode * node = nodes[i];
212 if (compare_strings(node->getName(), name))
214 int idx = node->getIndex();
215 if (idx > index) index = idx;
221 template<typename Itr>
222 inline SGPropertyNode*
223 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
225 int pos = find_child(begin, end, index, _children);
227 return _children[pos];
229 SGPropertyNode_ptr node;
230 pos = find_child(begin, end, index, _removedChildren);
232 PropertyList::iterator it = _removedChildren.begin();
234 node = _removedChildren[pos];
235 _removedChildren.erase(it);
236 node->setAttribute(REMOVED, false);
237 _children.push_back(node);
238 fireChildAdded(node);
245 template<typename Itr>
247 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
249 SGPropertyNode* node = getExistingChild(begin, end, index, create);
254 node = new SGPropertyNode(begin, end, index, this);
255 _children.push_back(node);
256 fireChildAdded(node);
263 template<typename SplitItr>
265 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
268 typedef typename SplitItr::value_type Range;
269 // Run off the end of the list
274 // Success! This is the one we want.
278 // Empty name at this point is empty, not root.
280 return find_node_aux(current, ++itr, create, last_index);
281 Range name = parse_name(current, token);
282 if (equals(name, "."))
283 return find_node_aux(current, ++itr, create, last_index);
284 if (equals(name, "..")) {
285 SGPropertyNode * parent = current->getParent();
287 throw string("attempt to move past root with '..'");
288 return find_node_aux(parent, ++itr, create, last_index);
291 if (last_index >= 0) {
292 // If we are at the last token and last_index is valid, use
293 // last_index as the index value
295 while (!(++itr).eof()) {
309 if (name.end() != token.end()) {
310 if (*name.end() == '[') {
311 typename Range::iterator i = name.end() + 1, end = token.end();
312 for (;i != end; ++i) {
314 index = (index * 10) + (*i - '0');
319 if (i == token.end() || *i != ']')
320 throw string("unterminated index (looking for ']')");
322 throw string("illegal characters in token: ")
323 + string(name.begin(), name.end());
327 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
328 index, create), itr, create,
332 // Internal function for parsing property paths. last_index provides
333 // and index value for the last node name token, if supplied.
334 template<typename Range>
336 find_node (SGPropertyNode * current,
341 using namespace boost;
342 typedef split_iterator<typename range_result_iterator<Range>::type>
345 PathSplitIterator itr
346 = make_split_iterator(path, first_finder("/", is_equal()));
347 if (*path.begin() == '/')
348 return find_node_aux(current->getRootNode(), itr, create, last_index);
350 return find_node_aux(current, itr, create, last_index);
354 ////////////////////////////////////////////////////////////////////////
355 // Private methods from SGPropertyNode (may be inlined for speed).
356 ////////////////////////////////////////////////////////////////////////
359 SGPropertyNode::get_bool () const
362 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
364 return _local_val.bool_val;
368 SGPropertyNode::get_int () const
371 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
373 return _local_val.int_val;
377 SGPropertyNode::get_long () const
380 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
382 return _local_val.long_val;
386 SGPropertyNode::get_float () const
389 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
391 return _local_val.float_val;
395 SGPropertyNode::get_double () const
398 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
400 return _local_val.double_val;
404 SGPropertyNode::get_string () const
407 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
409 return _local_val.string_val;
413 SGPropertyNode::set_bool (bool val)
416 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
423 _local_val.bool_val = val;
430 SGPropertyNode::set_int (int val)
433 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
440 _local_val.int_val = val;
447 SGPropertyNode::set_long (long val)
450 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
457 _local_val.long_val = val;
464 SGPropertyNode::set_float (float val)
467 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
474 _local_val.float_val = val;
481 SGPropertyNode::set_double (double val)
484 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
491 _local_val.double_val = val;
498 SGPropertyNode::set_string (const char * val)
501 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
508 delete [] _local_val.string_val;
509 _local_val.string_val = copy_string(val);
516 SGPropertyNode::clearValue ()
518 if (_type == props::ALIAS) {
521 } else if (_type != props::NONE) {
524 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
527 _local_val.int_val = SGRawValue<int>::DefaultValue();
530 _local_val.long_val = SGRawValue<long>::DefaultValue();
533 _local_val.float_val = SGRawValue<float>::DefaultValue();
536 _local_val.double_val = SGRawValue<double>::DefaultValue();
539 case props::UNSPECIFIED:
541 delete [] _local_val.string_val;
543 _local_val.string_val = 0;
545 default: // avoid compiler warning
557 * Get the value as a string.
560 SGPropertyNode::make_string () const
562 if (!getAttribute(READ))
566 return _value.alias->getStringValue();
568 return get_bool() ? "true" : "false";
570 case props::UNSPECIFIED:
589 sstr << std::setprecision(10) << get_double();
591 case props::EXTENDED:
593 props::Type realType = _value.val->getType();
594 // Perhaps this should be done for all types?
595 if (realType == props::VEC3D || realType == props::VEC4D)
597 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
603 _buffer = sstr.str();
604 return _buffer.c_str();
608 * Trace a write access for a property.
611 SGPropertyNode::trace_write () const
614 cerr << "TRACE: Write node " << getPath () << ", value \""
615 << make_string() << '"' << endl;
617 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
618 << ", value \"" << make_string() << '"');
623 * Trace a read access for a property.
626 SGPropertyNode::trace_read () const
629 cerr << "TRACE: Write node " << getPath () << ", value \""
630 << make_string() << '"' << endl;
632 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
633 << ", value \"" << make_string() << '"');
638 ////////////////////////////////////////////////////////////////////////
639 // Public methods from SGPropertyNode.
640 ////////////////////////////////////////////////////////////////////////
643 * Last used attribute
644 * Update as needed when enum Attribute is changed
646 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
649 * Default constructor: always creates a root node.
651 SGPropertyNode::SGPropertyNode ()
659 _local_val.string_val = 0;
667 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
668 : SGReferenced(node),
671 _parent(0), // don't copy the parent
675 _listeners(0) // CHECK!!
677 _local_val.string_val = 0;
679 if (_type == props::NONE)
681 if (_type == props::ALIAS) {
682 _value.alias = node._value.alias;
687 if (_tied || _type == props::EXTENDED) {
688 _value.val = node._value.val->clone();
693 set_bool(node.get_bool());
696 set_int(node.get_int());
699 set_long(node.get_long());
702 set_float(node.get_float());
705 set_double(node.get_double());
708 case props::UNSPECIFIED:
709 set_string(node.get_string());
718 * Convenience constructor.
720 template<typename Itr>
721 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
723 SGPropertyNode * parent)
732 _local_val.string_val = 0;
734 if (!validateName(_name))
735 throw string("plain name expected instead of '") + _name + '\'';
738 SGPropertyNode::SGPropertyNode (const string& name,
740 SGPropertyNode * parent)
749 _local_val.string_val = 0;
751 if (!validateName(name))
752 throw string("plain name expected instead of '") + _name + '\'';
758 SGPropertyNode::~SGPropertyNode ()
760 // zero out all parent pointers, else they might be dangling
761 for (unsigned i = 0; i < _children.size(); ++i)
762 _children[i]->_parent = 0;
763 for (unsigned i = 0; i < _removedChildren.size(); ++i)
764 _removedChildren[i]->_parent = 0;
768 vector<SGPropertyChangeListener*>::iterator it;
769 for (it = _listeners->begin(); it != _listeners->end(); ++it)
770 (*it)->unregister_property(this);
777 * Alias to another node.
780 SGPropertyNode::alias (SGPropertyNode * target)
782 if (target && (_type != props::ALIAS) && (!_tied))
786 _value.alias = target;
787 _type = props::ALIAS;
795 SG_LOG(SG_GENERAL, SG_ALERT,
796 "Failed to create alias for " << getPath() << ". "
797 "The target property does not exist.");
800 if (_type == props::ALIAS)
802 SG_LOG(SG_GENERAL, SG_ALERT,
803 "Failed to create alias at " << target->getPath() << ". "
804 "Source "<< getPath() << " is also an alias. Unsupported recursion.");
809 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
810 "Source " << getPath() << " is a tied property.");
819 * Alias to another node by path.
822 SGPropertyNode::alias (const char * path)
824 return alias(getNode(path, true));
832 SGPropertyNode::unalias ()
834 if (_type != props::ALIAS)
842 * Get the target of an alias.
845 SGPropertyNode::getAliasTarget ()
847 return (_type == props::ALIAS ? _value.alias : 0);
851 const SGPropertyNode *
852 SGPropertyNode::getAliasTarget () const
854 return (_type == props::ALIAS ? _value.alias : 0);
858 * create a non-const child by name after the last node with the same name.
861 SGPropertyNode::addChild (const char * name)
863 int pos = find_last_child(name, _children)+1;
865 SGPropertyNode_ptr node;
866 node = new SGPropertyNode(name, name + strlen(name), pos, this);
867 _children.push_back(node);
868 fireChildAdded(node);
874 * Get a non-const child by index.
877 SGPropertyNode::getChild (int position)
879 if (position >= 0 && position < nChildren())
880 return _children[position];
887 * Get a const child by index.
889 const SGPropertyNode *
890 SGPropertyNode::getChild (int position) const
892 if (position >= 0 && position < nChildren())
893 return _children[position];
900 * Get a non-const child by name and index, creating if necessary.
904 SGPropertyNode::getChild (const char * name, int index, bool create)
906 return getChildImpl(name, name + strlen(name), index, create);
910 SGPropertyNode::getChild (const string& name, int index, bool create)
912 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
917 node = new SGPropertyNode(name, index, this);
918 _children.push_back(node);
919 fireChildAdded(node);
927 * Get a const child by name and index.
929 const SGPropertyNode *
930 SGPropertyNode::getChild (const char * name, int index) const
932 int pos = find_child(name, name + strlen(name), index, _children);
934 return _children[pos];
941 * Get all children with the same name (but different indices).
944 SGPropertyNode::getChildren (const char * name) const
946 PropertyList children;
947 int max = _children.size();
949 for (int i = 0; i < max; i++)
950 if (compare_strings(_children[i]->getName(), name))
951 children.push_back(_children[i]);
953 sort(children.begin(), children.end(), CompareIndices());
959 * Remove child by position.
962 SGPropertyNode::removeChild (int pos, bool keep)
964 SGPropertyNode_ptr node;
965 if (pos < 0 || pos >= (int)_children.size())
968 PropertyList::iterator it = _children.begin();
970 node = _children[pos];
973 _removedChildren.push_back(node);
976 node->setAttribute(REMOVED, true);
978 fireChildRemoved(node);
984 * Remove a child node
987 SGPropertyNode::removeChild (const char * name, int index, bool keep)
989 SGPropertyNode_ptr ret;
990 int pos = find_child(name, name + strlen(name), index, _children);
992 ret = removeChild(pos, keep);
998 * Remove all children with the specified name.
1001 SGPropertyNode::removeChildren (const char * name, bool keep)
1003 PropertyList children;
1005 for (int pos = _children.size() - 1; pos >= 0; pos--)
1006 if (compare_strings(_children[pos]->getName(), name))
1007 children.push_back(removeChild(pos, keep));
1009 sort(children.begin(), children.end(), CompareIndices());
1014 SGPropertyNode::getDisplayName (bool simplify) const
1016 string display_name = _name;
1017 if (_index != 0 || !simplify) {
1019 sstr << '[' << _index << ']';
1020 display_name += sstr.str();
1022 return display_name;
1026 SGPropertyNode::getPath (bool simplify) const
1028 typedef std::vector<SGConstPropertyNode_ptr> PList;
1030 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1031 pathList.push_back(node);
1033 for (PList::reverse_iterator itr = pathList.rbegin(),
1034 rend = pathList.rend();
1038 result += (*itr)->getDisplayName(simplify);
1044 SGPropertyNode::getType () const
1046 if (_type == props::ALIAS)
1047 return _value.alias->getType();
1048 else if (_type == props::EXTENDED)
1049 return _value.val->getType();
1056 SGPropertyNode::getBoolValue () const
1058 // Shortcut for common case
1059 if (_attr == (READ|WRITE) && _type == props::BOOL)
1062 if (getAttribute(TRACE_READ))
1064 if (!getAttribute(READ))
1065 return SGRawValue<bool>::DefaultValue();
1068 return _value.alias->getBoolValue();
1072 return get_int() == 0 ? false : true;
1074 return get_long() == 0L ? false : true;
1076 return get_float() == 0.0 ? false : true;
1078 return get_double() == 0.0L ? false : true;
1080 case props::UNSPECIFIED:
1081 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1084 return SGRawValue<bool>::DefaultValue();
1089 SGPropertyNode::getIntValue () const
1091 // Shortcut for common case
1092 if (_attr == (READ|WRITE) && _type == props::INT)
1095 if (getAttribute(TRACE_READ))
1097 if (!getAttribute(READ))
1098 return SGRawValue<int>::DefaultValue();
1101 return _value.alias->getIntValue();
1103 return int(get_bool());
1107 return int(get_long());
1109 return int(get_float());
1111 return int(get_double());
1113 case props::UNSPECIFIED:
1114 return atoi(get_string());
1117 return SGRawValue<int>::DefaultValue();
1122 SGPropertyNode::getLongValue () const
1124 // Shortcut for common case
1125 if (_attr == (READ|WRITE) && _type == props::LONG)
1128 if (getAttribute(TRACE_READ))
1130 if (!getAttribute(READ))
1131 return SGRawValue<long>::DefaultValue();
1134 return _value.alias->getLongValue();
1136 return long(get_bool());
1138 return long(get_int());
1142 return long(get_float());
1144 return long(get_double());
1146 case props::UNSPECIFIED:
1147 return strtol(get_string(), 0, 0);
1150 return SGRawValue<long>::DefaultValue();
1155 SGPropertyNode::getFloatValue () const
1157 // Shortcut for common case
1158 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1161 if (getAttribute(TRACE_READ))
1163 if (!getAttribute(READ))
1164 return SGRawValue<float>::DefaultValue();
1167 return _value.alias->getFloatValue();
1169 return float(get_bool());
1171 return float(get_int());
1173 return float(get_long());
1177 return float(get_double());
1179 case props::UNSPECIFIED:
1180 return atof(get_string());
1183 return SGRawValue<float>::DefaultValue();
1188 SGPropertyNode::getDoubleValue () const
1190 // Shortcut for common case
1191 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1192 return get_double();
1194 if (getAttribute(TRACE_READ))
1196 if (!getAttribute(READ))
1197 return SGRawValue<double>::DefaultValue();
1201 return _value.alias->getDoubleValue();
1203 return double(get_bool());
1205 return double(get_int());
1207 return double(get_long());
1209 return double(get_float());
1211 return get_double();
1213 case props::UNSPECIFIED:
1214 return strtod(get_string(), 0);
1217 return SGRawValue<double>::DefaultValue();
1222 SGPropertyNode::getStringValue () const
1224 // Shortcut for common case
1225 if (_attr == (READ|WRITE) && _type == props::STRING)
1226 return get_string();
1228 if (getAttribute(TRACE_READ))
1230 if (!getAttribute(READ))
1231 return SGRawValue<const char *>::DefaultValue();
1232 return make_string();
1236 SGPropertyNode::setBoolValue (bool value)
1238 // Shortcut for common case
1239 if (_attr == (READ|WRITE) && _type == props::BOOL)
1240 return set_bool(value);
1242 bool result = false;
1244 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1247 _type = props::BOOL;
1252 result = _value.alias->setBoolValue(value);
1255 result = set_bool(value);
1258 result = set_int(int(value));
1261 result = set_long(long(value));
1264 result = set_float(float(value));
1267 result = set_double(double(value));
1270 case props::UNSPECIFIED:
1271 result = set_string(value ? "true" : "false");
1278 if (getAttribute(TRACE_WRITE))
1284 SGPropertyNode::setIntValue (int value)
1286 // Shortcut for common case
1287 if (_attr == (READ|WRITE) && _type == props::INT)
1288 return set_int(value);
1290 bool result = false;
1292 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1295 _local_val.int_val = 0;
1300 result = _value.alias->setIntValue(value);
1303 result = set_bool(value == 0 ? false : true);
1306 result = set_int(value);
1309 result = set_long(long(value));
1312 result = set_float(float(value));
1315 result = set_double(double(value));
1318 case props::UNSPECIFIED: {
1320 sprintf(buf, "%d", value);
1321 result = set_string(buf);
1329 if (getAttribute(TRACE_WRITE))
1335 SGPropertyNode::setLongValue (long value)
1337 // Shortcut for common case
1338 if (_attr == (READ|WRITE) && _type == props::LONG)
1339 return set_long(value);
1341 bool result = false;
1343 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1345 _type = props::LONG;
1346 _local_val.long_val = 0L;
1351 result = _value.alias->setLongValue(value);
1354 result = set_bool(value == 0L ? false : true);
1357 result = set_int(int(value));
1360 result = set_long(value);
1363 result = set_float(float(value));
1366 result = set_double(double(value));
1369 case props::UNSPECIFIED: {
1371 sprintf(buf, "%ld", value);
1372 result = set_string(buf);
1380 if (getAttribute(TRACE_WRITE))
1386 SGPropertyNode::setFloatValue (float value)
1388 // Shortcut for common case
1389 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1390 return set_float(value);
1392 bool result = false;
1394 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1396 _type = props::FLOAT;
1397 _local_val.float_val = 0;
1402 result = _value.alias->setFloatValue(value);
1405 result = set_bool(value == 0.0 ? false : true);
1408 result = set_int(int(value));
1411 result = set_long(long(value));
1414 result = set_float(value);
1417 result = set_double(double(value));
1420 case props::UNSPECIFIED: {
1422 sprintf(buf, "%f", value);
1423 result = set_string(buf);
1431 if (getAttribute(TRACE_WRITE))
1437 SGPropertyNode::setDoubleValue (double value)
1439 // Shortcut for common case
1440 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1441 return set_double(value);
1443 bool result = false;
1445 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1447 _local_val.double_val = value;
1448 _type = props::DOUBLE;
1453 result = _value.alias->setDoubleValue(value);
1456 result = set_bool(value == 0.0L ? false : true);
1459 result = set_int(int(value));
1462 result = set_long(long(value));
1465 result = set_float(float(value));
1468 result = set_double(value);
1471 case props::UNSPECIFIED: {
1473 sprintf(buf, "%f", value);
1474 result = set_string(buf);
1482 if (getAttribute(TRACE_WRITE))
1488 SGPropertyNode::setStringValue (const char * value)
1490 // Shortcut for common case
1491 if (_attr == (READ|WRITE) && _type == props::STRING)
1492 return set_string(value);
1494 bool result = false;
1496 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1498 _type = props::STRING;
1503 result = _value.alias->setStringValue(value);
1506 result = set_bool((compare_strings(value, "true")
1507 || atoi(value)) ? true : false);
1510 result = set_int(atoi(value));
1513 result = set_long(strtol(value, 0, 0));
1516 result = set_float(atof(value));
1519 result = set_double(strtod(value, 0));
1522 case props::UNSPECIFIED:
1523 result = set_string(value);
1525 case props::EXTENDED:
1527 stringstream sstr(value);
1528 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1536 if (getAttribute(TRACE_WRITE))
1542 SGPropertyNode::setUnspecifiedValue (const char * value)
1544 bool result = false;
1546 if (_type == props::NONE) {
1548 _type = props::UNSPECIFIED;
1550 props::Type type = _type;
1551 if (type == props::EXTENDED)
1552 type = _value.val->getType();
1555 result = _value.alias->setUnspecifiedValue(value);
1558 result = set_bool((compare_strings(value, "true")
1559 || atoi(value)) ? true : false);
1562 result = set_int(atoi(value));
1565 result = set_long(strtol(value, 0, 0));
1568 result = set_float(atof(value));
1571 result = set_double(strtod(value, 0));
1574 case props::UNSPECIFIED:
1575 result = set_string(value);
1578 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1581 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1588 if (getAttribute(TRACE_WRITE))
1593 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1595 if (!getAttribute(READ))
1599 return _value.alias->printOn(stream);
1601 stream << (get_bool() ? "true" : "false");
1604 stream << get_int();
1607 stream << get_long();
1610 stream << get_float();
1613 stream << get_double();
1616 case props::UNSPECIFIED:
1617 stream << get_string();
1619 case props::EXTENDED:
1620 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1624 default: // avoid compiler warning
1631 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1634 if (_type == props::ALIAS || _tied)
1637 useDefault = useDefault && hasValue();
1638 std::string old_val;
1640 old_val = getStringValue();
1642 _type = props::STRING;
1644 _value.val = rawValue.clone();
1647 int save_attributes = getAttributes();
1648 setAttribute( WRITE, true );
1649 setStringValue(old_val.c_str());
1650 setAttributes( save_attributes );
1656 SGPropertyNode::untie ()
1663 bool val = getBoolValue();
1665 _type = props::BOOL;
1666 _local_val.bool_val = val;
1670 int val = getIntValue();
1673 _local_val.int_val = val;
1677 long val = getLongValue();
1679 _type = props::LONG;
1680 _local_val.long_val = val;
1683 case props::FLOAT: {
1684 float val = getFloatValue();
1686 _type = props::FLOAT;
1687 _local_val.float_val = val;
1690 case props::DOUBLE: {
1691 double val = getDoubleValue();
1693 _type = props::DOUBLE;
1694 _local_val.double_val = val;
1698 case props::UNSPECIFIED: {
1699 string val = getStringValue();
1701 _type = props::STRING;
1702 _local_val.string_val = copy_string(val.c_str());
1705 case props::EXTENDED: {
1706 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1707 _value.val = 0; // Prevent clearValue() from deleting
1709 _type = props::EXTENDED;
1710 _value.val = val->makeContainer();
1724 SGPropertyNode::getRootNode ()
1729 return _parent->getRootNode();
1732 const SGPropertyNode *
1733 SGPropertyNode::getRootNode () const
1738 return _parent->getRootNode();
1742 SGPropertyNode::getNode (const char * relative_path, bool create)
1744 using namespace boost;
1746 return find_node(this, make_iterator_range(relative_path, relative_path
1747 + strlen(relative_path)),
1752 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1754 using namespace boost;
1755 return find_node(this, make_iterator_range(relative_path, relative_path
1756 + strlen(relative_path)),
1760 const SGPropertyNode *
1761 SGPropertyNode::getNode (const char * relative_path) const
1763 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1766 const SGPropertyNode *
1767 SGPropertyNode::getNode (const char * relative_path, int index) const
1769 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1773 ////////////////////////////////////////////////////////////////////////
1774 // Convenience methods using relative paths.
1775 ////////////////////////////////////////////////////////////////////////
1779 * Test whether another node has a value attached.
1782 SGPropertyNode::hasValue (const char * relative_path) const
1784 const SGPropertyNode * node = getNode(relative_path);
1785 return (node == 0 ? false : node->hasValue());
1790 * Get the value type for another node.
1793 SGPropertyNode::getType (const char * relative_path) const
1795 const SGPropertyNode * node = getNode(relative_path);
1796 return (node == 0 ? props::UNSPECIFIED : node->getType());
1801 * Get a bool value for another node.
1804 SGPropertyNode::getBoolValue (const char * relative_path,
1805 bool defaultValue) const
1807 const SGPropertyNode * node = getNode(relative_path);
1808 return (node == 0 ? defaultValue : node->getBoolValue());
1813 * Get an int value for another node.
1816 SGPropertyNode::getIntValue (const char * relative_path,
1817 int defaultValue) const
1819 const SGPropertyNode * node = getNode(relative_path);
1820 return (node == 0 ? defaultValue : node->getIntValue());
1825 * Get a long value for another node.
1828 SGPropertyNode::getLongValue (const char * relative_path,
1829 long defaultValue) const
1831 const SGPropertyNode * node = getNode(relative_path);
1832 return (node == 0 ? defaultValue : node->getLongValue());
1837 * Get a float value for another node.
1840 SGPropertyNode::getFloatValue (const char * relative_path,
1841 float defaultValue) const
1843 const SGPropertyNode * node = getNode(relative_path);
1844 return (node == 0 ? defaultValue : node->getFloatValue());
1849 * Get a double value for another node.
1852 SGPropertyNode::getDoubleValue (const char * relative_path,
1853 double defaultValue) const
1855 const SGPropertyNode * node = getNode(relative_path);
1856 return (node == 0 ? defaultValue : node->getDoubleValue());
1861 * Get a string value for another node.
1864 SGPropertyNode::getStringValue (const char * relative_path,
1865 const char * defaultValue) const
1867 const SGPropertyNode * node = getNode(relative_path);
1868 return (node == 0 ? defaultValue : node->getStringValue());
1873 * Set a bool value for another node.
1876 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1878 return getNode(relative_path, true)->setBoolValue(value);
1883 * Set an int value for another node.
1886 SGPropertyNode::setIntValue (const char * relative_path, int value)
1888 return getNode(relative_path, true)->setIntValue(value);
1893 * Set a long value for another node.
1896 SGPropertyNode::setLongValue (const char * relative_path, long value)
1898 return getNode(relative_path, true)->setLongValue(value);
1903 * Set a float value for another node.
1906 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1908 return getNode(relative_path, true)->setFloatValue(value);
1913 * Set a double value for another node.
1916 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1918 return getNode(relative_path, true)->setDoubleValue(value);
1923 * Set a string value for another node.
1926 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1928 return getNode(relative_path, true)->setStringValue(value);
1933 * Set an unknown value for another node.
1936 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1939 return getNode(relative_path, true)->setUnspecifiedValue(value);
1944 * Test whether another node is tied.
1947 SGPropertyNode::isTied (const char * relative_path) const
1949 const SGPropertyNode * node = getNode(relative_path);
1950 return (node == 0 ? false : node->isTied());
1955 * Tie a node reached by a relative path, creating it if necessary.
1958 SGPropertyNode::tie (const char * relative_path,
1959 const SGRawValue<bool> &rawValue,
1962 return getNode(relative_path, true)->tie(rawValue, useDefault);
1967 * Tie a node reached by a relative path, creating it if necessary.
1970 SGPropertyNode::tie (const char * relative_path,
1971 const SGRawValue<int> &rawValue,
1974 return getNode(relative_path, true)->tie(rawValue, useDefault);
1979 * Tie a node reached by a relative path, creating it if necessary.
1982 SGPropertyNode::tie (const char * relative_path,
1983 const SGRawValue<long> &rawValue,
1986 return getNode(relative_path, true)->tie(rawValue, useDefault);
1991 * Tie a node reached by a relative path, creating it if necessary.
1994 SGPropertyNode::tie (const char * relative_path,
1995 const SGRawValue<float> &rawValue,
1998 return getNode(relative_path, true)->tie(rawValue, useDefault);
2003 * Tie a node reached by a relative path, creating it if necessary.
2006 SGPropertyNode::tie (const char * relative_path,
2007 const SGRawValue<double> &rawValue,
2010 return getNode(relative_path, true)->tie(rawValue, useDefault);
2015 * Tie a node reached by a relative path, creating it if necessary.
2018 SGPropertyNode::tie (const char * relative_path,
2019 const SGRawValue<const char *> &rawValue,
2022 return getNode(relative_path, true)->tie(rawValue, useDefault);
2027 * Attempt to untie another node reached by a relative path.
2030 SGPropertyNode::untie (const char * relative_path)
2032 SGPropertyNode * node = getNode(relative_path);
2033 return (node == 0 ? false : node->untie());
2037 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2040 if (_listeners == 0)
2041 _listeners = new vector<SGPropertyChangeListener*>;
2042 _listeners->push_back(listener);
2043 listener->register_property(this);
2045 listener->valueChanged(this);
2049 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2051 if (_listeners == 0)
2053 vector<SGPropertyChangeListener*>::iterator it =
2054 find(_listeners->begin(), _listeners->end(), listener);
2055 if (it != _listeners->end()) {
2056 _listeners->erase(it);
2057 listener->unregister_property(this);
2058 if (_listeners->empty()) {
2059 vector<SGPropertyChangeListener*>* tmp = _listeners;
2067 SGPropertyNode::fireValueChanged ()
2069 fireValueChanged(this);
2073 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2075 fireChildAdded(this, child);
2079 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2081 fireChildRemoved(this, child);
2085 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2087 if (_listeners != 0) {
2088 for (unsigned int i = 0; i < _listeners->size(); i++) {
2089 (*_listeners)[i]->valueChanged(node);
2093 _parent->fireValueChanged(node);
2097 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2098 SGPropertyNode * child)
2100 if (_listeners != 0) {
2101 for (unsigned int i = 0; i < _listeners->size(); i++) {
2102 (*_listeners)[i]->childAdded(parent, child);
2106 _parent->fireChildAdded(parent, child);
2110 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2111 SGPropertyNode * child)
2113 if (_listeners != 0) {
2114 for (unsigned int i = 0; i < _listeners->size(); i++) {
2115 (*_listeners)[i]->childRemoved(parent, child);
2119 _parent->fireChildRemoved(parent, child);
2123 ////////////////////////////////////////////////////////////////////////
2124 // Implementation of SGPropertyChangeListener.
2125 ////////////////////////////////////////////////////////////////////////
2127 SGPropertyChangeListener::~SGPropertyChangeListener ()
2129 for (int i = _properties.size() - 1; i >= 0; i--)
2130 _properties[i]->removeChangeListener(this);
2134 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2140 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2141 SGPropertyNode * child)
2147 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2148 SGPropertyNode * child)
2154 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2156 _properties.push_back(node);
2160 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2162 vector<SGPropertyNode *>::iterator it =
2163 find(_properties.begin(), _properties.end(), node);
2164 if (it != _properties.end())
2165 _properties.erase(it);
2169 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2172 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2173 for (int i = 0; i < 3; ++i) {
2184 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2186 for (int i = 0; i < 3; ++i) {
2187 stream >> result[i];
2193 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2196 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2197 for (int i = 0; i < 4; ++i) {
2208 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2210 for (int i = 0; i < 4; ++i) {
2211 stream >> result[i];
2218 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2220 props::Type ltype = lhs.getType();
2221 props::Type rtype = rhs.getType();
2228 return false; // XXX Should we look in aliases?
2230 return lhs.getValue<bool>() == rhs.getValue<bool>();
2232 return lhs.getValue<int>() == rhs.getValue<int>();
2234 return lhs.getValue<long>() == rhs.getValue<long>();
2236 return lhs.getValue<float>() == rhs.getValue<float>();
2238 return lhs.getValue<double>() == rhs.getValue<double>();
2240 case props::UNSPECIFIED:
2241 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2243 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2245 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2253 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2254 const SGPropertyNode& rhs)
2258 int lhsChildren = lhs.nChildren();
2259 int rhsChildren = rhs.nChildren();
2260 if (lhsChildren != rhsChildren)
2262 if (lhsChildren == 0)
2263 return compareNodeValue(lhs, rhs);
2264 for (size_t i = 0; i < lhs._children.size(); ++i) {
2265 const SGPropertyNode* lchild = lhs._children[i];
2266 const SGPropertyNode* rchild = rhs._children[i];
2267 // I'm guessing that the nodes will usually be in the same
2269 if (lchild->getIndex() != rchild->getIndex()
2270 || lchild->getNameString() != rchild->getNameString()) {
2272 for (PropertyList::const_iterator itr = rhs._children.begin(),
2273 end = rhs._children.end();
2276 if (lchild->getIndex() == (*itr)->getIndex()
2277 && lchild->getNameString() == (*itr)->getNameString()) {
2284 if (!compare(*lchild, *rchild))
2290 struct PropertyPlaceLess {
2291 typedef bool result_type;
2292 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2294 int comp = lhs->getNameString().compare(rhs->getNameString());
2296 return lhs->getIndex() < rhs->getIndex();
2302 size_t hash_value(const SGPropertyNode& node)
2304 using namespace boost;
2306 if (node.nChildren() == 0) {
2307 switch (node.getType()) {
2312 return hash_value(node.getValue<bool>());
2314 return hash_value(node.getValue<int>());
2316 return hash_value(node.getValue<long>());
2318 return hash_value(node.getValue<float>());
2320 return hash_value(node.getValue<double>());
2322 case props::UNSPECIFIED:
2324 const char *val = node.getStringValue();
2325 return hash_range(val, val + strlen(val));
2329 const SGVec3d val = node.getValue<SGVec3d>();
2330 return hash_range(&val[0], &val[3]);
2334 const SGVec4d val = node.getValue<SGVec4d>();
2335 return hash_range(&val[0], &val[4]);
2337 case props::ALIAS: // XXX Should we look in aliases?
2343 PropertyList children(node._children.begin(), node._children.end());
2344 sort(children.begin(), children.end(), PropertyPlaceLess());
2345 for (PropertyList::const_iterator itr = children.begin(),
2346 end = children.end();
2349 hash_combine(seed, (*itr)->_name);
2350 hash_combine(seed, (*itr)->_index);
2351 hash_combine(seed, hash_value(**itr));