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 <simgear/math/SGMath.hxx>
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
31 #if ( _MSC_VER == 1200 )
32 // MSVC 6 is buggy, and needs something strange here
33 using std::vector<SGPropertyNode_ptr>;
34 using std::vector<SGPropertyChangeListener *>;
35 using std::vector<SGPropertyNode *>;
47 using std::stringstream;
49 using namespace simgear;
52 ////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
57 * Comparator class for sorting by index.
62 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
63 return (n1->getIndex() < n2->getIndex());
69 ////////////////////////////////////////////////////////////////////////
70 // Convenience macros for value access.
71 ////////////////////////////////////////////////////////////////////////
73 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
74 #define TEST_WRITE if (!getAttribute(WRITE)) return false
77 ////////////////////////////////////////////////////////////////////////
78 // Local path normalization code.
79 ////////////////////////////////////////////////////////////////////////
82 * A component in a path.
91 * Parse the name for a path component.
93 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
95 static inline const string
96 parse_name (const string &path, int &i)
99 int max = path.size();
101 if (path[i] == '.') {
103 if (i < max && path[i] == '.') {
109 if (i < max && path[i] != '/')
110 throw string("illegal character after " + name);
113 else if (isalpha(path[i]) || path[i] == '_') {
117 // The rules inside a name are a little
120 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
121 path[i] == '-' || path[i] == '.') {
123 } else if (path[i] == '[' || path[i] == '/') {
126 throw string("name may contain only ._- and alphanumeric characters");
133 if (name.size() == 0)
134 throw string("name must begin with alpha or '_'");
142 * Parse the optional integer index for a path component.
144 * Index: "[" [0-9]+ "]"
147 parse_index (const string &path, int &i)
156 for (int max = path.size(); i < max; i++) {
157 if (isdigit(path[i])) {
158 index = (index * 10) + (path[i] - '0');
159 } else if (path[i] == ']') {
167 throw string("unterminated index (looking for ']')");
172 * Parse a single path component.
174 * Component: Name Index?
176 static inline PathComponent
177 parse_component (const string &path, int &i)
179 PathComponent component;
180 component.name = parse_name(path, i);
181 if (component.name[0] != '.')
182 component.index = parse_index(path, i);
184 component.index = -1;
190 * Parse a path into its components.
193 parse_path (const string &path, vector<PathComponent> &components)
196 int max = path.size();
198 // Check for initial '/'
199 if (path[pos] == '/') {
203 components.push_back(root);
205 while (pos < max && path[pos] == '/')
210 components.push_back(parse_component(path, pos));
211 while (pos < max && path[pos] == '/')
218 ////////////////////////////////////////////////////////////////////////
219 // Other static utility functions.
220 ////////////////////////////////////////////////////////////////////////
224 copy_string (const char * s)
226 unsigned long int slen = strlen(s);
227 char * copy = new char[slen + 1];
229 // the source string length is known so no need to check for '\0'
230 // when copying every single character
231 memcpy(copy, s, slen);
232 *(copy + slen) = '\0';
237 compare_strings (const char * s1, const char * s2)
239 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
243 * Locate a child node by name and index.
246 find_child (const char * name, int index, const PropertyList& nodes)
248 int nNodes = nodes.size();
249 for (int i = 0; i < nNodes; i++) {
250 SGPropertyNode * node = nodes[i];
252 // searching for a mathing index is a lot less time consuming than
253 // comparing two strings so do that first.
254 if (node->getIndex() == index && compare_strings(node->getName(), name))
261 * Locate the child node with the highest index of the same name
264 find_last_child (const char * name, const PropertyList& nodes)
266 int nNodes = nodes.size();
269 for (int i = 0; i < nNodes; i++) {
270 SGPropertyNode * node = nodes[i];
271 if (compare_strings(node->getName(), name))
273 int idx = node->getIndex();
274 if (idx > index) index = idx;
282 * Locate another node, given a relative path.
284 static SGPropertyNode *
285 find_node (SGPropertyNode * current,
286 const vector<PathComponent> &components,
290 // Run off the end of the list
295 // Success! This is the one we want.
296 else if (position >= (int)components.size()) {
297 return (current->getAttribute(SGPropertyNode::REMOVED) ? 0 : current);
300 // Empty component means root.
301 else if (components[position].name == "") {
302 return find_node(current->getRootNode(), components, position + 1, create);
305 // . means current directory
306 else if (components[position].name == ".") {
307 return find_node(current, components, position + 1, create);
310 // .. means parent directory
311 else if (components[position].name == "..") {
312 SGPropertyNode * parent = current->getParent();
314 throw string("attempt to move past root with '..'");
316 return find_node(parent, components, position + 1, create);
319 // Otherwise, a child name
321 SGPropertyNode * child =
322 current->getChild(components[position].name.c_str(),
323 components[position].index,
325 return find_node(child, components, position + 1, create);
331 ////////////////////////////////////////////////////////////////////////
332 // Private methods from SGPropertyNode (may be inlined for speed).
333 ////////////////////////////////////////////////////////////////////////
336 SGPropertyNode::get_bool () const
339 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
341 return _local_val.bool_val;
345 SGPropertyNode::get_int () const
348 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
350 return _local_val.int_val;
354 SGPropertyNode::get_long () const
357 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
359 return _local_val.long_val;
363 SGPropertyNode::get_float () const
366 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
368 return _local_val.float_val;
372 SGPropertyNode::get_double () const
375 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
377 return _local_val.double_val;
381 SGPropertyNode::get_string () const
384 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
386 return _local_val.string_val;
390 SGPropertyNode::set_bool (bool val)
393 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
400 _local_val.bool_val = val;
407 SGPropertyNode::set_int (int val)
410 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
417 _local_val.int_val = val;
424 SGPropertyNode::set_long (long val)
427 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
434 _local_val.long_val = val;
441 SGPropertyNode::set_float (float val)
444 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
451 _local_val.float_val = val;
458 SGPropertyNode::set_double (double val)
461 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
468 _local_val.double_val = val;
475 SGPropertyNode::set_string (const char * val)
478 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
485 delete [] _local_val.string_val;
486 _local_val.string_val = copy_string(val);
493 SGPropertyNode::clearValue ()
495 if (_type == props::ALIAS) {
498 } else if (_type != props::NONE) {
501 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
504 _local_val.int_val = SGRawValue<int>::DefaultValue();
507 _local_val.long_val = SGRawValue<long>::DefaultValue();
510 _local_val.float_val = SGRawValue<float>::DefaultValue();
513 _local_val.double_val = SGRawValue<double>::DefaultValue();
516 case props::UNSPECIFIED:
518 delete [] _local_val.string_val;
520 _local_val.string_val = 0;
522 default: // avoid compiler warning
534 * Get the value as a string.
537 SGPropertyNode::make_string () const
539 if (!getAttribute(READ))
543 return _value.alias->getStringValue();
545 return get_bool() ? "true" : "false";
547 case props::UNSPECIFIED:
566 sstr << std::setprecision(10) << get_double();
568 case props::EXTENDED:
570 props::Type realType = _value.val->getType();
571 // Perhaps this should be done for all types?
572 if (realType == props::VEC3D || realType == props::VEC4D)
574 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
580 _buffer = sstr.str();
581 return _buffer.c_str();
585 * Trace a write access for a property.
588 SGPropertyNode::trace_write () const
591 cerr << "TRACE: Write node " << getPath () << ", value \""
592 << make_string() << '"' << endl;
594 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
595 << ", value \"" << make_string() << '"');
600 * Trace a read access for a property.
603 SGPropertyNode::trace_read () const
606 cerr << "TRACE: Write node " << getPath () << ", value \""
607 << make_string() << '"' << endl;
609 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
610 << ", value \"" << make_string() << '"');
615 ////////////////////////////////////////////////////////////////////////
616 // Public methods from SGPropertyNode.
617 ////////////////////////////////////////////////////////////////////////
620 * Last used attribute
621 * Update as needed when enum Attribute is changed
623 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
626 * Default constructor: always creates a root node.
628 SGPropertyNode::SGPropertyNode ()
637 _local_val.string_val = 0;
645 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
646 : _index(node._index),
648 _parent(0), // don't copy the parent
653 _listeners(0) // CHECK!!
655 _local_val.string_val = 0;
657 if (_type == props::NONE)
659 if (_type == props::ALIAS) {
660 _value.alias = node._value.alias;
665 if (_tied || _type == props::EXTENDED) {
666 _value.val = node._value.val->clone();
671 set_bool(node.get_bool());
674 set_int(node.get_int());
677 set_long(node.get_long());
680 set_float(node.get_float());
683 set_double(node.get_double());
686 case props::UNSPECIFIED:
687 set_string(node.get_string());
696 * Convenience constructor.
698 SGPropertyNode::SGPropertyNode (const char * name,
700 SGPropertyNode * parent)
710 _local_val.string_val = 0;
712 _name = parse_name(name, i);
713 if (i != int(strlen(name)) || name[0] == '.')
714 throw string("plain name expected instead of '") + name + '\'';
721 SGPropertyNode::~SGPropertyNode ()
723 // zero out all parent pointers, else they might be dangling
724 for (unsigned i = 0; i < _children.size(); ++i)
725 _children[i]->_parent = 0;
726 for (unsigned i = 0; i < _removedChildren.size(); ++i)
727 _removedChildren[i]->_parent = 0;
732 vector<SGPropertyChangeListener*>::iterator it;
733 for (it = _listeners->begin(); it != _listeners->end(); ++it)
734 (*it)->unregister_property(this);
741 * Alias to another node.
744 SGPropertyNode::alias (SGPropertyNode * target)
746 if (target == 0 || _type == props::ALIAS || _tied)
750 _value.alias = target;
751 _type = props::ALIAS;
757 * Alias to another node by path.
760 SGPropertyNode::alias (const char * path)
762 return alias(getNode(path, true));
770 SGPropertyNode::unalias ()
772 if (_type != props::ALIAS)
780 * Get the target of an alias.
783 SGPropertyNode::getAliasTarget ()
785 return (_type == props::ALIAS ? _value.alias : 0);
789 const SGPropertyNode *
790 SGPropertyNode::getAliasTarget () const
792 return (_type == props::ALIAS ? _value.alias : 0);
796 * create a non-const child by name after the last node with the same name.
799 SGPropertyNode::addChild (const char * name)
801 int pos = find_last_child(name, _children)+1;
803 SGPropertyNode_ptr node;
804 node = new SGPropertyNode(name, pos, this);
805 _children.push_back(node);
806 fireChildAdded(node);
812 * Get a non-const child by index.
815 SGPropertyNode::getChild (int position)
817 if (position >= 0 && position < nChildren())
818 return _children[position];
825 * Get a const child by index.
827 const SGPropertyNode *
828 SGPropertyNode::getChild (int position) const
830 if (position >= 0 && position < nChildren())
831 return _children[position];
838 * Get a non-const child by name and index, creating if necessary.
841 SGPropertyNode::getChild (const char * name, int index, bool create)
843 int pos = find_child(name, index, _children);
845 return _children[pos];
847 SGPropertyNode_ptr node;
848 pos = find_child(name, index, _removedChildren);
850 PropertyList::iterator it = _removedChildren.begin();
852 node = _removedChildren[pos];
853 _removedChildren.erase(it);
854 node->setAttribute(REMOVED, false);
856 node = new SGPropertyNode(name, index, this);
858 _children.push_back(node);
859 fireChildAdded(node);
868 * Get a const child by name and index.
870 const SGPropertyNode *
871 SGPropertyNode::getChild (const char * name, int index) const
873 int pos = find_child(name, index, _children);
875 return _children[pos];
882 * Get all children with the same name (but different indices).
885 SGPropertyNode::getChildren (const char * name) const
887 PropertyList children;
888 int max = _children.size();
890 for (int i = 0; i < max; i++)
891 if (compare_strings(_children[i]->getName(), name))
892 children.push_back(_children[i]);
894 sort(children.begin(), children.end(), CompareIndices());
900 * Remove this node and all children from nodes that link to them
901 * in their path cache.
904 SGPropertyNode::remove_from_path_caches ()
906 for (unsigned int i = 0; i < _children.size(); ++i)
907 _children[i]->remove_from_path_caches();
909 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
910 _linkedNodes[i]->erase(this);
911 _linkedNodes.clear();
916 * Remove child by position.
919 SGPropertyNode::removeChild (int pos, bool keep)
921 SGPropertyNode_ptr node;
922 if (pos < 0 || pos >= (int)_children.size())
925 PropertyList::iterator it = _children.begin();
927 node = _children[pos];
930 _removedChildren.push_back(node);
933 node->remove_from_path_caches();
934 node->setAttribute(REMOVED, true);
936 fireChildRemoved(node);
942 * Remove a child node
945 SGPropertyNode::removeChild (const char * name, int index, bool keep)
947 SGPropertyNode_ptr ret;
948 int pos = find_child(name, index, _children);
950 ret = removeChild(pos, keep);
956 * Remove all children with the specified name.
959 SGPropertyNode::removeChildren (const char * name, bool keep)
961 PropertyList children;
963 for (int pos = _children.size() - 1; pos >= 0; pos--)
964 if (compare_strings(_children[pos]->getName(), name))
965 children.push_back(removeChild(pos, keep));
967 sort(children.begin(), children.end(), CompareIndices());
973 * Remove a linked node.
976 SGPropertyNode::remove_linked_node (hash_table * node)
978 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
979 if (_linkedNodes[i] == node) {
980 vector<hash_table *>::iterator it = _linkedNodes.begin();
982 _linkedNodes.erase(it);
991 SGPropertyNode::getDisplayName (bool simplify) const
993 string display_name = _name;
994 if (_index != 0 || !simplify) {
996 sstr << '[' << _index << ']';
997 display_name += sstr.str();
1004 SGPropertyNode::getPath (bool simplify) const
1006 // Calculate the complete path only once.
1007 if (_parent != 0 && _path.empty()) {
1008 _path = _parent->getPath(simplify);
1010 _path += getDisplayName(simplify);
1013 return _path.c_str();
1017 SGPropertyNode::getType () const
1019 if (_type == props::ALIAS)
1020 return _value.alias->getType();
1021 else if (_type == props::EXTENDED)
1022 return _value.val->getType();
1029 SGPropertyNode::getBoolValue () const
1031 // Shortcut for common case
1032 if (_attr == (READ|WRITE) && _type == props::BOOL)
1035 if (getAttribute(TRACE_READ))
1037 if (!getAttribute(READ))
1038 return SGRawValue<bool>::DefaultValue();
1041 return _value.alias->getBoolValue();
1045 return get_int() == 0 ? false : true;
1047 return get_long() == 0L ? false : true;
1049 return get_float() == 0.0 ? false : true;
1051 return get_double() == 0.0L ? false : true;
1053 case props::UNSPECIFIED:
1054 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1057 return SGRawValue<bool>::DefaultValue();
1062 SGPropertyNode::getIntValue () const
1064 // Shortcut for common case
1065 if (_attr == (READ|WRITE) && _type == props::INT)
1068 if (getAttribute(TRACE_READ))
1070 if (!getAttribute(READ))
1071 return SGRawValue<int>::DefaultValue();
1074 return _value.alias->getIntValue();
1076 return int(get_bool());
1080 return int(get_long());
1082 return int(get_float());
1084 return int(get_double());
1086 case props::UNSPECIFIED:
1087 return atoi(get_string());
1090 return SGRawValue<int>::DefaultValue();
1095 SGPropertyNode::getLongValue () const
1097 // Shortcut for common case
1098 if (_attr == (READ|WRITE) && _type == props::LONG)
1101 if (getAttribute(TRACE_READ))
1103 if (!getAttribute(READ))
1104 return SGRawValue<long>::DefaultValue();
1107 return _value.alias->getLongValue();
1109 return long(get_bool());
1111 return long(get_int());
1115 return long(get_float());
1117 return long(get_double());
1119 case props::UNSPECIFIED:
1120 return strtol(get_string(), 0, 0);
1123 return SGRawValue<long>::DefaultValue();
1128 SGPropertyNode::getFloatValue () const
1130 // Shortcut for common case
1131 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1134 if (getAttribute(TRACE_READ))
1136 if (!getAttribute(READ))
1137 return SGRawValue<float>::DefaultValue();
1140 return _value.alias->getFloatValue();
1142 return float(get_bool());
1144 return float(get_int());
1146 return float(get_long());
1150 return float(get_double());
1152 case props::UNSPECIFIED:
1153 return atof(get_string());
1156 return SGRawValue<float>::DefaultValue();
1161 SGPropertyNode::getDoubleValue () const
1163 // Shortcut for common case
1164 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1165 return get_double();
1167 if (getAttribute(TRACE_READ))
1169 if (!getAttribute(READ))
1170 return SGRawValue<double>::DefaultValue();
1174 return _value.alias->getDoubleValue();
1176 return double(get_bool());
1178 return double(get_int());
1180 return double(get_long());
1182 return double(get_float());
1184 return get_double();
1186 case props::UNSPECIFIED:
1187 return strtod(get_string(), 0);
1190 return SGRawValue<double>::DefaultValue();
1195 SGPropertyNode::getStringValue () const
1197 // Shortcut for common case
1198 if (_attr == (READ|WRITE) && _type == props::STRING)
1199 return get_string();
1201 if (getAttribute(TRACE_READ))
1203 if (!getAttribute(READ))
1204 return SGRawValue<const char *>::DefaultValue();
1205 return make_string();
1209 SGPropertyNode::setBoolValue (bool value)
1211 // Shortcut for common case
1212 if (_attr == (READ|WRITE) && _type == props::BOOL)
1213 return set_bool(value);
1215 bool result = false;
1217 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1220 _type = props::BOOL;
1225 result = _value.alias->setBoolValue(value);
1228 result = set_bool(value);
1231 result = set_int(int(value));
1234 result = set_long(long(value));
1237 result = set_float(float(value));
1240 result = set_double(double(value));
1243 case props::UNSPECIFIED:
1244 result = set_string(value ? "true" : "false");
1251 if (getAttribute(TRACE_WRITE))
1257 SGPropertyNode::setIntValue (int value)
1259 // Shortcut for common case
1260 if (_attr == (READ|WRITE) && _type == props::INT)
1261 return set_int(value);
1263 bool result = false;
1265 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1268 _local_val.int_val = 0;
1273 result = _value.alias->setIntValue(value);
1276 result = set_bool(value == 0 ? false : true);
1279 result = set_int(value);
1282 result = set_long(long(value));
1285 result = set_float(float(value));
1288 result = set_double(double(value));
1291 case props::UNSPECIFIED: {
1293 sprintf(buf, "%d", value);
1294 result = set_string(buf);
1302 if (getAttribute(TRACE_WRITE))
1308 SGPropertyNode::setLongValue (long value)
1310 // Shortcut for common case
1311 if (_attr == (READ|WRITE) && _type == props::LONG)
1312 return set_long(value);
1314 bool result = false;
1316 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1318 _type = props::LONG;
1319 _local_val.long_val = 0L;
1324 result = _value.alias->setLongValue(value);
1327 result = set_bool(value == 0L ? false : true);
1330 result = set_int(int(value));
1333 result = set_long(value);
1336 result = set_float(float(value));
1339 result = set_double(double(value));
1342 case props::UNSPECIFIED: {
1344 sprintf(buf, "%ld", value);
1345 result = set_string(buf);
1353 if (getAttribute(TRACE_WRITE))
1359 SGPropertyNode::setFloatValue (float value)
1361 // Shortcut for common case
1362 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1363 return set_float(value);
1365 bool result = false;
1367 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1369 _type = props::FLOAT;
1370 _local_val.float_val = 0;
1375 result = _value.alias->setFloatValue(value);
1378 result = set_bool(value == 0.0 ? false : true);
1381 result = set_int(int(value));
1384 result = set_long(long(value));
1387 result = set_float(value);
1390 result = set_double(double(value));
1393 case props::UNSPECIFIED: {
1395 sprintf(buf, "%f", value);
1396 result = set_string(buf);
1404 if (getAttribute(TRACE_WRITE))
1410 SGPropertyNode::setDoubleValue (double value)
1412 // Shortcut for common case
1413 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1414 return set_double(value);
1416 bool result = false;
1418 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1420 _local_val.double_val = value;
1421 _type = props::DOUBLE;
1426 result = _value.alias->setDoubleValue(value);
1429 result = set_bool(value == 0.0L ? false : true);
1432 result = set_int(int(value));
1435 result = set_long(long(value));
1438 result = set_float(float(value));
1441 result = set_double(value);
1444 case props::UNSPECIFIED: {
1446 sprintf(buf, "%f", value);
1447 result = set_string(buf);
1455 if (getAttribute(TRACE_WRITE))
1461 SGPropertyNode::setStringValue (const char * value)
1463 // Shortcut for common case
1464 if (_attr == (READ|WRITE) && _type == props::STRING)
1465 return set_string(value);
1467 bool result = false;
1469 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1471 _type = props::STRING;
1476 result = _value.alias->setStringValue(value);
1479 result = set_bool((compare_strings(value, "true")
1480 || atoi(value)) ? true : false);
1483 result = set_int(atoi(value));
1486 result = set_long(strtol(value, 0, 0));
1489 result = set_float(atof(value));
1492 result = set_double(strtod(value, 0));
1495 case props::UNSPECIFIED:
1496 result = set_string(value);
1498 case props::EXTENDED:
1500 stringstream sstr(value);
1501 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1509 if (getAttribute(TRACE_WRITE))
1515 SGPropertyNode::setUnspecifiedValue (const char * value)
1517 bool result = false;
1519 if (_type == props::NONE) {
1521 _type = props::UNSPECIFIED;
1523 props::Type type = _type;
1524 if (type == props::EXTENDED)
1525 type = _value.val->getType();
1528 result = _value.alias->setUnspecifiedValue(value);
1531 result = set_bool((compare_strings(value, "true")
1532 || atoi(value)) ? true : false);
1535 result = set_int(atoi(value));
1538 result = set_long(strtol(value, 0, 0));
1541 result = set_float(atof(value));
1544 result = set_double(strtod(value, 0));
1547 case props::UNSPECIFIED:
1548 result = set_string(value);
1551 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1554 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1561 if (getAttribute(TRACE_WRITE))
1566 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1568 if (!getAttribute(READ))
1572 return _value.alias->printOn(stream);
1574 stream << (get_bool() ? "true" : "false");
1577 stream << get_int();
1580 stream << get_long();
1583 stream << get_float();
1586 stream << get_double();
1589 case props::UNSPECIFIED:
1590 stream << get_string();
1592 case props::EXTENDED:
1593 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1597 default: // avoid compiler warning
1604 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1607 if (_type == props::ALIAS || _tied)
1610 useDefault = useDefault && hasValue();
1611 std::string old_val;
1613 old_val = getStringValue();
1615 _type = props::STRING;
1617 _value.val = rawValue.clone();
1620 setStringValue(old_val.c_str());
1625 SGPropertyNode::untie ()
1632 bool val = getBoolValue();
1634 _type = props::BOOL;
1635 _local_val.bool_val = val;
1639 int val = getIntValue();
1642 _local_val.int_val = val;
1646 long val = getLongValue();
1648 _type = props::LONG;
1649 _local_val.long_val = val;
1652 case props::FLOAT: {
1653 float val = getFloatValue();
1655 _type = props::FLOAT;
1656 _local_val.float_val = val;
1659 case props::DOUBLE: {
1660 double val = getDoubleValue();
1662 _type = props::DOUBLE;
1663 _local_val.double_val = val;
1667 case props::UNSPECIFIED: {
1668 string val = getStringValue();
1670 _type = props::STRING;
1671 _local_val.string_val = copy_string(val.c_str());
1674 case props::EXTENDED: {
1675 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1676 _value.val = 0; // Prevent clearValue() from deleting
1678 _type = props::EXTENDED;
1679 _value.val = val->makeContainer();
1693 SGPropertyNode::getRootNode ()
1698 return _parent->getRootNode();
1701 const SGPropertyNode *
1702 SGPropertyNode::getRootNode () const
1707 return _parent->getRootNode();
1711 SGPropertyNode::getNode (const char * relative_path, bool create)
1713 if (_path_cache == 0)
1714 _path_cache = new hash_table;
1716 SGPropertyNode * result = _path_cache->get(relative_path);
1718 vector<PathComponent> components;
1719 parse_path(relative_path, components);
1720 result = find_node(this, components, 0, create);
1722 _path_cache->put(relative_path, result);
1729 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1731 vector<PathComponent> components;
1732 parse_path(relative_path, components);
1733 if (components.size() > 0)
1734 components.back().index = index;
1735 return find_node(this, components, 0, create);
1738 const SGPropertyNode *
1739 SGPropertyNode::getNode (const char * relative_path) const
1741 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1744 const SGPropertyNode *
1745 SGPropertyNode::getNode (const char * relative_path, int index) const
1747 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1751 ////////////////////////////////////////////////////////////////////////
1752 // Convenience methods using relative paths.
1753 ////////////////////////////////////////////////////////////////////////
1757 * Test whether another node has a value attached.
1760 SGPropertyNode::hasValue (const char * relative_path) const
1762 const SGPropertyNode * node = getNode(relative_path);
1763 return (node == 0 ? false : node->hasValue());
1768 * Get the value type for another node.
1771 SGPropertyNode::getType (const char * relative_path) const
1773 const SGPropertyNode * node = getNode(relative_path);
1774 return (node == 0 ? props::UNSPECIFIED : node->getType());
1779 * Get a bool value for another node.
1782 SGPropertyNode::getBoolValue (const char * relative_path,
1783 bool defaultValue) const
1785 const SGPropertyNode * node = getNode(relative_path);
1786 return (node == 0 ? defaultValue : node->getBoolValue());
1791 * Get an int value for another node.
1794 SGPropertyNode::getIntValue (const char * relative_path,
1795 int defaultValue) const
1797 const SGPropertyNode * node = getNode(relative_path);
1798 return (node == 0 ? defaultValue : node->getIntValue());
1803 * Get a long value for another node.
1806 SGPropertyNode::getLongValue (const char * relative_path,
1807 long defaultValue) const
1809 const SGPropertyNode * node = getNode(relative_path);
1810 return (node == 0 ? defaultValue : node->getLongValue());
1815 * Get a float value for another node.
1818 SGPropertyNode::getFloatValue (const char * relative_path,
1819 float defaultValue) const
1821 const SGPropertyNode * node = getNode(relative_path);
1822 return (node == 0 ? defaultValue : node->getFloatValue());
1827 * Get a double value for another node.
1830 SGPropertyNode::getDoubleValue (const char * relative_path,
1831 double defaultValue) const
1833 const SGPropertyNode * node = getNode(relative_path);
1834 return (node == 0 ? defaultValue : node->getDoubleValue());
1839 * Get a string value for another node.
1842 SGPropertyNode::getStringValue (const char * relative_path,
1843 const char * defaultValue) const
1845 const SGPropertyNode * node = getNode(relative_path);
1846 return (node == 0 ? defaultValue : node->getStringValue());
1851 * Set a bool value for another node.
1854 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1856 return getNode(relative_path, true)->setBoolValue(value);
1861 * Set an int value for another node.
1864 SGPropertyNode::setIntValue (const char * relative_path, int value)
1866 return getNode(relative_path, true)->setIntValue(value);
1871 * Set a long value for another node.
1874 SGPropertyNode::setLongValue (const char * relative_path, long value)
1876 return getNode(relative_path, true)->setLongValue(value);
1881 * Set a float value for another node.
1884 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1886 return getNode(relative_path, true)->setFloatValue(value);
1891 * Set a double value for another node.
1894 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1896 return getNode(relative_path, true)->setDoubleValue(value);
1901 * Set a string value for another node.
1904 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1906 return getNode(relative_path, true)->setStringValue(value);
1911 * Set an unknown value for another node.
1914 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1917 return getNode(relative_path, true)->setUnspecifiedValue(value);
1922 * Test whether another node is tied.
1925 SGPropertyNode::isTied (const char * relative_path) const
1927 const SGPropertyNode * node = getNode(relative_path);
1928 return (node == 0 ? false : node->isTied());
1933 * Tie a node reached by a relative path, creating it if necessary.
1936 SGPropertyNode::tie (const char * relative_path,
1937 const SGRawValue<bool> &rawValue,
1940 return getNode(relative_path, true)->tie(rawValue, useDefault);
1945 * Tie a node reached by a relative path, creating it if necessary.
1948 SGPropertyNode::tie (const char * relative_path,
1949 const SGRawValue<int> &rawValue,
1952 return getNode(relative_path, true)->tie(rawValue, useDefault);
1957 * Tie a node reached by a relative path, creating it if necessary.
1960 SGPropertyNode::tie (const char * relative_path,
1961 const SGRawValue<long> &rawValue,
1964 return getNode(relative_path, true)->tie(rawValue, useDefault);
1969 * Tie a node reached by a relative path, creating it if necessary.
1972 SGPropertyNode::tie (const char * relative_path,
1973 const SGRawValue<float> &rawValue,
1976 return getNode(relative_path, true)->tie(rawValue, useDefault);
1981 * Tie a node reached by a relative path, creating it if necessary.
1984 SGPropertyNode::tie (const char * relative_path,
1985 const SGRawValue<double> &rawValue,
1988 return getNode(relative_path, true)->tie(rawValue, useDefault);
1993 * Tie a node reached by a relative path, creating it if necessary.
1996 SGPropertyNode::tie (const char * relative_path,
1997 const SGRawValue<const char *> &rawValue,
2000 return getNode(relative_path, true)->tie(rawValue, useDefault);
2005 * Attempt to untie another node reached by a relative path.
2008 SGPropertyNode::untie (const char * relative_path)
2010 SGPropertyNode * node = getNode(relative_path);
2011 return (node == 0 ? false : node->untie());
2015 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2018 if (_listeners == 0)
2019 _listeners = new vector<SGPropertyChangeListener*>;
2020 _listeners->push_back(listener);
2021 listener->register_property(this);
2023 listener->valueChanged(this);
2027 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2029 vector<SGPropertyChangeListener*>::iterator it =
2030 find(_listeners->begin(), _listeners->end(), listener);
2031 if (it != _listeners->end()) {
2032 _listeners->erase(it);
2033 listener->unregister_property(this);
2034 if (_listeners->empty()) {
2035 vector<SGPropertyChangeListener*>* tmp = _listeners;
2043 SGPropertyNode::fireValueChanged ()
2045 fireValueChanged(this);
2049 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2051 fireChildAdded(this, child);
2055 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2057 fireChildRemoved(this, child);
2061 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2063 if (_listeners != 0) {
2064 for (unsigned int i = 0; i < _listeners->size(); i++) {
2065 (*_listeners)[i]->valueChanged(node);
2069 _parent->fireValueChanged(node);
2073 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2074 SGPropertyNode * child)
2076 if (_listeners != 0) {
2077 for (unsigned int i = 0; i < _listeners->size(); i++) {
2078 (*_listeners)[i]->childAdded(parent, child);
2082 _parent->fireChildAdded(parent, child);
2086 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2087 SGPropertyNode * child)
2089 if (_listeners != 0) {
2090 for (unsigned int i = 0; i < _listeners->size(); i++) {
2091 (*_listeners)[i]->childRemoved(parent, child);
2095 _parent->fireChildRemoved(parent, child);
2100 ////////////////////////////////////////////////////////////////////////
2101 // Simplified hash table for caching paths.
2102 ////////////////////////////////////////////////////////////////////////
2104 #define HASH_TABLE_SIZE 199
2106 SGPropertyNode::hash_table::entry::entry ()
2111 SGPropertyNode::hash_table::entry::~entry ()
2113 // Don't delete the value; we don't own
2118 SGPropertyNode::hash_table::entry::set_key (const char * key)
2124 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2129 SGPropertyNode::hash_table::bucket::bucket ()
2135 SGPropertyNode::hash_table::bucket::~bucket ()
2137 for (int i = 0; i < _length; i++)
2142 SGPropertyNode::hash_table::entry *
2143 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2146 for (i = 0; i < _length; i++) {
2147 if (!strcmp(_entries[i]->get_key(), key))
2151 entry ** new_entries = new entry*[_length+1];
2152 for (i = 0; i < _length; i++) {
2153 new_entries[i] = _entries[i];
2156 _entries = new_entries;
2157 _entries[_length] = new entry;
2158 _entries[_length]->set_key(key);
2160 return _entries[_length - 1];
2167 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2169 for (int i = 0; i < _length; i++) {
2170 if (_entries[i]->get_value() == node) {
2172 for (++i; i < _length; i++) {
2173 _entries[i-1] = _entries[i];
2183 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2185 for (int i = 0; i < _length; i++) {
2186 SGPropertyNode * node = _entries[i]->get_value();
2188 node->remove_linked_node(owner);
2192 SGPropertyNode::hash_table::hash_table ()
2198 SGPropertyNode::hash_table::~hash_table ()
2200 for (unsigned int i = 0; i < _data_length; i++) {
2202 _data[i]->clear(this);
2210 SGPropertyNode::hash_table::get (const char * key)
2212 if (_data_length == 0)
2214 unsigned int index = hashcode(key) % _data_length;
2215 if (_data[index] == 0)
2217 entry * e = _data[index]->get_entry(key);
2221 return e->get_value();
2225 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2227 if (_data_length == 0) {
2228 _data = new bucket*[HASH_TABLE_SIZE];
2229 _data_length = HASH_TABLE_SIZE;
2230 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2233 unsigned int index = hashcode(key) % _data_length;
2234 if (_data[index] == 0) {
2235 _data[index] = new bucket;
2237 entry * e = _data[index]->get_entry(key, true);
2238 e->set_value(value);
2239 value->add_linked_node(this);
2243 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2245 for (unsigned int i = 0; i < _data_length; i++)
2246 if (_data[i] && _data[i]->erase(node))
2253 SGPropertyNode::hash_table::hashcode (const char * key)
2255 unsigned int hash = 0;
2257 hash = 31 * hash + *key;
2265 ////////////////////////////////////////////////////////////////////////
2266 // Implementation of SGPropertyChangeListener.
2267 ////////////////////////////////////////////////////////////////////////
2269 SGPropertyChangeListener::~SGPropertyChangeListener ()
2271 for (int i = _properties.size() - 1; i >= 0; i--)
2272 _properties[i]->removeChangeListener(this);
2276 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2282 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2283 SGPropertyNode * child)
2289 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2290 SGPropertyNode * child)
2296 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2298 _properties.push_back(node);
2302 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2304 vector<SGPropertyNode *>::iterator it =
2305 find(_properties.begin(), _properties.end(), node);
2306 if (it != _properties.end())
2307 _properties.erase(it);
2311 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2314 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2315 for (int i = 0; i < 3; ++i) {
2326 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2328 for (int i = 0; i < 3; ++i) {
2329 stream >> result[i];
2335 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2338 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2339 for (int i = 0; i < 4; ++i) {
2350 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2352 for (int i = 0; i < 4; ++i) {
2353 stream >> result[i];