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);
934 strncpy(_buffer, path.c_str(), MAX_STRING_LEN);
939 SGPropertyNode::getType () const
942 return _value.alias->getType();
949 SGPropertyNode::getBoolValue () const
951 // Shortcut for common case
952 if (_attr == (READ|WRITE) && _type == BOOL)
955 if (getAttribute(TRACE_READ))
957 if (!getAttribute(READ))
958 return SGRawValue<bool>::DefaultValue;
961 return _value.alias->getBoolValue();
965 return get_int() == 0 ? false : true;
967 return get_long() == 0L ? false : true;
969 return get_float() == 0.0 ? false : true;
971 return get_double() == 0.0L ? false : true;
974 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
977 return SGRawValue<bool>::DefaultValue;
982 SGPropertyNode::getIntValue () const
984 // Shortcut for common case
985 if (_attr == (READ|WRITE) && _type == INT)
988 if (getAttribute(TRACE_READ))
990 if (!getAttribute(READ))
991 return SGRawValue<int>::DefaultValue;
994 return _value.alias->getIntValue();
996 return int(get_bool());
1000 return int(get_long());
1002 return int(get_float());
1004 return int(get_double());
1007 return atoi(get_string());
1010 return SGRawValue<int>::DefaultValue;
1015 SGPropertyNode::getLongValue () const
1017 // Shortcut for common case
1018 if (_attr == (READ|WRITE) && _type == LONG)
1021 if (getAttribute(TRACE_READ))
1023 if (!getAttribute(READ))
1024 return SGRawValue<long>::DefaultValue;
1027 return _value.alias->getLongValue();
1029 return long(get_bool());
1031 return long(get_int());
1035 return long(get_float());
1037 return long(get_double());
1040 return strtol(get_string(), 0, 0);
1043 return SGRawValue<long>::DefaultValue;
1048 SGPropertyNode::getFloatValue () const
1050 // Shortcut for common case
1051 if (_attr == (READ|WRITE) && _type == FLOAT)
1054 if (getAttribute(TRACE_READ))
1056 if (!getAttribute(READ))
1057 return SGRawValue<float>::DefaultValue;
1060 return _value.alias->getFloatValue();
1062 return float(get_bool());
1064 return float(get_int());
1066 return float(get_long());
1070 return float(get_double());
1073 return atof(get_string());
1076 return SGRawValue<float>::DefaultValue;
1081 SGPropertyNode::getDoubleValue () const
1083 // Shortcut for common case
1084 if (_attr == (READ|WRITE) && _type == DOUBLE)
1085 return get_double();
1087 if (getAttribute(TRACE_READ))
1089 if (!getAttribute(READ))
1090 return SGRawValue<double>::DefaultValue;
1094 return _value.alias->getDoubleValue();
1096 return double(get_bool());
1098 return double(get_int());
1100 return double(get_long());
1102 return double(get_float());
1104 return get_double();
1107 return strtod(get_string(), 0);
1110 return SGRawValue<double>::DefaultValue;
1115 SGPropertyNode::getStringValue () const
1117 // Shortcut for common case
1118 if (_attr == (READ|WRITE) && _type == STRING)
1119 return get_string();
1121 if (getAttribute(TRACE_READ))
1123 if (!getAttribute(READ))
1124 return SGRawValue<const char *>::DefaultValue;
1125 return make_string();
1129 SGPropertyNode::setBoolValue (bool value)
1131 // Shortcut for common case
1132 if (_attr == (READ|WRITE) && _type == BOOL)
1133 return set_bool(value);
1135 bool result = false;
1137 if (_type == NONE || _type == UNSPECIFIED) {
1145 result = _value.alias->setBoolValue(value);
1148 result = set_bool(value);
1151 result = set_int(int(value));
1154 result = set_long(long(value));
1157 result = set_float(float(value));
1160 result = set_double(double(value));
1164 result = set_string(value ? "true" : "false");
1171 if (getAttribute(TRACE_WRITE))
1177 SGPropertyNode::setIntValue (int value)
1179 // Shortcut for common case
1180 if (_attr == (READ|WRITE) && _type == INT)
1181 return set_int(value);
1183 bool result = false;
1185 if (_type == NONE || _type == UNSPECIFIED) {
1188 _local_val.int_val = 0;
1193 result = _value.alias->setIntValue(value);
1196 result = set_bool(value == 0 ? false : true);
1199 result = set_int(value);
1202 result = set_long(long(value));
1205 result = set_float(float(value));
1208 result = set_double(double(value));
1213 sprintf(buf, "%d", value);
1214 result = set_string(buf);
1222 if (getAttribute(TRACE_WRITE))
1228 SGPropertyNode::setLongValue (long value)
1230 // Shortcut for common case
1231 if (_attr == (READ|WRITE) && _type == LONG)
1232 return set_long(value);
1234 bool result = false;
1236 if (_type == NONE || _type == UNSPECIFIED) {
1239 _local_val.long_val = 0L;
1244 result = _value.alias->setLongValue(value);
1247 result = set_bool(value == 0L ? false : true);
1250 result = set_int(int(value));
1253 result = set_long(value);
1256 result = set_float(float(value));
1259 result = set_double(double(value));
1264 sprintf(buf, "%ld", value);
1265 result = set_string(buf);
1273 if (getAttribute(TRACE_WRITE))
1279 SGPropertyNode::setFloatValue (float value)
1281 // Shortcut for common case
1282 if (_attr == (READ|WRITE) && _type == FLOAT)
1283 return set_float(value);
1285 bool result = false;
1287 if (_type == NONE || _type == UNSPECIFIED) {
1290 _local_val.float_val = 0;
1295 result = _value.alias->setFloatValue(value);
1298 result = set_bool(value == 0.0 ? false : true);
1301 result = set_int(int(value));
1304 result = set_long(long(value));
1307 result = set_float(value);
1310 result = set_double(double(value));
1315 sprintf(buf, "%f", value);
1316 result = set_string(buf);
1324 if (getAttribute(TRACE_WRITE))
1330 SGPropertyNode::setDoubleValue (double value)
1332 // Shortcut for common case
1333 if (_attr == (READ|WRITE) && _type == DOUBLE)
1334 return set_double(value);
1336 bool result = false;
1338 if (_type == NONE || _type == UNSPECIFIED) {
1340 _local_val.double_val = value;
1346 result = _value.alias->setDoubleValue(value);
1349 result = set_bool(value == 0.0L ? false : true);
1352 result = set_int(int(value));
1355 result = set_long(long(value));
1358 result = set_float(float(value));
1361 result = set_double(value);
1366 sprintf(buf, "%f", value);
1367 result = set_string(buf);
1375 if (getAttribute(TRACE_WRITE))
1381 SGPropertyNode::setStringValue (const char * value)
1383 // Shortcut for common case
1384 if (_attr == (READ|WRITE) && _type == STRING)
1385 return set_string(value);
1387 bool result = false;
1389 if (_type == NONE || _type == UNSPECIFIED) {
1396 result = _value.alias->setStringValue(value);
1399 result = set_bool((compare_strings(value, "true")
1400 || atoi(value)) ? true : false);
1403 result = set_int(atoi(value));
1406 result = set_long(strtol(value, 0, 0));
1409 result = set_float(atof(value));
1412 result = set_double(strtod(value, 0));
1416 result = set_string(value);
1423 if (getAttribute(TRACE_WRITE))
1429 SGPropertyNode::setUnspecifiedValue (const char * value)
1431 bool result = false;
1433 if (_type == NONE) {
1435 _type = UNSPECIFIED;
1440 result = _value.alias->setUnspecifiedValue(value);
1443 result = set_bool((compare_strings(value, "true")
1444 || atoi(value)) ? true : false);
1447 result = set_int(atoi(value));
1450 result = set_long(strtol(value, 0, 0));
1453 result = set_float(atof(value));
1456 result = set_double(strtod(value, 0));
1460 result = set_string(value);
1467 if (getAttribute(TRACE_WRITE))
1473 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1475 if (_type == ALIAS || _tied)
1478 useDefault = useDefault && hasValue();
1479 bool old_val = false;
1481 old_val = getBoolValue();
1486 _value.bool_val = rawValue.clone();
1489 setBoolValue(old_val);
1495 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1497 if (_type == ALIAS || _tied)
1500 useDefault = useDefault && hasValue();
1503 old_val = getIntValue();
1508 _value.int_val = rawValue.clone();
1511 setIntValue(old_val);
1517 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1519 if (_type == ALIAS || _tied)
1522 useDefault = useDefault && hasValue();
1525 old_val = getLongValue();
1530 _value.long_val = rawValue.clone();
1533 setLongValue(old_val);
1539 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1541 if (_type == ALIAS || _tied)
1544 useDefault = useDefault && hasValue();
1545 float old_val = 0.0;
1547 old_val = getFloatValue();
1552 _value.float_val = rawValue.clone();
1555 setFloatValue(old_val);
1561 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1563 if (_type == ALIAS || _tied)
1566 useDefault = useDefault && hasValue();
1567 double old_val = 0.0;
1569 old_val = getDoubleValue();
1574 _value.double_val = rawValue.clone();
1577 setDoubleValue(old_val);
1584 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1586 if (_type == ALIAS || _tied)
1589 useDefault = useDefault && hasValue();
1592 old_val = getStringValue();
1597 _value.string_val = rawValue.clone();
1600 setStringValue(old_val.c_str());
1606 SGPropertyNode::untie ()
1613 bool val = getBoolValue();
1616 _local_val.bool_val = val;
1620 int val = getIntValue();
1623 _local_val.int_val = val;
1627 long val = getLongValue();
1630 _local_val.long_val = val;
1634 float val = getFloatValue();
1637 _local_val.float_val = val;
1641 double val = getDoubleValue();
1644 _local_val.double_val = val;
1649 string val = getStringValue();
1652 _local_val.string_val = copy_string(val.c_str());
1665 SGPropertyNode::getRootNode ()
1670 return _parent->getRootNode();
1673 const SGPropertyNode *
1674 SGPropertyNode::getRootNode () const
1679 return _parent->getRootNode();
1683 SGPropertyNode::getNode (const char * relative_path, bool create)
1685 if (_path_cache == 0)
1686 _path_cache = new hash_table;
1688 SGPropertyNode * result = _path_cache->get(relative_path);
1690 vector<PathComponent> components;
1691 parse_path(relative_path, components);
1692 result = find_node(this, components, 0, create);
1694 _path_cache->put(relative_path, result);
1701 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1703 vector<PathComponent> components;
1704 parse_path(relative_path, components);
1705 if (components.size() > 0)
1706 components[components.size()-1].index = index;
1707 return find_node(this, components, 0, create);
1710 const SGPropertyNode *
1711 SGPropertyNode::getNode (const char * relative_path) const
1713 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1716 const SGPropertyNode *
1717 SGPropertyNode::getNode (const char * relative_path, int index) const
1719 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1723 ////////////////////////////////////////////////////////////////////////
1724 // Convenience methods using relative paths.
1725 ////////////////////////////////////////////////////////////////////////
1729 * Test whether another node has a value attached.
1732 SGPropertyNode::hasValue (const char * relative_path) const
1734 const SGPropertyNode * node = getNode(relative_path);
1735 return (node == 0 ? false : node->hasValue());
1740 * Get the value type for another node.
1742 SGPropertyNode::Type
1743 SGPropertyNode::getType (const char * relative_path) const
1745 const SGPropertyNode * node = getNode(relative_path);
1746 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1751 * Get a bool value for another node.
1754 SGPropertyNode::getBoolValue (const char * relative_path,
1755 bool defaultValue) const
1757 const SGPropertyNode * node = getNode(relative_path);
1758 return (node == 0 ? defaultValue : node->getBoolValue());
1763 * Get an int value for another node.
1766 SGPropertyNode::getIntValue (const char * relative_path,
1767 int defaultValue) const
1769 const SGPropertyNode * node = getNode(relative_path);
1770 return (node == 0 ? defaultValue : node->getIntValue());
1775 * Get a long value for another node.
1778 SGPropertyNode::getLongValue (const char * relative_path,
1779 long defaultValue) const
1781 const SGPropertyNode * node = getNode(relative_path);
1782 return (node == 0 ? defaultValue : node->getLongValue());
1787 * Get a float value for another node.
1790 SGPropertyNode::getFloatValue (const char * relative_path,
1791 float defaultValue) const
1793 const SGPropertyNode * node = getNode(relative_path);
1794 return (node == 0 ? defaultValue : node->getFloatValue());
1799 * Get a double value for another node.
1802 SGPropertyNode::getDoubleValue (const char * relative_path,
1803 double defaultValue) const
1805 const SGPropertyNode * node = getNode(relative_path);
1806 return (node == 0 ? defaultValue : node->getDoubleValue());
1811 * Get a string value for another node.
1814 SGPropertyNode::getStringValue (const char * relative_path,
1815 const char * defaultValue) const
1817 const SGPropertyNode * node = getNode(relative_path);
1818 return (node == 0 ? defaultValue : node->getStringValue());
1823 * Set a bool value for another node.
1826 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1828 return getNode(relative_path, true)->setBoolValue(value);
1833 * Set an int value for another node.
1836 SGPropertyNode::setIntValue (const char * relative_path, int value)
1838 return getNode(relative_path, true)->setIntValue(value);
1843 * Set a long value for another node.
1846 SGPropertyNode::setLongValue (const char * relative_path, long value)
1848 return getNode(relative_path, true)->setLongValue(value);
1853 * Set a float value for another node.
1856 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1858 return getNode(relative_path, true)->setFloatValue(value);
1863 * Set a double value for another node.
1866 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1868 return getNode(relative_path, true)->setDoubleValue(value);
1873 * Set a string value for another node.
1876 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1878 return getNode(relative_path, true)->setStringValue(value);
1883 * Set an unknown value for another node.
1886 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1889 return getNode(relative_path, true)->setUnspecifiedValue(value);
1894 * Test whether another node is tied.
1897 SGPropertyNode::isTied (const char * relative_path) const
1899 const SGPropertyNode * node = getNode(relative_path);
1900 return (node == 0 ? false : node->isTied());
1905 * Tie a node reached by a relative path, creating it if necessary.
1908 SGPropertyNode::tie (const char * relative_path,
1909 const SGRawValue<bool> &rawValue,
1912 return getNode(relative_path, true)->tie(rawValue, useDefault);
1917 * Tie a node reached by a relative path, creating it if necessary.
1920 SGPropertyNode::tie (const char * relative_path,
1921 const SGRawValue<int> &rawValue,
1924 return getNode(relative_path, true)->tie(rawValue, useDefault);
1929 * Tie a node reached by a relative path, creating it if necessary.
1932 SGPropertyNode::tie (const char * relative_path,
1933 const SGRawValue<long> &rawValue,
1936 return getNode(relative_path, true)->tie(rawValue, useDefault);
1941 * Tie a node reached by a relative path, creating it if necessary.
1944 SGPropertyNode::tie (const char * relative_path,
1945 const SGRawValue<float> &rawValue,
1948 return getNode(relative_path, true)->tie(rawValue, useDefault);
1953 * Tie a node reached by a relative path, creating it if necessary.
1956 SGPropertyNode::tie (const char * relative_path,
1957 const SGRawValue<double> &rawValue,
1960 return getNode(relative_path, true)->tie(rawValue, useDefault);
1965 * Tie a node reached by a relative path, creating it if necessary.
1968 SGPropertyNode::tie (const char * relative_path,
1969 const SGRawValue<const char *> &rawValue,
1972 return getNode(relative_path, true)->tie(rawValue, useDefault);
1977 * Attempt to untie another node reached by a relative path.
1980 SGPropertyNode::untie (const char * relative_path)
1982 SGPropertyNode * node = getNode(relative_path);
1983 return (node == 0 ? false : node->untie());
1987 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
1989 if (_listeners == 0)
1990 _listeners = new vector<SGPropertyChangeListener *>;
1991 _listeners->push_back(listener);
1992 listener->register_property(this);
1996 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
1998 vector<SGPropertyChangeListener *>::iterator it =
1999 find(_listeners->begin(), _listeners->end(), listener);
2000 if (it != _listeners->end()) {
2001 _listeners->erase(it);
2002 listener->unregister_property(this);
2003 if (_listeners->empty()) {
2004 vector<SGPropertyChangeListener *> * tmp = _listeners;
2012 SGPropertyNode::fireValueChanged ()
2014 fireValueChanged(this);
2018 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2020 fireChildAdded(this, child);
2024 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2026 fireChildRemoved(this, child);
2030 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2032 if (_listeners != 0) {
2033 for (int i = 0; i < _listeners->size(); i++) {
2034 (*_listeners)[i]->valueChanged(node);
2038 _parent->fireValueChanged(node);
2042 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2043 SGPropertyNode * child)
2045 if (_listeners != 0) {
2046 for (int i = 0; i < _listeners->size(); i++) {
2047 (*_listeners)[i]->childAdded(parent, child);
2051 _parent->fireChildAdded(parent, child);
2055 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2056 SGPropertyNode * child)
2058 if (_listeners != 0) {
2059 for (int i = 0; i < _listeners->size(); i++) {
2060 (*_listeners)[i]->childRemoved(parent, child);
2064 _parent->fireChildRemoved(parent, child);
2069 ////////////////////////////////////////////////////////////////////////
2070 // Simplified hash table for caching paths.
2071 ////////////////////////////////////////////////////////////////////////
2073 #define HASH_TABLE_SIZE 199
2075 SGPropertyNode::hash_table::entry::entry ()
2081 SGPropertyNode::hash_table::entry::~entry ()
2083 // Don't delete the value; we don't own
2089 SGPropertyNode::hash_table::entry::set_key (const char * key)
2091 _key = copy_string(key);
2095 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2100 SGPropertyNode::hash_table::bucket::bucket ()
2106 SGPropertyNode::hash_table::bucket::~bucket ()
2108 for (int i = 0; i < _length; i++)
2112 SGPropertyNode::hash_table::entry *
2113 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2116 for (i = 0; i < _length; i++) {
2117 if (!strcmp(_entries[i]->get_key(), key))
2121 entry ** new_entries = new entry*[_length+1];
2122 for (i = 0; i < _length; i++) {
2123 new_entries[i] = _entries[i];
2126 _entries = new_entries;
2127 _entries[_length] = new entry;
2128 _entries[_length]->set_key(key);
2130 return _entries[_length - 1];
2137 SGPropertyNode::hash_table::hash_table ()
2143 SGPropertyNode::hash_table::~hash_table ()
2145 for (unsigned int i = 0; i < _data_length; i++)
2150 SGPropertyNode::hash_table::get (const char * key)
2152 if (_data_length == 0)
2154 unsigned int index = hashcode(key) % _data_length;
2155 if (_data[index] == 0)
2157 entry * e = _data[index]->get_entry(key);
2161 return e->get_value();
2165 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2167 if (_data_length == 0) {
2168 _data = new bucket*[HASH_TABLE_SIZE];
2169 _data_length = HASH_TABLE_SIZE;
2170 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2173 unsigned int index = hashcode(key) % _data_length;
2174 if (_data[index] == 0) {
2175 _data[index] = new bucket;
2177 entry * e = _data[index]->get_entry(key, true);
2178 e->set_value(value);
2182 SGPropertyNode::hash_table::hashcode (const char * key)
2184 unsigned int hash = 0;
2186 hash = 31 * hash + *key;
2195 * Default constructor
2197 SGPropertyNode_ptr::SGPropertyNode_ptr()
2205 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2209 _ptr->incrementRef();
2213 * Constructor from a pointer to a node
2215 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2219 _ptr->incrementRef();
2225 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2227 if (_ptr && _ptr->decrementRef() == 0)
2232 * Assignement operator
2234 SGPropertyNode_ptr &
2235 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2237 if (_ptr && _ptr->decrementRef() == 0)
2241 _ptr->incrementRef();
2247 * Pointer access operator
2250 SGPropertyNode_ptr::operator->()
2256 * Pointer access operator (const)
2258 const SGPropertyNode *
2259 SGPropertyNode_ptr::operator->() const
2265 * Conversion to SGPropertyNode * operator
2267 SGPropertyNode_ptr::operator SGPropertyNode *()
2273 * Conversion to const SGPropertyNode * operator
2275 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2284 SGPropertyNode_ptr::valid() const
2291 ////////////////////////////////////////////////////////////////////////
2292 // Implementation of SGPropertyChangeListener.
2293 ////////////////////////////////////////////////////////////////////////
2295 SGPropertyChangeListener::~SGPropertyChangeListener ()
2297 // This will come back and remove
2298 // the current item each time. Is
2300 vector<SGPropertyNode *>::iterator it;
2301 for (it = _properties.begin(); it != _properties.end(); it++)
2302 (*it)->removeChangeListener(this);
2306 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2312 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2313 SGPropertyNode * child)
2319 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2320 SGPropertyNode * child)
2326 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2328 _properties.push_back(node);
2332 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2334 vector<SGPropertyNode *>::iterator it =
2335 find(_properties.begin(), _properties.end(), node);
2336 if (it != _properties.end())
2337 _properties.erase(it);