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>
22 #include <boost/algorithm/string/find_iterator.hpp>
23 #include <boost/algorithm/string/predicate.hpp>
24 #include <boost/algorithm/string/classification.hpp>
25 #include <boost/range.hpp>
27 #include <simgear/math/SGMath.hxx>
33 #include <simgear/compiler.h>
34 #include <simgear/debug/logstream.hxx>
36 #if ( _MSC_VER == 1200 )
37 // MSVC 6 is buggy, and needs something strange here
38 using std::vector<SGPropertyNode_ptr>;
39 using std::vector<SGPropertyChangeListener *>;
40 using std::vector<SGPropertyNode *>;
52 using std::stringstream;
54 using namespace simgear;
57 ////////////////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////////////////
62 * Comparator class for sorting by index.
67 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
68 return (n1->getIndex() < n2->getIndex());
74 ////////////////////////////////////////////////////////////////////////
75 // Convenience macros for value access.
76 ////////////////////////////////////////////////////////////////////////
78 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
79 #define TEST_WRITE if (!getAttribute(WRITE)) return false
82 ////////////////////////////////////////////////////////////////////////
83 // Local path normalization code.
84 ////////////////////////////////////////////////////////////////////////
87 * Parse the name for a path component.
89 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
92 template<typename Range>
94 parse_name (const Range &path)
96 typename Range::iterator i = path.begin();
97 typename Range::iterator max = path.end();
101 if (i != path.end() && *i == '.') {
104 if (i != max && *i != '/')
105 throw string("illegal character after . or ..");
106 } else if (isalpha(*i) || *i == '_') {
109 // The rules inside a name are a little
112 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
113 *i == '-' || *i == '.') {
115 } else if (*i == '[' || *i == '/') {
118 throw string("name may contain only ._- and alphanumeric characters");
125 if (path.begin() == i)
126 throw string("name must begin with alpha or '_'");
128 return Range(path.begin(), i);
131 // Validate the name of a single node
132 inline bool validateName(const string& name)
134 using namespace boost;
137 if (!isalpha(name[0]) && name[0] != '_')
139 return all(make_iterator_range(name.begin(), name.end()),
140 is_alnum() || is_any_of("_-."));
144 ////////////////////////////////////////////////////////////////////////
145 // Other static utility functions.
146 ////////////////////////////////////////////////////////////////////////
150 copy_string (const char * s)
152 unsigned long int slen = strlen(s);
153 char * copy = new char[slen + 1];
155 // the source string length is known so no need to check for '\0'
156 // when copying every single character
157 memcpy(copy, s, slen);
158 *(copy + slen) = '\0';
163 compare_strings (const char * s1, const char * s2)
165 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
169 * Locate a child node by name and index.
171 template<typename Itr>
173 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
175 int nNodes = nodes.size();
176 boost::iterator_range<Itr> name(begin, end);
177 for (int i = 0; i < nNodes; i++) {
178 SGPropertyNode * node = nodes[i];
180 // searching for a mathing index is a lot less time consuming than
181 // comparing two strings so do that first.
182 if (node->getIndex() == index && boost::equals(node->getName(), name))
189 * Locate the child node with the highest index of the same name
192 find_last_child (const char * name, const PropertyList& nodes)
194 int nNodes = nodes.size();
197 for (int i = 0; i < nNodes; i++) {
198 SGPropertyNode * node = nodes[i];
199 if (compare_strings(node->getName(), name))
201 int idx = node->getIndex();
202 if (idx > index) index = idx;
208 template<typename Itr>
209 inline SGPropertyNode*
210 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create)
212 int pos = find_child(begin, end, index, _children);
214 return _children[pos];
216 SGPropertyNode_ptr node;
217 pos = find_child(begin, end, index, _removedChildren);
219 PropertyList::iterator it = _removedChildren.begin();
221 node = _removedChildren[pos];
222 _removedChildren.erase(it);
223 node->setAttribute(REMOVED, false);
224 _children.push_back(node);
225 fireChildAdded(node);
232 template<typename Itr>
234 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
236 SGPropertyNode* node = getExistingChild(begin, end, index, create);
241 node = new SGPropertyNode(begin, end, index, this);
242 _children.push_back(node);
243 fireChildAdded(node);
250 template<typename SplitItr>
252 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
255 typedef typename SplitItr::value_type Range;
256 // Run off the end of the list
261 // Success! This is the one we want.
265 // Empty name at this point is empty, not root.
267 return find_node_aux(current, ++itr, create, last_index);
268 Range name = parse_name(token);
269 if (equals(name, "."))
270 return find_node_aux(current, ++itr, create, last_index);
271 if (equals(name, "..")) {
272 SGPropertyNode * parent = current->getParent();
274 throw string("attempt to move past root with '..'");
275 return find_node_aux(parent, ++itr, create, last_index);
278 if (last_index >= 0) {
279 // If we are at the last token and last_index is valid, use
280 // last_index as the index value
282 while (!(++itr).eof()) {
296 if (name.end() != token.end()) {
297 if (*name.end() == '[') {
298 typename Range::iterator i = name.end() + 1, end = token.end();
299 for (;i != end; ++i) {
301 index = (index * 10) + (*i - '0');
306 if (i == token.end() || *i != ']')
307 throw string("unterminated index (looking for ']')");
309 throw string("illegal characters in token: ")
310 + string(name.begin(), name.end());
314 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
315 index, create), itr, create,
319 // Internal function for parsing property paths. last_index provides
320 // and index value for the last node name token, if supplied.
321 template<typename Range>
323 find_node (SGPropertyNode * current,
328 using namespace boost;
329 typedef split_iterator<typename range_result_iterator<Range>::type>
332 PathSplitIterator itr
333 = make_split_iterator(path, first_finder("/", is_equal()));
334 if (*path.begin() == '/')
335 return find_node_aux(current->getRootNode(), itr, create, last_index);
337 return find_node_aux(current, itr, create, last_index);
341 ////////////////////////////////////////////////////////////////////////
342 // Private methods from SGPropertyNode (may be inlined for speed).
343 ////////////////////////////////////////////////////////////////////////
346 SGPropertyNode::get_bool () const
349 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
351 return _local_val.bool_val;
355 SGPropertyNode::get_int () const
358 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
360 return _local_val.int_val;
364 SGPropertyNode::get_long () const
367 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
369 return _local_val.long_val;
373 SGPropertyNode::get_float () const
376 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
378 return _local_val.float_val;
382 SGPropertyNode::get_double () const
385 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
387 return _local_val.double_val;
391 SGPropertyNode::get_string () const
394 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
396 return _local_val.string_val;
400 SGPropertyNode::set_bool (bool val)
403 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
410 _local_val.bool_val = val;
417 SGPropertyNode::set_int (int val)
420 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
427 _local_val.int_val = val;
434 SGPropertyNode::set_long (long val)
437 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
444 _local_val.long_val = val;
451 SGPropertyNode::set_float (float val)
454 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
461 _local_val.float_val = val;
468 SGPropertyNode::set_double (double val)
471 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
478 _local_val.double_val = val;
485 SGPropertyNode::set_string (const char * val)
488 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
495 delete [] _local_val.string_val;
496 _local_val.string_val = copy_string(val);
503 SGPropertyNode::clearValue ()
505 if (_type == props::ALIAS) {
508 } else if (_type != props::NONE) {
511 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
514 _local_val.int_val = SGRawValue<int>::DefaultValue();
517 _local_val.long_val = SGRawValue<long>::DefaultValue();
520 _local_val.float_val = SGRawValue<float>::DefaultValue();
523 _local_val.double_val = SGRawValue<double>::DefaultValue();
526 case props::UNSPECIFIED:
528 delete [] _local_val.string_val;
530 _local_val.string_val = 0;
532 default: // avoid compiler warning
544 * Get the value as a string.
547 SGPropertyNode::make_string () const
549 if (!getAttribute(READ))
553 return _value.alias->getStringValue();
555 return get_bool() ? "true" : "false";
557 case props::UNSPECIFIED:
576 sstr << std::setprecision(10) << get_double();
578 case props::EXTENDED:
580 props::Type realType = _value.val->getType();
581 // Perhaps this should be done for all types?
582 if (realType == props::VEC3D || realType == props::VEC4D)
584 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
590 _buffer = sstr.str();
591 return _buffer.c_str();
595 * Trace a write access for a property.
598 SGPropertyNode::trace_write () const
601 cerr << "TRACE: Write node " << getPath () << ", value \""
602 << make_string() << '"' << endl;
604 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
605 << ", value \"" << make_string() << '"');
610 * Trace a read access for a property.
613 SGPropertyNode::trace_read () const
616 cerr << "TRACE: Write node " << getPath () << ", value \""
617 << make_string() << '"' << endl;
619 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
620 << ", value \"" << make_string() << '"');
625 ////////////////////////////////////////////////////////////////////////
626 // Public methods from SGPropertyNode.
627 ////////////////////////////////////////////////////////////////////////
630 * Last used attribute
631 * Update as needed when enum Attribute is changed
633 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
636 * Default constructor: always creates a root node.
638 SGPropertyNode::SGPropertyNode ()
647 _local_val.string_val = 0;
655 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
656 : _index(node._index),
658 _parent(0), // don't copy the parent
663 _listeners(0) // CHECK!!
665 _local_val.string_val = 0;
667 if (_type == props::NONE)
669 if (_type == props::ALIAS) {
670 _value.alias = node._value.alias;
675 if (_tied || _type == props::EXTENDED) {
676 _value.val = node._value.val->clone();
681 set_bool(node.get_bool());
684 set_int(node.get_int());
687 set_long(node.get_long());
690 set_float(node.get_float());
693 set_double(node.get_double());
696 case props::UNSPECIFIED:
697 set_string(node.get_string());
706 * Convenience constructor.
708 template<typename Itr>
709 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
711 SGPropertyNode * parent)
721 _local_val.string_val = 0;
723 if (!validateName(_name))
724 throw string("plain name expected instead of '") + _name + '\'';
727 SGPropertyNode::SGPropertyNode (const string& name,
729 SGPropertyNode * parent)
739 _local_val.string_val = 0;
741 if (!validateName(name))
742 throw string("plain name expected instead of '") + _name + '\'';
748 SGPropertyNode::~SGPropertyNode ()
750 // zero out all parent pointers, else they might be dangling
751 for (unsigned i = 0; i < _children.size(); ++i)
752 _children[i]->_parent = 0;
753 for (unsigned i = 0; i < _removedChildren.size(); ++i)
754 _removedChildren[i]->_parent = 0;
759 vector<SGPropertyChangeListener*>::iterator it;
760 for (it = _listeners->begin(); it != _listeners->end(); ++it)
761 (*it)->unregister_property(this);
768 * Alias to another node.
771 SGPropertyNode::alias (SGPropertyNode * target)
773 if (target == 0 || _type == props::ALIAS || _tied)
777 _value.alias = target;
778 _type = props::ALIAS;
784 * Alias to another node by path.
787 SGPropertyNode::alias (const char * path)
789 return alias(getNode(path, true));
797 SGPropertyNode::unalias ()
799 if (_type != props::ALIAS)
807 * Get the target of an alias.
810 SGPropertyNode::getAliasTarget ()
812 return (_type == props::ALIAS ? _value.alias : 0);
816 const SGPropertyNode *
817 SGPropertyNode::getAliasTarget () const
819 return (_type == props::ALIAS ? _value.alias : 0);
823 * create a non-const child by name after the last node with the same name.
826 SGPropertyNode::addChild (const char * name)
828 int pos = find_last_child(name, _children)+1;
830 SGPropertyNode_ptr node;
831 node = new SGPropertyNode(name, name + strlen(name), pos, this);
832 _children.push_back(node);
833 fireChildAdded(node);
839 * Get a non-const child by index.
842 SGPropertyNode::getChild (int position)
844 if (position >= 0 && position < nChildren())
845 return _children[position];
852 * Get a const child by index.
854 const SGPropertyNode *
855 SGPropertyNode::getChild (int position) const
857 if (position >= 0 && position < nChildren())
858 return _children[position];
865 * Get a non-const child by name and index, creating if necessary.
869 SGPropertyNode::getChild (const char * name, int index, bool create)
871 return getChildImpl(name, name + strlen(name), index, create);
875 SGPropertyNode::SGPropertyNode::getChild (const string& name, int index,
878 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index,
883 node = new SGPropertyNode(name, index, this);
884 _children.push_back(node);
885 fireChildAdded(node);
893 * Get a const child by name and index.
895 const SGPropertyNode *
896 SGPropertyNode::getChild (const char * name, int index) const
898 int pos = find_child(name, name + strlen(name), index, _children);
900 return _children[pos];
907 * Get all children with the same name (but different indices).
910 SGPropertyNode::getChildren (const char * name) const
912 PropertyList children;
913 int max = _children.size();
915 for (int i = 0; i < max; i++)
916 if (compare_strings(_children[i]->getName(), name))
917 children.push_back(_children[i]);
919 sort(children.begin(), children.end(), CompareIndices());
925 * Remove this node and all children from nodes that link to them
926 * in their path cache.
929 SGPropertyNode::remove_from_path_caches ()
931 for (unsigned int i = 0; i < _children.size(); ++i)
932 _children[i]->remove_from_path_caches();
934 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
935 _linkedNodes[i]->erase(this);
936 _linkedNodes.clear();
941 * Remove child by position.
944 SGPropertyNode::removeChild (int pos, bool keep)
946 SGPropertyNode_ptr node;
947 if (pos < 0 || pos >= (int)_children.size())
950 PropertyList::iterator it = _children.begin();
952 node = _children[pos];
955 _removedChildren.push_back(node);
958 node->remove_from_path_caches();
959 node->setAttribute(REMOVED, true);
961 fireChildRemoved(node);
967 * Remove a child node
970 SGPropertyNode::removeChild (const char * name, int index, bool keep)
972 SGPropertyNode_ptr ret;
973 int pos = find_child(name, name + strlen(name), index, _children);
975 ret = removeChild(pos, keep);
981 * Remove all children with the specified name.
984 SGPropertyNode::removeChildren (const char * name, bool keep)
986 PropertyList children;
988 for (int pos = _children.size() - 1; pos >= 0; pos--)
989 if (compare_strings(_children[pos]->getName(), name))
990 children.push_back(removeChild(pos, keep));
992 sort(children.begin(), children.end(), CompareIndices());
998 * Remove a linked node.
1001 SGPropertyNode::remove_linked_node (hash_table * node)
1003 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
1004 if (_linkedNodes[i] == node) {
1005 vector<hash_table *>::iterator it = _linkedNodes.begin();
1007 _linkedNodes.erase(it);
1016 SGPropertyNode::getDisplayName (bool simplify) const
1018 string display_name = _name;
1019 if (_index != 0 || !simplify) {
1021 sstr << '[' << _index << ']';
1022 display_name += sstr.str();
1024 return display_name;
1029 SGPropertyNode::getPath (bool simplify) const
1031 // Calculate the complete path only once.
1032 if (_parent != 0 && _path.empty()) {
1033 _path = _parent->getPath(simplify);
1035 _path += getDisplayName(simplify);
1038 return _path.c_str();
1042 SGPropertyNode::getType () const
1044 if (_type == props::ALIAS)
1045 return _value.alias->getType();
1046 else if (_type == props::EXTENDED)
1047 return _value.val->getType();
1054 SGPropertyNode::getBoolValue () const
1056 // Shortcut for common case
1057 if (_attr == (READ|WRITE) && _type == props::BOOL)
1060 if (getAttribute(TRACE_READ))
1062 if (!getAttribute(READ))
1063 return SGRawValue<bool>::DefaultValue();
1066 return _value.alias->getBoolValue();
1070 return get_int() == 0 ? false : true;
1072 return get_long() == 0L ? false : true;
1074 return get_float() == 0.0 ? false : true;
1076 return get_double() == 0.0L ? false : true;
1078 case props::UNSPECIFIED:
1079 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1082 return SGRawValue<bool>::DefaultValue();
1087 SGPropertyNode::getIntValue () const
1089 // Shortcut for common case
1090 if (_attr == (READ|WRITE) && _type == props::INT)
1093 if (getAttribute(TRACE_READ))
1095 if (!getAttribute(READ))
1096 return SGRawValue<int>::DefaultValue();
1099 return _value.alias->getIntValue();
1101 return int(get_bool());
1105 return int(get_long());
1107 return int(get_float());
1109 return int(get_double());
1111 case props::UNSPECIFIED:
1112 return atoi(get_string());
1115 return SGRawValue<int>::DefaultValue();
1120 SGPropertyNode::getLongValue () const
1122 // Shortcut for common case
1123 if (_attr == (READ|WRITE) && _type == props::LONG)
1126 if (getAttribute(TRACE_READ))
1128 if (!getAttribute(READ))
1129 return SGRawValue<long>::DefaultValue();
1132 return _value.alias->getLongValue();
1134 return long(get_bool());
1136 return long(get_int());
1140 return long(get_float());
1142 return long(get_double());
1144 case props::UNSPECIFIED:
1145 return strtol(get_string(), 0, 0);
1148 return SGRawValue<long>::DefaultValue();
1153 SGPropertyNode::getFloatValue () const
1155 // Shortcut for common case
1156 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1159 if (getAttribute(TRACE_READ))
1161 if (!getAttribute(READ))
1162 return SGRawValue<float>::DefaultValue();
1165 return _value.alias->getFloatValue();
1167 return float(get_bool());
1169 return float(get_int());
1171 return float(get_long());
1175 return float(get_double());
1177 case props::UNSPECIFIED:
1178 return atof(get_string());
1181 return SGRawValue<float>::DefaultValue();
1186 SGPropertyNode::getDoubleValue () const
1188 // Shortcut for common case
1189 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1190 return get_double();
1192 if (getAttribute(TRACE_READ))
1194 if (!getAttribute(READ))
1195 return SGRawValue<double>::DefaultValue();
1199 return _value.alias->getDoubleValue();
1201 return double(get_bool());
1203 return double(get_int());
1205 return double(get_long());
1207 return double(get_float());
1209 return get_double();
1211 case props::UNSPECIFIED:
1212 return strtod(get_string(), 0);
1215 return SGRawValue<double>::DefaultValue();
1220 SGPropertyNode::getStringValue () const
1222 // Shortcut for common case
1223 if (_attr == (READ|WRITE) && _type == props::STRING)
1224 return get_string();
1226 if (getAttribute(TRACE_READ))
1228 if (!getAttribute(READ))
1229 return SGRawValue<const char *>::DefaultValue();
1230 return make_string();
1234 SGPropertyNode::setBoolValue (bool value)
1236 // Shortcut for common case
1237 if (_attr == (READ|WRITE) && _type == props::BOOL)
1238 return set_bool(value);
1240 bool result = false;
1242 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1245 _type = props::BOOL;
1250 result = _value.alias->setBoolValue(value);
1253 result = set_bool(value);
1256 result = set_int(int(value));
1259 result = set_long(long(value));
1262 result = set_float(float(value));
1265 result = set_double(double(value));
1268 case props::UNSPECIFIED:
1269 result = set_string(value ? "true" : "false");
1276 if (getAttribute(TRACE_WRITE))
1282 SGPropertyNode::setIntValue (int value)
1284 // Shortcut for common case
1285 if (_attr == (READ|WRITE) && _type == props::INT)
1286 return set_int(value);
1288 bool result = false;
1290 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1293 _local_val.int_val = 0;
1298 result = _value.alias->setIntValue(value);
1301 result = set_bool(value == 0 ? false : true);
1304 result = set_int(value);
1307 result = set_long(long(value));
1310 result = set_float(float(value));
1313 result = set_double(double(value));
1316 case props::UNSPECIFIED: {
1318 sprintf(buf, "%d", value);
1319 result = set_string(buf);
1327 if (getAttribute(TRACE_WRITE))
1333 SGPropertyNode::setLongValue (long value)
1335 // Shortcut for common case
1336 if (_attr == (READ|WRITE) && _type == props::LONG)
1337 return set_long(value);
1339 bool result = false;
1341 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1343 _type = props::LONG;
1344 _local_val.long_val = 0L;
1349 result = _value.alias->setLongValue(value);
1352 result = set_bool(value == 0L ? false : true);
1355 result = set_int(int(value));
1358 result = set_long(value);
1361 result = set_float(float(value));
1364 result = set_double(double(value));
1367 case props::UNSPECIFIED: {
1369 sprintf(buf, "%ld", value);
1370 result = set_string(buf);
1378 if (getAttribute(TRACE_WRITE))
1384 SGPropertyNode::setFloatValue (float value)
1386 // Shortcut for common case
1387 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1388 return set_float(value);
1390 bool result = false;
1392 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1394 _type = props::FLOAT;
1395 _local_val.float_val = 0;
1400 result = _value.alias->setFloatValue(value);
1403 result = set_bool(value == 0.0 ? false : true);
1406 result = set_int(int(value));
1409 result = set_long(long(value));
1412 result = set_float(value);
1415 result = set_double(double(value));
1418 case props::UNSPECIFIED: {
1420 sprintf(buf, "%f", value);
1421 result = set_string(buf);
1429 if (getAttribute(TRACE_WRITE))
1435 SGPropertyNode::setDoubleValue (double value)
1437 // Shortcut for common case
1438 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1439 return set_double(value);
1441 bool result = false;
1443 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1445 _local_val.double_val = value;
1446 _type = props::DOUBLE;
1451 result = _value.alias->setDoubleValue(value);
1454 result = set_bool(value == 0.0L ? false : true);
1457 result = set_int(int(value));
1460 result = set_long(long(value));
1463 result = set_float(float(value));
1466 result = set_double(value);
1469 case props::UNSPECIFIED: {
1471 sprintf(buf, "%f", value);
1472 result = set_string(buf);
1480 if (getAttribute(TRACE_WRITE))
1486 SGPropertyNode::setStringValue (const char * value)
1488 // Shortcut for common case
1489 if (_attr == (READ|WRITE) && _type == props::STRING)
1490 return set_string(value);
1492 bool result = false;
1494 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1496 _type = props::STRING;
1501 result = _value.alias->setStringValue(value);
1504 result = set_bool((compare_strings(value, "true")
1505 || atoi(value)) ? true : false);
1508 result = set_int(atoi(value));
1511 result = set_long(strtol(value, 0, 0));
1514 result = set_float(atof(value));
1517 result = set_double(strtod(value, 0));
1520 case props::UNSPECIFIED:
1521 result = set_string(value);
1523 case props::EXTENDED:
1525 stringstream sstr(value);
1526 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1534 if (getAttribute(TRACE_WRITE))
1540 SGPropertyNode::setUnspecifiedValue (const char * value)
1542 bool result = false;
1544 if (_type == props::NONE) {
1546 _type = props::UNSPECIFIED;
1548 props::Type type = _type;
1549 if (type == props::EXTENDED)
1550 type = _value.val->getType();
1553 result = _value.alias->setUnspecifiedValue(value);
1556 result = set_bool((compare_strings(value, "true")
1557 || atoi(value)) ? true : false);
1560 result = set_int(atoi(value));
1563 result = set_long(strtol(value, 0, 0));
1566 result = set_float(atof(value));
1569 result = set_double(strtod(value, 0));
1572 case props::UNSPECIFIED:
1573 result = set_string(value);
1576 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1579 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1586 if (getAttribute(TRACE_WRITE))
1591 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1593 if (!getAttribute(READ))
1597 return _value.alias->printOn(stream);
1599 stream << (get_bool() ? "true" : "false");
1602 stream << get_int();
1605 stream << get_long();
1608 stream << get_float();
1611 stream << get_double();
1614 case props::UNSPECIFIED:
1615 stream << get_string();
1617 case props::EXTENDED:
1618 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1622 default: // avoid compiler warning
1629 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1632 if (_type == props::ALIAS || _tied)
1635 useDefault = useDefault && hasValue();
1636 std::string old_val;
1638 old_val = getStringValue();
1640 _type = props::STRING;
1642 _value.val = rawValue.clone();
1645 setStringValue(old_val.c_str());
1650 SGPropertyNode::untie ()
1657 bool val = getBoolValue();
1659 _type = props::BOOL;
1660 _local_val.bool_val = val;
1664 int val = getIntValue();
1667 _local_val.int_val = val;
1671 long val = getLongValue();
1673 _type = props::LONG;
1674 _local_val.long_val = val;
1677 case props::FLOAT: {
1678 float val = getFloatValue();
1680 _type = props::FLOAT;
1681 _local_val.float_val = val;
1684 case props::DOUBLE: {
1685 double val = getDoubleValue();
1687 _type = props::DOUBLE;
1688 _local_val.double_val = val;
1692 case props::UNSPECIFIED: {
1693 string val = getStringValue();
1695 _type = props::STRING;
1696 _local_val.string_val = copy_string(val.c_str());
1699 case props::EXTENDED: {
1700 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1701 _value.val = 0; // Prevent clearValue() from deleting
1703 _type = props::EXTENDED;
1704 _value.val = val->makeContainer();
1718 SGPropertyNode::getRootNode ()
1723 return _parent->getRootNode();
1726 const SGPropertyNode *
1727 SGPropertyNode::getRootNode () const
1732 return _parent->getRootNode();
1736 SGPropertyNode::getNode (const char * relative_path, bool create)
1738 using namespace boost;
1739 if (_path_cache == 0)
1740 _path_cache = new hash_table;
1742 SGPropertyNode * result = _path_cache->get(relative_path);
1744 result = find_node(this,
1745 make_iterator_range(relative_path, relative_path
1746 + strlen(relative_path)),
1749 _path_cache->put(relative_path, result);
1756 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1758 using namespace boost;
1759 return find_node(this, make_iterator_range(relative_path, relative_path
1760 + strlen(relative_path)),
1764 const SGPropertyNode *
1765 SGPropertyNode::getNode (const char * relative_path) const
1767 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1770 const SGPropertyNode *
1771 SGPropertyNode::getNode (const char * relative_path, int index) const
1773 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1777 ////////////////////////////////////////////////////////////////////////
1778 // Convenience methods using relative paths.
1779 ////////////////////////////////////////////////////////////////////////
1783 * Test whether another node has a value attached.
1786 SGPropertyNode::hasValue (const char * relative_path) const
1788 const SGPropertyNode * node = getNode(relative_path);
1789 return (node == 0 ? false : node->hasValue());
1794 * Get the value type for another node.
1797 SGPropertyNode::getType (const char * relative_path) const
1799 const SGPropertyNode * node = getNode(relative_path);
1800 return (node == 0 ? props::UNSPECIFIED : node->getType());
1805 * Get a bool value for another node.
1808 SGPropertyNode::getBoolValue (const char * relative_path,
1809 bool defaultValue) const
1811 const SGPropertyNode * node = getNode(relative_path);
1812 return (node == 0 ? defaultValue : node->getBoolValue());
1817 * Get an int value for another node.
1820 SGPropertyNode::getIntValue (const char * relative_path,
1821 int defaultValue) const
1823 const SGPropertyNode * node = getNode(relative_path);
1824 return (node == 0 ? defaultValue : node->getIntValue());
1829 * Get a long value for another node.
1832 SGPropertyNode::getLongValue (const char * relative_path,
1833 long defaultValue) const
1835 const SGPropertyNode * node = getNode(relative_path);
1836 return (node == 0 ? defaultValue : node->getLongValue());
1841 * Get a float value for another node.
1844 SGPropertyNode::getFloatValue (const char * relative_path,
1845 float defaultValue) const
1847 const SGPropertyNode * node = getNode(relative_path);
1848 return (node == 0 ? defaultValue : node->getFloatValue());
1853 * Get a double value for another node.
1856 SGPropertyNode::getDoubleValue (const char * relative_path,
1857 double defaultValue) const
1859 const SGPropertyNode * node = getNode(relative_path);
1860 return (node == 0 ? defaultValue : node->getDoubleValue());
1865 * Get a string value for another node.
1868 SGPropertyNode::getStringValue (const char * relative_path,
1869 const char * defaultValue) const
1871 const SGPropertyNode * node = getNode(relative_path);
1872 return (node == 0 ? defaultValue : node->getStringValue());
1877 * Set a bool value for another node.
1880 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1882 return getNode(relative_path, true)->setBoolValue(value);
1887 * Set an int value for another node.
1890 SGPropertyNode::setIntValue (const char * relative_path, int value)
1892 return getNode(relative_path, true)->setIntValue(value);
1897 * Set a long value for another node.
1900 SGPropertyNode::setLongValue (const char * relative_path, long value)
1902 return getNode(relative_path, true)->setLongValue(value);
1907 * Set a float value for another node.
1910 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1912 return getNode(relative_path, true)->setFloatValue(value);
1917 * Set a double value for another node.
1920 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1922 return getNode(relative_path, true)->setDoubleValue(value);
1927 * Set a string value for another node.
1930 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1932 return getNode(relative_path, true)->setStringValue(value);
1937 * Set an unknown value for another node.
1940 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1943 return getNode(relative_path, true)->setUnspecifiedValue(value);
1948 * Test whether another node is tied.
1951 SGPropertyNode::isTied (const char * relative_path) const
1953 const SGPropertyNode * node = getNode(relative_path);
1954 return (node == 0 ? false : node->isTied());
1959 * Tie a node reached by a relative path, creating it if necessary.
1962 SGPropertyNode::tie (const char * relative_path,
1963 const SGRawValue<bool> &rawValue,
1966 return getNode(relative_path, true)->tie(rawValue, useDefault);
1971 * Tie a node reached by a relative path, creating it if necessary.
1974 SGPropertyNode::tie (const char * relative_path,
1975 const SGRawValue<int> &rawValue,
1978 return getNode(relative_path, true)->tie(rawValue, useDefault);
1983 * Tie a node reached by a relative path, creating it if necessary.
1986 SGPropertyNode::tie (const char * relative_path,
1987 const SGRawValue<long> &rawValue,
1990 return getNode(relative_path, true)->tie(rawValue, useDefault);
1995 * Tie a node reached by a relative path, creating it if necessary.
1998 SGPropertyNode::tie (const char * relative_path,
1999 const SGRawValue<float> &rawValue,
2002 return getNode(relative_path, true)->tie(rawValue, useDefault);
2007 * Tie a node reached by a relative path, creating it if necessary.
2010 SGPropertyNode::tie (const char * relative_path,
2011 const SGRawValue<double> &rawValue,
2014 return getNode(relative_path, true)->tie(rawValue, useDefault);
2019 * Tie a node reached by a relative path, creating it if necessary.
2022 SGPropertyNode::tie (const char * relative_path,
2023 const SGRawValue<const char *> &rawValue,
2026 return getNode(relative_path, true)->tie(rawValue, useDefault);
2031 * Attempt to untie another node reached by a relative path.
2034 SGPropertyNode::untie (const char * relative_path)
2036 SGPropertyNode * node = getNode(relative_path);
2037 return (node == 0 ? false : node->untie());
2041 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2044 if (_listeners == 0)
2045 _listeners = new vector<SGPropertyChangeListener*>;
2046 _listeners->push_back(listener);
2047 listener->register_property(this);
2049 listener->valueChanged(this);
2053 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2055 vector<SGPropertyChangeListener*>::iterator it =
2056 find(_listeners->begin(), _listeners->end(), listener);
2057 if (it != _listeners->end()) {
2058 _listeners->erase(it);
2059 listener->unregister_property(this);
2060 if (_listeners->empty()) {
2061 vector<SGPropertyChangeListener*>* tmp = _listeners;
2069 SGPropertyNode::fireValueChanged ()
2071 fireValueChanged(this);
2075 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2077 fireChildAdded(this, child);
2081 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2083 fireChildRemoved(this, child);
2087 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2089 if (_listeners != 0) {
2090 for (unsigned int i = 0; i < _listeners->size(); i++) {
2091 (*_listeners)[i]->valueChanged(node);
2095 _parent->fireValueChanged(node);
2099 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2100 SGPropertyNode * child)
2102 if (_listeners != 0) {
2103 for (unsigned int i = 0; i < _listeners->size(); i++) {
2104 (*_listeners)[i]->childAdded(parent, child);
2108 _parent->fireChildAdded(parent, child);
2112 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2113 SGPropertyNode * child)
2115 if (_listeners != 0) {
2116 for (unsigned int i = 0; i < _listeners->size(); i++) {
2117 (*_listeners)[i]->childRemoved(parent, child);
2121 _parent->fireChildRemoved(parent, child);
2126 ////////////////////////////////////////////////////////////////////////
2127 // Simplified hash table for caching paths.
2128 ////////////////////////////////////////////////////////////////////////
2130 #define HASH_TABLE_SIZE 199
2132 SGPropertyNode::hash_table::entry::entry ()
2137 SGPropertyNode::hash_table::entry::~entry ()
2139 // Don't delete the value; we don't own
2144 SGPropertyNode::hash_table::entry::set_key (const char * key)
2150 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2155 SGPropertyNode::hash_table::bucket::bucket ()
2161 SGPropertyNode::hash_table::bucket::~bucket ()
2163 for (int i = 0; i < _length; i++)
2168 SGPropertyNode::hash_table::entry *
2169 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2172 for (i = 0; i < _length; i++) {
2173 if (!strcmp(_entries[i]->get_key(), key))
2177 entry ** new_entries = new entry*[_length+1];
2178 for (i = 0; i < _length; i++) {
2179 new_entries[i] = _entries[i];
2182 _entries = new_entries;
2183 _entries[_length] = new entry;
2184 _entries[_length]->set_key(key);
2186 return _entries[_length - 1];
2193 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2195 for (int i = 0; i < _length; i++) {
2196 if (_entries[i]->get_value() == node) {
2198 for (++i; i < _length; i++) {
2199 _entries[i-1] = _entries[i];
2209 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2211 for (int i = 0; i < _length; i++) {
2212 SGPropertyNode * node = _entries[i]->get_value();
2214 node->remove_linked_node(owner);
2218 SGPropertyNode::hash_table::hash_table ()
2224 SGPropertyNode::hash_table::~hash_table ()
2226 for (unsigned int i = 0; i < _data_length; i++) {
2228 _data[i]->clear(this);
2236 SGPropertyNode::hash_table::get (const char * key)
2238 if (_data_length == 0)
2240 unsigned int index = hashcode(key) % _data_length;
2241 if (_data[index] == 0)
2243 entry * e = _data[index]->get_entry(key);
2247 return e->get_value();
2251 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2253 if (_data_length == 0) {
2254 _data = new bucket*[HASH_TABLE_SIZE];
2255 _data_length = HASH_TABLE_SIZE;
2256 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2259 unsigned int index = hashcode(key) % _data_length;
2260 if (_data[index] == 0) {
2261 _data[index] = new bucket;
2263 entry * e = _data[index]->get_entry(key, true);
2264 e->set_value(value);
2265 value->add_linked_node(this);
2269 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2271 for (unsigned int i = 0; i < _data_length; i++)
2272 if (_data[i] && _data[i]->erase(node))
2279 SGPropertyNode::hash_table::hashcode (const char * key)
2281 unsigned int hash = 0;
2283 hash = 31 * hash + *key;
2291 ////////////////////////////////////////////////////////////////////////
2292 // Implementation of SGPropertyChangeListener.
2293 ////////////////////////////////////////////////////////////////////////
2295 SGPropertyChangeListener::~SGPropertyChangeListener ()
2297 for (int i = _properties.size() - 1; i >= 0; i--)
2298 _properties[i]->removeChangeListener(this);
2302 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2308 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2309 SGPropertyNode * child)
2315 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2316 SGPropertyNode * child)
2322 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2324 _properties.push_back(node);
2328 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2330 vector<SGPropertyNode *>::iterator it =
2331 find(_properties.begin(), _properties.end(), node);
2332 if (it != _properties.end())
2333 _properties.erase(it);
2337 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2340 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2341 for (int i = 0; i < 3; ++i) {
2352 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2354 for (int i = 0; i < 3; ++i) {
2355 stream >> result[i];
2361 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2364 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2365 for (int i = 0; i < 4; ++i) {
2376 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2378 for (int i = 0; i < 4; ++i) {
2379 stream >> result[i];