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>
23 #include <boost/algorithm/string/find_iterator.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25 #include <boost/algorithm/string/classification.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/functional/hash.hpp>
28 #include <boost/range.hpp>
30 #include <simgear/math/SGMath.hxx>
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 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 == '/') {
121 throw string("name may contain only ._- and alphanumeric characters");
128 if (path.begin() == i)
129 throw string("name must begin with alpha or '_'");
131 return Range(path.begin(), i);
134 // Validate the name of a single node
135 inline bool validateName(const string& name)
137 using namespace boost;
140 if (!isalpha(name[0]) && name[0] != '_')
142 return all(make_iterator_range(name.begin(), name.end()),
143 is_alnum() || is_any_of("_-."));
147 ////////////////////////////////////////////////////////////////////////
148 // Other static utility functions.
149 ////////////////////////////////////////////////////////////////////////
153 copy_string (const char * s)
155 unsigned long int slen = strlen(s);
156 char * copy = new char[slen + 1];
158 // the source string length is known so no need to check for '\0'
159 // when copying every single character
160 memcpy(copy, s, slen);
161 *(copy + slen) = '\0';
166 compare_strings (const char * s1, const char * s2)
168 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
172 * Locate a child node by name and index.
174 template<typename Itr>
176 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
178 int nNodes = nodes.size();
179 boost::iterator_range<Itr> name(begin, end);
180 for (int i = 0; i < nNodes; i++) {
181 SGPropertyNode * node = nodes[i];
183 // searching for a mathing index is a lot less time consuming than
184 // comparing two strings so do that first.
185 if (node->getIndex() == index && boost::equals(node->getName(), name))
192 * Locate the child node with the highest index of the same name
195 find_last_child (const char * name, const PropertyList& nodes)
197 int nNodes = nodes.size();
200 for (int i = 0; i < nNodes; i++) {
201 SGPropertyNode * node = nodes[i];
202 if (compare_strings(node->getName(), name))
204 int idx = node->getIndex();
205 if (idx > index) index = idx;
211 template<typename Itr>
212 inline SGPropertyNode*
213 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
215 int pos = find_child(begin, end, index, _children);
217 return _children[pos];
219 SGPropertyNode_ptr node;
220 pos = find_child(begin, end, index, _removedChildren);
222 PropertyList::iterator it = _removedChildren.begin();
224 node = _removedChildren[pos];
225 _removedChildren.erase(it);
226 node->setAttribute(REMOVED, false);
227 _children.push_back(node);
228 fireChildAdded(node);
235 template<typename Itr>
237 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
239 SGPropertyNode* node = getExistingChild(begin, end, index, create);
244 node = new SGPropertyNode(begin, end, index, this);
245 _children.push_back(node);
246 fireChildAdded(node);
253 template<typename SplitItr>
255 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
258 typedef typename SplitItr::value_type Range;
259 // Run off the end of the list
264 // Success! This is the one we want.
268 // Empty name at this point is empty, not root.
270 return find_node_aux(current, ++itr, create, last_index);
271 Range name = parse_name(token);
272 if (equals(name, "."))
273 return find_node_aux(current, ++itr, create, last_index);
274 if (equals(name, "..")) {
275 SGPropertyNode * parent = current->getParent();
277 throw string("attempt to move past root with '..'");
278 return find_node_aux(parent, ++itr, create, last_index);
281 if (last_index >= 0) {
282 // If we are at the last token and last_index is valid, use
283 // last_index as the index value
285 while (!(++itr).eof()) {
299 if (name.end() != token.end()) {
300 if (*name.end() == '[') {
301 typename Range::iterator i = name.end() + 1, end = token.end();
302 for (;i != end; ++i) {
304 index = (index * 10) + (*i - '0');
309 if (i == token.end() || *i != ']')
310 throw string("unterminated index (looking for ']')");
312 throw string("illegal characters in token: ")
313 + string(name.begin(), name.end());
317 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
318 index, create), itr, create,
322 // Internal function for parsing property paths. last_index provides
323 // and index value for the last node name token, if supplied.
324 template<typename Range>
326 find_node (SGPropertyNode * current,
331 using namespace boost;
332 typedef split_iterator<typename range_result_iterator<Range>::type>
335 PathSplitIterator itr
336 = make_split_iterator(path, first_finder("/", is_equal()));
337 if (*path.begin() == '/')
338 return find_node_aux(current->getRootNode(), itr, create, last_index);
340 return find_node_aux(current, itr, create, last_index);
344 ////////////////////////////////////////////////////////////////////////
345 // Private methods from SGPropertyNode (may be inlined for speed).
346 ////////////////////////////////////////////////////////////////////////
349 SGPropertyNode::get_bool () const
352 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
354 return _local_val.bool_val;
358 SGPropertyNode::get_int () const
361 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
363 return _local_val.int_val;
367 SGPropertyNode::get_long () const
370 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
372 return _local_val.long_val;
376 SGPropertyNode::get_float () const
379 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
381 return _local_val.float_val;
385 SGPropertyNode::get_double () const
388 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
390 return _local_val.double_val;
394 SGPropertyNode::get_string () const
397 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
399 return _local_val.string_val;
403 SGPropertyNode::set_bool (bool val)
406 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
413 _local_val.bool_val = val;
420 SGPropertyNode::set_int (int val)
423 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
430 _local_val.int_val = val;
437 SGPropertyNode::set_long (long val)
440 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
447 _local_val.long_val = val;
454 SGPropertyNode::set_float (float val)
457 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
464 _local_val.float_val = val;
471 SGPropertyNode::set_double (double val)
474 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
481 _local_val.double_val = val;
488 SGPropertyNode::set_string (const char * val)
491 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
498 delete [] _local_val.string_val;
499 _local_val.string_val = copy_string(val);
506 SGPropertyNode::clearValue ()
508 if (_type == props::ALIAS) {
511 } else if (_type != props::NONE) {
514 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
517 _local_val.int_val = SGRawValue<int>::DefaultValue();
520 _local_val.long_val = SGRawValue<long>::DefaultValue();
523 _local_val.float_val = SGRawValue<float>::DefaultValue();
526 _local_val.double_val = SGRawValue<double>::DefaultValue();
529 case props::UNSPECIFIED:
531 delete [] _local_val.string_val;
533 _local_val.string_val = 0;
535 default: // avoid compiler warning
547 * Get the value as a string.
550 SGPropertyNode::make_string () const
552 if (!getAttribute(READ))
556 return _value.alias->getStringValue();
558 return get_bool() ? "true" : "false";
560 case props::UNSPECIFIED:
579 sstr << std::setprecision(10) << get_double();
581 case props::EXTENDED:
583 props::Type realType = _value.val->getType();
584 // Perhaps this should be done for all types?
585 if (realType == props::VEC3D || realType == props::VEC4D)
587 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
593 _buffer = sstr.str();
594 return _buffer.c_str();
598 * Trace a write access for a property.
601 SGPropertyNode::trace_write () const
604 cerr << "TRACE: Write node " << getPath () << ", value \""
605 << make_string() << '"' << endl;
607 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
608 << ", value \"" << make_string() << '"');
613 * Trace a read access for a property.
616 SGPropertyNode::trace_read () const
619 cerr << "TRACE: Write node " << getPath () << ", value \""
620 << make_string() << '"' << endl;
622 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
623 << ", value \"" << make_string() << '"');
628 ////////////////////////////////////////////////////////////////////////
629 // Public methods from SGPropertyNode.
630 ////////////////////////////////////////////////////////////////////////
633 * Last used attribute
634 * Update as needed when enum Attribute is changed
636 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
639 * Default constructor: always creates a root node.
641 SGPropertyNode::SGPropertyNode ()
650 _local_val.string_val = 0;
658 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
659 : _index(node._index),
661 _parent(0), // don't copy the parent
666 _listeners(0) // CHECK!!
668 _local_val.string_val = 0;
670 if (_type == props::NONE)
672 if (_type == props::ALIAS) {
673 _value.alias = node._value.alias;
678 if (_tied || _type == props::EXTENDED) {
679 _value.val = node._value.val->clone();
684 set_bool(node.get_bool());
687 set_int(node.get_int());
690 set_long(node.get_long());
693 set_float(node.get_float());
696 set_double(node.get_double());
699 case props::UNSPECIFIED:
700 set_string(node.get_string());
709 * Convenience constructor.
711 template<typename Itr>
712 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
714 SGPropertyNode * parent)
724 _local_val.string_val = 0;
726 if (!validateName(_name))
727 throw string("plain name expected instead of '") + _name + '\'';
730 SGPropertyNode::SGPropertyNode (const string& name,
732 SGPropertyNode * parent)
742 _local_val.string_val = 0;
744 if (!validateName(name))
745 throw string("plain name expected instead of '") + _name + '\'';
751 SGPropertyNode::~SGPropertyNode ()
753 // zero out all parent pointers, else they might be dangling
754 for (unsigned i = 0; i < _children.size(); ++i)
755 _children[i]->_parent = 0;
756 for (unsigned i = 0; i < _removedChildren.size(); ++i)
757 _removedChildren[i]->_parent = 0;
762 vector<SGPropertyChangeListener*>::iterator it;
763 for (it = _listeners->begin(); it != _listeners->end(); ++it)
764 (*it)->unregister_property(this);
771 * Alias to another node.
774 SGPropertyNode::alias (SGPropertyNode * target)
776 if (target == 0 || _type == props::ALIAS || _tied)
780 _value.alias = target;
781 _type = props::ALIAS;
787 * Alias to another node by path.
790 SGPropertyNode::alias (const char * path)
792 return alias(getNode(path, true));
800 SGPropertyNode::unalias ()
802 if (_type != props::ALIAS)
810 * Get the target of an alias.
813 SGPropertyNode::getAliasTarget ()
815 return (_type == props::ALIAS ? _value.alias : 0);
819 const SGPropertyNode *
820 SGPropertyNode::getAliasTarget () const
822 return (_type == props::ALIAS ? _value.alias : 0);
826 * create a non-const child by name after the last node with the same name.
829 SGPropertyNode::addChild (const char * name)
831 int pos = find_last_child(name, _children)+1;
833 SGPropertyNode_ptr node;
834 node = new SGPropertyNode(name, name + strlen(name), pos, this);
835 _children.push_back(node);
836 fireChildAdded(node);
842 * Get a non-const child by index.
845 SGPropertyNode::getChild (int position)
847 if (position >= 0 && position < nChildren())
848 return _children[position];
855 * Get a const child by index.
857 const SGPropertyNode *
858 SGPropertyNode::getChild (int position) const
860 if (position >= 0 && position < nChildren())
861 return _children[position];
868 * Get a non-const child by name and index, creating if necessary.
872 SGPropertyNode::getChild (const char * name, int index, bool create)
874 return getChildImpl(name, name + strlen(name), index, create);
878 SGPropertyNode::getChild (const string& name, int index, bool create)
880 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
885 node = new SGPropertyNode(name, index, this);
886 _children.push_back(node);
887 fireChildAdded(node);
895 * Get a const child by name and index.
897 const SGPropertyNode *
898 SGPropertyNode::getChild (const char * name, int index) const
900 int pos = find_child(name, name + strlen(name), index, _children);
902 return _children[pos];
909 * Get all children with the same name (but different indices).
912 SGPropertyNode::getChildren (const char * name) const
914 PropertyList children;
915 int max = _children.size();
917 for (int i = 0; i < max; i++)
918 if (compare_strings(_children[i]->getName(), name))
919 children.push_back(_children[i]);
921 sort(children.begin(), children.end(), CompareIndices());
927 * Remove this node and all children from nodes that link to them
928 * in their path cache.
931 SGPropertyNode::remove_from_path_caches ()
933 for (unsigned int i = 0; i < _children.size(); ++i)
934 _children[i]->remove_from_path_caches();
936 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
937 _linkedNodes[i]->erase(this);
938 _linkedNodes.clear();
943 * Remove child by position.
946 SGPropertyNode::removeChild (int pos, bool keep)
948 SGPropertyNode_ptr node;
949 if (pos < 0 || pos >= (int)_children.size())
952 PropertyList::iterator it = _children.begin();
954 node = _children[pos];
957 _removedChildren.push_back(node);
960 node->remove_from_path_caches();
961 node->setAttribute(REMOVED, true);
963 fireChildRemoved(node);
969 * Remove a child node
972 SGPropertyNode::removeChild (const char * name, int index, bool keep)
974 SGPropertyNode_ptr ret;
975 int pos = find_child(name, name + strlen(name), index, _children);
977 ret = removeChild(pos, keep);
983 * Remove all children with the specified name.
986 SGPropertyNode::removeChildren (const char * name, bool keep)
988 PropertyList children;
990 for (int pos = _children.size() - 1; pos >= 0; pos--)
991 if (compare_strings(_children[pos]->getName(), name))
992 children.push_back(removeChild(pos, keep));
994 sort(children.begin(), children.end(), CompareIndices());
1000 * Remove a linked node.
1003 SGPropertyNode::remove_linked_node (hash_table * node)
1005 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
1006 if (_linkedNodes[i] == node) {
1007 vector<hash_table *>::iterator it = _linkedNodes.begin();
1009 _linkedNodes.erase(it);
1018 SGPropertyNode::getDisplayName (bool simplify) const
1020 string display_name = _name;
1021 if (_index != 0 || !simplify) {
1023 sstr << '[' << _index << ']';
1024 display_name += sstr.str();
1026 return display_name;
1031 SGPropertyNode::getPath (bool simplify) const
1033 typedef std::vector<SGConstPropertyNode_ptr> PList;
1035 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1036 pathList.push_back(node);
1038 for (PList::reverse_iterator itr = pathList.rbegin(),
1039 rend = pathList.rend();
1043 result += (*itr)->getDisplayName(simplify);
1049 SGPropertyNode::getType () const
1051 if (_type == props::ALIAS)
1052 return _value.alias->getType();
1053 else if (_type == props::EXTENDED)
1054 return _value.val->getType();
1061 SGPropertyNode::getBoolValue () const
1063 // Shortcut for common case
1064 if (_attr == (READ|WRITE) && _type == props::BOOL)
1067 if (getAttribute(TRACE_READ))
1069 if (!getAttribute(READ))
1070 return SGRawValue<bool>::DefaultValue();
1073 return _value.alias->getBoolValue();
1077 return get_int() == 0 ? false : true;
1079 return get_long() == 0L ? false : true;
1081 return get_float() == 0.0 ? false : true;
1083 return get_double() == 0.0L ? false : true;
1085 case props::UNSPECIFIED:
1086 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1089 return SGRawValue<bool>::DefaultValue();
1094 SGPropertyNode::getIntValue () const
1096 // Shortcut for common case
1097 if (_attr == (READ|WRITE) && _type == props::INT)
1100 if (getAttribute(TRACE_READ))
1102 if (!getAttribute(READ))
1103 return SGRawValue<int>::DefaultValue();
1106 return _value.alias->getIntValue();
1108 return int(get_bool());
1112 return int(get_long());
1114 return int(get_float());
1116 return int(get_double());
1118 case props::UNSPECIFIED:
1119 return atoi(get_string());
1122 return SGRawValue<int>::DefaultValue();
1127 SGPropertyNode::getLongValue () const
1129 // Shortcut for common case
1130 if (_attr == (READ|WRITE) && _type == props::LONG)
1133 if (getAttribute(TRACE_READ))
1135 if (!getAttribute(READ))
1136 return SGRawValue<long>::DefaultValue();
1139 return _value.alias->getLongValue();
1141 return long(get_bool());
1143 return long(get_int());
1147 return long(get_float());
1149 return long(get_double());
1151 case props::UNSPECIFIED:
1152 return strtol(get_string(), 0, 0);
1155 return SGRawValue<long>::DefaultValue();
1160 SGPropertyNode::getFloatValue () const
1162 // Shortcut for common case
1163 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1166 if (getAttribute(TRACE_READ))
1168 if (!getAttribute(READ))
1169 return SGRawValue<float>::DefaultValue();
1172 return _value.alias->getFloatValue();
1174 return float(get_bool());
1176 return float(get_int());
1178 return float(get_long());
1182 return float(get_double());
1184 case props::UNSPECIFIED:
1185 return atof(get_string());
1188 return SGRawValue<float>::DefaultValue();
1193 SGPropertyNode::getDoubleValue () const
1195 // Shortcut for common case
1196 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1197 return get_double();
1199 if (getAttribute(TRACE_READ))
1201 if (!getAttribute(READ))
1202 return SGRawValue<double>::DefaultValue();
1206 return _value.alias->getDoubleValue();
1208 return double(get_bool());
1210 return double(get_int());
1212 return double(get_long());
1214 return double(get_float());
1216 return get_double();
1218 case props::UNSPECIFIED:
1219 return strtod(get_string(), 0);
1222 return SGRawValue<double>::DefaultValue();
1227 SGPropertyNode::getStringValue () const
1229 // Shortcut for common case
1230 if (_attr == (READ|WRITE) && _type == props::STRING)
1231 return get_string();
1233 if (getAttribute(TRACE_READ))
1235 if (!getAttribute(READ))
1236 return SGRawValue<const char *>::DefaultValue();
1237 return make_string();
1241 SGPropertyNode::setBoolValue (bool value)
1243 // Shortcut for common case
1244 if (_attr == (READ|WRITE) && _type == props::BOOL)
1245 return set_bool(value);
1247 bool result = false;
1249 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1252 _type = props::BOOL;
1257 result = _value.alias->setBoolValue(value);
1260 result = set_bool(value);
1263 result = set_int(int(value));
1266 result = set_long(long(value));
1269 result = set_float(float(value));
1272 result = set_double(double(value));
1275 case props::UNSPECIFIED:
1276 result = set_string(value ? "true" : "false");
1283 if (getAttribute(TRACE_WRITE))
1289 SGPropertyNode::setIntValue (int value)
1291 // Shortcut for common case
1292 if (_attr == (READ|WRITE) && _type == props::INT)
1293 return set_int(value);
1295 bool result = false;
1297 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1300 _local_val.int_val = 0;
1305 result = _value.alias->setIntValue(value);
1308 result = set_bool(value == 0 ? false : true);
1311 result = set_int(value);
1314 result = set_long(long(value));
1317 result = set_float(float(value));
1320 result = set_double(double(value));
1323 case props::UNSPECIFIED: {
1325 sprintf(buf, "%d", value);
1326 result = set_string(buf);
1334 if (getAttribute(TRACE_WRITE))
1340 SGPropertyNode::setLongValue (long value)
1342 // Shortcut for common case
1343 if (_attr == (READ|WRITE) && _type == props::LONG)
1344 return set_long(value);
1346 bool result = false;
1348 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1350 _type = props::LONG;
1351 _local_val.long_val = 0L;
1356 result = _value.alias->setLongValue(value);
1359 result = set_bool(value == 0L ? false : true);
1362 result = set_int(int(value));
1365 result = set_long(value);
1368 result = set_float(float(value));
1371 result = set_double(double(value));
1374 case props::UNSPECIFIED: {
1376 sprintf(buf, "%ld", value);
1377 result = set_string(buf);
1385 if (getAttribute(TRACE_WRITE))
1391 SGPropertyNode::setFloatValue (float value)
1393 // Shortcut for common case
1394 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1395 return set_float(value);
1397 bool result = false;
1399 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1401 _type = props::FLOAT;
1402 _local_val.float_val = 0;
1407 result = _value.alias->setFloatValue(value);
1410 result = set_bool(value == 0.0 ? false : true);
1413 result = set_int(int(value));
1416 result = set_long(long(value));
1419 result = set_float(value);
1422 result = set_double(double(value));
1425 case props::UNSPECIFIED: {
1427 sprintf(buf, "%f", value);
1428 result = set_string(buf);
1436 if (getAttribute(TRACE_WRITE))
1442 SGPropertyNode::setDoubleValue (double value)
1444 // Shortcut for common case
1445 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1446 return set_double(value);
1448 bool result = false;
1450 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1452 _local_val.double_val = value;
1453 _type = props::DOUBLE;
1458 result = _value.alias->setDoubleValue(value);
1461 result = set_bool(value == 0.0L ? false : true);
1464 result = set_int(int(value));
1467 result = set_long(long(value));
1470 result = set_float(float(value));
1473 result = set_double(value);
1476 case props::UNSPECIFIED: {
1478 sprintf(buf, "%f", value);
1479 result = set_string(buf);
1487 if (getAttribute(TRACE_WRITE))
1493 SGPropertyNode::setStringValue (const char * value)
1495 // Shortcut for common case
1496 if (_attr == (READ|WRITE) && _type == props::STRING)
1497 return set_string(value);
1499 bool result = false;
1501 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1503 _type = props::STRING;
1508 result = _value.alias->setStringValue(value);
1511 result = set_bool((compare_strings(value, "true")
1512 || atoi(value)) ? true : false);
1515 result = set_int(atoi(value));
1518 result = set_long(strtol(value, 0, 0));
1521 result = set_float(atof(value));
1524 result = set_double(strtod(value, 0));
1527 case props::UNSPECIFIED:
1528 result = set_string(value);
1530 case props::EXTENDED:
1532 stringstream sstr(value);
1533 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1541 if (getAttribute(TRACE_WRITE))
1547 SGPropertyNode::setUnspecifiedValue (const char * value)
1549 bool result = false;
1551 if (_type == props::NONE) {
1553 _type = props::UNSPECIFIED;
1555 props::Type type = _type;
1556 if (type == props::EXTENDED)
1557 type = _value.val->getType();
1560 result = _value.alias->setUnspecifiedValue(value);
1563 result = set_bool((compare_strings(value, "true")
1564 || atoi(value)) ? true : false);
1567 result = set_int(atoi(value));
1570 result = set_long(strtol(value, 0, 0));
1573 result = set_float(atof(value));
1576 result = set_double(strtod(value, 0));
1579 case props::UNSPECIFIED:
1580 result = set_string(value);
1583 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1586 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1593 if (getAttribute(TRACE_WRITE))
1598 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1600 if (!getAttribute(READ))
1604 return _value.alias->printOn(stream);
1606 stream << (get_bool() ? "true" : "false");
1609 stream << get_int();
1612 stream << get_long();
1615 stream << get_float();
1618 stream << get_double();
1621 case props::UNSPECIFIED:
1622 stream << get_string();
1624 case props::EXTENDED:
1625 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1629 default: // avoid compiler warning
1636 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1639 if (_type == props::ALIAS || _tied)
1642 useDefault = useDefault && hasValue();
1643 std::string old_val;
1645 old_val = getStringValue();
1647 _type = props::STRING;
1649 _value.val = rawValue.clone();
1652 setStringValue(old_val.c_str());
1657 SGPropertyNode::untie ()
1664 bool val = getBoolValue();
1666 _type = props::BOOL;
1667 _local_val.bool_val = val;
1671 int val = getIntValue();
1674 _local_val.int_val = val;
1678 long val = getLongValue();
1680 _type = props::LONG;
1681 _local_val.long_val = val;
1684 case props::FLOAT: {
1685 float val = getFloatValue();
1687 _type = props::FLOAT;
1688 _local_val.float_val = val;
1691 case props::DOUBLE: {
1692 double val = getDoubleValue();
1694 _type = props::DOUBLE;
1695 _local_val.double_val = val;
1699 case props::UNSPECIFIED: {
1700 string val = getStringValue();
1702 _type = props::STRING;
1703 _local_val.string_val = copy_string(val.c_str());
1706 case props::EXTENDED: {
1707 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1708 _value.val = 0; // Prevent clearValue() from deleting
1710 _type = props::EXTENDED;
1711 _value.val = val->makeContainer();
1725 SGPropertyNode::getRootNode ()
1730 return _parent->getRootNode();
1733 const SGPropertyNode *
1734 SGPropertyNode::getRootNode () const
1739 return _parent->getRootNode();
1743 SGPropertyNode::getNode (const char * relative_path, bool create)
1745 using namespace boost;
1746 if (_path_cache == 0)
1747 _path_cache = new hash_table;
1749 SGPropertyNode * result = _path_cache->get(relative_path);
1751 result = find_node(this,
1752 make_iterator_range(relative_path, relative_path
1753 + strlen(relative_path)),
1756 _path_cache->put(relative_path, result);
1763 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1765 using namespace boost;
1766 return find_node(this, make_iterator_range(relative_path, relative_path
1767 + strlen(relative_path)),
1771 const SGPropertyNode *
1772 SGPropertyNode::getNode (const char * relative_path) const
1774 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1777 const SGPropertyNode *
1778 SGPropertyNode::getNode (const char * relative_path, int index) const
1780 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1784 ////////////////////////////////////////////////////////////////////////
1785 // Convenience methods using relative paths.
1786 ////////////////////////////////////////////////////////////////////////
1790 * Test whether another node has a value attached.
1793 SGPropertyNode::hasValue (const char * relative_path) const
1795 const SGPropertyNode * node = getNode(relative_path);
1796 return (node == 0 ? false : node->hasValue());
1801 * Get the value type for another node.
1804 SGPropertyNode::getType (const char * relative_path) const
1806 const SGPropertyNode * node = getNode(relative_path);
1807 return (node == 0 ? props::UNSPECIFIED : node->getType());
1812 * Get a bool value for another node.
1815 SGPropertyNode::getBoolValue (const char * relative_path,
1816 bool defaultValue) const
1818 const SGPropertyNode * node = getNode(relative_path);
1819 return (node == 0 ? defaultValue : node->getBoolValue());
1824 * Get an int value for another node.
1827 SGPropertyNode::getIntValue (const char * relative_path,
1828 int defaultValue) const
1830 const SGPropertyNode * node = getNode(relative_path);
1831 return (node == 0 ? defaultValue : node->getIntValue());
1836 * Get a long value for another node.
1839 SGPropertyNode::getLongValue (const char * relative_path,
1840 long defaultValue) const
1842 const SGPropertyNode * node = getNode(relative_path);
1843 return (node == 0 ? defaultValue : node->getLongValue());
1848 * Get a float value for another node.
1851 SGPropertyNode::getFloatValue (const char * relative_path,
1852 float defaultValue) const
1854 const SGPropertyNode * node = getNode(relative_path);
1855 return (node == 0 ? defaultValue : node->getFloatValue());
1860 * Get a double value for another node.
1863 SGPropertyNode::getDoubleValue (const char * relative_path,
1864 double defaultValue) const
1866 const SGPropertyNode * node = getNode(relative_path);
1867 return (node == 0 ? defaultValue : node->getDoubleValue());
1872 * Get a string value for another node.
1875 SGPropertyNode::getStringValue (const char * relative_path,
1876 const char * defaultValue) const
1878 const SGPropertyNode * node = getNode(relative_path);
1879 return (node == 0 ? defaultValue : node->getStringValue());
1884 * Set a bool value for another node.
1887 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1889 return getNode(relative_path, true)->setBoolValue(value);
1894 * Set an int value for another node.
1897 SGPropertyNode::setIntValue (const char * relative_path, int value)
1899 return getNode(relative_path, true)->setIntValue(value);
1904 * Set a long value for another node.
1907 SGPropertyNode::setLongValue (const char * relative_path, long value)
1909 return getNode(relative_path, true)->setLongValue(value);
1914 * Set a float value for another node.
1917 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1919 return getNode(relative_path, true)->setFloatValue(value);
1924 * Set a double value for another node.
1927 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1929 return getNode(relative_path, true)->setDoubleValue(value);
1934 * Set a string value for another node.
1937 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1939 return getNode(relative_path, true)->setStringValue(value);
1944 * Set an unknown value for another node.
1947 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1950 return getNode(relative_path, true)->setUnspecifiedValue(value);
1955 * Test whether another node is tied.
1958 SGPropertyNode::isTied (const char * relative_path) const
1960 const SGPropertyNode * node = getNode(relative_path);
1961 return (node == 0 ? false : node->isTied());
1966 * Tie a node reached by a relative path, creating it if necessary.
1969 SGPropertyNode::tie (const char * relative_path,
1970 const SGRawValue<bool> &rawValue,
1973 return getNode(relative_path, true)->tie(rawValue, useDefault);
1978 * Tie a node reached by a relative path, creating it if necessary.
1981 SGPropertyNode::tie (const char * relative_path,
1982 const SGRawValue<int> &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<long> &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<float> &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<double> &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<const char *> &rawValue,
2033 return getNode(relative_path, true)->tie(rawValue, useDefault);
2038 * Attempt to untie another node reached by a relative path.
2041 SGPropertyNode::untie (const char * relative_path)
2043 SGPropertyNode * node = getNode(relative_path);
2044 return (node == 0 ? false : node->untie());
2048 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2051 if (_listeners == 0)
2052 _listeners = new vector<SGPropertyChangeListener*>;
2053 _listeners->push_back(listener);
2054 listener->register_property(this);
2056 listener->valueChanged(this);
2060 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2062 vector<SGPropertyChangeListener*>::iterator it =
2063 find(_listeners->begin(), _listeners->end(), listener);
2064 if (it != _listeners->end()) {
2065 _listeners->erase(it);
2066 listener->unregister_property(this);
2067 if (_listeners->empty()) {
2068 vector<SGPropertyChangeListener*>* tmp = _listeners;
2076 SGPropertyNode::fireValueChanged ()
2078 fireValueChanged(this);
2082 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2084 fireChildAdded(this, child);
2088 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2090 fireChildRemoved(this, child);
2094 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2096 if (_listeners != 0) {
2097 for (unsigned int i = 0; i < _listeners->size(); i++) {
2098 (*_listeners)[i]->valueChanged(node);
2102 _parent->fireValueChanged(node);
2106 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2107 SGPropertyNode * child)
2109 if (_listeners != 0) {
2110 for (unsigned int i = 0; i < _listeners->size(); i++) {
2111 (*_listeners)[i]->childAdded(parent, child);
2115 _parent->fireChildAdded(parent, child);
2119 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2120 SGPropertyNode * child)
2122 if (_listeners != 0) {
2123 for (unsigned int i = 0; i < _listeners->size(); i++) {
2124 (*_listeners)[i]->childRemoved(parent, child);
2128 _parent->fireChildRemoved(parent, child);
2133 ////////////////////////////////////////////////////////////////////////
2134 // Simplified hash table for caching paths.
2135 ////////////////////////////////////////////////////////////////////////
2137 #define HASH_TABLE_SIZE 199
2139 SGPropertyNode::hash_table::entry::entry ()
2144 SGPropertyNode::hash_table::entry::~entry ()
2146 // Don't delete the value; we don't own
2151 SGPropertyNode::hash_table::entry::set_key (const char * key)
2157 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2162 SGPropertyNode::hash_table::bucket::bucket ()
2168 SGPropertyNode::hash_table::bucket::~bucket ()
2170 for (int i = 0; i < _length; i++)
2175 SGPropertyNode::hash_table::entry *
2176 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2179 for (i = 0; i < _length; i++) {
2180 if (!strcmp(_entries[i]->get_key(), key))
2184 entry ** new_entries = new entry*[_length+1];
2185 for (i = 0; i < _length; i++) {
2186 new_entries[i] = _entries[i];
2189 _entries = new_entries;
2190 _entries[_length] = new entry;
2191 _entries[_length]->set_key(key);
2193 return _entries[_length - 1];
2200 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2202 for (int i = 0; i < _length; i++) {
2203 if (_entries[i]->get_value() == node) {
2205 for (++i; i < _length; i++) {
2206 _entries[i-1] = _entries[i];
2216 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2218 for (int i = 0; i < _length; i++) {
2219 SGPropertyNode * node = _entries[i]->get_value();
2221 node->remove_linked_node(owner);
2225 SGPropertyNode::hash_table::hash_table ()
2231 SGPropertyNode::hash_table::~hash_table ()
2233 for (unsigned int i = 0; i < _data_length; i++) {
2235 _data[i]->clear(this);
2243 SGPropertyNode::hash_table::get (const char * key)
2245 if (_data_length == 0)
2247 unsigned int index = hashcode(key) % _data_length;
2248 if (_data[index] == 0)
2250 entry * e = _data[index]->get_entry(key);
2254 return e->get_value();
2258 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2260 if (_data_length == 0) {
2261 _data = new bucket*[HASH_TABLE_SIZE];
2262 _data_length = HASH_TABLE_SIZE;
2263 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2266 unsigned int index = hashcode(key) % _data_length;
2267 if (_data[index] == 0) {
2268 _data[index] = new bucket;
2270 entry * e = _data[index]->get_entry(key, true);
2271 e->set_value(value);
2272 value->add_linked_node(this);
2276 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2278 for (unsigned int i = 0; i < _data_length; i++)
2279 if (_data[i] && _data[i]->erase(node))
2286 SGPropertyNode::hash_table::hashcode (const char * key)
2288 unsigned int hash = 0;
2290 hash = 31 * hash + *key;
2298 ////////////////////////////////////////////////////////////////////////
2299 // Implementation of SGPropertyChangeListener.
2300 ////////////////////////////////////////////////////////////////////////
2302 SGPropertyChangeListener::~SGPropertyChangeListener ()
2304 for (int i = _properties.size() - 1; i >= 0; i--)
2305 _properties[i]->removeChangeListener(this);
2309 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2315 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2316 SGPropertyNode * child)
2322 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2323 SGPropertyNode * child)
2329 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2331 _properties.push_back(node);
2335 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2337 vector<SGPropertyNode *>::iterator it =
2338 find(_properties.begin(), _properties.end(), node);
2339 if (it != _properties.end())
2340 _properties.erase(it);
2344 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2347 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2348 for (int i = 0; i < 3; ++i) {
2359 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2361 for (int i = 0; i < 3; ++i) {
2362 stream >> result[i];
2368 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2371 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2372 for (int i = 0; i < 4; ++i) {
2383 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2385 for (int i = 0; i < 4; ++i) {
2386 stream >> result[i];
2393 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2395 props::Type ltype = lhs.getType();
2396 props::Type rtype = rhs.getType();
2403 return false; // XXX Should we look in aliases?
2405 return lhs.getValue<bool>() == rhs.getValue<bool>();
2407 return lhs.getValue<int>() == rhs.getValue<int>();
2409 return lhs.getValue<long>() == rhs.getValue<long>();
2411 return lhs.getValue<float>() == rhs.getValue<float>();
2413 return lhs.getValue<double>() == rhs.getValue<double>();
2415 case props::UNSPECIFIED:
2416 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2418 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2420 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2428 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2429 const SGPropertyNode& rhs)
2433 int lhsChildren = lhs.nChildren();
2434 int rhsChildren = rhs.nChildren();
2435 if (lhsChildren != rhsChildren)
2437 if (lhsChildren == 0)
2438 return compareNodeValue(lhs, rhs);
2439 for (size_t i = 0; i < lhs._children.size(); ++i) {
2440 const SGPropertyNode* lchild = lhs._children[i];
2441 const SGPropertyNode* rchild = rhs._children[i];
2442 // I'm guessing that the nodes will usually be in the same
2444 if (lchild->getIndex() != rchild->getIndex()
2445 || lchild->getNameString() != rchild->getNameString()) {
2447 for (PropertyList::const_iterator itr = rhs._children.begin(),
2448 end = rhs._children.end();
2451 if (lchild->getIndex() == (*itr)->getIndex()
2452 && lchild->getNameString() == (*itr)->getNameString()) {
2459 if (!compare(*lchild, *rchild))
2465 struct PropertyPlaceLess {
2466 typedef bool result_type;
2467 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2469 int comp = lhs->getNameString().compare(rhs->getNameString());
2471 return lhs->getIndex() < rhs->getIndex();
2477 size_t hash_value(const SGPropertyNode& node)
2479 using namespace boost;
2481 if (node.nChildren() == 0) {
2482 switch (node.getType()) {
2487 return hash_value(node.getValue<bool>());
2489 return hash_value(node.getValue<int>());
2491 return hash_value(node.getValue<long>());
2493 return hash_value(node.getValue<float>());
2495 return hash_value(node.getValue<double>());
2497 case props::UNSPECIFIED:
2499 const char *val = node.getStringValue();
2500 return hash_range(val, val + strlen(val));
2504 const SGVec3d val = node.getValue<SGVec3d>();
2505 return hash_range(&val[0], &val[3]);
2509 const SGVec4d val = node.getValue<SGVec4d>();
2510 return hash_range(&val[0], &val[4]);
2512 case props::ALIAS: // XXX Should we look in aliases?
2518 PropertyList children(node._children.begin(), node._children.end());
2519 sort(children.begin(), children.end(), PropertyPlaceLess());
2520 for (PropertyList::const_iterator itr = children.begin(),
2521 end = children.end();
2524 hash_combine(seed, (*itr)->_name);
2525 hash_combine(seed, (*itr)->_index);
2526 hash_combine(seed, hash_value(**itr));