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 SG_LOG(SG_GENERAL, SG_ALERT,
801 "Failed to create alias at " << target->getPath() << ". "
802 "Source "<< getPath() << " is also an alias. Unsupported recursion.");
807 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
808 "Source " << getPath() << " is a tied property.");
817 * Alias to another node by path.
820 SGPropertyNode::alias (const char * path)
822 return alias(getNode(path, true));
830 SGPropertyNode::unalias ()
832 if (_type != props::ALIAS)
840 * Get the target of an alias.
843 SGPropertyNode::getAliasTarget ()
845 return (_type == props::ALIAS ? _value.alias : 0);
849 const SGPropertyNode *
850 SGPropertyNode::getAliasTarget () const
852 return (_type == props::ALIAS ? _value.alias : 0);
856 * create a non-const child by name after the last node with the same name.
859 SGPropertyNode::addChild (const char * name)
861 int pos = find_last_child(name, _children)+1;
863 SGPropertyNode_ptr node;
864 node = new SGPropertyNode(name, name + strlen(name), pos, this);
865 _children.push_back(node);
866 fireChildAdded(node);
872 * Get a non-const child by index.
875 SGPropertyNode::getChild (int position)
877 if (position >= 0 && position < nChildren())
878 return _children[position];
885 * Get a const child by index.
887 const SGPropertyNode *
888 SGPropertyNode::getChild (int position) const
890 if (position >= 0 && position < nChildren())
891 return _children[position];
898 * Get a non-const child by name and index, creating if necessary.
902 SGPropertyNode::getChild (const char * name, int index, bool create)
904 return getChildImpl(name, name + strlen(name), index, create);
908 SGPropertyNode::getChild (const string& name, int index, bool create)
910 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
915 node = new SGPropertyNode(name, index, this);
916 _children.push_back(node);
917 fireChildAdded(node);
925 * Get a const child by name and index.
927 const SGPropertyNode *
928 SGPropertyNode::getChild (const char * name, int index) const
930 int pos = find_child(name, name + strlen(name), index, _children);
932 return _children[pos];
939 * Get all children with the same name (but different indices).
942 SGPropertyNode::getChildren (const char * name) const
944 PropertyList children;
945 int max = _children.size();
947 for (int i = 0; i < max; i++)
948 if (compare_strings(_children[i]->getName(), name))
949 children.push_back(_children[i]);
951 sort(children.begin(), children.end(), CompareIndices());
957 * Remove child by position.
960 SGPropertyNode::removeChild (int pos, bool keep)
962 SGPropertyNode_ptr node;
963 if (pos < 0 || pos >= (int)_children.size())
966 PropertyList::iterator it = _children.begin();
968 node = _children[pos];
971 _removedChildren.push_back(node);
974 node->setAttribute(REMOVED, true);
976 fireChildRemoved(node);
982 * Remove a child node
985 SGPropertyNode::removeChild (const char * name, int index, bool keep)
987 SGPropertyNode_ptr ret;
988 int pos = find_child(name, name + strlen(name), index, _children);
990 ret = removeChild(pos, keep);
996 * Remove all children with the specified name.
999 SGPropertyNode::removeChildren (const char * name, bool keep)
1001 PropertyList children;
1003 for (int pos = _children.size() - 1; pos >= 0; pos--)
1004 if (compare_strings(_children[pos]->getName(), name))
1005 children.push_back(removeChild(pos, keep));
1007 sort(children.begin(), children.end(), CompareIndices());
1012 SGPropertyNode::getDisplayName (bool simplify) const
1014 string display_name = _name;
1015 if (_index != 0 || !simplify) {
1017 sstr << '[' << _index << ']';
1018 display_name += sstr.str();
1020 return display_name;
1024 SGPropertyNode::getPath (bool simplify) const
1026 typedef std::vector<SGConstPropertyNode_ptr> PList;
1028 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1029 pathList.push_back(node);
1031 for (PList::reverse_iterator itr = pathList.rbegin(),
1032 rend = pathList.rend();
1036 result += (*itr)->getDisplayName(simplify);
1042 SGPropertyNode::getType () const
1044 if (_type == props::ALIAS)
1045 return _value.alias->getType();
1046 else if (_type == props::EXTENDED)
1047 return _value.val->getType();
1054 SGPropertyNode::getBoolValue () const
1056 // Shortcut for common case
1057 if (_attr == (READ|WRITE) && _type == props::BOOL)
1060 if (getAttribute(TRACE_READ))
1062 if (!getAttribute(READ))
1063 return SGRawValue<bool>::DefaultValue();
1066 return _value.alias->getBoolValue();
1070 return get_int() == 0 ? false : true;
1072 return get_long() == 0L ? false : true;
1074 return get_float() == 0.0 ? false : true;
1076 return get_double() == 0.0L ? false : true;
1078 case props::UNSPECIFIED:
1079 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1082 return SGRawValue<bool>::DefaultValue();
1087 SGPropertyNode::getIntValue () const
1089 // Shortcut for common case
1090 if (_attr == (READ|WRITE) && _type == props::INT)
1093 if (getAttribute(TRACE_READ))
1095 if (!getAttribute(READ))
1096 return SGRawValue<int>::DefaultValue();
1099 return _value.alias->getIntValue();
1101 return int(get_bool());
1105 return int(get_long());
1107 return int(get_float());
1109 return int(get_double());
1111 case props::UNSPECIFIED:
1112 return atoi(get_string());
1115 return SGRawValue<int>::DefaultValue();
1120 SGPropertyNode::getLongValue () const
1122 // Shortcut for common case
1123 if (_attr == (READ|WRITE) && _type == props::LONG)
1126 if (getAttribute(TRACE_READ))
1128 if (!getAttribute(READ))
1129 return SGRawValue<long>::DefaultValue();
1132 return _value.alias->getLongValue();
1134 return long(get_bool());
1136 return long(get_int());
1140 return long(get_float());
1142 return long(get_double());
1144 case props::UNSPECIFIED:
1145 return strtol(get_string(), 0, 0);
1148 return SGRawValue<long>::DefaultValue();
1153 SGPropertyNode::getFloatValue () const
1155 // Shortcut for common case
1156 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1159 if (getAttribute(TRACE_READ))
1161 if (!getAttribute(READ))
1162 return SGRawValue<float>::DefaultValue();
1165 return _value.alias->getFloatValue();
1167 return float(get_bool());
1169 return float(get_int());
1171 return float(get_long());
1175 return float(get_double());
1177 case props::UNSPECIFIED:
1178 return atof(get_string());
1181 return SGRawValue<float>::DefaultValue();
1186 SGPropertyNode::getDoubleValue () const
1188 // Shortcut for common case
1189 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1190 return get_double();
1192 if (getAttribute(TRACE_READ))
1194 if (!getAttribute(READ))
1195 return SGRawValue<double>::DefaultValue();
1199 return _value.alias->getDoubleValue();
1201 return double(get_bool());
1203 return double(get_int());
1205 return double(get_long());
1207 return double(get_float());
1209 return get_double();
1211 case props::UNSPECIFIED:
1212 return strtod(get_string(), 0);
1215 return SGRawValue<double>::DefaultValue();
1220 SGPropertyNode::getStringValue () const
1222 // Shortcut for common case
1223 if (_attr == (READ|WRITE) && _type == props::STRING)
1224 return get_string();
1226 if (getAttribute(TRACE_READ))
1228 if (!getAttribute(READ))
1229 return SGRawValue<const char *>::DefaultValue();
1230 return make_string();
1234 SGPropertyNode::setBoolValue (bool value)
1236 // Shortcut for common case
1237 if (_attr == (READ|WRITE) && _type == props::BOOL)
1238 return set_bool(value);
1240 bool result = false;
1242 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1245 _type = props::BOOL;
1250 result = _value.alias->setBoolValue(value);
1253 result = set_bool(value);
1256 result = set_int(int(value));
1259 result = set_long(long(value));
1262 result = set_float(float(value));
1265 result = set_double(double(value));
1268 case props::UNSPECIFIED:
1269 result = set_string(value ? "true" : "false");
1276 if (getAttribute(TRACE_WRITE))
1282 SGPropertyNode::setIntValue (int value)
1284 // Shortcut for common case
1285 if (_attr == (READ|WRITE) && _type == props::INT)
1286 return set_int(value);
1288 bool result = false;
1290 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1293 _local_val.int_val = 0;
1298 result = _value.alias->setIntValue(value);
1301 result = set_bool(value == 0 ? false : true);
1304 result = set_int(value);
1307 result = set_long(long(value));
1310 result = set_float(float(value));
1313 result = set_double(double(value));
1316 case props::UNSPECIFIED: {
1318 sprintf(buf, "%d", value);
1319 result = set_string(buf);
1327 if (getAttribute(TRACE_WRITE))
1333 SGPropertyNode::setLongValue (long value)
1335 // Shortcut for common case
1336 if (_attr == (READ|WRITE) && _type == props::LONG)
1337 return set_long(value);
1339 bool result = false;
1341 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1343 _type = props::LONG;
1344 _local_val.long_val = 0L;
1349 result = _value.alias->setLongValue(value);
1352 result = set_bool(value == 0L ? false : true);
1355 result = set_int(int(value));
1358 result = set_long(value);
1361 result = set_float(float(value));
1364 result = set_double(double(value));
1367 case props::UNSPECIFIED: {
1369 sprintf(buf, "%ld", value);
1370 result = set_string(buf);
1378 if (getAttribute(TRACE_WRITE))
1384 SGPropertyNode::setFloatValue (float value)
1386 // Shortcut for common case
1387 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1388 return set_float(value);
1390 bool result = false;
1392 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1394 _type = props::FLOAT;
1395 _local_val.float_val = 0;
1400 result = _value.alias->setFloatValue(value);
1403 result = set_bool(value == 0.0 ? false : true);
1406 result = set_int(int(value));
1409 result = set_long(long(value));
1412 result = set_float(value);
1415 result = set_double(double(value));
1418 case props::UNSPECIFIED: {
1420 sprintf(buf, "%f", value);
1421 result = set_string(buf);
1429 if (getAttribute(TRACE_WRITE))
1435 SGPropertyNode::setDoubleValue (double value)
1437 // Shortcut for common case
1438 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1439 return set_double(value);
1441 bool result = false;
1443 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1445 _local_val.double_val = value;
1446 _type = props::DOUBLE;
1451 result = _value.alias->setDoubleValue(value);
1454 result = set_bool(value == 0.0L ? false : true);
1457 result = set_int(int(value));
1460 result = set_long(long(value));
1463 result = set_float(float(value));
1466 result = set_double(value);
1469 case props::UNSPECIFIED: {
1471 sprintf(buf, "%f", value);
1472 result = set_string(buf);
1480 if (getAttribute(TRACE_WRITE))
1486 SGPropertyNode::setStringValue (const char * value)
1488 // Shortcut for common case
1489 if (_attr == (READ|WRITE) && _type == props::STRING)
1490 return set_string(value);
1492 bool result = false;
1494 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1496 _type = props::STRING;
1501 result = _value.alias->setStringValue(value);
1504 result = set_bool((compare_strings(value, "true")
1505 || atoi(value)) ? true : false);
1508 result = set_int(atoi(value));
1511 result = set_long(strtol(value, 0, 0));
1514 result = set_float(atof(value));
1517 result = set_double(strtod(value, 0));
1520 case props::UNSPECIFIED:
1521 result = set_string(value);
1523 case props::EXTENDED:
1525 stringstream sstr(value);
1526 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1534 if (getAttribute(TRACE_WRITE))
1540 SGPropertyNode::setUnspecifiedValue (const char * value)
1542 bool result = false;
1544 if (_type == props::NONE) {
1546 _type = props::UNSPECIFIED;
1548 props::Type type = _type;
1549 if (type == props::EXTENDED)
1550 type = _value.val->getType();
1553 result = _value.alias->setUnspecifiedValue(value);
1556 result = set_bool((compare_strings(value, "true")
1557 || atoi(value)) ? true : false);
1560 result = set_int(atoi(value));
1563 result = set_long(strtol(value, 0, 0));
1566 result = set_float(atof(value));
1569 result = set_double(strtod(value, 0));
1572 case props::UNSPECIFIED:
1573 result = set_string(value);
1576 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1579 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1586 if (getAttribute(TRACE_WRITE))
1591 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1593 if (!getAttribute(READ))
1597 return _value.alias->printOn(stream);
1599 stream << (get_bool() ? "true" : "false");
1602 stream << get_int();
1605 stream << get_long();
1608 stream << get_float();
1611 stream << get_double();
1614 case props::UNSPECIFIED:
1615 stream << get_string();
1617 case props::EXTENDED:
1618 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1622 default: // avoid compiler warning
1629 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1632 if (_type == props::ALIAS || _tied)
1635 useDefault = useDefault && hasValue();
1636 std::string old_val;
1638 old_val = getStringValue();
1640 _type = props::STRING;
1642 _value.val = rawValue.clone();
1645 int save_attributes = getAttributes();
1646 setAttribute( WRITE, true );
1647 setStringValue(old_val.c_str());
1648 setAttributes( save_attributes );
1654 SGPropertyNode::untie ()
1661 bool val = getBoolValue();
1663 _type = props::BOOL;
1664 _local_val.bool_val = val;
1668 int val = getIntValue();
1671 _local_val.int_val = val;
1675 long val = getLongValue();
1677 _type = props::LONG;
1678 _local_val.long_val = val;
1681 case props::FLOAT: {
1682 float val = getFloatValue();
1684 _type = props::FLOAT;
1685 _local_val.float_val = val;
1688 case props::DOUBLE: {
1689 double val = getDoubleValue();
1691 _type = props::DOUBLE;
1692 _local_val.double_val = val;
1696 case props::UNSPECIFIED: {
1697 string val = getStringValue();
1699 _type = props::STRING;
1700 _local_val.string_val = copy_string(val.c_str());
1703 case props::EXTENDED: {
1704 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1705 _value.val = 0; // Prevent clearValue() from deleting
1707 _type = props::EXTENDED;
1708 _value.val = val->makeContainer();
1722 SGPropertyNode::getRootNode ()
1727 return _parent->getRootNode();
1730 const SGPropertyNode *
1731 SGPropertyNode::getRootNode () const
1736 return _parent->getRootNode();
1740 SGPropertyNode::getNode (const char * relative_path, bool create)
1742 using namespace boost;
1744 return find_node(this, make_iterator_range(relative_path, relative_path
1745 + strlen(relative_path)),
1750 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1752 using namespace boost;
1753 return find_node(this, make_iterator_range(relative_path, relative_path
1754 + strlen(relative_path)),
1758 const SGPropertyNode *
1759 SGPropertyNode::getNode (const char * relative_path) const
1761 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1764 const SGPropertyNode *
1765 SGPropertyNode::getNode (const char * relative_path, int index) const
1767 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1771 ////////////////////////////////////////////////////////////////////////
1772 // Convenience methods using relative paths.
1773 ////////////////////////////////////////////////////////////////////////
1777 * Test whether another node has a value attached.
1780 SGPropertyNode::hasValue (const char * relative_path) const
1782 const SGPropertyNode * node = getNode(relative_path);
1783 return (node == 0 ? false : node->hasValue());
1788 * Get the value type for another node.
1791 SGPropertyNode::getType (const char * relative_path) const
1793 const SGPropertyNode * node = getNode(relative_path);
1794 return (node == 0 ? props::UNSPECIFIED : node->getType());
1799 * Get a bool value for another node.
1802 SGPropertyNode::getBoolValue (const char * relative_path,
1803 bool defaultValue) const
1805 const SGPropertyNode * node = getNode(relative_path);
1806 return (node == 0 ? defaultValue : node->getBoolValue());
1811 * Get an int value for another node.
1814 SGPropertyNode::getIntValue (const char * relative_path,
1815 int defaultValue) const
1817 const SGPropertyNode * node = getNode(relative_path);
1818 return (node == 0 ? defaultValue : node->getIntValue());
1823 * Get a long value for another node.
1826 SGPropertyNode::getLongValue (const char * relative_path,
1827 long defaultValue) const
1829 const SGPropertyNode * node = getNode(relative_path);
1830 return (node == 0 ? defaultValue : node->getLongValue());
1835 * Get a float value for another node.
1838 SGPropertyNode::getFloatValue (const char * relative_path,
1839 float defaultValue) const
1841 const SGPropertyNode * node = getNode(relative_path);
1842 return (node == 0 ? defaultValue : node->getFloatValue());
1847 * Get a double value for another node.
1850 SGPropertyNode::getDoubleValue (const char * relative_path,
1851 double defaultValue) const
1853 const SGPropertyNode * node = getNode(relative_path);
1854 return (node == 0 ? defaultValue : node->getDoubleValue());
1859 * Get a string value for another node.
1862 SGPropertyNode::getStringValue (const char * relative_path,
1863 const char * defaultValue) const
1865 const SGPropertyNode * node = getNode(relative_path);
1866 return (node == 0 ? defaultValue : node->getStringValue());
1871 * Set a bool value for another node.
1874 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1876 return getNode(relative_path, true)->setBoolValue(value);
1881 * Set an int value for another node.
1884 SGPropertyNode::setIntValue (const char * relative_path, int value)
1886 return getNode(relative_path, true)->setIntValue(value);
1891 * Set a long value for another node.
1894 SGPropertyNode::setLongValue (const char * relative_path, long value)
1896 return getNode(relative_path, true)->setLongValue(value);
1901 * Set a float value for another node.
1904 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1906 return getNode(relative_path, true)->setFloatValue(value);
1911 * Set a double value for another node.
1914 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1916 return getNode(relative_path, true)->setDoubleValue(value);
1921 * Set a string value for another node.
1924 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1926 return getNode(relative_path, true)->setStringValue(value);
1931 * Set an unknown value for another node.
1934 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1937 return getNode(relative_path, true)->setUnspecifiedValue(value);
1942 * Test whether another node is tied.
1945 SGPropertyNode::isTied (const char * relative_path) const
1947 const SGPropertyNode * node = getNode(relative_path);
1948 return (node == 0 ? false : node->isTied());
1953 * Tie a node reached by a relative path, creating it if necessary.
1956 SGPropertyNode::tie (const char * relative_path,
1957 const SGRawValue<bool> &rawValue,
1960 return getNode(relative_path, true)->tie(rawValue, useDefault);
1965 * Tie a node reached by a relative path, creating it if necessary.
1968 SGPropertyNode::tie (const char * relative_path,
1969 const SGRawValue<int> &rawValue,
1972 return getNode(relative_path, true)->tie(rawValue, useDefault);
1977 * Tie a node reached by a relative path, creating it if necessary.
1980 SGPropertyNode::tie (const char * relative_path,
1981 const SGRawValue<long> &rawValue,
1984 return getNode(relative_path, true)->tie(rawValue, useDefault);
1989 * Tie a node reached by a relative path, creating it if necessary.
1992 SGPropertyNode::tie (const char * relative_path,
1993 const SGRawValue<float> &rawValue,
1996 return getNode(relative_path, true)->tie(rawValue, useDefault);
2001 * Tie a node reached by a relative path, creating it if necessary.
2004 SGPropertyNode::tie (const char * relative_path,
2005 const SGRawValue<double> &rawValue,
2008 return getNode(relative_path, true)->tie(rawValue, useDefault);
2013 * Tie a node reached by a relative path, creating it if necessary.
2016 SGPropertyNode::tie (const char * relative_path,
2017 const SGRawValue<const char *> &rawValue,
2020 return getNode(relative_path, true)->tie(rawValue, useDefault);
2025 * Attempt to untie another node reached by a relative path.
2028 SGPropertyNode::untie (const char * relative_path)
2030 SGPropertyNode * node = getNode(relative_path);
2031 return (node == 0 ? false : node->untie());
2035 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2038 if (_listeners == 0)
2039 _listeners = new vector<SGPropertyChangeListener*>;
2040 _listeners->push_back(listener);
2041 listener->register_property(this);
2043 listener->valueChanged(this);
2047 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2049 if (_listeners == 0)
2051 vector<SGPropertyChangeListener*>::iterator it =
2052 find(_listeners->begin(), _listeners->end(), listener);
2053 if (it != _listeners->end()) {
2054 _listeners->erase(it);
2055 listener->unregister_property(this);
2056 if (_listeners->empty()) {
2057 vector<SGPropertyChangeListener*>* tmp = _listeners;
2065 SGPropertyNode::fireValueChanged ()
2067 fireValueChanged(this);
2071 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2073 fireChildAdded(this, child);
2077 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2079 fireChildRemoved(this, child);
2083 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2085 if (_listeners != 0) {
2086 for (unsigned int i = 0; i < _listeners->size(); i++) {
2087 (*_listeners)[i]->valueChanged(node);
2091 _parent->fireValueChanged(node);
2095 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2096 SGPropertyNode * child)
2098 if (_listeners != 0) {
2099 for (unsigned int i = 0; i < _listeners->size(); i++) {
2100 (*_listeners)[i]->childAdded(parent, child);
2104 _parent->fireChildAdded(parent, child);
2108 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2109 SGPropertyNode * child)
2111 if (_listeners != 0) {
2112 for (unsigned int i = 0; i < _listeners->size(); i++) {
2113 (*_listeners)[i]->childRemoved(parent, child);
2117 _parent->fireChildRemoved(parent, child);
2121 ////////////////////////////////////////////////////////////////////////
2122 // Implementation of SGPropertyChangeListener.
2123 ////////////////////////////////////////////////////////////////////////
2125 SGPropertyChangeListener::~SGPropertyChangeListener ()
2127 for (int i = _properties.size() - 1; i >= 0; i--)
2128 _properties[i]->removeChangeListener(this);
2132 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2138 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2139 SGPropertyNode * child)
2145 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2146 SGPropertyNode * child)
2152 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2154 _properties.push_back(node);
2158 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2160 vector<SGPropertyNode *>::iterator it =
2161 find(_properties.begin(), _properties.end(), node);
2162 if (it != _properties.end())
2163 _properties.erase(it);
2167 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2170 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2171 for (int i = 0; i < 3; ++i) {
2182 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2184 for (int i = 0; i < 3; ++i) {
2185 stream >> result[i];
2191 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2194 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2195 for (int i = 0; i < 4; ++i) {
2206 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2208 for (int i = 0; i < 4; ++i) {
2209 stream >> result[i];
2216 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2218 props::Type ltype = lhs.getType();
2219 props::Type rtype = rhs.getType();
2226 return false; // XXX Should we look in aliases?
2228 return lhs.getValue<bool>() == rhs.getValue<bool>();
2230 return lhs.getValue<int>() == rhs.getValue<int>();
2232 return lhs.getValue<long>() == rhs.getValue<long>();
2234 return lhs.getValue<float>() == rhs.getValue<float>();
2236 return lhs.getValue<double>() == rhs.getValue<double>();
2238 case props::UNSPECIFIED:
2239 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2241 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2243 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2251 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2252 const SGPropertyNode& rhs)
2256 int lhsChildren = lhs.nChildren();
2257 int rhsChildren = rhs.nChildren();
2258 if (lhsChildren != rhsChildren)
2260 if (lhsChildren == 0)
2261 return compareNodeValue(lhs, rhs);
2262 for (size_t i = 0; i < lhs._children.size(); ++i) {
2263 const SGPropertyNode* lchild = lhs._children[i];
2264 const SGPropertyNode* rchild = rhs._children[i];
2265 // I'm guessing that the nodes will usually be in the same
2267 if (lchild->getIndex() != rchild->getIndex()
2268 || lchild->getNameString() != rchild->getNameString()) {
2270 for (PropertyList::const_iterator itr = rhs._children.begin(),
2271 end = rhs._children.end();
2274 if (lchild->getIndex() == (*itr)->getIndex()
2275 && lchild->getNameString() == (*itr)->getNameString()) {
2282 if (!compare(*lchild, *rchild))
2288 struct PropertyPlaceLess {
2289 typedef bool result_type;
2290 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2292 int comp = lhs->getNameString().compare(rhs->getNameString());
2294 return lhs->getIndex() < rhs->getIndex();
2300 size_t hash_value(const SGPropertyNode& node)
2302 using namespace boost;
2304 if (node.nChildren() == 0) {
2305 switch (node.getType()) {
2310 return hash_value(node.getValue<bool>());
2312 return hash_value(node.getValue<int>());
2314 return hash_value(node.getValue<long>());
2316 return hash_value(node.getValue<float>());
2318 return hash_value(node.getValue<double>());
2320 case props::UNSPECIFIED:
2322 const char *val = node.getStringValue();
2323 return hash_range(val, val + strlen(val));
2327 const SGVec3d val = node.getValue<SGVec3d>();
2328 return hash_range(&val[0], &val[3]);
2332 const SGVec4d val = node.getValue<SGVec4d>();
2333 return hash_range(&val[0], &val[4]);
2335 case props::ALIAS: // XXX Should we look in aliases?
2341 PropertyList children(node._children.begin(), node._children.end());
2342 sort(children.begin(), children.end(), PropertyPlaceLess());
2343 for (PropertyList::const_iterator itr = children.begin(),
2344 end = children.end();
2347 hash_combine(seed, (*itr)->_name);
2348 hash_combine(seed, (*itr)->_index);
2349 hash_combine(seed, hash_value(**itr));