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].
26 #include <simgear/compiler.h>
27 #include <simgear/debug/logstream.hxx>
34 // MSVC is buggy, and needs something strange here
35 SG_USING_STD(vector<SGPropertyNode_ptr>);
36 SG_USING_STD(vector<SGPropertyChangeListener *>);
37 SG_USING_STD(vector<SGPropertyNode *>);
44 ////////////////////////////////////////////////////////////////////////
46 ////////////////////////////////////////////////////////////////////////
49 * Comparator class for sorting by index.
54 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
55 return (n1->getIndex() < n2->getIndex());
61 ////////////////////////////////////////////////////////////////////////
62 // Convenience macros for value access.
63 ////////////////////////////////////////////////////////////////////////
65 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
66 #define TEST_WRITE if (!getAttribute(WRITE)) return false
70 ////////////////////////////////////////////////////////////////////////
71 // Default values for every type.
72 ////////////////////////////////////////////////////////////////////////
74 const bool SGRawValue<bool>::DefaultValue = false;
75 const int SGRawValue<int>::DefaultValue = 0;
76 const long SGRawValue<long>::DefaultValue = 0L;
77 const float SGRawValue<float>::DefaultValue = 0.0;
78 const double SGRawValue<double>::DefaultValue = 0.0L;
79 const char * const SGRawValue<const char *>::DefaultValue = "";
83 ////////////////////////////////////////////////////////////////////////
84 // Local path normalization code.
85 ////////////////////////////////////////////////////////////////////////
88 * A component in a path.
97 * Parse the name for a path component.
99 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
101 static inline const string
102 parse_name (const string &path, int &i)
105 int max = path.size();
107 if (path[i] == '.') {
109 if (i < max && path[i] == '.') {
115 if (i < max && path[i] != '/')
116 throw string(string("Illegal character after ") + name);
119 else if (isalpha(path[i]) || path[i] == '_') {
123 // The rules inside a name are a little
126 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
127 path[i] == '-' || path[i] == '.') {
129 } else if (path[i] == '[' || path[i] == '/') {
132 throw string("name may contain only ._- and alphanumeric characters");
139 if (name.size() == 0)
140 throw string("name must begin with alpha or '_'");
148 * Parse the optional integer index for a path component.
150 * Index: "[" [0-9]+ "]"
153 parse_index (const string &path, int &i)
162 for (int max = path.size(); i < max; i++) {
163 if (isdigit(path[i])) {
164 index = (index * 10) + (path[i] - '0');
165 } else if (path[i] == ']') {
173 throw string("unterminated index (looking for ']')");
178 * Parse a single path component.
180 * Component: Name Index?
182 static inline PathComponent
183 parse_component (const string &path, int &i)
185 PathComponent component;
186 component.name = parse_name(path, i);
187 if (component.name[0] != '.')
188 component.index = parse_index(path, i);
190 component.index = -1;
196 * Parse a path into its components.
199 parse_path (const string &path, vector<PathComponent> &components)
202 int max = path.size();
204 // Check for initial '/'
205 if (path[pos] == '/') {
209 components.push_back(root);
211 while (pos < max && path[pos] == '/')
216 components.push_back(parse_component(path, pos));
217 while (pos < max && path[pos] == '/')
224 ////////////////////////////////////////////////////////////////////////
225 // Other static utility functions.
226 ////////////////////////////////////////////////////////////////////////
230 copy_string (const char * s)
232 // FIXME: potential buffer overflow.
233 // For some reason, strnlen and
234 // strncpy cause all kinds of crashes.
235 char * copy = new char[strlen(s) + 1];
241 compare_strings (const char * s1, const char * s2)
243 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
247 * Locate a child node by name and index.
250 find_child (const char * name, int index, vector<SGPropertyNode_ptr> nodes)
252 int nNodes = nodes.size();
253 for (int i = 0; i < nNodes; i++) {
254 SGPropertyNode * node = nodes[i];
255 if (compare_strings(node->getName(), name) && node->getIndex() == index)
263 * Locate another node, given a relative path.
265 static SGPropertyNode *
266 find_node (SGPropertyNode * current,
267 const vector<PathComponent> &components,
271 // Run off the end of the list
276 // Success! This is the one we want.
277 else if (position >= (int)components.size()) {
281 // Empty component means root.
282 else if (components[position].name == "") {
283 return find_node(current->getRootNode(), components, position + 1, create);
286 // . means current directory
287 else if (components[position].name == ".") {
288 return find_node(current, components, position + 1, create);
291 // .. means parent directory
292 else if (components[position].name == "..") {
293 SGPropertyNode * parent = current->getParent();
295 throw string("Attempt to move past root with '..'");
297 return find_node(parent, components, position + 1, create);
300 // Otherwise, a child name
302 SGPropertyNode * child =
303 current->getChild(components[position].name.c_str(),
304 components[position].index,
306 return find_node(child, components, position + 1, create);
312 ////////////////////////////////////////////////////////////////////////
313 // Private methods from SGPropertyNode (may be inlined for speed).
314 ////////////////////////////////////////////////////////////////////////
317 SGPropertyNode::get_bool () const
320 return _value.bool_val->getValue();
322 return _local_val.bool_val;
326 SGPropertyNode::get_int () const
329 return _value.int_val->getValue();
331 return _local_val.int_val;
335 SGPropertyNode::get_long () const
338 return _value.long_val->getValue();
340 return _local_val.long_val;
344 SGPropertyNode::get_float () const
347 return _value.float_val->getValue();
349 return _local_val.float_val;
353 SGPropertyNode::get_double () const
356 return _value.double_val->getValue();
358 return _local_val.double_val;
362 SGPropertyNode::get_string () const
365 return _value.string_val->getValue();
367 return _local_val.string_val;
371 SGPropertyNode::set_bool (bool val)
374 if (_value.bool_val->setValue(val)) {
381 _local_val.bool_val = val;
388 SGPropertyNode::set_int (int val)
391 if (_value.int_val->setValue(val)) {
398 _local_val.int_val = val;
405 SGPropertyNode::set_long (long val)
408 if (_value.long_val->setValue(val)) {
415 _local_val.long_val = val;
422 SGPropertyNode::set_float (float val)
425 if (_value.float_val->setValue(val)) {
432 _local_val.float_val = val;
439 SGPropertyNode::set_double (double val)
442 if (_value.double_val->setValue(val)) {
449 _local_val.double_val = val;
456 SGPropertyNode::set_string (const char * val)
459 if (_value.string_val->setValue(val)) {
466 delete [] _local_val.string_val;
467 _local_val.string_val = copy_string(val);
474 SGPropertyNode::clear_value ()
484 delete _value.bool_val;
487 _local_val.bool_val = SGRawValue<bool>::DefaultValue;
491 delete _value.int_val;
494 _local_val.int_val = SGRawValue<int>::DefaultValue;
498 delete _value.long_val;
499 _value.long_val = 0L;
501 _local_val.long_val = SGRawValue<long>::DefaultValue;
505 delete _value.float_val;
506 _value.float_val = 0;
508 _local_val.float_val = SGRawValue<float>::DefaultValue;
512 delete _value.double_val;
513 _value.double_val = 0;
515 _local_val.double_val = SGRawValue<double>::DefaultValue;
520 delete _value.string_val;
521 _value.string_val = 0;
523 delete [] _local_val.string_val;
525 _local_val.string_val = 0;
534 * Get the value as a string.
537 SGPropertyNode::make_string () const
539 if (!getAttribute(READ))
544 return _value.alias->getStringValue();
551 sprintf(_buffer, "%d", get_int());
554 sprintf(_buffer, "%ld", get_long());
557 sprintf(_buffer, "%f", get_float());
560 sprintf(_buffer, "%f", get_double());
572 * Trace a write access for a property.
575 SGPropertyNode::trace_write () const
578 cerr << "TRACE: Write node " << getPath () << ", value\""
579 << make_string() << '"' << endl;
581 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
582 << ", value\"" << make_string() << '"');
587 * Trace a read access for a property.
590 SGPropertyNode::trace_read () const
593 cerr << "TRACE: Write node " << getPath () << ", value \""
594 << make_string() << '"' << endl;
596 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
597 << ", value \"" << make_string() << '"');
602 * Increment reference counter
605 SGPropertyNode::incrementRef()
611 * Decrement reference counter
614 SGPropertyNode::decrementRef()
621 ////////////////////////////////////////////////////////////////////////
622 // Public methods from SGPropertyNode.
623 ////////////////////////////////////////////////////////////////////////
626 * Last used attribute
627 * Update as needed when enum Attribute is changed
629 const int SGPropertyNode::LAST_USED_ATTRIBUTE = TRACE_WRITE;
632 * Default constructor: always creates a root node.
634 SGPropertyNode::SGPropertyNode ()
635 : _name(copy_string("")),
646 _local_val.string_val = 0;
653 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
654 : _index(node._index),
655 _parent(0), // don't copy the parent
662 _listeners(0) // CHECK!!
664 _name = copy_string(node._name);
665 _local_val.string_val = 0;
670 _value.alias = node._value.alias;
676 _value.bool_val = node._value.bool_val->clone();
679 set_bool(node.get_bool());
685 _value.int_val = node._value.int_val->clone();
688 set_int(node.get_int());
694 _value.long_val = node._value.long_val->clone();
697 set_long(node.get_long());
703 _value.float_val = node._value.float_val->clone();
706 set_float(node.get_float());
712 _value.double_val = node._value.double_val->clone();
715 set_double(node.get_double());
722 _value.string_val = node._value.string_val->clone();
725 set_string(node.get_string());
733 * Convenience constructor.
735 SGPropertyNode::SGPropertyNode (const char * name,
737 SGPropertyNode * parent)
748 _name = copy_string(name);
749 _local_val.string_val = 0;
756 SGPropertyNode::~SGPropertyNode ()
767 * Alias to another node.
770 SGPropertyNode::alias (SGPropertyNode * target)
772 if (target == 0 || _type == ALIAS || _tied)
775 _value.alias = target;
782 * Alias to another node by path.
785 SGPropertyNode::alias (const char * path)
787 return alias(getNode(path, true));
795 SGPropertyNode::unalias ()
806 * Get the target of an alias.
809 SGPropertyNode::getAliasTarget ()
811 return (_type == ALIAS ? _value.alias : 0);
815 const SGPropertyNode *
816 SGPropertyNode::getAliasTarget () const
818 return (_type == ALIAS ? _value.alias : 0);
823 * Get a non-const child by index.
826 SGPropertyNode::getChild (int position)
828 if (position >= 0 && position < nChildren())
829 return _children[position];
836 * Get a const child by index.
838 const SGPropertyNode *
839 SGPropertyNode::getChild (int position) const
841 if (position >= 0 && position < nChildren())
842 return _children[position];
849 * Get a non-const child by name and index, creating if necessary.
852 SGPropertyNode::getChild (const char * name, int index, bool create)
854 int pos = find_child(name, index, _children);
856 return _children[pos];
858 SGPropertyNode_ptr node;
859 pos = find_child(name, index, _removedChildren);
861 vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
863 node = _removedChildren[pos];
864 _removedChildren.erase(it);
865 node->setAttribute(REMOVED, false);
867 node = new SGPropertyNode(name, index, this);
869 _children.push_back(node);
870 fireChildAdded(node);
879 * Get a const child by name and index.
881 const SGPropertyNode *
882 SGPropertyNode::getChild (const char * name, int index) const
884 int pos = find_child(name, index, _children);
886 return _children[pos];
893 * Get all children with the same name (but different indices).
895 vector<SGPropertyNode_ptr>
896 SGPropertyNode::getChildren (const char * name) const
898 vector<SGPropertyNode_ptr> children;
899 int max = _children.size();
901 for (int i = 0; i < max; i++)
902 if (compare_strings(_children[i]->getName(), name))
903 children.push_back(_children[i]);
905 sort(children.begin(), children.end(), CompareIndices());
911 * Remove a child node
914 SGPropertyNode::removeChild (const char * name, int index, bool keep)
916 SGPropertyNode_ptr ret;
917 int pos = find_child(name, index, _children);
919 vector<SGPropertyNode_ptr>::iterator it = _children.begin();
921 SGPropertyNode_ptr node = _children[pos];
924 _removedChildren.push_back(node);
926 node->setAttribute(REMOVED, true);
928 fireChildRemoved(node);
935 SGPropertyNode::getPath (bool simplify) const
937 // Calculate the complete path only once.
943 path = _parent->getPath(simplify);
946 if (_index != 0 || !simplify) {
948 sprintf(buffer, "[%d]", _index);
952 _path = copy_string(path.c_str());
959 SGPropertyNode::getType () const
962 return _value.alias->getType();
969 SGPropertyNode::getBoolValue () const
971 // Shortcut for common case
972 if (_attr == (READ|WRITE) && _type == BOOL)
975 if (getAttribute(TRACE_READ))
977 if (!getAttribute(READ))
978 return SGRawValue<bool>::DefaultValue;
981 return _value.alias->getBoolValue();
985 return get_int() == 0 ? false : true;
987 return get_long() == 0L ? false : true;
989 return get_float() == 0.0 ? false : true;
991 return get_double() == 0.0L ? false : true;
994 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
997 return SGRawValue<bool>::DefaultValue;
1002 SGPropertyNode::getIntValue () const
1004 // Shortcut for common case
1005 if (_attr == (READ|WRITE) && _type == INT)
1008 if (getAttribute(TRACE_READ))
1010 if (!getAttribute(READ))
1011 return SGRawValue<int>::DefaultValue;
1014 return _value.alias->getIntValue();
1016 return int(get_bool());
1020 return int(get_long());
1022 return int(get_float());
1024 return int(get_double());
1027 return atoi(get_string());
1030 return SGRawValue<int>::DefaultValue;
1035 SGPropertyNode::getLongValue () const
1037 // Shortcut for common case
1038 if (_attr == (READ|WRITE) && _type == LONG)
1041 if (getAttribute(TRACE_READ))
1043 if (!getAttribute(READ))
1044 return SGRawValue<long>::DefaultValue;
1047 return _value.alias->getLongValue();
1049 return long(get_bool());
1051 return long(get_int());
1055 return long(get_float());
1057 return long(get_double());
1060 return strtol(get_string(), 0, 0);
1063 return SGRawValue<long>::DefaultValue;
1068 SGPropertyNode::getFloatValue () const
1070 // Shortcut for common case
1071 if (_attr == (READ|WRITE) && _type == FLOAT)
1074 if (getAttribute(TRACE_READ))
1076 if (!getAttribute(READ))
1077 return SGRawValue<float>::DefaultValue;
1080 return _value.alias->getFloatValue();
1082 return float(get_bool());
1084 return float(get_int());
1086 return float(get_long());
1090 return float(get_double());
1093 return atof(get_string());
1096 return SGRawValue<float>::DefaultValue;
1101 SGPropertyNode::getDoubleValue () const
1103 // Shortcut for common case
1104 if (_attr == (READ|WRITE) && _type == DOUBLE)
1105 return get_double();
1107 if (getAttribute(TRACE_READ))
1109 if (!getAttribute(READ))
1110 return SGRawValue<double>::DefaultValue;
1114 return _value.alias->getDoubleValue();
1116 return double(get_bool());
1118 return double(get_int());
1120 return double(get_long());
1122 return double(get_float());
1124 return get_double();
1127 return strtod(get_string(), 0);
1130 return SGRawValue<double>::DefaultValue;
1135 SGPropertyNode::getStringValue () const
1137 // Shortcut for common case
1138 if (_attr == (READ|WRITE) && _type == STRING)
1139 return get_string();
1141 if (getAttribute(TRACE_READ))
1143 if (!getAttribute(READ))
1144 return SGRawValue<const char *>::DefaultValue;
1145 return make_string();
1149 SGPropertyNode::setBoolValue (bool value)
1151 // Shortcut for common case
1152 if (_attr == (READ|WRITE) && _type == BOOL)
1153 return set_bool(value);
1155 bool result = false;
1157 if (_type == NONE || _type == UNSPECIFIED) {
1165 result = _value.alias->setBoolValue(value);
1168 result = set_bool(value);
1171 result = set_int(int(value));
1174 result = set_long(long(value));
1177 result = set_float(float(value));
1180 result = set_double(double(value));
1184 result = set_string(value ? "true" : "false");
1191 if (getAttribute(TRACE_WRITE))
1197 SGPropertyNode::setIntValue (int value)
1199 // Shortcut for common case
1200 if (_attr == (READ|WRITE) && _type == INT)
1201 return set_int(value);
1203 bool result = false;
1205 if (_type == NONE || _type == UNSPECIFIED) {
1208 _local_val.int_val = 0;
1213 result = _value.alias->setIntValue(value);
1216 result = set_bool(value == 0 ? false : true);
1219 result = set_int(value);
1222 result = set_long(long(value));
1225 result = set_float(float(value));
1228 result = set_double(double(value));
1233 sprintf(buf, "%d", value);
1234 result = set_string(buf);
1242 if (getAttribute(TRACE_WRITE))
1248 SGPropertyNode::setLongValue (long value)
1250 // Shortcut for common case
1251 if (_attr == (READ|WRITE) && _type == LONG)
1252 return set_long(value);
1254 bool result = false;
1256 if (_type == NONE || _type == UNSPECIFIED) {
1259 _local_val.long_val = 0L;
1264 result = _value.alias->setLongValue(value);
1267 result = set_bool(value == 0L ? false : true);
1270 result = set_int(int(value));
1273 result = set_long(value);
1276 result = set_float(float(value));
1279 result = set_double(double(value));
1284 sprintf(buf, "%ld", value);
1285 result = set_string(buf);
1293 if (getAttribute(TRACE_WRITE))
1299 SGPropertyNode::setFloatValue (float value)
1301 // Shortcut for common case
1302 if (_attr == (READ|WRITE) && _type == FLOAT)
1303 return set_float(value);
1305 bool result = false;
1307 if (_type == NONE || _type == UNSPECIFIED) {
1310 _local_val.float_val = 0;
1315 result = _value.alias->setFloatValue(value);
1318 result = set_bool(value == 0.0 ? false : true);
1321 result = set_int(int(value));
1324 result = set_long(long(value));
1327 result = set_float(value);
1330 result = set_double(double(value));
1335 sprintf(buf, "%f", value);
1336 result = set_string(buf);
1344 if (getAttribute(TRACE_WRITE))
1350 SGPropertyNode::setDoubleValue (double value)
1352 // Shortcut for common case
1353 if (_attr == (READ|WRITE) && _type == DOUBLE)
1354 return set_double(value);
1356 bool result = false;
1358 if (_type == NONE || _type == UNSPECIFIED) {
1360 _local_val.double_val = value;
1366 result = _value.alias->setDoubleValue(value);
1369 result = set_bool(value == 0.0L ? false : true);
1372 result = set_int(int(value));
1375 result = set_long(long(value));
1378 result = set_float(float(value));
1381 result = set_double(value);
1386 sprintf(buf, "%f", value);
1387 result = set_string(buf);
1395 if (getAttribute(TRACE_WRITE))
1401 SGPropertyNode::setStringValue (const char * value)
1403 // Shortcut for common case
1404 if (_attr == (READ|WRITE) && _type == STRING)
1405 return set_string(value);
1407 bool result = false;
1409 if (_type == NONE || _type == UNSPECIFIED) {
1416 result = _value.alias->setStringValue(value);
1419 result = set_bool((compare_strings(value, "true")
1420 || atoi(value)) ? true : false);
1423 result = set_int(atoi(value));
1426 result = set_long(strtol(value, 0, 0));
1429 result = set_float(atof(value));
1432 result = set_double(strtod(value, 0));
1436 result = set_string(value);
1443 if (getAttribute(TRACE_WRITE))
1449 SGPropertyNode::setUnspecifiedValue (const char * value)
1451 bool result = false;
1453 if (_type == NONE) {
1455 _type = UNSPECIFIED;
1460 result = _value.alias->setUnspecifiedValue(value);
1463 result = set_bool((compare_strings(value, "true")
1464 || atoi(value)) ? true : false);
1467 result = set_int(atoi(value));
1470 result = set_long(strtol(value, 0, 0));
1473 result = set_float(atof(value));
1476 result = set_double(strtod(value, 0));
1480 result = set_string(value);
1487 if (getAttribute(TRACE_WRITE))
1493 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1495 if (_type == ALIAS || _tied)
1498 useDefault = useDefault && hasValue();
1499 bool old_val = false;
1501 old_val = getBoolValue();
1506 _value.bool_val = rawValue.clone();
1509 setBoolValue(old_val);
1515 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1517 if (_type == ALIAS || _tied)
1520 useDefault = useDefault && hasValue();
1523 old_val = getIntValue();
1528 _value.int_val = rawValue.clone();
1531 setIntValue(old_val);
1537 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1539 if (_type == ALIAS || _tied)
1542 useDefault = useDefault && hasValue();
1545 old_val = getLongValue();
1550 _value.long_val = rawValue.clone();
1553 setLongValue(old_val);
1559 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1561 if (_type == ALIAS || _tied)
1564 useDefault = useDefault && hasValue();
1565 float old_val = 0.0;
1567 old_val = getFloatValue();
1572 _value.float_val = rawValue.clone();
1575 setFloatValue(old_val);
1581 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1583 if (_type == ALIAS || _tied)
1586 useDefault = useDefault && hasValue();
1587 double old_val = 0.0;
1589 old_val = getDoubleValue();
1594 _value.double_val = rawValue.clone();
1597 setDoubleValue(old_val);
1604 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1606 if (_type == ALIAS || _tied)
1609 useDefault = useDefault && hasValue();
1612 old_val = getStringValue();
1617 _value.string_val = rawValue.clone();
1620 setStringValue(old_val.c_str());
1626 SGPropertyNode::untie ()
1633 bool val = getBoolValue();
1636 _local_val.bool_val = val;
1640 int val = getIntValue();
1643 _local_val.int_val = val;
1647 long val = getLongValue();
1650 _local_val.long_val = val;
1654 float val = getFloatValue();
1657 _local_val.float_val = val;
1661 double val = getDoubleValue();
1664 _local_val.double_val = val;
1669 string val = getStringValue();
1672 _local_val.string_val = copy_string(val.c_str());
1685 SGPropertyNode::getRootNode ()
1690 return _parent->getRootNode();
1693 const SGPropertyNode *
1694 SGPropertyNode::getRootNode () const
1699 return _parent->getRootNode();
1703 SGPropertyNode::getNode (const char * relative_path, bool create)
1705 if (_path_cache == 0)
1706 _path_cache = new hash_table;
1708 SGPropertyNode * result = _path_cache->get(relative_path);
1710 vector<PathComponent> components;
1711 parse_path(relative_path, components);
1712 result = find_node(this, components, 0, create);
1714 _path_cache->put(relative_path, result);
1721 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1723 vector<PathComponent> components;
1724 parse_path(relative_path, components);
1725 if (components.size() > 0)
1726 components[components.size()-1].index = index;
1727 return find_node(this, components, 0, create);
1730 const SGPropertyNode *
1731 SGPropertyNode::getNode (const char * relative_path) const
1733 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1736 const SGPropertyNode *
1737 SGPropertyNode::getNode (const char * relative_path, int index) const
1739 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1743 ////////////////////////////////////////////////////////////////////////
1744 // Convenience methods using relative paths.
1745 ////////////////////////////////////////////////////////////////////////
1749 * Test whether another node has a value attached.
1752 SGPropertyNode::hasValue (const char * relative_path) const
1754 const SGPropertyNode * node = getNode(relative_path);
1755 return (node == 0 ? false : node->hasValue());
1760 * Get the value type for another node.
1762 SGPropertyNode::Type
1763 SGPropertyNode::getType (const char * relative_path) const
1765 const SGPropertyNode * node = getNode(relative_path);
1766 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1771 * Get a bool value for another node.
1774 SGPropertyNode::getBoolValue (const char * relative_path,
1775 bool defaultValue) const
1777 const SGPropertyNode * node = getNode(relative_path);
1778 return (node == 0 ? defaultValue : node->getBoolValue());
1783 * Get an int value for another node.
1786 SGPropertyNode::getIntValue (const char * relative_path,
1787 int defaultValue) const
1789 const SGPropertyNode * node = getNode(relative_path);
1790 return (node == 0 ? defaultValue : node->getIntValue());
1795 * Get a long value for another node.
1798 SGPropertyNode::getLongValue (const char * relative_path,
1799 long defaultValue) const
1801 const SGPropertyNode * node = getNode(relative_path);
1802 return (node == 0 ? defaultValue : node->getLongValue());
1807 * Get a float value for another node.
1810 SGPropertyNode::getFloatValue (const char * relative_path,
1811 float defaultValue) const
1813 const SGPropertyNode * node = getNode(relative_path);
1814 return (node == 0 ? defaultValue : node->getFloatValue());
1819 * Get a double value for another node.
1822 SGPropertyNode::getDoubleValue (const char * relative_path,
1823 double defaultValue) const
1825 const SGPropertyNode * node = getNode(relative_path);
1826 return (node == 0 ? defaultValue : node->getDoubleValue());
1831 * Get a string value for another node.
1834 SGPropertyNode::getStringValue (const char * relative_path,
1835 const char * defaultValue) const
1837 const SGPropertyNode * node = getNode(relative_path);
1838 return (node == 0 ? defaultValue : node->getStringValue());
1843 * Set a bool value for another node.
1846 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1848 return getNode(relative_path, true)->setBoolValue(value);
1853 * Set an int value for another node.
1856 SGPropertyNode::setIntValue (const char * relative_path, int value)
1858 return getNode(relative_path, true)->setIntValue(value);
1863 * Set a long value for another node.
1866 SGPropertyNode::setLongValue (const char * relative_path, long value)
1868 return getNode(relative_path, true)->setLongValue(value);
1873 * Set a float value for another node.
1876 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1878 return getNode(relative_path, true)->setFloatValue(value);
1883 * Set a double value for another node.
1886 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1888 return getNode(relative_path, true)->setDoubleValue(value);
1893 * Set a string value for another node.
1896 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1898 return getNode(relative_path, true)->setStringValue(value);
1903 * Set an unknown value for another node.
1906 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1909 return getNode(relative_path, true)->setUnspecifiedValue(value);
1914 * Test whether another node is tied.
1917 SGPropertyNode::isTied (const char * relative_path) const
1919 const SGPropertyNode * node = getNode(relative_path);
1920 return (node == 0 ? false : node->isTied());
1925 * Tie a node reached by a relative path, creating it if necessary.
1928 SGPropertyNode::tie (const char * relative_path,
1929 const SGRawValue<bool> &rawValue,
1932 return getNode(relative_path, true)->tie(rawValue, useDefault);
1937 * Tie a node reached by a relative path, creating it if necessary.
1940 SGPropertyNode::tie (const char * relative_path,
1941 const SGRawValue<int> &rawValue,
1944 return getNode(relative_path, true)->tie(rawValue, useDefault);
1949 * Tie a node reached by a relative path, creating it if necessary.
1952 SGPropertyNode::tie (const char * relative_path,
1953 const SGRawValue<long> &rawValue,
1956 return getNode(relative_path, true)->tie(rawValue, useDefault);
1961 * Tie a node reached by a relative path, creating it if necessary.
1964 SGPropertyNode::tie (const char * relative_path,
1965 const SGRawValue<float> &rawValue,
1968 return getNode(relative_path, true)->tie(rawValue, useDefault);
1973 * Tie a node reached by a relative path, creating it if necessary.
1976 SGPropertyNode::tie (const char * relative_path,
1977 const SGRawValue<double> &rawValue,
1980 return getNode(relative_path, true)->tie(rawValue, useDefault);
1985 * Tie a node reached by a relative path, creating it if necessary.
1988 SGPropertyNode::tie (const char * relative_path,
1989 const SGRawValue<const char *> &rawValue,
1992 return getNode(relative_path, true)->tie(rawValue, useDefault);
1997 * Attempt to untie another node reached by a relative path.
2000 SGPropertyNode::untie (const char * relative_path)
2002 SGPropertyNode * node = getNode(relative_path);
2003 return (node == 0 ? false : node->untie());
2007 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
2009 if (_listeners == 0)
2010 _listeners = new vector<SGPropertyChangeListener *>;
2011 _listeners->push_back(listener);
2012 listener->register_property(this);
2016 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2018 vector<SGPropertyChangeListener *>::iterator it =
2019 find(_listeners->begin(), _listeners->end(), listener);
2020 if (it != _listeners->end()) {
2021 _listeners->erase(it);
2022 listener->unregister_property(this);
2023 if (_listeners->empty()) {
2024 vector<SGPropertyChangeListener *> * tmp = _listeners;
2032 SGPropertyNode::fireValueChanged ()
2034 fireValueChanged(this);
2038 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2040 fireChildAdded(this, child);
2044 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2046 fireChildRemoved(this, child);
2050 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2052 if (_listeners != 0) {
2053 for (unsigned int i = 0; i < _listeners->size(); i++) {
2054 (*_listeners)[i]->valueChanged(node);
2058 _parent->fireValueChanged(node);
2062 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2063 SGPropertyNode * child)
2065 if (_listeners != 0) {
2066 for (unsigned int i = 0; i < _listeners->size(); i++) {
2067 (*_listeners)[i]->childAdded(parent, child);
2071 _parent->fireChildAdded(parent, child);
2075 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2076 SGPropertyNode * child)
2078 if (_listeners != 0) {
2079 for (unsigned int i = 0; i < _listeners->size(); i++) {
2080 (*_listeners)[i]->childRemoved(parent, child);
2084 _parent->fireChildRemoved(parent, child);
2089 ////////////////////////////////////////////////////////////////////////
2090 // Simplified hash table for caching paths.
2091 ////////////////////////////////////////////////////////////////////////
2093 #define HASH_TABLE_SIZE 199
2095 SGPropertyNode::hash_table::entry::entry ()
2101 SGPropertyNode::hash_table::entry::~entry ()
2103 // Don't delete the value; we don't own
2109 SGPropertyNode::hash_table::entry::set_key (const char * key)
2111 _key = copy_string(key);
2115 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2120 SGPropertyNode::hash_table::bucket::bucket ()
2126 SGPropertyNode::hash_table::bucket::~bucket ()
2128 for (int i = 0; i < _length; i++)
2132 SGPropertyNode::hash_table::entry *
2133 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2136 for (i = 0; i < _length; i++) {
2137 if (!strcmp(_entries[i]->get_key(), key))
2141 entry ** new_entries = new entry*[_length+1];
2142 for (i = 0; i < _length; i++) {
2143 new_entries[i] = _entries[i];
2146 _entries = new_entries;
2147 _entries[_length] = new entry;
2148 _entries[_length]->set_key(key);
2150 return _entries[_length - 1];
2157 SGPropertyNode::hash_table::hash_table ()
2163 SGPropertyNode::hash_table::~hash_table ()
2165 for (unsigned int i = 0; i < _data_length; i++)
2170 SGPropertyNode::hash_table::get (const char * key)
2172 if (_data_length == 0)
2174 unsigned int index = hashcode(key) % _data_length;
2175 if (_data[index] == 0)
2177 entry * e = _data[index]->get_entry(key);
2181 return e->get_value();
2185 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2187 if (_data_length == 0) {
2188 _data = new bucket*[HASH_TABLE_SIZE];
2189 _data_length = HASH_TABLE_SIZE;
2190 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2193 unsigned int index = hashcode(key) % _data_length;
2194 if (_data[index] == 0) {
2195 _data[index] = new bucket;
2197 entry * e = _data[index]->get_entry(key, true);
2198 e->set_value(value);
2202 SGPropertyNode::hash_table::hashcode (const char * key)
2204 unsigned int hash = 0;
2206 hash = 31 * hash + *key;
2215 * Default constructor
2217 SGPropertyNode_ptr::SGPropertyNode_ptr()
2225 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2229 _ptr->incrementRef();
2233 * Constructor from a pointer to a node
2235 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2239 _ptr->incrementRef();
2245 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2247 if (_ptr && _ptr->decrementRef() == 0)
2252 * Assignement operator
2254 SGPropertyNode_ptr &
2255 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2257 if (_ptr && _ptr->decrementRef() == 0)
2261 _ptr->incrementRef();
2267 * Pointer access operator
2270 SGPropertyNode_ptr::operator->()
2276 * Pointer access operator (const)
2278 const SGPropertyNode *
2279 SGPropertyNode_ptr::operator->() const
2285 * Conversion to SGPropertyNode * operator
2287 SGPropertyNode_ptr::operator SGPropertyNode *()
2293 * Conversion to const SGPropertyNode * operator
2295 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2304 SGPropertyNode_ptr::valid() const
2311 ////////////////////////////////////////////////////////////////////////
2312 // Implementation of SGPropertyChangeListener.
2313 ////////////////////////////////////////////////////////////////////////
2315 SGPropertyChangeListener::~SGPropertyChangeListener ()
2317 // This will come back and remove
2318 // the current item each time. Is
2320 vector<SGPropertyNode *>::iterator it;
2321 for (it = _properties.begin(); it != _properties.end(); it++)
2322 (*it)->removeChangeListener(this);
2326 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2332 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2333 SGPropertyNode * child)
2339 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2340 SGPropertyNode * child)
2346 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2348 _properties.push_back(node);
2352 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2354 vector<SGPropertyNode *>::iterator it =
2355 find(_properties.begin(), _properties.end(), node);
2356 if (it != _properties.end())
2357 _properties.erase(it);