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"
26 #include <boost/algorithm/string/find_iterator.hpp>
27 #include <boost/algorithm/string/predicate.hpp>
28 #include <boost/algorithm/string/classification.hpp>
29 #include <boost/bind.hpp>
30 #include <boost/functional/hash.hpp>
31 #include <boost/range.hpp>
37 #include <simgear/compiler.h>
38 #include <simgear/debug/logstream.hxx>
40 #if ( _MSC_VER == 1200 )
41 // MSVC 6 is buggy, and needs something strange here
42 using std::vector<SGPropertyNode_ptr>;
43 using std::vector<SGPropertyChangeListener *>;
44 using std::vector<SGPropertyNode *>;
56 using std::stringstream;
58 using namespace simgear;
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * Comparator class for sorting by index.
71 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
72 return (n1->getIndex() < n2->getIndex());
78 ////////////////////////////////////////////////////////////////////////
79 // Convenience macros for value access.
80 ////////////////////////////////////////////////////////////////////////
82 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
83 #define TEST_WRITE if (!getAttribute(WRITE)) return false
86 ////////////////////////////////////////////////////////////////////////
87 // Local path normalization code.
88 ////////////////////////////////////////////////////////////////////////
91 * Parse the name for a path component.
93 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
96 template<typename Range>
98 parse_name (const SGPropertyNode *node, const Range &path)
100 typename Range::iterator i = path.begin();
101 typename Range::iterator max = path.end();
105 if (i != path.end() && *i == '.') {
108 if (i != max && *i != '/')
109 throw string("illegal character after . or ..");
110 } else if (isalpha(*i) || *i == '_') {
113 // The rules inside a name are a little
116 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
117 *i == '-' || *i == '.') {
119 } else if (*i == '[' || *i == '/') {
124 err.append("' found in propertyname after '"+node->getNameString()+"'");
125 err.append("\nname may contain only ._- and alphanumeric characters");
133 if (path.begin() == i) {
136 err.append("' found in propertyname after '"+node->getNameString()+"'");
137 err.append("\nname must begin with alpha or '_'");
141 return Range(path.begin(), i);
144 // Validate the name of a single node
145 inline bool validateName(const string& name)
147 using namespace boost;
150 if (!isalpha(name[0]) && name[0] != '_')
152 return all(make_iterator_range(name.begin(), name.end()),
153 is_alnum() || is_any_of("_-."));
157 ////////////////////////////////////////////////////////////////////////
158 // Other static utility functions.
159 ////////////////////////////////////////////////////////////////////////
163 copy_string (const char * s)
165 unsigned long int slen = strlen(s);
166 char * copy = new char[slen + 1];
168 // the source string length is known so no need to check for '\0'
169 // when copying every single character
170 memcpy(copy, s, slen);
171 *(copy + slen) = '\0';
176 compare_strings (const char * s1, const char * s2)
178 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
182 * Locate a child node by name and index.
184 template<typename Itr>
186 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
188 int nNodes = nodes.size();
189 boost::iterator_range<Itr> name(begin, end);
190 for (int i = 0; i < nNodes; i++) {
191 SGPropertyNode * node = nodes[i];
193 // searching for a mathing index is a lot less time consuming than
194 // comparing two strings so do that first.
195 if (node->getIndex() == index && boost::equals(node->getName(), name))
202 * Locate the child node with the highest index of the same name
205 find_last_child (const char * name, const PropertyList& nodes)
207 int nNodes = nodes.size();
210 for (int i = 0; i < nNodes; i++) {
211 SGPropertyNode * node = nodes[i];
212 if (compare_strings(node->getName(), name))
214 int idx = node->getIndex();
215 if (idx > index) index = idx;
222 * Get first unused index for child nodes with the given name
225 first_unused_index( const char * name,
226 const PropertyList& nodes,
229 const char* nameEnd = name + strlen(name);
231 for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
233 if( find_child(name, nameEnd, index, nodes) < 0 )
237 SG_LOG(SG_GENERAL, SG_ALERT, "Too much nodes: " << name);
241 template<typename Itr>
242 inline SGPropertyNode*
243 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
245 int pos = find_child(begin, end, index, _children);
247 return _children[pos];
249 SGPropertyNode_ptr node;
250 pos = find_child(begin, end, index, _removedChildren);
252 PropertyList::iterator it = _removedChildren.begin();
254 node = _removedChildren[pos];
255 _removedChildren.erase(it);
256 node->setAttribute(REMOVED, false);
257 _children.push_back(node);
258 fireChildAdded(node);
265 template<typename Itr>
267 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
269 SGPropertyNode* node = getExistingChild(begin, end, index, create);
274 node = new SGPropertyNode(begin, end, index, this);
275 _children.push_back(node);
276 fireChildAdded(node);
283 template<typename SplitItr>
285 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
288 typedef typename SplitItr::value_type Range;
289 // Run off the end of the list
294 // Success! This is the one we want.
298 // Empty name at this point is empty, not root.
300 return find_node_aux(current, ++itr, create, last_index);
301 Range name = parse_name(current, token);
302 if (equals(name, "."))
303 return find_node_aux(current, ++itr, create, last_index);
304 if (equals(name, "..")) {
305 SGPropertyNode * parent = current->getParent();
307 throw string("attempt to move past root with '..'");
308 return find_node_aux(parent, ++itr, create, last_index);
311 if (last_index >= 0) {
312 // If we are at the last token and last_index is valid, use
313 // last_index as the index value
315 while (!(++itr).eof()) {
329 if (name.end() != token.end()) {
330 if (*name.end() == '[') {
331 typename Range::iterator i = name.end() + 1, end = token.end();
332 for (;i != end; ++i) {
334 index = (index * 10) + (*i - '0');
339 if (i == token.end() || *i != ']')
340 throw string("unterminated index (looking for ']')");
342 throw string("illegal characters in token: ")
343 + string(name.begin(), name.end());
347 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
348 index, create), itr, create,
352 // Internal function for parsing property paths. last_index provides
353 // and index value for the last node name token, if supplied.
354 template<typename Range>
356 find_node (SGPropertyNode * current,
361 using namespace boost;
362 typedef split_iterator<typename range_result_iterator<Range>::type>
365 PathSplitIterator itr
366 = make_split_iterator(path, first_finder("/", is_equal()));
367 if (*path.begin() == '/')
368 return find_node_aux(current->getRootNode(), itr, create, last_index);
370 return find_node_aux(current, itr, create, last_index);
374 ////////////////////////////////////////////////////////////////////////
375 // Private methods from SGPropertyNode (may be inlined for speed).
376 ////////////////////////////////////////////////////////////////////////
379 SGPropertyNode::get_bool () const
382 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
384 return _local_val.bool_val;
388 SGPropertyNode::get_int () const
391 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
393 return _local_val.int_val;
397 SGPropertyNode::get_long () const
400 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
402 return _local_val.long_val;
406 SGPropertyNode::get_float () const
409 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
411 return _local_val.float_val;
415 SGPropertyNode::get_double () const
418 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
420 return _local_val.double_val;
424 SGPropertyNode::get_string () const
427 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
429 return _local_val.string_val;
433 SGPropertyNode::set_bool (bool val)
436 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
443 _local_val.bool_val = val;
450 SGPropertyNode::set_int (int val)
453 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
460 _local_val.int_val = val;
467 SGPropertyNode::set_long (long val)
470 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
477 _local_val.long_val = val;
484 SGPropertyNode::set_float (float val)
487 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
494 _local_val.float_val = val;
501 SGPropertyNode::set_double (double val)
504 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
511 _local_val.double_val = val;
518 SGPropertyNode::set_string (const char * val)
521 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
528 delete [] _local_val.string_val;
529 _local_val.string_val = copy_string(val);
536 SGPropertyNode::clearValue ()
538 if (_type == props::ALIAS) {
541 } else if (_type != props::NONE) {
544 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
547 _local_val.int_val = SGRawValue<int>::DefaultValue();
550 _local_val.long_val = SGRawValue<long>::DefaultValue();
553 _local_val.float_val = SGRawValue<float>::DefaultValue();
556 _local_val.double_val = SGRawValue<double>::DefaultValue();
559 case props::UNSPECIFIED:
561 delete [] _local_val.string_val;
563 _local_val.string_val = 0;
565 default: // avoid compiler warning
577 * Get the value as a string.
580 SGPropertyNode::make_string () const
582 if (!getAttribute(READ))
586 return _value.alias->getStringValue();
588 return get_bool() ? "true" : "false";
590 case props::UNSPECIFIED:
609 sstr << std::setprecision(10) << get_double();
611 case props::EXTENDED:
613 props::Type realType = _value.val->getType();
614 // Perhaps this should be done for all types?
615 if (realType == props::VEC3D || realType == props::VEC4D)
617 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
623 _buffer = sstr.str();
624 return _buffer.c_str();
628 * Trace a write access for a property.
631 SGPropertyNode::trace_write () const
634 cerr << "TRACE: Write node " << getPath () << ", value \""
635 << make_string() << '"' << endl;
637 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
638 << ", value \"" << make_string() << '"');
643 * Trace a read access for a property.
646 SGPropertyNode::trace_read () const
649 cerr << "TRACE: Write node " << getPath () << ", value \""
650 << make_string() << '"' << endl;
652 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
653 << ", value \"" << make_string() << '"');
658 ////////////////////////////////////////////////////////////////////////
659 // Public methods from SGPropertyNode.
660 ////////////////////////////////////////////////////////////////////////
663 * Last used attribute
664 * Update as needed when enum Attribute is changed
666 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
669 * Default constructor: always creates a root node.
671 SGPropertyNode::SGPropertyNode ()
679 _local_val.string_val = 0;
687 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
688 : SGReferenced(node),
691 _parent(0), // don't copy the parent
695 _listeners(0) // CHECK!!
697 _local_val.string_val = 0;
699 if (_type == props::NONE)
701 if (_type == props::ALIAS) {
702 _value.alias = node._value.alias;
707 if (_tied || _type == props::EXTENDED) {
708 _value.val = node._value.val->clone();
713 set_bool(node.get_bool());
716 set_int(node.get_int());
719 set_long(node.get_long());
722 set_float(node.get_float());
725 set_double(node.get_double());
728 case props::UNSPECIFIED:
729 set_string(node.get_string());
738 * Convenience constructor.
740 template<typename Itr>
741 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
743 SGPropertyNode * parent)
752 _local_val.string_val = 0;
754 if (!validateName(_name))
755 throw string("plain name expected instead of '") + _name + '\'';
758 SGPropertyNode::SGPropertyNode (const string& name,
760 SGPropertyNode * parent)
769 _local_val.string_val = 0;
771 if (!validateName(name))
772 throw string("plain name expected instead of '") + _name + '\'';
778 SGPropertyNode::~SGPropertyNode ()
780 // zero out all parent pointers, else they might be dangling
781 for (unsigned i = 0; i < _children.size(); ++i)
782 _children[i]->_parent = 0;
783 for (unsigned i = 0; i < _removedChildren.size(); ++i)
784 _removedChildren[i]->_parent = 0;
788 vector<SGPropertyChangeListener*>::iterator it;
789 for (it = _listeners->begin(); it != _listeners->end(); ++it)
790 (*it)->unregister_property(this);
797 * Alias to another node.
800 SGPropertyNode::alias (SGPropertyNode * target)
802 if (target && (_type != props::ALIAS) && (!_tied))
806 _value.alias = target;
807 _type = props::ALIAS;
815 SG_LOG(SG_GENERAL, SG_ALERT,
816 "Failed to create alias for " << getPath() << ". "
817 "The target property does not exist.");
820 if (_type == props::ALIAS)
822 if (_value.alias == target)
823 return true; // ok, identical alias requested
824 SG_LOG(SG_GENERAL, SG_ALERT,
825 "Failed to create alias at " << target->getPath() << ". "
826 "Source "<< getPath() << " is already aliasing another property.");
831 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
832 "Source " << getPath() << " is a tied property.");
841 * Alias to another node by path.
844 SGPropertyNode::alias (const char * path)
846 return alias(getNode(path, true));
854 SGPropertyNode::unalias ()
856 if (_type != props::ALIAS)
864 * Get the target of an alias.
867 SGPropertyNode::getAliasTarget ()
869 return (_type == props::ALIAS ? _value.alias : 0);
873 const SGPropertyNode *
874 SGPropertyNode::getAliasTarget () const
876 return (_type == props::ALIAS ? _value.alias : 0);
880 * create a non-const child by name after the last node with the same name.
883 SGPropertyNode::addChild(const char * name, int min_index, bool append)
886 ? std::max(find_last_child(name, _children) + 1, min_index)
887 : first_unused_index(name, _children, min_index);
889 SGPropertyNode_ptr node;
890 node = new SGPropertyNode(name, name + strlen(name), pos, this);
891 _children.push_back(node);
892 fireChildAdded(node);
897 * Create multiple children with unused indices
899 simgear::PropertyList
900 SGPropertyNode::addChildren( const std::string& name,
905 simgear::PropertyList nodes;
906 std::set<int> used_indices;
910 // First grab all used indices. This saves us of testing every index if it
911 // is used for every element to be created
912 for( size_t i = 0; i < nodes.size(); i++ )
914 const SGPropertyNode* node = nodes[i];
916 if( node->getNameString() == name && node->getIndex() >= min_index )
917 used_indices.insert(node->getIndex());
922 // If we don't want to fill the holes just find last node
923 min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index);
926 for( int index = min_index;
927 index < std::numeric_limits<int>::max() && nodes.size() < count;
930 if( used_indices.find(index) == used_indices.end() )
932 SGPropertyNode_ptr node;
933 node = new SGPropertyNode(name, index, this);
934 _children.push_back(node);
935 fireChildAdded(node);
936 nodes.push_back(node);
944 * Get a non-const child by index.
947 SGPropertyNode::getChild (int position)
949 if (position >= 0 && position < nChildren())
950 return _children[position];
957 * Get a const child by index.
959 const SGPropertyNode *
960 SGPropertyNode::getChild (int position) const
962 if (position >= 0 && position < nChildren())
963 return _children[position];
970 * Get a non-const child by name and index, creating if necessary.
974 SGPropertyNode::getChild (const char * name, int index, bool create)
976 return getChildImpl(name, name + strlen(name), index, create);
980 SGPropertyNode::getChild (const string& name, int index, bool create)
982 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
987 node = new SGPropertyNode(name, index, this);
988 _children.push_back(node);
989 fireChildAdded(node);
997 * Get a const child by name and index.
999 const SGPropertyNode *
1000 SGPropertyNode::getChild (const char * name, int index) const
1002 int pos = find_child(name, name + strlen(name), index, _children);
1004 return _children[pos];
1011 * Get all children with the same name (but different indices).
1014 SGPropertyNode::getChildren (const char * name) const
1016 PropertyList children;
1017 int max = _children.size();
1019 for (int i = 0; i < max; i++)
1020 if (compare_strings(_children[i]->getName(), name))
1021 children.push_back(_children[i]);
1023 sort(children.begin(), children.end(), CompareIndices());
1029 * Remove child by position.
1032 SGPropertyNode::removeChild (int pos, bool keep)
1034 SGPropertyNode_ptr node;
1035 if (pos < 0 || pos >= (int)_children.size())
1038 PropertyList::iterator it = _children.begin();
1040 node = _children[pos];
1041 _children.erase(it);
1043 _removedChildren.push_back(node);
1046 node->setAttribute(REMOVED, true);
1048 fireChildRemoved(node);
1054 * Remove a child node
1057 SGPropertyNode::removeChild (const char * name, int index, bool keep)
1059 SGPropertyNode_ptr ret;
1060 int pos = find_child(name, name + strlen(name), index, _children);
1062 ret = removeChild(pos, keep);
1068 * Remove all children with the specified name.
1071 SGPropertyNode::removeChildren (const char * name, bool keep)
1073 PropertyList children;
1075 for (int pos = _children.size() - 1; pos >= 0; pos--)
1076 if (compare_strings(_children[pos]->getName(), name))
1077 children.push_back(removeChild(pos, keep));
1079 sort(children.begin(), children.end(), CompareIndices());
1084 SGPropertyNode::getDisplayName (bool simplify) const
1086 string display_name = _name;
1087 if (_index != 0 || !simplify) {
1089 sstr << '[' << _index << ']';
1090 display_name += sstr.str();
1092 return display_name;
1096 SGPropertyNode::getPath (bool simplify) const
1098 typedef std::vector<SGConstPropertyNode_ptr> PList;
1100 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1101 pathList.push_back(node);
1103 for (PList::reverse_iterator itr = pathList.rbegin(),
1104 rend = pathList.rend();
1108 result += (*itr)->getDisplayName(simplify);
1114 SGPropertyNode::getType () const
1116 if (_type == props::ALIAS)
1117 return _value.alias->getType();
1118 else if (_type == props::EXTENDED)
1119 return _value.val->getType();
1126 SGPropertyNode::getBoolValue () const
1128 // Shortcut for common case
1129 if (_attr == (READ|WRITE) && _type == props::BOOL)
1132 if (getAttribute(TRACE_READ))
1134 if (!getAttribute(READ))
1135 return SGRawValue<bool>::DefaultValue();
1138 return _value.alias->getBoolValue();
1142 return get_int() == 0 ? false : true;
1144 return get_long() == 0L ? false : true;
1146 return get_float() == 0.0 ? false : true;
1148 return get_double() == 0.0L ? false : true;
1150 case props::UNSPECIFIED:
1151 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1154 return SGRawValue<bool>::DefaultValue();
1159 SGPropertyNode::getIntValue () const
1161 // Shortcut for common case
1162 if (_attr == (READ|WRITE) && _type == props::INT)
1165 if (getAttribute(TRACE_READ))
1167 if (!getAttribute(READ))
1168 return SGRawValue<int>::DefaultValue();
1171 return _value.alias->getIntValue();
1173 return int(get_bool());
1177 return int(get_long());
1179 return int(get_float());
1181 return int(get_double());
1183 case props::UNSPECIFIED:
1184 return atoi(get_string());
1187 return SGRawValue<int>::DefaultValue();
1192 SGPropertyNode::getLongValue () const
1194 // Shortcut for common case
1195 if (_attr == (READ|WRITE) && _type == props::LONG)
1198 if (getAttribute(TRACE_READ))
1200 if (!getAttribute(READ))
1201 return SGRawValue<long>::DefaultValue();
1204 return _value.alias->getLongValue();
1206 return long(get_bool());
1208 return long(get_int());
1212 return long(get_float());
1214 return long(get_double());
1216 case props::UNSPECIFIED:
1217 return strtol(get_string(), 0, 0);
1220 return SGRawValue<long>::DefaultValue();
1225 SGPropertyNode::getFloatValue () const
1227 // Shortcut for common case
1228 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1231 if (getAttribute(TRACE_READ))
1233 if (!getAttribute(READ))
1234 return SGRawValue<float>::DefaultValue();
1237 return _value.alias->getFloatValue();
1239 return float(get_bool());
1241 return float(get_int());
1243 return float(get_long());
1247 return float(get_double());
1249 case props::UNSPECIFIED:
1250 return atof(get_string());
1253 return SGRawValue<float>::DefaultValue();
1258 SGPropertyNode::getDoubleValue () const
1260 // Shortcut for common case
1261 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1262 return get_double();
1264 if (getAttribute(TRACE_READ))
1266 if (!getAttribute(READ))
1267 return SGRawValue<double>::DefaultValue();
1271 return _value.alias->getDoubleValue();
1273 return double(get_bool());
1275 return double(get_int());
1277 return double(get_long());
1279 return double(get_float());
1281 return get_double();
1283 case props::UNSPECIFIED:
1284 return strtod(get_string(), 0);
1287 return SGRawValue<double>::DefaultValue();
1292 SGPropertyNode::getStringValue () const
1294 // Shortcut for common case
1295 if (_attr == (READ|WRITE) && _type == props::STRING)
1296 return get_string();
1298 if (getAttribute(TRACE_READ))
1300 if (!getAttribute(READ))
1301 return SGRawValue<const char *>::DefaultValue();
1302 return make_string();
1306 SGPropertyNode::setBoolValue (bool value)
1308 // Shortcut for common case
1309 if (_attr == (READ|WRITE) && _type == props::BOOL)
1310 return set_bool(value);
1312 bool result = false;
1314 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1317 _type = props::BOOL;
1322 result = _value.alias->setBoolValue(value);
1325 result = set_bool(value);
1328 result = set_int(int(value));
1331 result = set_long(long(value));
1334 result = set_float(float(value));
1337 result = set_double(double(value));
1340 case props::UNSPECIFIED:
1341 result = set_string(value ? "true" : "false");
1348 if (getAttribute(TRACE_WRITE))
1354 SGPropertyNode::setIntValue (int value)
1356 // Shortcut for common case
1357 if (_attr == (READ|WRITE) && _type == props::INT)
1358 return set_int(value);
1360 bool result = false;
1362 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1365 _local_val.int_val = 0;
1370 result = _value.alias->setIntValue(value);
1373 result = set_bool(value == 0 ? false : true);
1376 result = set_int(value);
1379 result = set_long(long(value));
1382 result = set_float(float(value));
1385 result = set_double(double(value));
1388 case props::UNSPECIFIED: {
1390 sprintf(buf, "%d", value);
1391 result = set_string(buf);
1399 if (getAttribute(TRACE_WRITE))
1405 SGPropertyNode::setLongValue (long value)
1407 // Shortcut for common case
1408 if (_attr == (READ|WRITE) && _type == props::LONG)
1409 return set_long(value);
1411 bool result = false;
1413 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1415 _type = props::LONG;
1416 _local_val.long_val = 0L;
1421 result = _value.alias->setLongValue(value);
1424 result = set_bool(value == 0L ? false : true);
1427 result = set_int(int(value));
1430 result = set_long(value);
1433 result = set_float(float(value));
1436 result = set_double(double(value));
1439 case props::UNSPECIFIED: {
1441 sprintf(buf, "%ld", value);
1442 result = set_string(buf);
1450 if (getAttribute(TRACE_WRITE))
1456 SGPropertyNode::setFloatValue (float value)
1458 // Shortcut for common case
1459 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1460 return set_float(value);
1462 bool result = false;
1464 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1466 _type = props::FLOAT;
1467 _local_val.float_val = 0;
1472 result = _value.alias->setFloatValue(value);
1475 result = set_bool(value == 0.0 ? false : true);
1478 result = set_int(int(value));
1481 result = set_long(long(value));
1484 result = set_float(value);
1487 result = set_double(double(value));
1490 case props::UNSPECIFIED: {
1492 sprintf(buf, "%f", value);
1493 result = set_string(buf);
1501 if (getAttribute(TRACE_WRITE))
1507 SGPropertyNode::setDoubleValue (double value)
1509 // Shortcut for common case
1510 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1511 return set_double(value);
1513 bool result = false;
1515 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1517 _local_val.double_val = value;
1518 _type = props::DOUBLE;
1523 result = _value.alias->setDoubleValue(value);
1526 result = set_bool(value == 0.0L ? false : true);
1529 result = set_int(int(value));
1532 result = set_long(long(value));
1535 result = set_float(float(value));
1538 result = set_double(value);
1541 case props::UNSPECIFIED: {
1543 sprintf(buf, "%f", value);
1544 result = set_string(buf);
1552 if (getAttribute(TRACE_WRITE))
1558 SGPropertyNode::setStringValue (const char * value)
1560 // Shortcut for common case
1561 if (_attr == (READ|WRITE) && _type == props::STRING)
1562 return set_string(value);
1564 bool result = false;
1566 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1568 _type = props::STRING;
1573 result = _value.alias->setStringValue(value);
1576 result = set_bool((compare_strings(value, "true")
1577 || atoi(value)) ? true : false);
1580 result = set_int(atoi(value));
1583 result = set_long(strtol(value, 0, 0));
1586 result = set_float(atof(value));
1589 result = set_double(strtod(value, 0));
1592 case props::UNSPECIFIED:
1593 result = set_string(value);
1595 case props::EXTENDED:
1597 stringstream sstr(value);
1598 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1606 if (getAttribute(TRACE_WRITE))
1612 SGPropertyNode::setUnspecifiedValue (const char * value)
1614 bool result = false;
1616 if (_type == props::NONE) {
1618 _type = props::UNSPECIFIED;
1620 props::Type type = _type;
1621 if (type == props::EXTENDED)
1622 type = _value.val->getType();
1625 result = _value.alias->setUnspecifiedValue(value);
1628 result = set_bool((compare_strings(value, "true")
1629 || atoi(value)) ? true : false);
1632 result = set_int(atoi(value));
1635 result = set_long(strtol(value, 0, 0));
1638 result = set_float(atof(value));
1641 result = set_double(strtod(value, 0));
1644 case props::UNSPECIFIED:
1645 result = set_string(value);
1648 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1651 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1658 if (getAttribute(TRACE_WRITE))
1663 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1665 if (!getAttribute(READ))
1669 return _value.alias->printOn(stream);
1671 stream << (get_bool() ? "true" : "false");
1674 stream << get_int();
1677 stream << get_long();
1680 stream << get_float();
1683 stream << get_double();
1686 case props::UNSPECIFIED:
1687 stream << get_string();
1689 case props::EXTENDED:
1690 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1694 default: // avoid compiler warning
1701 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1704 if (_type == props::ALIAS || _tied)
1707 useDefault = useDefault && hasValue();
1708 std::string old_val;
1710 old_val = getStringValue();
1712 _type = props::STRING;
1714 _value.val = rawValue.clone();
1717 int save_attributes = getAttributes();
1718 setAttribute( WRITE, true );
1719 setStringValue(old_val.c_str());
1720 setAttributes( save_attributes );
1726 SGPropertyNode::untie ()
1733 bool val = getBoolValue();
1735 _type = props::BOOL;
1736 _local_val.bool_val = val;
1740 int val = getIntValue();
1743 _local_val.int_val = val;
1747 long val = getLongValue();
1749 _type = props::LONG;
1750 _local_val.long_val = val;
1753 case props::FLOAT: {
1754 float val = getFloatValue();
1756 _type = props::FLOAT;
1757 _local_val.float_val = val;
1760 case props::DOUBLE: {
1761 double val = getDoubleValue();
1763 _type = props::DOUBLE;
1764 _local_val.double_val = val;
1768 case props::UNSPECIFIED: {
1769 string val = getStringValue();
1771 _type = props::STRING;
1772 _local_val.string_val = copy_string(val.c_str());
1775 case props::EXTENDED: {
1776 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1777 _value.val = 0; // Prevent clearValue() from deleting
1779 _type = props::EXTENDED;
1780 _value.val = val->makeContainer();
1794 SGPropertyNode::getRootNode ()
1799 return _parent->getRootNode();
1802 const SGPropertyNode *
1803 SGPropertyNode::getRootNode () const
1808 return _parent->getRootNode();
1812 SGPropertyNode::getNode (const char * relative_path, bool create)
1814 using namespace boost;
1816 return find_node(this, make_iterator_range(relative_path, relative_path
1817 + strlen(relative_path)),
1822 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1824 using namespace boost;
1825 return find_node(this, make_iterator_range(relative_path, relative_path
1826 + strlen(relative_path)),
1830 const SGPropertyNode *
1831 SGPropertyNode::getNode (const char * relative_path) const
1833 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1836 const SGPropertyNode *
1837 SGPropertyNode::getNode (const char * relative_path, int index) const
1839 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1843 ////////////////////////////////////////////////////////////////////////
1844 // Convenience methods using relative paths.
1845 ////////////////////////////////////////////////////////////////////////
1849 * Test whether another node has a value attached.
1852 SGPropertyNode::hasValue (const char * relative_path) const
1854 const SGPropertyNode * node = getNode(relative_path);
1855 return (node == 0 ? false : node->hasValue());
1860 * Get the value type for another node.
1863 SGPropertyNode::getType (const char * relative_path) const
1865 const SGPropertyNode * node = getNode(relative_path);
1866 return (node == 0 ? props::UNSPECIFIED : node->getType());
1871 * Get a bool value for another node.
1874 SGPropertyNode::getBoolValue (const char * relative_path,
1875 bool defaultValue) const
1877 const SGPropertyNode * node = getNode(relative_path);
1878 return (node == 0 ? defaultValue : node->getBoolValue());
1883 * Get an int value for another node.
1886 SGPropertyNode::getIntValue (const char * relative_path,
1887 int defaultValue) const
1889 const SGPropertyNode * node = getNode(relative_path);
1890 return (node == 0 ? defaultValue : node->getIntValue());
1895 * Get a long value for another node.
1898 SGPropertyNode::getLongValue (const char * relative_path,
1899 long defaultValue) const
1901 const SGPropertyNode * node = getNode(relative_path);
1902 return (node == 0 ? defaultValue : node->getLongValue());
1907 * Get a float value for another node.
1910 SGPropertyNode::getFloatValue (const char * relative_path,
1911 float defaultValue) const
1913 const SGPropertyNode * node = getNode(relative_path);
1914 return (node == 0 ? defaultValue : node->getFloatValue());
1919 * Get a double value for another node.
1922 SGPropertyNode::getDoubleValue (const char * relative_path,
1923 double defaultValue) const
1925 const SGPropertyNode * node = getNode(relative_path);
1926 return (node == 0 ? defaultValue : node->getDoubleValue());
1931 * Get a string value for another node.
1934 SGPropertyNode::getStringValue (const char * relative_path,
1935 const char * defaultValue) const
1937 const SGPropertyNode * node = getNode(relative_path);
1938 return (node == 0 ? defaultValue : node->getStringValue());
1943 * Set a bool value for another node.
1946 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1948 return getNode(relative_path, true)->setBoolValue(value);
1953 * Set an int value for another node.
1956 SGPropertyNode::setIntValue (const char * relative_path, int value)
1958 return getNode(relative_path, true)->setIntValue(value);
1963 * Set a long value for another node.
1966 SGPropertyNode::setLongValue (const char * relative_path, long value)
1968 return getNode(relative_path, true)->setLongValue(value);
1973 * Set a float value for another node.
1976 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1978 return getNode(relative_path, true)->setFloatValue(value);
1983 * Set a double value for another node.
1986 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1988 return getNode(relative_path, true)->setDoubleValue(value);
1993 * Set a string value for another node.
1996 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1998 return getNode(relative_path, true)->setStringValue(value);
2003 * Set an unknown value for another node.
2006 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
2009 return getNode(relative_path, true)->setUnspecifiedValue(value);
2014 * Test whether another node is tied.
2017 SGPropertyNode::isTied (const char * relative_path) const
2019 const SGPropertyNode * node = getNode(relative_path);
2020 return (node == 0 ? false : node->isTied());
2025 * Tie a node reached by a relative path, creating it if necessary.
2028 SGPropertyNode::tie (const char * relative_path,
2029 const SGRawValue<bool> &rawValue,
2032 return getNode(relative_path, true)->tie(rawValue, useDefault);
2037 * Tie a node reached by a relative path, creating it if necessary.
2040 SGPropertyNode::tie (const char * relative_path,
2041 const SGRawValue<int> &rawValue,
2044 return getNode(relative_path, true)->tie(rawValue, useDefault);
2049 * Tie a node reached by a relative path, creating it if necessary.
2052 SGPropertyNode::tie (const char * relative_path,
2053 const SGRawValue<long> &rawValue,
2056 return getNode(relative_path, true)->tie(rawValue, useDefault);
2061 * Tie a node reached by a relative path, creating it if necessary.
2064 SGPropertyNode::tie (const char * relative_path,
2065 const SGRawValue<float> &rawValue,
2068 return getNode(relative_path, true)->tie(rawValue, useDefault);
2073 * Tie a node reached by a relative path, creating it if necessary.
2076 SGPropertyNode::tie (const char * relative_path,
2077 const SGRawValue<double> &rawValue,
2080 return getNode(relative_path, true)->tie(rawValue, useDefault);
2085 * Tie a node reached by a relative path, creating it if necessary.
2088 SGPropertyNode::tie (const char * relative_path,
2089 const SGRawValue<const char *> &rawValue,
2092 return getNode(relative_path, true)->tie(rawValue, useDefault);
2097 * Attempt to untie another node reached by a relative path.
2100 SGPropertyNode::untie (const char * relative_path)
2102 SGPropertyNode * node = getNode(relative_path);
2103 return (node == 0 ? false : node->untie());
2107 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2110 if (_listeners == 0)
2111 _listeners = new vector<SGPropertyChangeListener*>;
2112 _listeners->push_back(listener);
2113 listener->register_property(this);
2115 listener->valueChanged(this);
2119 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2121 if (_listeners == 0)
2123 vector<SGPropertyChangeListener*>::iterator it =
2124 find(_listeners->begin(), _listeners->end(), listener);
2125 if (it != _listeners->end()) {
2126 _listeners->erase(it);
2127 listener->unregister_property(this);
2128 if (_listeners->empty()) {
2129 vector<SGPropertyChangeListener*>* tmp = _listeners;
2137 SGPropertyNode::fireValueChanged ()
2139 fireValueChanged(this);
2143 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2145 fireChildAdded(this, child);
2149 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2151 fireChildRemoved(this, child);
2155 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2157 if (_listeners != 0) {
2158 for (unsigned int i = 0; i < _listeners->size(); i++) {
2159 (*_listeners)[i]->valueChanged(node);
2163 _parent->fireValueChanged(node);
2167 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2168 SGPropertyNode * child)
2170 if (_listeners != 0) {
2171 for (unsigned int i = 0; i < _listeners->size(); i++) {
2172 (*_listeners)[i]->childAdded(parent, child);
2176 _parent->fireChildAdded(parent, child);
2180 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2181 SGPropertyNode * child)
2183 if (_listeners != 0) {
2184 for (unsigned int i = 0; i < _listeners->size(); i++) {
2185 (*_listeners)[i]->childRemoved(parent, child);
2189 _parent->fireChildRemoved(parent, child);
2193 ////////////////////////////////////////////////////////////////////////
2194 // Implementation of SGPropertyChangeListener.
2195 ////////////////////////////////////////////////////////////////////////
2197 SGPropertyChangeListener::~SGPropertyChangeListener ()
2199 for (int i = _properties.size() - 1; i >= 0; i--)
2200 _properties[i]->removeChangeListener(this);
2204 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2210 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2211 SGPropertyNode * child)
2217 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2218 SGPropertyNode * child)
2224 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2226 _properties.push_back(node);
2230 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2232 vector<SGPropertyNode *>::iterator it =
2233 find(_properties.begin(), _properties.end(), node);
2234 if (it != _properties.end())
2235 _properties.erase(it);
2239 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2242 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2243 for (int i = 0; i < 3; ++i) {
2254 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2256 for (int i = 0; i < 3; ++i) {
2257 stream >> result[i];
2263 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2266 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2267 for (int i = 0; i < 4; ++i) {
2278 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2280 for (int i = 0; i < 4; ++i) {
2281 stream >> result[i];
2288 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2290 props::Type ltype = lhs.getType();
2291 props::Type rtype = rhs.getType();
2298 return false; // XXX Should we look in aliases?
2300 return lhs.getValue<bool>() == rhs.getValue<bool>();
2302 return lhs.getValue<int>() == rhs.getValue<int>();
2304 return lhs.getValue<long>() == rhs.getValue<long>();
2306 return lhs.getValue<float>() == rhs.getValue<float>();
2308 return lhs.getValue<double>() == rhs.getValue<double>();
2310 case props::UNSPECIFIED:
2311 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2313 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2315 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2323 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2324 const SGPropertyNode& rhs)
2328 int lhsChildren = lhs.nChildren();
2329 int rhsChildren = rhs.nChildren();
2330 if (lhsChildren != rhsChildren)
2332 if (lhsChildren == 0)
2333 return compareNodeValue(lhs, rhs);
2334 for (size_t i = 0; i < lhs._children.size(); ++i) {
2335 const SGPropertyNode* lchild = lhs._children[i];
2336 const SGPropertyNode* rchild = rhs._children[i];
2337 // I'm guessing that the nodes will usually be in the same
2339 if (lchild->getIndex() != rchild->getIndex()
2340 || lchild->getNameString() != rchild->getNameString()) {
2342 for (PropertyList::const_iterator itr = rhs._children.begin(),
2343 end = rhs._children.end();
2346 if (lchild->getIndex() == (*itr)->getIndex()
2347 && lchild->getNameString() == (*itr)->getNameString()) {
2354 if (!compare(*lchild, *rchild))
2360 struct PropertyPlaceLess {
2361 typedef bool result_type;
2362 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2364 int comp = lhs->getNameString().compare(rhs->getNameString());
2366 return lhs->getIndex() < rhs->getIndex();
2372 size_t hash_value(const SGPropertyNode& node)
2374 using namespace boost;
2376 if (node.nChildren() == 0) {
2377 switch (node.getType()) {
2382 return hash_value(node.getValue<bool>());
2384 return hash_value(node.getValue<int>());
2386 return hash_value(node.getValue<long>());
2388 return hash_value(node.getValue<float>());
2390 return hash_value(node.getValue<double>());
2392 case props::UNSPECIFIED:
2394 const char *val = node.getStringValue();
2395 return hash_range(val, val + strlen(val));
2399 const SGVec3d val = node.getValue<SGVec3d>();
2400 return hash_range(&val[0], &val[3]);
2404 const SGVec4d val = node.getValue<SGVec4d>();
2405 return hash_range(&val[0], &val[4]);
2407 case props::ALIAS: // XXX Should we look in aliases?
2413 PropertyList children(node._children.begin(), node._children.end());
2414 sort(children.begin(), children.end(), PropertyPlaceLess());
2415 for (PropertyList::const_iterator itr = children.begin(),
2416 end = children.end();
2419 hash_combine(seed, (*itr)->_name);
2420 hash_combine(seed, (*itr)->_index);
2421 hash_combine(seed, hash_value(**itr));