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>
35 #include <simgear/compiler.h>
36 #include <simgear/debug/logstream.hxx>
38 #if ( _MSC_VER == 1200 )
39 // MSVC 6 is buggy, and needs something strange here
40 using std::vector<SGPropertyNode_ptr>;
41 using std::vector<SGPropertyChangeListener *>;
42 using std::vector<SGPropertyNode *>;
54 using std::stringstream;
56 using namespace simgear;
59 ////////////////////////////////////////////////////////////////////////
61 ////////////////////////////////////////////////////////////////////////
64 * Comparator class for sorting by index.
69 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
70 return (n1->getIndex() < n2->getIndex());
76 ////////////////////////////////////////////////////////////////////////
77 // Convenience macros for value access.
78 ////////////////////////////////////////////////////////////////////////
80 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
81 #define TEST_WRITE if (!getAttribute(WRITE)) return false
84 ////////////////////////////////////////////////////////////////////////
85 // Local path normalization code.
86 ////////////////////////////////////////////////////////////////////////
89 * Parse the name for a path component.
91 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
94 template<typename Range>
96 parse_name (const SGPropertyNode *node, const Range &path)
98 typename Range::iterator i = path.begin();
99 typename Range::iterator max = path.end();
103 if (i != path.end() && *i == '.') {
106 if (i != max && *i != '/')
107 throw string("illegal character after . or ..");
108 } else if (isalpha(*i) || *i == '_') {
111 // The rules inside a name are a little
114 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
115 *i == '-' || *i == '.') {
117 } else if (*i == '[' || *i == '/') {
122 err.append("' found in propertyname after '"+node->getNameString()+"'");
123 err.append("\nname may contain only ._- and alphanumeric characters");
131 if (path.begin() == i) {
134 err.append("' found in propertyname after '"+node->getNameString()+"'");
135 err.append("\nname must begin with alpha or '_'");
139 return Range(path.begin(), i);
142 // Validate the name of a single node
143 inline bool validateName(const string& name)
145 using namespace boost;
148 if (!isalpha(name[0]) && name[0] != '_')
150 return all(make_iterator_range(name.begin(), name.end()),
151 is_alnum() || is_any_of("_-."));
155 ////////////////////////////////////////////////////////////////////////
156 // Other static utility functions.
157 ////////////////////////////////////////////////////////////////////////
161 copy_string (const char * s)
163 unsigned long int slen = strlen(s);
164 char * copy = new char[slen + 1];
166 // the source string length is known so no need to check for '\0'
167 // when copying every single character
168 memcpy(copy, s, slen);
169 *(copy + slen) = '\0';
174 compare_strings (const char * s1, const char * s2)
176 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
180 * Locate a child node by name and index.
182 template<typename Itr>
184 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
186 int nNodes = nodes.size();
187 boost::iterator_range<Itr> name(begin, end);
188 for (int i = 0; i < nNodes; i++) {
189 SGPropertyNode * node = nodes[i];
191 // searching for a mathing index is a lot less time consuming than
192 // comparing two strings so do that first.
193 if (node->getIndex() == index && boost::equals(node->getName(), name))
200 * Locate the child node with the highest index of the same name
203 find_last_child (const char * name, const PropertyList& nodes)
205 int nNodes = nodes.size();
208 for (int i = 0; i < nNodes; i++) {
209 SGPropertyNode * node = nodes[i];
210 if (compare_strings(node->getName(), name))
212 int idx = node->getIndex();
213 if (idx > index) index = idx;
219 template<typename Itr>
220 inline SGPropertyNode*
221 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
223 int pos = find_child(begin, end, index, _children);
225 return _children[pos];
227 SGPropertyNode_ptr node;
228 pos = find_child(begin, end, index, _removedChildren);
230 PropertyList::iterator it = _removedChildren.begin();
232 node = _removedChildren[pos];
233 _removedChildren.erase(it);
234 node->setAttribute(REMOVED, false);
235 _children.push_back(node);
236 fireChildAdded(node);
243 template<typename Itr>
245 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
247 SGPropertyNode* node = getExistingChild(begin, end, index, create);
252 node = new SGPropertyNode(begin, end, index, this);
253 _children.push_back(node);
254 fireChildAdded(node);
261 template<typename SplitItr>
263 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
266 typedef typename SplitItr::value_type Range;
267 // Run off the end of the list
272 // Success! This is the one we want.
276 // Empty name at this point is empty, not root.
278 return find_node_aux(current, ++itr, create, last_index);
279 Range name = parse_name(current, token);
280 if (equals(name, "."))
281 return find_node_aux(current, ++itr, create, last_index);
282 if (equals(name, "..")) {
283 SGPropertyNode * parent = current->getParent();
285 throw string("attempt to move past root with '..'");
286 return find_node_aux(parent, ++itr, create, last_index);
289 if (last_index >= 0) {
290 // If we are at the last token and last_index is valid, use
291 // last_index as the index value
293 while (!(++itr).eof()) {
307 if (name.end() != token.end()) {
308 if (*name.end() == '[') {
309 typename Range::iterator i = name.end() + 1, end = token.end();
310 for (;i != end; ++i) {
312 index = (index * 10) + (*i - '0');
317 if (i == token.end() || *i != ']')
318 throw string("unterminated index (looking for ']')");
320 throw string("illegal characters in token: ")
321 + string(name.begin(), name.end());
325 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
326 index, create), itr, create,
330 // Internal function for parsing property paths. last_index provides
331 // and index value for the last node name token, if supplied.
332 template<typename Range>
334 find_node (SGPropertyNode * current,
339 using namespace boost;
340 typedef split_iterator<typename range_result_iterator<Range>::type>
343 PathSplitIterator itr
344 = make_split_iterator(path, first_finder("/", is_equal()));
345 if (*path.begin() == '/')
346 return find_node_aux(current->getRootNode(), itr, create, last_index);
348 return find_node_aux(current, itr, create, last_index);
352 ////////////////////////////////////////////////////////////////////////
353 // Private methods from SGPropertyNode (may be inlined for speed).
354 ////////////////////////////////////////////////////////////////////////
357 SGPropertyNode::get_bool () const
360 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
362 return _local_val.bool_val;
366 SGPropertyNode::get_int () const
369 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
371 return _local_val.int_val;
375 SGPropertyNode::get_long () const
378 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
380 return _local_val.long_val;
384 SGPropertyNode::get_float () const
387 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
389 return _local_val.float_val;
393 SGPropertyNode::get_double () const
396 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
398 return _local_val.double_val;
402 SGPropertyNode::get_string () const
405 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
407 return _local_val.string_val;
411 SGPropertyNode::set_bool (bool val)
414 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
421 _local_val.bool_val = val;
428 SGPropertyNode::set_int (int val)
431 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
438 _local_val.int_val = val;
445 SGPropertyNode::set_long (long val)
448 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
455 _local_val.long_val = val;
462 SGPropertyNode::set_float (float val)
465 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
472 _local_val.float_val = val;
479 SGPropertyNode::set_double (double val)
482 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
489 _local_val.double_val = val;
496 SGPropertyNode::set_string (const char * val)
499 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
506 delete [] _local_val.string_val;
507 _local_val.string_val = copy_string(val);
514 SGPropertyNode::clearValue ()
516 if (_type == props::ALIAS) {
519 } else if (_type != props::NONE) {
522 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
525 _local_val.int_val = SGRawValue<int>::DefaultValue();
528 _local_val.long_val = SGRawValue<long>::DefaultValue();
531 _local_val.float_val = SGRawValue<float>::DefaultValue();
534 _local_val.double_val = SGRawValue<double>::DefaultValue();
537 case props::UNSPECIFIED:
539 delete [] _local_val.string_val;
541 _local_val.string_val = 0;
543 default: // avoid compiler warning
555 * Get the value as a string.
558 SGPropertyNode::make_string () const
560 if (!getAttribute(READ))
564 return _value.alias->getStringValue();
566 return get_bool() ? "true" : "false";
568 case props::UNSPECIFIED:
587 sstr << std::setprecision(10) << get_double();
589 case props::EXTENDED:
591 props::Type realType = _value.val->getType();
592 // Perhaps this should be done for all types?
593 if (realType == props::VEC3D || realType == props::VEC4D)
595 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
601 _buffer = sstr.str();
602 return _buffer.c_str();
606 * Trace a write access for a property.
609 SGPropertyNode::trace_write () const
612 cerr << "TRACE: Write node " << getPath () << ", value \""
613 << make_string() << '"' << endl;
615 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
616 << ", value \"" << make_string() << '"');
621 * Trace a read access for a property.
624 SGPropertyNode::trace_read () const
627 cerr << "TRACE: Write node " << getPath () << ", value \""
628 << make_string() << '"' << endl;
630 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
631 << ", value \"" << make_string() << '"');
636 ////////////////////////////////////////////////////////////////////////
637 // Public methods from SGPropertyNode.
638 ////////////////////////////////////////////////////////////////////////
641 * Last used attribute
642 * Update as needed when enum Attribute is changed
644 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
647 * Default constructor: always creates a root node.
649 SGPropertyNode::SGPropertyNode ()
657 _local_val.string_val = 0;
665 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
666 : SGReferenced(node),
669 _parent(0), // don't copy the parent
673 _listeners(0) // CHECK!!
675 _local_val.string_val = 0;
677 if (_type == props::NONE)
679 if (_type == props::ALIAS) {
680 _value.alias = node._value.alias;
685 if (_tied || _type == props::EXTENDED) {
686 _value.val = node._value.val->clone();
691 set_bool(node.get_bool());
694 set_int(node.get_int());
697 set_long(node.get_long());
700 set_float(node.get_float());
703 set_double(node.get_double());
706 case props::UNSPECIFIED:
707 set_string(node.get_string());
716 * Convenience constructor.
718 template<typename Itr>
719 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
721 SGPropertyNode * parent)
730 _local_val.string_val = 0;
732 if (!validateName(_name))
733 throw string("plain name expected instead of '") + _name + '\'';
736 SGPropertyNode::SGPropertyNode (const string& name,
738 SGPropertyNode * parent)
747 _local_val.string_val = 0;
749 if (!validateName(name))
750 throw string("plain name expected instead of '") + _name + '\'';
756 SGPropertyNode::~SGPropertyNode ()
758 // zero out all parent pointers, else they might be dangling
759 for (unsigned i = 0; i < _children.size(); ++i)
760 _children[i]->_parent = 0;
761 for (unsigned i = 0; i < _removedChildren.size(); ++i)
762 _removedChildren[i]->_parent = 0;
766 vector<SGPropertyChangeListener*>::iterator it;
767 for (it = _listeners->begin(); it != _listeners->end(); ++it)
768 (*it)->unregister_property(this);
775 * Alias to another node.
778 SGPropertyNode::alias (SGPropertyNode * target)
780 if (target && (_type != props::ALIAS) && (!_tied))
784 _value.alias = target;
785 _type = props::ALIAS;
793 SG_LOG(SG_GENERAL, SG_ALERT,
794 "Failed to create alias for " << getPath() << ". "
795 "The target property does not exist.");
798 if (_type == props::ALIAS)
800 if (_value.alias == target)
801 return true; // ok, identical alias requested
802 SG_LOG(SG_GENERAL, SG_ALERT,
803 "Failed to create alias at " << target->getPath() << ". "
804 "Source "<< getPath() << " is already aliasing another property.");
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));