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"
25 #include <boost/algorithm/string/find_iterator.hpp>
26 #include <boost/algorithm/string/predicate.hpp>
27 #include <boost/algorithm/string/classification.hpp>
28 #include <boost/bind.hpp>
29 #include <boost/functional/hash.hpp>
30 #include <boost/range.hpp>
36 #include <simgear/compiler.h>
37 #include <simgear/debug/logstream.hxx>
39 #if ( _MSC_VER == 1200 )
40 // MSVC 6 is buggy, and needs something strange here
41 using std::vector<SGPropertyNode_ptr>;
42 using std::vector<SGPropertyChangeListener *>;
43 using std::vector<SGPropertyNode *>;
55 using std::stringstream;
57 using namespace simgear;
60 ////////////////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////////////////
65 * Comparator class for sorting by index.
70 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
71 return (n1->getIndex() < n2->getIndex());
77 ////////////////////////////////////////////////////////////////////////
78 // Convenience macros for value access.
79 ////////////////////////////////////////////////////////////////////////
81 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
82 #define TEST_WRITE if (!getAttribute(WRITE)) return false
85 ////////////////////////////////////////////////////////////////////////
86 // Local path normalization code.
87 ////////////////////////////////////////////////////////////////////////
90 * Parse the name for a path component.
92 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
95 template<typename Range>
97 parse_name (const SGPropertyNode *node, const Range &path)
99 typename Range::iterator i = path.begin();
100 typename Range::iterator max = path.end();
104 if (i != path.end() && *i == '.') {
107 if (i != max && *i != '/')
108 throw string("illegal character after . or ..");
109 } else if (isalpha(*i) || *i == '_') {
112 // The rules inside a name are a little
115 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
116 *i == '-' || *i == '.') {
118 } else if (*i == '[' || *i == '/') {
123 err.append("' found in propertyname after '"+node->getNameString()+"'");
124 err.append("\nname may contain only ._- and alphanumeric characters");
132 if (path.begin() == i) {
135 err.append("' found in propertyname after '"+node->getNameString()+"'");
136 err.append("\nname must begin with alpha or '_'");
140 return Range(path.begin(), i);
143 // Validate the name of a single node
144 inline bool validateName(const string& name)
146 using namespace boost;
149 if (!isalpha(name[0]) && name[0] != '_')
151 return all(make_iterator_range(name.begin(), name.end()),
152 is_alnum() || is_any_of("_-."));
156 ////////////////////////////////////////////////////////////////////////
157 // Other static utility functions.
158 ////////////////////////////////////////////////////////////////////////
162 copy_string (const char * s)
164 unsigned long int slen = strlen(s);
165 char * copy = new char[slen + 1];
167 // the source string length is known so no need to check for '\0'
168 // when copying every single character
169 memcpy(copy, s, slen);
170 *(copy + slen) = '\0';
175 compare_strings (const char * s1, const char * s2)
177 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
181 * Locate a child node by name and index.
183 template<typename Itr>
185 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
187 int nNodes = nodes.size();
188 boost::iterator_range<Itr> name(begin, end);
189 for (int i = 0; i < nNodes; i++) {
190 SGPropertyNode * node = nodes[i];
192 // searching for a mathing index is a lot less time consuming than
193 // comparing two strings so do that first.
194 if (node->getIndex() == index && boost::equals(node->getName(), name))
201 * Locate the child node with the highest index of the same name
204 find_last_child (const char * name, const PropertyList& nodes)
206 int nNodes = nodes.size();
209 for (int i = 0; i < nNodes; i++) {
210 SGPropertyNode * node = nodes[i];
211 if (compare_strings(node->getName(), name))
213 int idx = node->getIndex();
214 if (idx > index) index = idx;
221 * Get first unused index for child nodes with the given name
224 first_unused_index( const char * name,
225 const PropertyList& nodes,
228 const char* nameEnd = name + strlen(name);
230 for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
232 if( find_child(name, nameEnd, index, nodes) < 0 )
236 SG_LOG(SG_GENERAL, SG_ALERT, "Too much nodes: " << name);
240 template<typename Itr>
241 inline SGPropertyNode*
242 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
244 int pos = find_child(begin, end, index, _children);
246 return _children[pos];
248 SGPropertyNode_ptr node;
249 pos = find_child(begin, end, index, _removedChildren);
251 PropertyList::iterator it = _removedChildren.begin();
253 node = _removedChildren[pos];
254 _removedChildren.erase(it);
255 node->setAttribute(REMOVED, false);
256 _children.push_back(node);
257 fireChildAdded(node);
264 template<typename Itr>
266 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
268 SGPropertyNode* node = getExistingChild(begin, end, index, create);
273 node = new SGPropertyNode(begin, end, index, this);
274 _children.push_back(node);
275 fireChildAdded(node);
282 template<typename SplitItr>
284 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
287 typedef typename SplitItr::value_type Range;
288 // Run off the end of the list
293 // Success! This is the one we want.
297 // Empty name at this point is empty, not root.
299 return find_node_aux(current, ++itr, create, last_index);
300 Range name = parse_name(current, token);
301 if (equals(name, "."))
302 return find_node_aux(current, ++itr, create, last_index);
303 if (equals(name, "..")) {
304 SGPropertyNode * parent = current->getParent();
306 throw string("attempt to move past root with '..'");
307 return find_node_aux(parent, ++itr, create, last_index);
310 if (last_index >= 0) {
311 // If we are at the last token and last_index is valid, use
312 // last_index as the index value
314 while (!(++itr).eof()) {
328 if (name.end() != token.end()) {
329 if (*name.end() == '[') {
330 typename Range::iterator i = name.end() + 1, end = token.end();
331 for (;i != end; ++i) {
333 index = (index * 10) + (*i - '0');
338 if (i == token.end() || *i != ']')
339 throw string("unterminated index (looking for ']')");
341 throw string("illegal characters in token: ")
342 + string(name.begin(), name.end());
346 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
347 index, create), itr, create,
351 // Internal function for parsing property paths. last_index provides
352 // and index value for the last node name token, if supplied.
353 template<typename Range>
355 find_node (SGPropertyNode * current,
360 using namespace boost;
361 typedef split_iterator<typename range_result_iterator<Range>::type>
364 PathSplitIterator itr
365 = make_split_iterator(path, first_finder("/", is_equal()));
366 if (*path.begin() == '/')
367 return find_node_aux(current->getRootNode(), itr, create, last_index);
369 return find_node_aux(current, itr, create, last_index);
373 ////////////////////////////////////////////////////////////////////////
374 // Private methods from SGPropertyNode (may be inlined for speed).
375 ////////////////////////////////////////////////////////////////////////
378 SGPropertyNode::get_bool () const
381 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
383 return _local_val.bool_val;
387 SGPropertyNode::get_int () const
390 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
392 return _local_val.int_val;
396 SGPropertyNode::get_long () const
399 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
401 return _local_val.long_val;
405 SGPropertyNode::get_float () const
408 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
410 return _local_val.float_val;
414 SGPropertyNode::get_double () const
417 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
419 return _local_val.double_val;
423 SGPropertyNode::get_string () const
426 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
428 return _local_val.string_val;
432 SGPropertyNode::set_bool (bool val)
435 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
442 _local_val.bool_val = val;
449 SGPropertyNode::set_int (int val)
452 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
459 _local_val.int_val = val;
466 SGPropertyNode::set_long (long val)
469 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
476 _local_val.long_val = val;
483 SGPropertyNode::set_float (float val)
486 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
493 _local_val.float_val = val;
500 SGPropertyNode::set_double (double val)
503 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
510 _local_val.double_val = val;
517 SGPropertyNode::set_string (const char * val)
520 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
527 delete [] _local_val.string_val;
528 _local_val.string_val = copy_string(val);
535 SGPropertyNode::clearValue ()
537 if (_type == props::ALIAS) {
540 } else if (_type != props::NONE) {
543 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
546 _local_val.int_val = SGRawValue<int>::DefaultValue();
549 _local_val.long_val = SGRawValue<long>::DefaultValue();
552 _local_val.float_val = SGRawValue<float>::DefaultValue();
555 _local_val.double_val = SGRawValue<double>::DefaultValue();
558 case props::UNSPECIFIED:
560 delete [] _local_val.string_val;
562 _local_val.string_val = 0;
564 default: // avoid compiler warning
576 * Get the value as a string.
579 SGPropertyNode::make_string () const
581 if (!getAttribute(READ))
585 return _value.alias->getStringValue();
587 return get_bool() ? "true" : "false";
589 case props::UNSPECIFIED:
608 sstr << std::setprecision(10) << get_double();
610 case props::EXTENDED:
612 props::Type realType = _value.val->getType();
613 // Perhaps this should be done for all types?
614 if (realType == props::VEC3D || realType == props::VEC4D)
616 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
622 _buffer = sstr.str();
623 return _buffer.c_str();
627 * Trace a write access for a property.
630 SGPropertyNode::trace_write () const
633 cerr << "TRACE: Write node " << getPath () << ", value \""
634 << make_string() << '"' << endl;
636 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
637 << ", value \"" << make_string() << '"');
642 * Trace a read access for a property.
645 SGPropertyNode::trace_read () const
648 cerr << "TRACE: Write node " << getPath () << ", value \""
649 << make_string() << '"' << endl;
651 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
652 << ", value \"" << make_string() << '"');
657 ////////////////////////////////////////////////////////////////////////
658 // Public methods from SGPropertyNode.
659 ////////////////////////////////////////////////////////////////////////
662 * Last used attribute
663 * Update as needed when enum Attribute is changed
665 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
668 * Default constructor: always creates a root node.
670 SGPropertyNode::SGPropertyNode ()
678 _local_val.string_val = 0;
686 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
687 : SGReferenced(node),
690 _parent(0), // don't copy the parent
694 _listeners(0) // CHECK!!
696 _local_val.string_val = 0;
698 if (_type == props::NONE)
700 if (_type == props::ALIAS) {
701 _value.alias = node._value.alias;
706 if (_tied || _type == props::EXTENDED) {
707 _value.val = node._value.val->clone();
712 set_bool(node.get_bool());
715 set_int(node.get_int());
718 set_long(node.get_long());
721 set_float(node.get_float());
724 set_double(node.get_double());
727 case props::UNSPECIFIED:
728 set_string(node.get_string());
737 * Convenience constructor.
739 template<typename Itr>
740 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
742 SGPropertyNode * parent)
751 _local_val.string_val = 0;
753 if (!validateName(_name))
754 throw string("plain name expected instead of '") + _name + '\'';
757 SGPropertyNode::SGPropertyNode (const string& name,
759 SGPropertyNode * parent)
768 _local_val.string_val = 0;
770 if (!validateName(name))
771 throw string("plain name expected instead of '") + _name + '\'';
777 SGPropertyNode::~SGPropertyNode ()
779 // zero out all parent pointers, else they might be dangling
780 for (unsigned i = 0; i < _children.size(); ++i)
781 _children[i]->_parent = 0;
782 for (unsigned i = 0; i < _removedChildren.size(); ++i)
783 _removedChildren[i]->_parent = 0;
787 vector<SGPropertyChangeListener*>::iterator it;
788 for (it = _listeners->begin(); it != _listeners->end(); ++it)
789 (*it)->unregister_property(this);
796 * Alias to another node.
799 SGPropertyNode::alias (SGPropertyNode * target)
801 if (target && (_type != props::ALIAS) && (!_tied))
805 _value.alias = target;
806 _type = props::ALIAS;
814 SG_LOG(SG_GENERAL, SG_ALERT,
815 "Failed to create alias for " << getPath() << ". "
816 "The target property does not exist.");
819 if (_type == props::ALIAS)
821 if (_value.alias == target)
822 return true; // ok, identical alias requested
823 SG_LOG(SG_GENERAL, SG_ALERT,
824 "Failed to create alias at " << target->getPath() << ". "
825 "Source "<< getPath() << " is already aliasing another property.");
830 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
831 "Source " << getPath() << " is a tied property.");
840 * Alias to another node by path.
843 SGPropertyNode::alias (const char * path)
845 return alias(getNode(path, true));
853 SGPropertyNode::unalias ()
855 if (_type != props::ALIAS)
863 * Get the target of an alias.
866 SGPropertyNode::getAliasTarget ()
868 return (_type == props::ALIAS ? _value.alias : 0);
872 const SGPropertyNode *
873 SGPropertyNode::getAliasTarget () const
875 return (_type == props::ALIAS ? _value.alias : 0);
879 * create a non-const child by name after the last node with the same name.
882 SGPropertyNode::addChild(const char * name, int min_index, bool append)
885 ? std::max(find_last_child(name, _children) + 1, min_index)
886 : first_unused_index(name, _children, min_index);
888 SGPropertyNode_ptr node;
889 node = new SGPropertyNode(name, name + strlen(name), pos, this);
890 _children.push_back(node);
891 fireChildAdded(node);
897 * Get a non-const child by index.
900 SGPropertyNode::getChild (int position)
902 if (position >= 0 && position < nChildren())
903 return _children[position];
910 * Get a const child by index.
912 const SGPropertyNode *
913 SGPropertyNode::getChild (int position) const
915 if (position >= 0 && position < nChildren())
916 return _children[position];
923 * Get a non-const child by name and index, creating if necessary.
927 SGPropertyNode::getChild (const char * name, int index, bool create)
929 return getChildImpl(name, name + strlen(name), index, create);
933 SGPropertyNode::getChild (const string& name, int index, bool create)
935 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
940 node = new SGPropertyNode(name, index, this);
941 _children.push_back(node);
942 fireChildAdded(node);
950 * Get a const child by name and index.
952 const SGPropertyNode *
953 SGPropertyNode::getChild (const char * name, int index) const
955 int pos = find_child(name, name + strlen(name), index, _children);
957 return _children[pos];
964 * Get all children with the same name (but different indices).
967 SGPropertyNode::getChildren (const char * name) const
969 PropertyList children;
970 int max = _children.size();
972 for (int i = 0; i < max; i++)
973 if (compare_strings(_children[i]->getName(), name))
974 children.push_back(_children[i]);
976 sort(children.begin(), children.end(), CompareIndices());
982 * Remove child by position.
985 SGPropertyNode::removeChild (int pos, bool keep)
987 SGPropertyNode_ptr node;
988 if (pos < 0 || pos >= (int)_children.size())
991 PropertyList::iterator it = _children.begin();
993 node = _children[pos];
996 _removedChildren.push_back(node);
999 node->setAttribute(REMOVED, true);
1001 fireChildRemoved(node);
1007 * Remove a child node
1010 SGPropertyNode::removeChild (const char * name, int index, bool keep)
1012 SGPropertyNode_ptr ret;
1013 int pos = find_child(name, name + strlen(name), index, _children);
1015 ret = removeChild(pos, keep);
1021 * Remove all children with the specified name.
1024 SGPropertyNode::removeChildren (const char * name, bool keep)
1026 PropertyList children;
1028 for (int pos = _children.size() - 1; pos >= 0; pos--)
1029 if (compare_strings(_children[pos]->getName(), name))
1030 children.push_back(removeChild(pos, keep));
1032 sort(children.begin(), children.end(), CompareIndices());
1037 SGPropertyNode::getDisplayName (bool simplify) const
1039 string display_name = _name;
1040 if (_index != 0 || !simplify) {
1042 sstr << '[' << _index << ']';
1043 display_name += sstr.str();
1045 return display_name;
1049 SGPropertyNode::getPath (bool simplify) const
1051 typedef std::vector<SGConstPropertyNode_ptr> PList;
1053 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1054 pathList.push_back(node);
1056 for (PList::reverse_iterator itr = pathList.rbegin(),
1057 rend = pathList.rend();
1061 result += (*itr)->getDisplayName(simplify);
1067 SGPropertyNode::getType () const
1069 if (_type == props::ALIAS)
1070 return _value.alias->getType();
1071 else if (_type == props::EXTENDED)
1072 return _value.val->getType();
1079 SGPropertyNode::getBoolValue () const
1081 // Shortcut for common case
1082 if (_attr == (READ|WRITE) && _type == props::BOOL)
1085 if (getAttribute(TRACE_READ))
1087 if (!getAttribute(READ))
1088 return SGRawValue<bool>::DefaultValue();
1091 return _value.alias->getBoolValue();
1095 return get_int() == 0 ? false : true;
1097 return get_long() == 0L ? false : true;
1099 return get_float() == 0.0 ? false : true;
1101 return get_double() == 0.0L ? false : true;
1103 case props::UNSPECIFIED:
1104 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1107 return SGRawValue<bool>::DefaultValue();
1112 SGPropertyNode::getIntValue () const
1114 // Shortcut for common case
1115 if (_attr == (READ|WRITE) && _type == props::INT)
1118 if (getAttribute(TRACE_READ))
1120 if (!getAttribute(READ))
1121 return SGRawValue<int>::DefaultValue();
1124 return _value.alias->getIntValue();
1126 return int(get_bool());
1130 return int(get_long());
1132 return int(get_float());
1134 return int(get_double());
1136 case props::UNSPECIFIED:
1137 return atoi(get_string());
1140 return SGRawValue<int>::DefaultValue();
1145 SGPropertyNode::getLongValue () const
1147 // Shortcut for common case
1148 if (_attr == (READ|WRITE) && _type == props::LONG)
1151 if (getAttribute(TRACE_READ))
1153 if (!getAttribute(READ))
1154 return SGRawValue<long>::DefaultValue();
1157 return _value.alias->getLongValue();
1159 return long(get_bool());
1161 return long(get_int());
1165 return long(get_float());
1167 return long(get_double());
1169 case props::UNSPECIFIED:
1170 return strtol(get_string(), 0, 0);
1173 return SGRawValue<long>::DefaultValue();
1178 SGPropertyNode::getFloatValue () const
1180 // Shortcut for common case
1181 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1184 if (getAttribute(TRACE_READ))
1186 if (!getAttribute(READ))
1187 return SGRawValue<float>::DefaultValue();
1190 return _value.alias->getFloatValue();
1192 return float(get_bool());
1194 return float(get_int());
1196 return float(get_long());
1200 return float(get_double());
1202 case props::UNSPECIFIED:
1203 return atof(get_string());
1206 return SGRawValue<float>::DefaultValue();
1211 SGPropertyNode::getDoubleValue () const
1213 // Shortcut for common case
1214 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1215 return get_double();
1217 if (getAttribute(TRACE_READ))
1219 if (!getAttribute(READ))
1220 return SGRawValue<double>::DefaultValue();
1224 return _value.alias->getDoubleValue();
1226 return double(get_bool());
1228 return double(get_int());
1230 return double(get_long());
1232 return double(get_float());
1234 return get_double();
1236 case props::UNSPECIFIED:
1237 return strtod(get_string(), 0);
1240 return SGRawValue<double>::DefaultValue();
1245 SGPropertyNode::getStringValue () const
1247 // Shortcut for common case
1248 if (_attr == (READ|WRITE) && _type == props::STRING)
1249 return get_string();
1251 if (getAttribute(TRACE_READ))
1253 if (!getAttribute(READ))
1254 return SGRawValue<const char *>::DefaultValue();
1255 return make_string();
1259 SGPropertyNode::setBoolValue (bool value)
1261 // Shortcut for common case
1262 if (_attr == (READ|WRITE) && _type == props::BOOL)
1263 return set_bool(value);
1265 bool result = false;
1267 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1270 _type = props::BOOL;
1275 result = _value.alias->setBoolValue(value);
1278 result = set_bool(value);
1281 result = set_int(int(value));
1284 result = set_long(long(value));
1287 result = set_float(float(value));
1290 result = set_double(double(value));
1293 case props::UNSPECIFIED:
1294 result = set_string(value ? "true" : "false");
1301 if (getAttribute(TRACE_WRITE))
1307 SGPropertyNode::setIntValue (int value)
1309 // Shortcut for common case
1310 if (_attr == (READ|WRITE) && _type == props::INT)
1311 return set_int(value);
1313 bool result = false;
1315 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1318 _local_val.int_val = 0;
1323 result = _value.alias->setIntValue(value);
1326 result = set_bool(value == 0 ? false : true);
1329 result = set_int(value);
1332 result = set_long(long(value));
1335 result = set_float(float(value));
1338 result = set_double(double(value));
1341 case props::UNSPECIFIED: {
1343 sprintf(buf, "%d", value);
1344 result = set_string(buf);
1352 if (getAttribute(TRACE_WRITE))
1358 SGPropertyNode::setLongValue (long value)
1360 // Shortcut for common case
1361 if (_attr == (READ|WRITE) && _type == props::LONG)
1362 return set_long(value);
1364 bool result = false;
1366 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1368 _type = props::LONG;
1369 _local_val.long_val = 0L;
1374 result = _value.alias->setLongValue(value);
1377 result = set_bool(value == 0L ? false : true);
1380 result = set_int(int(value));
1383 result = set_long(value);
1386 result = set_float(float(value));
1389 result = set_double(double(value));
1392 case props::UNSPECIFIED: {
1394 sprintf(buf, "%ld", value);
1395 result = set_string(buf);
1403 if (getAttribute(TRACE_WRITE))
1409 SGPropertyNode::setFloatValue (float value)
1411 // Shortcut for common case
1412 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1413 return set_float(value);
1415 bool result = false;
1417 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1419 _type = props::FLOAT;
1420 _local_val.float_val = 0;
1425 result = _value.alias->setFloatValue(value);
1428 result = set_bool(value == 0.0 ? false : true);
1431 result = set_int(int(value));
1434 result = set_long(long(value));
1437 result = set_float(value);
1440 result = set_double(double(value));
1443 case props::UNSPECIFIED: {
1445 sprintf(buf, "%f", value);
1446 result = set_string(buf);
1454 if (getAttribute(TRACE_WRITE))
1460 SGPropertyNode::setDoubleValue (double value)
1462 // Shortcut for common case
1463 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1464 return set_double(value);
1466 bool result = false;
1468 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1470 _local_val.double_val = value;
1471 _type = props::DOUBLE;
1476 result = _value.alias->setDoubleValue(value);
1479 result = set_bool(value == 0.0L ? false : true);
1482 result = set_int(int(value));
1485 result = set_long(long(value));
1488 result = set_float(float(value));
1491 result = set_double(value);
1494 case props::UNSPECIFIED: {
1496 sprintf(buf, "%f", value);
1497 result = set_string(buf);
1505 if (getAttribute(TRACE_WRITE))
1511 SGPropertyNode::setStringValue (const char * value)
1513 // Shortcut for common case
1514 if (_attr == (READ|WRITE) && _type == props::STRING)
1515 return set_string(value);
1517 bool result = false;
1519 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1521 _type = props::STRING;
1526 result = _value.alias->setStringValue(value);
1529 result = set_bool((compare_strings(value, "true")
1530 || atoi(value)) ? true : false);
1533 result = set_int(atoi(value));
1536 result = set_long(strtol(value, 0, 0));
1539 result = set_float(atof(value));
1542 result = set_double(strtod(value, 0));
1545 case props::UNSPECIFIED:
1546 result = set_string(value);
1548 case props::EXTENDED:
1550 stringstream sstr(value);
1551 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1559 if (getAttribute(TRACE_WRITE))
1565 SGPropertyNode::setUnspecifiedValue (const char * value)
1567 bool result = false;
1569 if (_type == props::NONE) {
1571 _type = props::UNSPECIFIED;
1573 props::Type type = _type;
1574 if (type == props::EXTENDED)
1575 type = _value.val->getType();
1578 result = _value.alias->setUnspecifiedValue(value);
1581 result = set_bool((compare_strings(value, "true")
1582 || atoi(value)) ? true : false);
1585 result = set_int(atoi(value));
1588 result = set_long(strtol(value, 0, 0));
1591 result = set_float(atof(value));
1594 result = set_double(strtod(value, 0));
1597 case props::UNSPECIFIED:
1598 result = set_string(value);
1601 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1604 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1611 if (getAttribute(TRACE_WRITE))
1616 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1618 if (!getAttribute(READ))
1622 return _value.alias->printOn(stream);
1624 stream << (get_bool() ? "true" : "false");
1627 stream << get_int();
1630 stream << get_long();
1633 stream << get_float();
1636 stream << get_double();
1639 case props::UNSPECIFIED:
1640 stream << get_string();
1642 case props::EXTENDED:
1643 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1647 default: // avoid compiler warning
1654 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1657 if (_type == props::ALIAS || _tied)
1660 useDefault = useDefault && hasValue();
1661 std::string old_val;
1663 old_val = getStringValue();
1665 _type = props::STRING;
1667 _value.val = rawValue.clone();
1670 int save_attributes = getAttributes();
1671 setAttribute( WRITE, true );
1672 setStringValue(old_val.c_str());
1673 setAttributes( save_attributes );
1679 SGPropertyNode::untie ()
1686 bool val = getBoolValue();
1688 _type = props::BOOL;
1689 _local_val.bool_val = val;
1693 int val = getIntValue();
1696 _local_val.int_val = val;
1700 long val = getLongValue();
1702 _type = props::LONG;
1703 _local_val.long_val = val;
1706 case props::FLOAT: {
1707 float val = getFloatValue();
1709 _type = props::FLOAT;
1710 _local_val.float_val = val;
1713 case props::DOUBLE: {
1714 double val = getDoubleValue();
1716 _type = props::DOUBLE;
1717 _local_val.double_val = val;
1721 case props::UNSPECIFIED: {
1722 string val = getStringValue();
1724 _type = props::STRING;
1725 _local_val.string_val = copy_string(val.c_str());
1728 case props::EXTENDED: {
1729 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1730 _value.val = 0; // Prevent clearValue() from deleting
1732 _type = props::EXTENDED;
1733 _value.val = val->makeContainer();
1747 SGPropertyNode::getRootNode ()
1752 return _parent->getRootNode();
1755 const SGPropertyNode *
1756 SGPropertyNode::getRootNode () const
1761 return _parent->getRootNode();
1765 SGPropertyNode::getNode (const char * relative_path, bool create)
1767 using namespace boost;
1769 return find_node(this, make_iterator_range(relative_path, relative_path
1770 + strlen(relative_path)),
1775 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1777 using namespace boost;
1778 return find_node(this, make_iterator_range(relative_path, relative_path
1779 + strlen(relative_path)),
1783 const SGPropertyNode *
1784 SGPropertyNode::getNode (const char * relative_path) const
1786 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1789 const SGPropertyNode *
1790 SGPropertyNode::getNode (const char * relative_path, int index) const
1792 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1796 ////////////////////////////////////////////////////////////////////////
1797 // Convenience methods using relative paths.
1798 ////////////////////////////////////////////////////////////////////////
1802 * Test whether another node has a value attached.
1805 SGPropertyNode::hasValue (const char * relative_path) const
1807 const SGPropertyNode * node = getNode(relative_path);
1808 return (node == 0 ? false : node->hasValue());
1813 * Get the value type for another node.
1816 SGPropertyNode::getType (const char * relative_path) const
1818 const SGPropertyNode * node = getNode(relative_path);
1819 return (node == 0 ? props::UNSPECIFIED : node->getType());
1824 * Get a bool value for another node.
1827 SGPropertyNode::getBoolValue (const char * relative_path,
1828 bool defaultValue) const
1830 const SGPropertyNode * node = getNode(relative_path);
1831 return (node == 0 ? defaultValue : node->getBoolValue());
1836 * Get an int value for another node.
1839 SGPropertyNode::getIntValue (const char * relative_path,
1840 int defaultValue) const
1842 const SGPropertyNode * node = getNode(relative_path);
1843 return (node == 0 ? defaultValue : node->getIntValue());
1848 * Get a long value for another node.
1851 SGPropertyNode::getLongValue (const char * relative_path,
1852 long defaultValue) const
1854 const SGPropertyNode * node = getNode(relative_path);
1855 return (node == 0 ? defaultValue : node->getLongValue());
1860 * Get a float value for another node.
1863 SGPropertyNode::getFloatValue (const char * relative_path,
1864 float defaultValue) const
1866 const SGPropertyNode * node = getNode(relative_path);
1867 return (node == 0 ? defaultValue : node->getFloatValue());
1872 * Get a double value for another node.
1875 SGPropertyNode::getDoubleValue (const char * relative_path,
1876 double defaultValue) const
1878 const SGPropertyNode * node = getNode(relative_path);
1879 return (node == 0 ? defaultValue : node->getDoubleValue());
1884 * Get a string value for another node.
1887 SGPropertyNode::getStringValue (const char * relative_path,
1888 const char * defaultValue) const
1890 const SGPropertyNode * node = getNode(relative_path);
1891 return (node == 0 ? defaultValue : node->getStringValue());
1896 * Set a bool value for another node.
1899 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1901 return getNode(relative_path, true)->setBoolValue(value);
1906 * Set an int value for another node.
1909 SGPropertyNode::setIntValue (const char * relative_path, int value)
1911 return getNode(relative_path, true)->setIntValue(value);
1916 * Set a long value for another node.
1919 SGPropertyNode::setLongValue (const char * relative_path, long value)
1921 return getNode(relative_path, true)->setLongValue(value);
1926 * Set a float value for another node.
1929 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1931 return getNode(relative_path, true)->setFloatValue(value);
1936 * Set a double value for another node.
1939 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1941 return getNode(relative_path, true)->setDoubleValue(value);
1946 * Set a string value for another node.
1949 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1951 return getNode(relative_path, true)->setStringValue(value);
1956 * Set an unknown value for another node.
1959 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1962 return getNode(relative_path, true)->setUnspecifiedValue(value);
1967 * Test whether another node is tied.
1970 SGPropertyNode::isTied (const char * relative_path) const
1972 const SGPropertyNode * node = getNode(relative_path);
1973 return (node == 0 ? false : node->isTied());
1978 * Tie a node reached by a relative path, creating it if necessary.
1981 SGPropertyNode::tie (const char * relative_path,
1982 const SGRawValue<bool> &rawValue,
1985 return getNode(relative_path, true)->tie(rawValue, useDefault);
1990 * Tie a node reached by a relative path, creating it if necessary.
1993 SGPropertyNode::tie (const char * relative_path,
1994 const SGRawValue<int> &rawValue,
1997 return getNode(relative_path, true)->tie(rawValue, useDefault);
2002 * Tie a node reached by a relative path, creating it if necessary.
2005 SGPropertyNode::tie (const char * relative_path,
2006 const SGRawValue<long> &rawValue,
2009 return getNode(relative_path, true)->tie(rawValue, useDefault);
2014 * Tie a node reached by a relative path, creating it if necessary.
2017 SGPropertyNode::tie (const char * relative_path,
2018 const SGRawValue<float> &rawValue,
2021 return getNode(relative_path, true)->tie(rawValue, useDefault);
2026 * Tie a node reached by a relative path, creating it if necessary.
2029 SGPropertyNode::tie (const char * relative_path,
2030 const SGRawValue<double> &rawValue,
2033 return getNode(relative_path, true)->tie(rawValue, useDefault);
2038 * Tie a node reached by a relative path, creating it if necessary.
2041 SGPropertyNode::tie (const char * relative_path,
2042 const SGRawValue<const char *> &rawValue,
2045 return getNode(relative_path, true)->tie(rawValue, useDefault);
2050 * Attempt to untie another node reached by a relative path.
2053 SGPropertyNode::untie (const char * relative_path)
2055 SGPropertyNode * node = getNode(relative_path);
2056 return (node == 0 ? false : node->untie());
2060 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2063 if (_listeners == 0)
2064 _listeners = new vector<SGPropertyChangeListener*>;
2065 _listeners->push_back(listener);
2066 listener->register_property(this);
2068 listener->valueChanged(this);
2072 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2074 if (_listeners == 0)
2076 vector<SGPropertyChangeListener*>::iterator it =
2077 find(_listeners->begin(), _listeners->end(), listener);
2078 if (it != _listeners->end()) {
2079 _listeners->erase(it);
2080 listener->unregister_property(this);
2081 if (_listeners->empty()) {
2082 vector<SGPropertyChangeListener*>* tmp = _listeners;
2090 SGPropertyNode::fireValueChanged ()
2092 fireValueChanged(this);
2096 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2098 fireChildAdded(this, child);
2102 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2104 fireChildRemoved(this, child);
2108 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2110 if (_listeners != 0) {
2111 for (unsigned int i = 0; i < _listeners->size(); i++) {
2112 (*_listeners)[i]->valueChanged(node);
2116 _parent->fireValueChanged(node);
2120 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2121 SGPropertyNode * child)
2123 if (_listeners != 0) {
2124 for (unsigned int i = 0; i < _listeners->size(); i++) {
2125 (*_listeners)[i]->childAdded(parent, child);
2129 _parent->fireChildAdded(parent, child);
2133 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2134 SGPropertyNode * child)
2136 if (_listeners != 0) {
2137 for (unsigned int i = 0; i < _listeners->size(); i++) {
2138 (*_listeners)[i]->childRemoved(parent, child);
2142 _parent->fireChildRemoved(parent, child);
2146 ////////////////////////////////////////////////////////////////////////
2147 // Implementation of SGPropertyChangeListener.
2148 ////////////////////////////////////////////////////////////////////////
2150 SGPropertyChangeListener::~SGPropertyChangeListener ()
2152 for (int i = _properties.size() - 1; i >= 0; i--)
2153 _properties[i]->removeChangeListener(this);
2157 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2163 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2164 SGPropertyNode * child)
2170 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2171 SGPropertyNode * child)
2177 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2179 _properties.push_back(node);
2183 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2185 vector<SGPropertyNode *>::iterator it =
2186 find(_properties.begin(), _properties.end(), node);
2187 if (it != _properties.end())
2188 _properties.erase(it);
2192 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2195 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2196 for (int i = 0; i < 3; ++i) {
2207 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2209 for (int i = 0; i < 3; ++i) {
2210 stream >> result[i];
2216 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2219 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2220 for (int i = 0; i < 4; ++i) {
2231 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2233 for (int i = 0; i < 4; ++i) {
2234 stream >> result[i];
2241 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2243 props::Type ltype = lhs.getType();
2244 props::Type rtype = rhs.getType();
2251 return false; // XXX Should we look in aliases?
2253 return lhs.getValue<bool>() == rhs.getValue<bool>();
2255 return lhs.getValue<int>() == rhs.getValue<int>();
2257 return lhs.getValue<long>() == rhs.getValue<long>();
2259 return lhs.getValue<float>() == rhs.getValue<float>();
2261 return lhs.getValue<double>() == rhs.getValue<double>();
2263 case props::UNSPECIFIED:
2264 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2266 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2268 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2276 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2277 const SGPropertyNode& rhs)
2281 int lhsChildren = lhs.nChildren();
2282 int rhsChildren = rhs.nChildren();
2283 if (lhsChildren != rhsChildren)
2285 if (lhsChildren == 0)
2286 return compareNodeValue(lhs, rhs);
2287 for (size_t i = 0; i < lhs._children.size(); ++i) {
2288 const SGPropertyNode* lchild = lhs._children[i];
2289 const SGPropertyNode* rchild = rhs._children[i];
2290 // I'm guessing that the nodes will usually be in the same
2292 if (lchild->getIndex() != rchild->getIndex()
2293 || lchild->getNameString() != rchild->getNameString()) {
2295 for (PropertyList::const_iterator itr = rhs._children.begin(),
2296 end = rhs._children.end();
2299 if (lchild->getIndex() == (*itr)->getIndex()
2300 && lchild->getNameString() == (*itr)->getNameString()) {
2307 if (!compare(*lchild, *rchild))
2313 struct PropertyPlaceLess {
2314 typedef bool result_type;
2315 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2317 int comp = lhs->getNameString().compare(rhs->getNameString());
2319 return lhs->getIndex() < rhs->getIndex();
2325 size_t hash_value(const SGPropertyNode& node)
2327 using namespace boost;
2329 if (node.nChildren() == 0) {
2330 switch (node.getType()) {
2335 return hash_value(node.getValue<bool>());
2337 return hash_value(node.getValue<int>());
2339 return hash_value(node.getValue<long>());
2341 return hash_value(node.getValue<float>());
2343 return hash_value(node.getValue<double>());
2345 case props::UNSPECIFIED:
2347 const char *val = node.getStringValue();
2348 return hash_range(val, val + strlen(val));
2352 const SGVec3d val = node.getValue<SGVec3d>();
2353 return hash_range(&val[0], &val[3]);
2357 const SGVec4d val = node.getValue<SGVec4d>();
2358 return hash_range(&val[0], &val[4]);
2360 case props::ALIAS: // XXX Should we look in aliases?
2366 PropertyList children(node._children.begin(), node._children.end());
2367 sort(children.begin(), children.end(), PropertyPlaceLess());
2368 for (PropertyList::const_iterator itr = children.begin(),
2369 end = children.end();
2372 hash_combine(seed, (*itr)->_name);
2373 hash_combine(seed, (*itr)->_index);
2374 hash_combine(seed, hash_value(**itr));