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 // Calculate the complete path only once.
1034 if (_parent != 0 && _path.empty()) {
1035 _path = _parent->getPath(simplify);
1037 _path += getDisplayName(simplify);
1040 return _path.c_str();
1044 SGPropertyNode::getType () const
1046 if (_type == props::ALIAS)
1047 return _value.alias->getType();
1048 else if (_type == props::EXTENDED)
1049 return _value.val->getType();
1056 SGPropertyNode::getBoolValue () const
1058 // Shortcut for common case
1059 if (_attr == (READ|WRITE) && _type == props::BOOL)
1062 if (getAttribute(TRACE_READ))
1064 if (!getAttribute(READ))
1065 return SGRawValue<bool>::DefaultValue();
1068 return _value.alias->getBoolValue();
1072 return get_int() == 0 ? false : true;
1074 return get_long() == 0L ? false : true;
1076 return get_float() == 0.0 ? false : true;
1078 return get_double() == 0.0L ? false : true;
1080 case props::UNSPECIFIED:
1081 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1084 return SGRawValue<bool>::DefaultValue();
1089 SGPropertyNode::getIntValue () const
1091 // Shortcut for common case
1092 if (_attr == (READ|WRITE) && _type == props::INT)
1095 if (getAttribute(TRACE_READ))
1097 if (!getAttribute(READ))
1098 return SGRawValue<int>::DefaultValue();
1101 return _value.alias->getIntValue();
1103 return int(get_bool());
1107 return int(get_long());
1109 return int(get_float());
1111 return int(get_double());
1113 case props::UNSPECIFIED:
1114 return atoi(get_string());
1117 return SGRawValue<int>::DefaultValue();
1122 SGPropertyNode::getLongValue () const
1124 // Shortcut for common case
1125 if (_attr == (READ|WRITE) && _type == props::LONG)
1128 if (getAttribute(TRACE_READ))
1130 if (!getAttribute(READ))
1131 return SGRawValue<long>::DefaultValue();
1134 return _value.alias->getLongValue();
1136 return long(get_bool());
1138 return long(get_int());
1142 return long(get_float());
1144 return long(get_double());
1146 case props::UNSPECIFIED:
1147 return strtol(get_string(), 0, 0);
1150 return SGRawValue<long>::DefaultValue();
1155 SGPropertyNode::getFloatValue () const
1157 // Shortcut for common case
1158 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1161 if (getAttribute(TRACE_READ))
1163 if (!getAttribute(READ))
1164 return SGRawValue<float>::DefaultValue();
1167 return _value.alias->getFloatValue();
1169 return float(get_bool());
1171 return float(get_int());
1173 return float(get_long());
1177 return float(get_double());
1179 case props::UNSPECIFIED:
1180 return atof(get_string());
1183 return SGRawValue<float>::DefaultValue();
1188 SGPropertyNode::getDoubleValue () const
1190 // Shortcut for common case
1191 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1192 return get_double();
1194 if (getAttribute(TRACE_READ))
1196 if (!getAttribute(READ))
1197 return SGRawValue<double>::DefaultValue();
1201 return _value.alias->getDoubleValue();
1203 return double(get_bool());
1205 return double(get_int());
1207 return double(get_long());
1209 return double(get_float());
1211 return get_double();
1213 case props::UNSPECIFIED:
1214 return strtod(get_string(), 0);
1217 return SGRawValue<double>::DefaultValue();
1222 SGPropertyNode::getStringValue () const
1224 // Shortcut for common case
1225 if (_attr == (READ|WRITE) && _type == props::STRING)
1226 return get_string();
1228 if (getAttribute(TRACE_READ))
1230 if (!getAttribute(READ))
1231 return SGRawValue<const char *>::DefaultValue();
1232 return make_string();
1236 SGPropertyNode::setBoolValue (bool value)
1238 // Shortcut for common case
1239 if (_attr == (READ|WRITE) && _type == props::BOOL)
1240 return set_bool(value);
1242 bool result = false;
1244 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1247 _type = props::BOOL;
1252 result = _value.alias->setBoolValue(value);
1255 result = set_bool(value);
1258 result = set_int(int(value));
1261 result = set_long(long(value));
1264 result = set_float(float(value));
1267 result = set_double(double(value));
1270 case props::UNSPECIFIED:
1271 result = set_string(value ? "true" : "false");
1278 if (getAttribute(TRACE_WRITE))
1284 SGPropertyNode::setIntValue (int value)
1286 // Shortcut for common case
1287 if (_attr == (READ|WRITE) && _type == props::INT)
1288 return set_int(value);
1290 bool result = false;
1292 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1295 _local_val.int_val = 0;
1300 result = _value.alias->setIntValue(value);
1303 result = set_bool(value == 0 ? false : true);
1306 result = set_int(value);
1309 result = set_long(long(value));
1312 result = set_float(float(value));
1315 result = set_double(double(value));
1318 case props::UNSPECIFIED: {
1320 sprintf(buf, "%d", value);
1321 result = set_string(buf);
1329 if (getAttribute(TRACE_WRITE))
1335 SGPropertyNode::setLongValue (long value)
1337 // Shortcut for common case
1338 if (_attr == (READ|WRITE) && _type == props::LONG)
1339 return set_long(value);
1341 bool result = false;
1343 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1345 _type = props::LONG;
1346 _local_val.long_val = 0L;
1351 result = _value.alias->setLongValue(value);
1354 result = set_bool(value == 0L ? false : true);
1357 result = set_int(int(value));
1360 result = set_long(value);
1363 result = set_float(float(value));
1366 result = set_double(double(value));
1369 case props::UNSPECIFIED: {
1371 sprintf(buf, "%ld", value);
1372 result = set_string(buf);
1380 if (getAttribute(TRACE_WRITE))
1386 SGPropertyNode::setFloatValue (float value)
1388 // Shortcut for common case
1389 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1390 return set_float(value);
1392 bool result = false;
1394 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1396 _type = props::FLOAT;
1397 _local_val.float_val = 0;
1402 result = _value.alias->setFloatValue(value);
1405 result = set_bool(value == 0.0 ? false : true);
1408 result = set_int(int(value));
1411 result = set_long(long(value));
1414 result = set_float(value);
1417 result = set_double(double(value));
1420 case props::UNSPECIFIED: {
1422 sprintf(buf, "%f", value);
1423 result = set_string(buf);
1431 if (getAttribute(TRACE_WRITE))
1437 SGPropertyNode::setDoubleValue (double value)
1439 // Shortcut for common case
1440 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1441 return set_double(value);
1443 bool result = false;
1445 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1447 _local_val.double_val = value;
1448 _type = props::DOUBLE;
1453 result = _value.alias->setDoubleValue(value);
1456 result = set_bool(value == 0.0L ? false : true);
1459 result = set_int(int(value));
1462 result = set_long(long(value));
1465 result = set_float(float(value));
1468 result = set_double(value);
1471 case props::UNSPECIFIED: {
1473 sprintf(buf, "%f", value);
1474 result = set_string(buf);
1482 if (getAttribute(TRACE_WRITE))
1488 SGPropertyNode::setStringValue (const char * value)
1490 // Shortcut for common case
1491 if (_attr == (READ|WRITE) && _type == props::STRING)
1492 return set_string(value);
1494 bool result = false;
1496 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1498 _type = props::STRING;
1503 result = _value.alias->setStringValue(value);
1506 result = set_bool((compare_strings(value, "true")
1507 || atoi(value)) ? true : false);
1510 result = set_int(atoi(value));
1513 result = set_long(strtol(value, 0, 0));
1516 result = set_float(atof(value));
1519 result = set_double(strtod(value, 0));
1522 case props::UNSPECIFIED:
1523 result = set_string(value);
1525 case props::EXTENDED:
1527 stringstream sstr(value);
1528 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1536 if (getAttribute(TRACE_WRITE))
1542 SGPropertyNode::setUnspecifiedValue (const char * value)
1544 bool result = false;
1546 if (_type == props::NONE) {
1548 _type = props::UNSPECIFIED;
1550 props::Type type = _type;
1551 if (type == props::EXTENDED)
1552 type = _value.val->getType();
1555 result = _value.alias->setUnspecifiedValue(value);
1558 result = set_bool((compare_strings(value, "true")
1559 || atoi(value)) ? true : false);
1562 result = set_int(atoi(value));
1565 result = set_long(strtol(value, 0, 0));
1568 result = set_float(atof(value));
1571 result = set_double(strtod(value, 0));
1574 case props::UNSPECIFIED:
1575 result = set_string(value);
1578 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1581 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1588 if (getAttribute(TRACE_WRITE))
1593 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1595 if (!getAttribute(READ))
1599 return _value.alias->printOn(stream);
1601 stream << (get_bool() ? "true" : "false");
1604 stream << get_int();
1607 stream << get_long();
1610 stream << get_float();
1613 stream << get_double();
1616 case props::UNSPECIFIED:
1617 stream << get_string();
1619 case props::EXTENDED:
1620 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1624 default: // avoid compiler warning
1631 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1634 if (_type == props::ALIAS || _tied)
1637 useDefault = useDefault && hasValue();
1638 std::string old_val;
1640 old_val = getStringValue();
1642 _type = props::STRING;
1644 _value.val = rawValue.clone();
1647 setStringValue(old_val.c_str());
1652 SGPropertyNode::untie ()
1659 bool val = getBoolValue();
1661 _type = props::BOOL;
1662 _local_val.bool_val = val;
1666 int val = getIntValue();
1669 _local_val.int_val = val;
1673 long val = getLongValue();
1675 _type = props::LONG;
1676 _local_val.long_val = val;
1679 case props::FLOAT: {
1680 float val = getFloatValue();
1682 _type = props::FLOAT;
1683 _local_val.float_val = val;
1686 case props::DOUBLE: {
1687 double val = getDoubleValue();
1689 _type = props::DOUBLE;
1690 _local_val.double_val = val;
1694 case props::UNSPECIFIED: {
1695 string val = getStringValue();
1697 _type = props::STRING;
1698 _local_val.string_val = copy_string(val.c_str());
1701 case props::EXTENDED: {
1702 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1703 _value.val = 0; // Prevent clearValue() from deleting
1705 _type = props::EXTENDED;
1706 _value.val = val->makeContainer();
1720 SGPropertyNode::getRootNode ()
1725 return _parent->getRootNode();
1728 const SGPropertyNode *
1729 SGPropertyNode::getRootNode () const
1734 return _parent->getRootNode();
1738 SGPropertyNode::getNode (const char * relative_path, bool create)
1740 using namespace boost;
1741 if (_path_cache == 0)
1742 _path_cache = new hash_table;
1744 SGPropertyNode * result = _path_cache->get(relative_path);
1746 result = find_node(this,
1747 make_iterator_range(relative_path, relative_path
1748 + strlen(relative_path)),
1751 _path_cache->put(relative_path, result);
1758 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1760 using namespace boost;
1761 return find_node(this, make_iterator_range(relative_path, relative_path
1762 + strlen(relative_path)),
1766 const SGPropertyNode *
1767 SGPropertyNode::getNode (const char * relative_path) const
1769 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1772 const SGPropertyNode *
1773 SGPropertyNode::getNode (const char * relative_path, int index) const
1775 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1779 ////////////////////////////////////////////////////////////////////////
1780 // Convenience methods using relative paths.
1781 ////////////////////////////////////////////////////////////////////////
1785 * Test whether another node has a value attached.
1788 SGPropertyNode::hasValue (const char * relative_path) const
1790 const SGPropertyNode * node = getNode(relative_path);
1791 return (node == 0 ? false : node->hasValue());
1796 * Get the value type for another node.
1799 SGPropertyNode::getType (const char * relative_path) const
1801 const SGPropertyNode * node = getNode(relative_path);
1802 return (node == 0 ? props::UNSPECIFIED : node->getType());
1807 * Get a bool value for another node.
1810 SGPropertyNode::getBoolValue (const char * relative_path,
1811 bool defaultValue) const
1813 const SGPropertyNode * node = getNode(relative_path);
1814 return (node == 0 ? defaultValue : node->getBoolValue());
1819 * Get an int value for another node.
1822 SGPropertyNode::getIntValue (const char * relative_path,
1823 int defaultValue) const
1825 const SGPropertyNode * node = getNode(relative_path);
1826 return (node == 0 ? defaultValue : node->getIntValue());
1831 * Get a long value for another node.
1834 SGPropertyNode::getLongValue (const char * relative_path,
1835 long defaultValue) const
1837 const SGPropertyNode * node = getNode(relative_path);
1838 return (node == 0 ? defaultValue : node->getLongValue());
1843 * Get a float value for another node.
1846 SGPropertyNode::getFloatValue (const char * relative_path,
1847 float defaultValue) const
1849 const SGPropertyNode * node = getNode(relative_path);
1850 return (node == 0 ? defaultValue : node->getFloatValue());
1855 * Get a double value for another node.
1858 SGPropertyNode::getDoubleValue (const char * relative_path,
1859 double defaultValue) const
1861 const SGPropertyNode * node = getNode(relative_path);
1862 return (node == 0 ? defaultValue : node->getDoubleValue());
1867 * Get a string value for another node.
1870 SGPropertyNode::getStringValue (const char * relative_path,
1871 const char * defaultValue) const
1873 const SGPropertyNode * node = getNode(relative_path);
1874 return (node == 0 ? defaultValue : node->getStringValue());
1879 * Set a bool value for another node.
1882 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1884 return getNode(relative_path, true)->setBoolValue(value);
1889 * Set an int value for another node.
1892 SGPropertyNode::setIntValue (const char * relative_path, int value)
1894 return getNode(relative_path, true)->setIntValue(value);
1899 * Set a long value for another node.
1902 SGPropertyNode::setLongValue (const char * relative_path, long value)
1904 return getNode(relative_path, true)->setLongValue(value);
1909 * Set a float value for another node.
1912 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1914 return getNode(relative_path, true)->setFloatValue(value);
1919 * Set a double value for another node.
1922 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1924 return getNode(relative_path, true)->setDoubleValue(value);
1929 * Set a string value for another node.
1932 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1934 return getNode(relative_path, true)->setStringValue(value);
1939 * Set an unknown value for another node.
1942 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1945 return getNode(relative_path, true)->setUnspecifiedValue(value);
1950 * Test whether another node is tied.
1953 SGPropertyNode::isTied (const char * relative_path) const
1955 const SGPropertyNode * node = getNode(relative_path);
1956 return (node == 0 ? false : node->isTied());
1961 * Tie a node reached by a relative path, creating it if necessary.
1964 SGPropertyNode::tie (const char * relative_path,
1965 const SGRawValue<bool> &rawValue,
1968 return getNode(relative_path, true)->tie(rawValue, useDefault);
1973 * Tie a node reached by a relative path, creating it if necessary.
1976 SGPropertyNode::tie (const char * relative_path,
1977 const SGRawValue<int> &rawValue,
1980 return getNode(relative_path, true)->tie(rawValue, useDefault);
1985 * Tie a node reached by a relative path, creating it if necessary.
1988 SGPropertyNode::tie (const char * relative_path,
1989 const SGRawValue<long> &rawValue,
1992 return getNode(relative_path, true)->tie(rawValue, useDefault);
1997 * Tie a node reached by a relative path, creating it if necessary.
2000 SGPropertyNode::tie (const char * relative_path,
2001 const SGRawValue<float> &rawValue,
2004 return getNode(relative_path, true)->tie(rawValue, useDefault);
2009 * Tie a node reached by a relative path, creating it if necessary.
2012 SGPropertyNode::tie (const char * relative_path,
2013 const SGRawValue<double> &rawValue,
2016 return getNode(relative_path, true)->tie(rawValue, useDefault);
2021 * Tie a node reached by a relative path, creating it if necessary.
2024 SGPropertyNode::tie (const char * relative_path,
2025 const SGRawValue<const char *> &rawValue,
2028 return getNode(relative_path, true)->tie(rawValue, useDefault);
2033 * Attempt to untie another node reached by a relative path.
2036 SGPropertyNode::untie (const char * relative_path)
2038 SGPropertyNode * node = getNode(relative_path);
2039 return (node == 0 ? false : node->untie());
2043 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2046 if (_listeners == 0)
2047 _listeners = new vector<SGPropertyChangeListener*>;
2048 _listeners->push_back(listener);
2049 listener->register_property(this);
2051 listener->valueChanged(this);
2055 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2057 vector<SGPropertyChangeListener*>::iterator it =
2058 find(_listeners->begin(), _listeners->end(), listener);
2059 if (it != _listeners->end()) {
2060 _listeners->erase(it);
2061 listener->unregister_property(this);
2062 if (_listeners->empty()) {
2063 vector<SGPropertyChangeListener*>* tmp = _listeners;
2071 SGPropertyNode::fireValueChanged ()
2073 fireValueChanged(this);
2077 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2079 fireChildAdded(this, child);
2083 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2085 fireChildRemoved(this, child);
2089 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2091 if (_listeners != 0) {
2092 for (unsigned int i = 0; i < _listeners->size(); i++) {
2093 (*_listeners)[i]->valueChanged(node);
2097 _parent->fireValueChanged(node);
2101 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2102 SGPropertyNode * child)
2104 if (_listeners != 0) {
2105 for (unsigned int i = 0; i < _listeners->size(); i++) {
2106 (*_listeners)[i]->childAdded(parent, child);
2110 _parent->fireChildAdded(parent, child);
2114 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2115 SGPropertyNode * child)
2117 if (_listeners != 0) {
2118 for (unsigned int i = 0; i < _listeners->size(); i++) {
2119 (*_listeners)[i]->childRemoved(parent, child);
2123 _parent->fireChildRemoved(parent, child);
2128 ////////////////////////////////////////////////////////////////////////
2129 // Simplified hash table for caching paths.
2130 ////////////////////////////////////////////////////////////////////////
2132 #define HASH_TABLE_SIZE 199
2134 SGPropertyNode::hash_table::entry::entry ()
2139 SGPropertyNode::hash_table::entry::~entry ()
2141 // Don't delete the value; we don't own
2146 SGPropertyNode::hash_table::entry::set_key (const char * key)
2152 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2157 SGPropertyNode::hash_table::bucket::bucket ()
2163 SGPropertyNode::hash_table::bucket::~bucket ()
2165 for (int i = 0; i < _length; i++)
2170 SGPropertyNode::hash_table::entry *
2171 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2174 for (i = 0; i < _length; i++) {
2175 if (!strcmp(_entries[i]->get_key(), key))
2179 entry ** new_entries = new entry*[_length+1];
2180 for (i = 0; i < _length; i++) {
2181 new_entries[i] = _entries[i];
2184 _entries = new_entries;
2185 _entries[_length] = new entry;
2186 _entries[_length]->set_key(key);
2188 return _entries[_length - 1];
2195 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2197 for (int i = 0; i < _length; i++) {
2198 if (_entries[i]->get_value() == node) {
2200 for (++i; i < _length; i++) {
2201 _entries[i-1] = _entries[i];
2211 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2213 for (int i = 0; i < _length; i++) {
2214 SGPropertyNode * node = _entries[i]->get_value();
2216 node->remove_linked_node(owner);
2220 SGPropertyNode::hash_table::hash_table ()
2226 SGPropertyNode::hash_table::~hash_table ()
2228 for (unsigned int i = 0; i < _data_length; i++) {
2230 _data[i]->clear(this);
2238 SGPropertyNode::hash_table::get (const char * key)
2240 if (_data_length == 0)
2242 unsigned int index = hashcode(key) % _data_length;
2243 if (_data[index] == 0)
2245 entry * e = _data[index]->get_entry(key);
2249 return e->get_value();
2253 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2255 if (_data_length == 0) {
2256 _data = new bucket*[HASH_TABLE_SIZE];
2257 _data_length = HASH_TABLE_SIZE;
2258 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2261 unsigned int index = hashcode(key) % _data_length;
2262 if (_data[index] == 0) {
2263 _data[index] = new bucket;
2265 entry * e = _data[index]->get_entry(key, true);
2266 e->set_value(value);
2267 value->add_linked_node(this);
2271 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2273 for (unsigned int i = 0; i < _data_length; i++)
2274 if (_data[i] && _data[i]->erase(node))
2281 SGPropertyNode::hash_table::hashcode (const char * key)
2283 unsigned int hash = 0;
2285 hash = 31 * hash + *key;
2293 ////////////////////////////////////////////////////////////////////////
2294 // Implementation of SGPropertyChangeListener.
2295 ////////////////////////////////////////////////////////////////////////
2297 SGPropertyChangeListener::~SGPropertyChangeListener ()
2299 for (int i = _properties.size() - 1; i >= 0; i--)
2300 _properties[i]->removeChangeListener(this);
2304 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2310 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2311 SGPropertyNode * child)
2317 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2318 SGPropertyNode * child)
2324 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2326 _properties.push_back(node);
2330 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2332 vector<SGPropertyNode *>::iterator it =
2333 find(_properties.begin(), _properties.end(), node);
2334 if (it != _properties.end())
2335 _properties.erase(it);
2339 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2342 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2343 for (int i = 0; i < 3; ++i) {
2354 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2356 for (int i = 0; i < 3; ++i) {
2357 stream >> result[i];
2363 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2366 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2367 for (int i = 0; i < 4; ++i) {
2378 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2380 for (int i = 0; i < 4; ++i) {
2381 stream >> result[i];
2388 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2390 props::Type ltype = lhs.getType();
2391 props::Type rtype = rhs.getType();
2398 return false; // XXX Should we look in aliases?
2400 return lhs.getValue<bool>() == rhs.getValue<bool>();
2402 return lhs.getValue<int>() == rhs.getValue<int>();
2404 return lhs.getValue<long>() == rhs.getValue<long>();
2406 return lhs.getValue<float>() == rhs.getValue<float>();
2408 return lhs.getValue<double>() == rhs.getValue<double>();
2410 case props::UNSPECIFIED:
2411 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2413 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2415 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2423 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2424 const SGPropertyNode& rhs)
2428 int lhsChildren = lhs.nChildren();
2429 int rhsChildren = rhs.nChildren();
2430 if (lhsChildren != rhsChildren)
2432 if (lhsChildren == 0)
2433 return compareNodeValue(lhs, rhs);
2434 for (size_t i = 0; i < lhs._children.size(); ++i) {
2435 const SGPropertyNode* lchild = lhs._children[i];
2436 const SGPropertyNode* rchild = rhs._children[i];
2437 // I'm guessing that the nodes will usually be in the same
2439 if (lchild->getIndex() != rchild->getIndex()
2440 || lchild->getNameString() != rchild->getNameString()) {
2442 for (PropertyList::const_iterator itr = rhs._children.begin(),
2443 end = rhs._children.end();
2446 if (lchild->getIndex() == (*itr)->getIndex()
2447 && lchild->getNameString() == (*itr)->getNameString()) {
2454 if (!compare(*lchild, *rchild))
2460 struct PropertyPlaceLess {
2461 typedef bool result_type;
2462 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2464 int comp = lhs->getNameString().compare(rhs->getNameString());
2466 return lhs->getIndex() < rhs->getIndex();
2472 size_t hash_value(const SGPropertyNode& node)
2474 using namespace boost;
2476 if (node.nChildren() == 0) {
2477 switch (node.getType()) {
2482 return hash_value(node.getValue<bool>());
2484 return hash_value(node.getValue<int>());
2486 return hash_value(node.getValue<long>());
2488 return hash_value(node.getValue<float>());
2490 return hash_value(node.getValue<double>());
2492 case props::UNSPECIFIED:
2494 const char *val = node.getStringValue();
2495 return hash_range(val, val + strlen(val));
2499 const SGVec3d val = node.getValue<SGVec3d>();
2500 return hash_range(&val[0], &val[3]);
2504 const SGVec4d val = node.getValue<SGVec4d>();
2505 return hash_range(&val[0], &val[4]);
2507 case props::ALIAS: // XXX Should we look in aliases?
2513 PropertyList children(node._children.begin(), node._children.end());
2514 sort(children.begin(), children.end(), PropertyPlaceLess());
2515 for (PropertyList::const_iterator itr = children.begin(),
2516 end = children.end();
2519 hash_combine(seed, (*itr)->_name);
2520 hash_combine(seed, (*itr)->_index);
2521 hash_combine(seed, hash_value(**itr));