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].
25 #include <simgear/compiler.h>
26 #include <simgear/debug/logstream.hxx>
34 ////////////////////////////////////////////////////////////////////////
36 ////////////////////////////////////////////////////////////////////////
39 * Comparator class for sorting by index.
44 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
45 return (n1->getIndex() < n2->getIndex());
51 ////////////////////////////////////////////////////////////////////////
52 // Convenience macros for value access.
53 ////////////////////////////////////////////////////////////////////////
55 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
56 #define TEST_WRITE if (!getAttribute(WRITE)) return false
60 ////////////////////////////////////////////////////////////////////////
61 // Default values for every type.
62 ////////////////////////////////////////////////////////////////////////
64 const bool SGRawValue<bool>::DefaultValue = false;
65 const int SGRawValue<int>::DefaultValue = 0;
66 const long SGRawValue<long>::DefaultValue = 0L;
67 const float SGRawValue<float>::DefaultValue = 0.0;
68 const double SGRawValue<double>::DefaultValue = 0.0L;
69 const char * const SGRawValue<const char *>::DefaultValue = "";
73 ////////////////////////////////////////////////////////////////////////
74 // Local path normalization code.
75 ////////////////////////////////////////////////////////////////////////
78 * A component in a path.
87 * Parse the name for a path component.
89 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
91 static inline const string
92 parse_name (const string &path, int &i)
95 int max = path.size();
99 if (i < max && path[i] == '.') {
105 if (i < max && path[i] != '/')
106 throw string(string("Illegal character after ") + name);
109 else if (isalpha(path[i]) || path[i] == '_') {
113 // The rules inside a name are a little
116 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
117 path[i] == '-' || path[i] == '.') {
119 } else if (path[i] == '[' || path[i] == '/') {
122 throw string("name may contain only ._- and alphanumeric characters");
129 if (name.size() == 0)
130 throw string("name must begin with alpha or '_'");
138 * Parse the optional integer index for a path component.
140 * Index: "[" [0-9]+ "]"
143 parse_index (const string &path, int &i)
152 for (int max = path.size(); i < max; i++) {
153 if (isdigit(path[i])) {
154 index = (index * 10) + (path[i] - '0');
155 } else if (path[i] == ']') {
163 throw string("unterminated index (looking for ']')");
168 * Parse a single path component.
170 * Component: Name Index?
172 static inline PathComponent
173 parse_component (const string &path, int &i)
175 PathComponent component;
176 component.name = parse_name(path, i);
177 if (component.name[0] != '.')
178 component.index = parse_index(path, i);
180 component.index = -1;
186 * Parse a path into its components.
189 parse_path (const string &path, vector<PathComponent> &components)
192 int max = path.size();
194 // Check for initial '/'
195 if (path[pos] == '/') {
199 components.push_back(root);
201 while (pos < max && path[pos] == '/')
206 components.push_back(parse_component(path, pos));
207 while (pos < max && path[pos] == '/')
214 ////////////////////////////////////////////////////////////////////////
215 // Other static utility functions.
216 ////////////////////////////////////////////////////////////////////////
220 copy_string (const char * s)
222 // FIXME: potential buffer overflow.
223 // For some reason, strnlen and
224 // strncpy cause all kinds of crashes.
225 char * copy = new char[strlen(s) + 1];
231 compare_strings (const char * s1, const char * s2)
233 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
237 * Locate a child node by name and index.
240 find_child (const char * name, int index, vector<SGPropertyNode_ptr> nodes)
242 int nNodes = nodes.size();
243 for (int i = 0; i < nNodes; i++) {
244 SGPropertyNode * node = nodes[i];
245 if (compare_strings(node->getName(), name) && node->getIndex() == index)
253 * Locate another node, given a relative path.
255 static SGPropertyNode *
256 find_node (SGPropertyNode * current,
257 const vector<PathComponent> &components,
261 // Run off the end of the list
266 // Success! This is the one we want.
267 else if (position >= (int)components.size()) {
271 // Empty component means root.
272 else if (components[position].name == "") {
273 return find_node(current->getRootNode(), components, position + 1, create);
276 // . means current directory
277 else if (components[position].name == ".") {
278 return find_node(current, components, position + 1, create);
281 // .. means parent directory
282 else if (components[position].name == "..") {
283 SGPropertyNode * parent = current->getParent();
285 throw string("Attempt to move past root with '..'");
287 return find_node(parent, components, position + 1, create);
290 // Otherwise, a child name
292 SGPropertyNode * child =
293 current->getChild(components[position].name.c_str(),
294 components[position].index,
296 return find_node(child, components, position + 1, create);
302 ////////////////////////////////////////////////////////////////////////
303 // Private methods from SGPropertyNode (may be inlined for speed).
304 ////////////////////////////////////////////////////////////////////////
307 SGPropertyNode::get_bool () const
310 return _value.bool_val->getValue();
312 return _local_val.bool_val;
316 SGPropertyNode::get_int () const
319 return _value.int_val->getValue();
321 return _local_val.int_val;
325 SGPropertyNode::get_long () const
328 return _value.long_val->getValue();
330 return _local_val.long_val;
334 SGPropertyNode::get_float () const
337 return _value.float_val->getValue();
339 return _local_val.float_val;
343 SGPropertyNode::get_double () const
346 return _value.double_val->getValue();
348 return _local_val.double_val;
352 SGPropertyNode::get_string () const
355 return _value.string_val->getValue();
357 return _local_val.string_val;
361 SGPropertyNode::set_bool (bool val)
364 if (_value.bool_val->setValue(val)) {
371 _local_val.bool_val = val;
378 SGPropertyNode::set_int (int val)
381 if (_value.int_val->setValue(val)) {
388 _local_val.int_val = val;
395 SGPropertyNode::set_long (long val)
398 if (_value.long_val->setValue(val)) {
405 _local_val.long_val = val;
412 SGPropertyNode::set_float (float val)
415 if (_value.float_val->setValue(val)) {
422 _local_val.float_val = val;
429 SGPropertyNode::set_double (double val)
432 if (_value.double_val->setValue(val)) {
439 _local_val.double_val = val;
446 SGPropertyNode::set_string (const char * val)
449 if (_value.string_val->setValue(val)) {
456 delete [] _local_val.string_val;
457 _local_val.string_val = copy_string(val);
464 SGPropertyNode::clear_value ()
474 delete _value.bool_val;
477 _local_val.bool_val = SGRawValue<bool>::DefaultValue;
481 delete _value.int_val;
484 _local_val.int_val = SGRawValue<int>::DefaultValue;
488 delete _value.long_val;
489 _value.long_val = 0L;
491 _local_val.long_val = SGRawValue<long>::DefaultValue;
495 delete _value.float_val;
496 _value.float_val = 0;
498 _local_val.float_val = SGRawValue<float>::DefaultValue;
502 delete _value.double_val;
503 _value.double_val = 0;
505 _local_val.double_val = SGRawValue<double>::DefaultValue;
510 delete _value.string_val;
511 _value.string_val = 0;
513 delete [] _local_val.string_val;
515 _local_val.string_val = 0;
524 * Get the value as a string.
527 SGPropertyNode::make_string () const
529 if (!getAttribute(READ))
534 return _value.alias->getStringValue();
541 sprintf(_buffer, "%d", get_int());
544 sprintf(_buffer, "%ld", get_long());
547 sprintf(_buffer, "%f", get_float());
550 sprintf(_buffer, "%f", get_double());
562 * Trace a write access for a property.
565 SGPropertyNode::trace_write () const
568 cerr << "TRACE: Write node " << getPath () << ", value\""
569 << make_string() << '"' << endl;
571 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
572 << ", value\"" << make_string() << '"');
577 * Trace a read access for a property.
580 SGPropertyNode::trace_read () const
583 cerr << "TRACE: Write node " << getPath () << ", value \""
584 << make_string() << '"' << endl;
586 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
587 << ", value \"" << make_string() << '"');
592 * Increment reference counter
595 SGPropertyNode::incrementRef()
601 * Decrement reference counter
604 SGPropertyNode::decrementRef()
611 ////////////////////////////////////////////////////////////////////////
612 // Public methods from SGPropertyNode.
613 ////////////////////////////////////////////////////////////////////////
616 * Last used attribute
617 * Update as needed when enum Attribute is changed
619 const int SGPropertyNode::LAST_USED_ATTRIBUTE = TRACE_WRITE;
622 * Default constructor: always creates a root node.
624 SGPropertyNode::SGPropertyNode ()
625 : _name(copy_string("")),
635 _local_val.string_val = 0;
642 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
643 : _index(node._index),
644 _parent(0), // don't copy the parent
650 _listeners(0) // CHECK!!
652 _name = copy_string(node._name);
653 _local_val.string_val = 0;
658 _value.alias = node._value.alias;
664 _value.bool_val = node._value.bool_val->clone();
667 set_bool(node.get_bool());
673 _value.int_val = node._value.int_val->clone();
676 set_int(node.get_int());
682 _value.long_val = node._value.long_val->clone();
685 set_long(node.get_long());
691 _value.float_val = node._value.float_val->clone();
694 set_float(node.get_float());
700 _value.double_val = node._value.double_val->clone();
703 set_double(node.get_double());
710 _value.string_val = node._value.string_val->clone();
713 set_string(node.get_string());
721 * Convenience constructor.
723 SGPropertyNode::SGPropertyNode (const char * name,
725 SGPropertyNode * parent)
735 _name = copy_string(name);
736 _local_val.string_val = 0;
743 SGPropertyNode::~SGPropertyNode ()
753 * Alias to another node.
756 SGPropertyNode::alias (SGPropertyNode * target)
758 if (target == 0 || _type == ALIAS || _tied)
761 _value.alias = target;
768 * Alias to another node by path.
771 SGPropertyNode::alias (const char * path)
773 return alias(getNode(path, true));
781 SGPropertyNode::unalias ()
792 * Get the target of an alias.
795 SGPropertyNode::getAliasTarget ()
797 return (_type == ALIAS ? _value.alias : 0);
801 const SGPropertyNode *
802 SGPropertyNode::getAliasTarget () const
804 return (_type == ALIAS ? _value.alias : 0);
809 * Get a non-const child by index.
812 SGPropertyNode::getChild (int position)
814 if (position >= 0 && position < nChildren())
815 return _children[position];
822 * Get a const child by index.
824 const SGPropertyNode *
825 SGPropertyNode::getChild (int position) const
827 if (position >= 0 && position < nChildren())
828 return _children[position];
835 * Get a non-const child by name and index, creating if necessary.
838 SGPropertyNode::getChild (const char * name, int index, bool create)
840 int pos = find_child(name, index, _children);
842 return _children[pos];
844 SGPropertyNode_ptr node;
845 pos = find_child(name, index, _removedChildren);
847 std::vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
849 node = _removedChildren[pos];
850 _removedChildren.erase(it);
851 node->setAttribute(REMOVED, false);
853 node = new SGPropertyNode(name, index, this);
855 _children.push_back(node);
856 fireChildAdded(node);
865 * Get a const child by name and index.
867 const SGPropertyNode *
868 SGPropertyNode::getChild (const char * name, int index) const
870 int pos = find_child(name, index, _children);
872 return _children[pos];
879 * Get all children with the same name (but different indices).
881 vector<SGPropertyNode_ptr>
882 SGPropertyNode::getChildren (const char * name) const
884 vector<SGPropertyNode_ptr> children;
885 int max = _children.size();
887 for (int i = 0; i < max; i++)
888 if (compare_strings(_children[i]->getName(), name))
889 children.push_back(_children[i]);
891 sort(children.begin(), children.end(), CompareIndices());
897 * Remove a child node
900 SGPropertyNode::removeChild (const char * name, int index, bool keep)
902 SGPropertyNode_ptr ret;
903 int pos = find_child(name, index, _children);
905 std::vector<SGPropertyNode_ptr>::iterator it = _children.begin();
907 SGPropertyNode_ptr node = _children[pos];
910 _removedChildren.push_back(node);
912 node->setAttribute(REMOVED, true);
914 fireChildRemoved(node);
921 SGPropertyNode::getPath (bool simplify) const
926 string path = _parent->getPath(simplify);
929 if (_index != 0 || !simplify) {
931 sprintf(buffer, "[%d]", _index);
938 SGPropertyNode::getType () const
941 return _value.alias->getType();
948 SGPropertyNode::getBoolValue () const
950 // Shortcut for common case
951 if (_attr == (READ|WRITE) && _type == BOOL)
954 if (getAttribute(TRACE_READ))
956 if (!getAttribute(READ))
957 return SGRawValue<bool>::DefaultValue;
960 return _value.alias->getBoolValue();
964 return get_int() == 0 ? false : true;
966 return get_long() == 0L ? false : true;
968 return get_float() == 0.0 ? false : true;
970 return get_double() == 0.0L ? false : true;
973 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
976 return SGRawValue<bool>::DefaultValue;
981 SGPropertyNode::getIntValue () const
983 // Shortcut for common case
984 if (_attr == (READ|WRITE) && _type == INT)
987 if (getAttribute(TRACE_READ))
989 if (!getAttribute(READ))
990 return SGRawValue<int>::DefaultValue;
993 return _value.alias->getIntValue();
995 return int(get_bool());
999 return int(get_long());
1001 return int(get_float());
1003 return int(get_double());
1006 return atoi(get_string());
1009 return SGRawValue<int>::DefaultValue;
1014 SGPropertyNode::getLongValue () const
1016 // Shortcut for common case
1017 if (_attr == (READ|WRITE) && _type == LONG)
1020 if (getAttribute(TRACE_READ))
1022 if (!getAttribute(READ))
1023 return SGRawValue<long>::DefaultValue;
1026 return _value.alias->getLongValue();
1028 return long(get_bool());
1030 return long(get_int());
1034 return long(get_float());
1036 return long(get_double());
1039 return strtol(get_string(), 0, 0);
1042 return SGRawValue<long>::DefaultValue;
1047 SGPropertyNode::getFloatValue () const
1049 // Shortcut for common case
1050 if (_attr == (READ|WRITE) && _type == FLOAT)
1053 if (getAttribute(TRACE_READ))
1055 if (!getAttribute(READ))
1056 return SGRawValue<float>::DefaultValue;
1059 return _value.alias->getFloatValue();
1061 return float(get_bool());
1063 return float(get_int());
1065 return float(get_long());
1069 return float(get_double());
1072 return atof(get_string());
1075 return SGRawValue<float>::DefaultValue;
1080 SGPropertyNode::getDoubleValue () const
1082 // Shortcut for common case
1083 if (_attr == (READ|WRITE) && _type == DOUBLE)
1084 return get_double();
1086 if (getAttribute(TRACE_READ))
1088 if (!getAttribute(READ))
1089 return SGRawValue<double>::DefaultValue;
1093 return _value.alias->getDoubleValue();
1095 return double(get_bool());
1097 return double(get_int());
1099 return double(get_long());
1101 return double(get_float());
1103 return get_double();
1106 return strtod(get_string(), 0);
1109 return SGRawValue<double>::DefaultValue;
1114 SGPropertyNode::getStringValue () const
1116 // Shortcut for common case
1117 if (_attr == (READ|WRITE) && _type == STRING)
1118 return get_string();
1120 if (getAttribute(TRACE_READ))
1122 if (!getAttribute(READ))
1123 return SGRawValue<const char *>::DefaultValue;
1124 return make_string();
1128 SGPropertyNode::setBoolValue (bool value)
1130 // Shortcut for common case
1131 if (_attr == (READ|WRITE) && _type == BOOL)
1132 return set_bool(value);
1134 bool result = false;
1136 if (_type == NONE || _type == UNSPECIFIED) {
1144 result = _value.alias->setBoolValue(value);
1147 result = set_bool(value);
1150 result = set_int(int(value));
1153 result = set_long(long(value));
1156 result = set_float(float(value));
1159 result = set_double(double(value));
1163 result = set_string(value ? "true" : "false");
1170 if (getAttribute(TRACE_WRITE))
1176 SGPropertyNode::setIntValue (int value)
1178 // Shortcut for common case
1179 if (_attr == (READ|WRITE) && _type == INT)
1180 return set_int(value);
1182 bool result = false;
1184 if (_type == NONE || _type == UNSPECIFIED) {
1187 _local_val.int_val = 0;
1192 result = _value.alias->setIntValue(value);
1195 result = set_bool(value == 0 ? false : true);
1198 result = set_int(value);
1201 result = set_long(long(value));
1204 result = set_float(float(value));
1207 result = set_double(double(value));
1212 sprintf(buf, "%d", value);
1213 result = set_string(buf);
1221 if (getAttribute(TRACE_WRITE))
1227 SGPropertyNode::setLongValue (long value)
1229 // Shortcut for common case
1230 if (_attr == (READ|WRITE) && _type == LONG)
1231 return set_long(value);
1233 bool result = false;
1235 if (_type == NONE || _type == UNSPECIFIED) {
1238 _local_val.long_val = 0L;
1243 result = _value.alias->setLongValue(value);
1246 result = set_bool(value == 0L ? false : true);
1249 result = set_int(int(value));
1252 result = set_long(value);
1255 result = set_float(float(value));
1258 result = set_double(double(value));
1263 sprintf(buf, "%ld", value);
1264 result = set_string(buf);
1272 if (getAttribute(TRACE_WRITE))
1278 SGPropertyNode::setFloatValue (float value)
1280 // Shortcut for common case
1281 if (_attr == (READ|WRITE) && _type == FLOAT)
1282 return set_float(value);
1284 bool result = false;
1286 if (_type == NONE || _type == UNSPECIFIED) {
1289 _local_val.float_val = 0;
1294 result = _value.alias->setFloatValue(value);
1297 result = set_bool(value == 0.0 ? false : true);
1300 result = set_int(int(value));
1303 result = set_long(long(value));
1306 result = set_float(value);
1309 result = set_double(double(value));
1314 sprintf(buf, "%f", value);
1315 result = set_string(buf);
1323 if (getAttribute(TRACE_WRITE))
1329 SGPropertyNode::setDoubleValue (double value)
1331 // Shortcut for common case
1332 if (_attr == (READ|WRITE) && _type == DOUBLE)
1333 return set_double(value);
1335 bool result = false;
1337 if (_type == NONE || _type == UNSPECIFIED) {
1339 _local_val.double_val = value;
1345 result = _value.alias->setDoubleValue(value);
1348 result = set_bool(value == 0.0L ? false : true);
1351 result = set_int(int(value));
1354 result = set_long(long(value));
1357 result = set_float(float(value));
1360 result = set_double(value);
1365 sprintf(buf, "%f", value);
1366 result = set_string(buf);
1374 if (getAttribute(TRACE_WRITE))
1380 SGPropertyNode::setStringValue (const char * value)
1382 // Shortcut for common case
1383 if (_attr == (READ|WRITE) && _type == STRING)
1384 return set_string(value);
1386 bool result = false;
1388 if (_type == NONE || _type == UNSPECIFIED) {
1395 result = _value.alias->setStringValue(value);
1398 result = set_bool((compare_strings(value, "true")
1399 || atoi(value)) ? true : false);
1402 result = set_int(atoi(value));
1405 result = set_long(strtol(value, 0, 0));
1408 result = set_float(atof(value));
1411 result = set_double(strtod(value, 0));
1415 result = set_string(value);
1422 if (getAttribute(TRACE_WRITE))
1428 SGPropertyNode::setUnspecifiedValue (const char * value)
1430 bool result = false;
1432 if (_type == NONE) {
1434 _type = UNSPECIFIED;
1439 result = _value.alias->setUnspecifiedValue(value);
1442 result = set_bool((compare_strings(value, "true")
1443 || atoi(value)) ? true : false);
1446 result = set_int(atoi(value));
1449 result = set_long(strtol(value, 0, 0));
1452 result = set_float(atof(value));
1455 result = set_double(strtod(value, 0));
1459 result = set_string(value);
1466 if (getAttribute(TRACE_WRITE))
1472 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1474 if (_type == ALIAS || _tied)
1477 useDefault = useDefault && hasValue();
1478 bool old_val = false;
1480 old_val = getBoolValue();
1485 _value.bool_val = rawValue.clone();
1488 setBoolValue(old_val);
1494 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1496 if (_type == ALIAS || _tied)
1499 useDefault = useDefault && hasValue();
1502 old_val = getIntValue();
1507 _value.int_val = rawValue.clone();
1510 setIntValue(old_val);
1516 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1518 if (_type == ALIAS || _tied)
1521 useDefault = useDefault && hasValue();
1524 old_val = getLongValue();
1529 _value.long_val = rawValue.clone();
1532 setLongValue(old_val);
1538 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1540 if (_type == ALIAS || _tied)
1543 useDefault = useDefault && hasValue();
1544 float old_val = 0.0;
1546 old_val = getFloatValue();
1551 _value.float_val = rawValue.clone();
1554 setFloatValue(old_val);
1560 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1562 if (_type == ALIAS || _tied)
1565 useDefault = useDefault && hasValue();
1566 double old_val = 0.0;
1568 old_val = getDoubleValue();
1573 _value.double_val = rawValue.clone();
1576 setDoubleValue(old_val);
1583 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1585 if (_type == ALIAS || _tied)
1588 useDefault = useDefault && hasValue();
1591 old_val = getStringValue();
1596 _value.string_val = rawValue.clone();
1599 setStringValue(old_val.c_str());
1605 SGPropertyNode::untie ()
1612 bool val = getBoolValue();
1615 _local_val.bool_val = val;
1619 int val = getIntValue();
1622 _local_val.int_val = val;
1626 long val = getLongValue();
1629 _local_val.long_val = val;
1633 float val = getFloatValue();
1636 _local_val.float_val = val;
1640 double val = getDoubleValue();
1643 _local_val.double_val = val;
1648 string val = getStringValue();
1651 _local_val.string_val = copy_string(val.c_str());
1664 SGPropertyNode::getRootNode ()
1669 return _parent->getRootNode();
1672 const SGPropertyNode *
1673 SGPropertyNode::getRootNode () const
1678 return _parent->getRootNode();
1682 SGPropertyNode::getNode (const char * relative_path, bool create)
1684 if (_path_cache == 0)
1685 _path_cache = new hash_table;
1687 SGPropertyNode * result = _path_cache->get(relative_path);
1689 vector<PathComponent> components;
1690 parse_path(relative_path, components);
1691 result = find_node(this, components, 0, create);
1693 _path_cache->put(relative_path, result);
1700 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1702 vector<PathComponent> components;
1703 parse_path(relative_path, components);
1704 if (components.size() > 0)
1705 components[components.size()-1].index = index;
1706 return find_node(this, components, 0, create);
1709 const SGPropertyNode *
1710 SGPropertyNode::getNode (const char * relative_path) const
1712 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1715 const SGPropertyNode *
1716 SGPropertyNode::getNode (const char * relative_path, int index) const
1718 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1722 ////////////////////////////////////////////////////////////////////////
1723 // Convenience methods using relative paths.
1724 ////////////////////////////////////////////////////////////////////////
1728 * Test whether another node has a value attached.
1731 SGPropertyNode::hasValue (const char * relative_path) const
1733 const SGPropertyNode * node = getNode(relative_path);
1734 return (node == 0 ? false : node->hasValue());
1739 * Get the value type for another node.
1741 SGPropertyNode::Type
1742 SGPropertyNode::getType (const char * relative_path) const
1744 const SGPropertyNode * node = getNode(relative_path);
1745 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1750 * Get a bool value for another node.
1753 SGPropertyNode::getBoolValue (const char * relative_path,
1754 bool defaultValue) const
1756 const SGPropertyNode * node = getNode(relative_path);
1757 return (node == 0 ? defaultValue : node->getBoolValue());
1762 * Get an int value for another node.
1765 SGPropertyNode::getIntValue (const char * relative_path,
1766 int defaultValue) const
1768 const SGPropertyNode * node = getNode(relative_path);
1769 return (node == 0 ? defaultValue : node->getIntValue());
1774 * Get a long value for another node.
1777 SGPropertyNode::getLongValue (const char * relative_path,
1778 long defaultValue) const
1780 const SGPropertyNode * node = getNode(relative_path);
1781 return (node == 0 ? defaultValue : node->getLongValue());
1786 * Get a float value for another node.
1789 SGPropertyNode::getFloatValue (const char * relative_path,
1790 float defaultValue) const
1792 const SGPropertyNode * node = getNode(relative_path);
1793 return (node == 0 ? defaultValue : node->getFloatValue());
1798 * Get a double value for another node.
1801 SGPropertyNode::getDoubleValue (const char * relative_path,
1802 double defaultValue) const
1804 const SGPropertyNode * node = getNode(relative_path);
1805 return (node == 0 ? defaultValue : node->getDoubleValue());
1810 * Get a string value for another node.
1813 SGPropertyNode::getStringValue (const char * relative_path,
1814 const char * defaultValue) const
1816 const SGPropertyNode * node = getNode(relative_path);
1817 return (node == 0 ? defaultValue : node->getStringValue());
1822 * Set a bool value for another node.
1825 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1827 return getNode(relative_path, true)->setBoolValue(value);
1832 * Set an int value for another node.
1835 SGPropertyNode::setIntValue (const char * relative_path, int value)
1837 return getNode(relative_path, true)->setIntValue(value);
1842 * Set a long value for another node.
1845 SGPropertyNode::setLongValue (const char * relative_path, long value)
1847 return getNode(relative_path, true)->setLongValue(value);
1852 * Set a float value for another node.
1855 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1857 return getNode(relative_path, true)->setFloatValue(value);
1862 * Set a double value for another node.
1865 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1867 return getNode(relative_path, true)->setDoubleValue(value);
1872 * Set a string value for another node.
1875 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1877 return getNode(relative_path, true)->setStringValue(value);
1882 * Set an unknown value for another node.
1885 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1888 return getNode(relative_path, true)->setUnspecifiedValue(value);
1893 * Test whether another node is tied.
1896 SGPropertyNode::isTied (const char * relative_path) const
1898 const SGPropertyNode * node = getNode(relative_path);
1899 return (node == 0 ? false : node->isTied());
1904 * Tie a node reached by a relative path, creating it if necessary.
1907 SGPropertyNode::tie (const char * relative_path,
1908 const SGRawValue<bool> &rawValue,
1911 return getNode(relative_path, true)->tie(rawValue, useDefault);
1916 * Tie a node reached by a relative path, creating it if necessary.
1919 SGPropertyNode::tie (const char * relative_path,
1920 const SGRawValue<int> &rawValue,
1923 return getNode(relative_path, true)->tie(rawValue, useDefault);
1928 * Tie a node reached by a relative path, creating it if necessary.
1931 SGPropertyNode::tie (const char * relative_path,
1932 const SGRawValue<long> &rawValue,
1935 return getNode(relative_path, true)->tie(rawValue, useDefault);
1940 * Tie a node reached by a relative path, creating it if necessary.
1943 SGPropertyNode::tie (const char * relative_path,
1944 const SGRawValue<float> &rawValue,
1947 return getNode(relative_path, true)->tie(rawValue, useDefault);
1952 * Tie a node reached by a relative path, creating it if necessary.
1955 SGPropertyNode::tie (const char * relative_path,
1956 const SGRawValue<double> &rawValue,
1959 return getNode(relative_path, true)->tie(rawValue, useDefault);
1964 * Tie a node reached by a relative path, creating it if necessary.
1967 SGPropertyNode::tie (const char * relative_path,
1968 const SGRawValue<const char *> &rawValue,
1971 return getNode(relative_path, true)->tie(rawValue, useDefault);
1976 * Attempt to untie another node reached by a relative path.
1979 SGPropertyNode::untie (const char * relative_path)
1981 SGPropertyNode * node = getNode(relative_path);
1982 return (node == 0 ? false : node->untie());
1986 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
1988 if (_listeners == 0)
1989 _listeners = new vector<SGPropertyChangeListener *>;
1990 _listeners->push_back(listener);
1991 listener->register_property(this);
1995 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
1997 vector<SGPropertyChangeListener *>::iterator it =
1998 find(_listeners->begin(), _listeners->end(), listener);
1999 if (it != _listeners->end()) {
2000 _listeners->erase(it);
2001 listener->unregister_property(this);
2002 if (_listeners->empty()) {
2003 vector<SGPropertyChangeListener *> * tmp = _listeners;
2011 SGPropertyNode::fireValueChanged ()
2013 fireValueChanged(this);
2017 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2019 fireChildAdded(this, child);
2023 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2025 fireChildRemoved(this, child);
2029 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2031 if (_listeners != 0) {
2032 for (int i = 0; i < _listeners->size(); i++) {
2033 (*_listeners)[i]->valueChanged(node);
2037 _parent->fireValueChanged(node);
2041 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2042 SGPropertyNode * child)
2044 if (_listeners != 0) {
2045 for (int i = 0; i < _listeners->size(); i++) {
2046 (*_listeners)[i]->childAdded(parent, child);
2050 _parent->fireChildAdded(parent, child);
2054 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2055 SGPropertyNode * child)
2057 if (_listeners != 0) {
2058 for (int i = 0; i < _listeners->size(); i++) {
2059 (*_listeners)[i]->childRemoved(parent, child);
2063 _parent->fireChildRemoved(parent, child);
2068 ////////////////////////////////////////////////////////////////////////
2069 // Simplified hash table for caching paths.
2070 ////////////////////////////////////////////////////////////////////////
2072 #define HASH_TABLE_SIZE 199
2074 SGPropertyNode::hash_table::entry::entry ()
2080 SGPropertyNode::hash_table::entry::~entry ()
2082 // Don't delete the value; we don't own
2088 SGPropertyNode::hash_table::entry::set_key (const char * key)
2090 _key = copy_string(key);
2094 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2099 SGPropertyNode::hash_table::bucket::bucket ()
2105 SGPropertyNode::hash_table::bucket::~bucket ()
2107 for (int i = 0; i < _length; i++)
2111 SGPropertyNode::hash_table::entry *
2112 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2115 for (i = 0; i < _length; i++) {
2116 if (!strcmp(_entries[i]->get_key(), key))
2120 entry ** new_entries = new entry*[_length+1];
2121 for (i = 0; i < _length; i++) {
2122 new_entries[i] = _entries[i];
2125 _entries = new_entries;
2126 _entries[_length] = new entry;
2127 _entries[_length]->set_key(key);
2129 return _entries[_length - 1];
2136 SGPropertyNode::hash_table::hash_table ()
2142 SGPropertyNode::hash_table::~hash_table ()
2144 for (unsigned int i = 0; i < _data_length; i++)
2149 SGPropertyNode::hash_table::get (const char * key)
2151 if (_data_length == 0)
2153 unsigned int index = hashcode(key) % _data_length;
2154 if (_data[index] == 0)
2156 entry * e = _data[index]->get_entry(key);
2160 return e->get_value();
2164 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2166 if (_data_length == 0) {
2167 _data = new bucket*[HASH_TABLE_SIZE];
2168 _data_length = HASH_TABLE_SIZE;
2169 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2172 unsigned int index = hashcode(key) % _data_length;
2173 if (_data[index] == 0) {
2174 _data[index] = new bucket;
2176 entry * e = _data[index]->get_entry(key, true);
2177 e->set_value(value);
2181 SGPropertyNode::hash_table::hashcode (const char * key)
2183 unsigned int hash = 0;
2185 hash = 31 * hash + *key;
2194 * Default constructor
2196 SGPropertyNode_ptr::SGPropertyNode_ptr()
2204 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2208 _ptr->incrementRef();
2212 * Constructor from a pointer to a node
2214 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2218 _ptr->incrementRef();
2224 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2226 if (_ptr && _ptr->decrementRef() == 0)
2231 * Assignement operator
2233 SGPropertyNode_ptr &
2234 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2236 if (_ptr && _ptr->decrementRef() == 0)
2240 _ptr->incrementRef();
2246 * Pointer access operator
2249 SGPropertyNode_ptr::operator->()
2255 * Pointer access operator (const)
2257 const SGPropertyNode *
2258 SGPropertyNode_ptr::operator->() const
2264 * Conversion to SGPropertyNode * operator
2266 SGPropertyNode_ptr::operator SGPropertyNode *()
2272 * Conversion to const SGPropertyNode * operator
2274 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2283 SGPropertyNode_ptr::valid() const
2290 ////////////////////////////////////////////////////////////////////////
2291 // Implementation of SGPropertyChangeListener.
2292 ////////////////////////////////////////////////////////////////////////
2294 SGPropertyChangeListener::~SGPropertyChangeListener ()
2296 // This will come back and remove
2297 // the current item each time. Is
2299 vector<SGPropertyNode *>::iterator it;
2300 for (it = _properties.begin(); it != _properties.end(); it++)
2301 (*it)->removeChangeListener(this);
2305 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2311 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2312 SGPropertyNode * child)
2318 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2319 SGPropertyNode * child)
2325 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2327 _properties.push_back(node);
2331 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2333 vector<SGPropertyNode *>::iterator it =
2334 find(_properties.begin(), _properties.end(), node);
2335 if (it != _properties.end())
2336 _properties.erase(it);