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].
24 #include <simgear/compiler.h>
25 #include <simgear/debug/logstream.hxx>
33 ////////////////////////////////////////////////////////////////////////
35 ////////////////////////////////////////////////////////////////////////
38 * Comparator class for sorting by index.
43 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
44 return (n1->getIndex() < n2->getIndex());
50 ////////////////////////////////////////////////////////////////////////
51 // Convenience macros for value access.
52 ////////////////////////////////////////////////////////////////////////
54 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
55 #define TEST_WRITE if (!getAttribute(WRITE)) return false
59 ////////////////////////////////////////////////////////////////////////
60 // Default values for every type.
61 ////////////////////////////////////////////////////////////////////////
63 const bool SGRawValue<bool>::DefaultValue = false;
64 const int SGRawValue<int>::DefaultValue = 0;
65 const long SGRawValue<long>::DefaultValue = 0L;
66 const float SGRawValue<float>::DefaultValue = 0.0;
67 const double SGRawValue<double>::DefaultValue = 0.0L;
68 const char * const SGRawValue<const char *>::DefaultValue = "";
72 ////////////////////////////////////////////////////////////////////////
73 // Local path normalization code.
74 ////////////////////////////////////////////////////////////////////////
77 * A component in a path.
86 * Parse the name for a path component.
88 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
90 static inline const string
91 parse_name (const string &path, int &i)
94 int max = path.size();
98 if (i < max && path[i] == '.') {
104 if (i < max && path[i] != '/')
105 throw string(string("Illegal character after ") + name);
108 else if (isalpha(path[i]) || path[i] == '_') {
112 // The rules inside a name are a little
115 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
116 path[i] == '-' || path[i] == '.') {
118 } else if (path[i] == '[' || path[i] == '/') {
121 throw string("name may contain only ._- and alphanumeric characters");
128 if (name.size() == 0)
129 throw string("name must begin with alpha or '_'");
137 * Parse the optional integer index for a path component.
139 * Index: "[" [0-9]+ "]"
142 parse_index (const string &path, int &i)
151 for (int max = path.size(); i < max; i++) {
152 if (isdigit(path[i])) {
153 index = (index * 10) + (path[i] - '0');
154 } else if (path[i] == ']') {
162 throw string("unterminated index (looking for ']')");
167 * Parse a single path component.
169 * Component: Name Index?
171 static inline PathComponent
172 parse_component (const string &path, int &i)
174 PathComponent component;
175 component.name = parse_name(path, i);
176 if (component.name[0] != '.')
177 component.index = parse_index(path, i);
179 component.index = -1;
185 * Parse a path into its components.
188 parse_path (const string &path, vector<PathComponent> &components)
191 int max = path.size();
193 // Check for initial '/'
194 if (path[pos] == '/') {
198 components.push_back(root);
200 while (pos < max && path[pos] == '/')
205 components.push_back(parse_component(path, pos));
206 while (pos < max && path[pos] == '/')
213 ////////////////////////////////////////////////////////////////////////
214 // Other static utility functions.
215 ////////////////////////////////////////////////////////////////////////
219 copy_string (const char * s)
221 // FIXME: potential buffer overflow.
222 // For some reason, strnlen and
223 // strncpy cause all kinds of crashes.
224 char * copy = new char[strlen(s) + 1];
230 compare_strings (const char * s1, const char * s2)
232 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
236 * Locate a child node by name and index.
239 find_child (const char * name, int index, vector<SGPropertyNode_ptr> nodes)
241 int nNodes = nodes.size();
242 for (int i = 0; i < nNodes; i++) {
243 SGPropertyNode * node = nodes[i];
244 if (compare_strings(node->getName(), name) && node->getIndex() == index)
252 * Locate another node, given a relative path.
254 static SGPropertyNode *
255 find_node (SGPropertyNode * current,
256 const vector<PathComponent> &components,
260 // Run off the end of the list
265 // Success! This is the one we want.
266 else if (position >= (int)components.size()) {
270 // Empty component means root.
271 else if (components[position].name == "") {
272 return find_node(current->getRootNode(), components, position + 1, create);
275 // . means current directory
276 else if (components[position].name == ".") {
277 return find_node(current, components, position + 1, create);
280 // .. means parent directory
281 else if (components[position].name == "..") {
282 SGPropertyNode * parent = current->getParent();
284 throw string("Attempt to move past root with '..'");
286 return find_node(parent, components, position + 1, create);
289 // Otherwise, a child name
291 SGPropertyNode * child =
292 current->getChild(components[position].name.c_str(),
293 components[position].index,
295 return find_node(child, components, position + 1, create);
301 ////////////////////////////////////////////////////////////////////////
302 // Private methods from SGPropertyNode (may be inlined for speed).
303 ////////////////////////////////////////////////////////////////////////
306 SGPropertyNode::get_bool () const
309 return _value.bool_val->getValue();
311 return _local_val.bool_val;
315 SGPropertyNode::get_int () const
318 return _value.int_val->getValue();
320 return _local_val.int_val;
324 SGPropertyNode::get_long () const
327 return _value.long_val->getValue();
329 return _local_val.long_val;
333 SGPropertyNode::get_float () const
336 return _value.float_val->getValue();
338 return _local_val.float_val;
342 SGPropertyNode::get_double () const
345 return _value.double_val->getValue();
347 return _local_val.double_val;
351 SGPropertyNode::get_string () const
354 return _value.string_val->getValue();
356 return _local_val.string_val;
360 SGPropertyNode::set_bool (bool val)
363 if (_value.bool_val->setValue(val)) {
364 firePropertyChange();
370 _local_val.bool_val = val;
371 firePropertyChange();
377 SGPropertyNode::set_int (int val)
380 if (_value.int_val->setValue(val)) {
381 firePropertyChange();
387 _local_val.int_val = val;
388 firePropertyChange();
394 SGPropertyNode::set_long (long val)
397 if (_value.long_val->setValue(val)) {
398 firePropertyChange();
404 _local_val.long_val = val;
405 firePropertyChange();
411 SGPropertyNode::set_float (float val)
414 if (_value.float_val->setValue(val)) {
415 firePropertyChange();
421 _local_val.float_val = val;
422 firePropertyChange();
428 SGPropertyNode::set_double (double val)
431 if (_value.double_val->setValue(val)) {
432 firePropertyChange();
438 _local_val.double_val = val;
439 firePropertyChange();
445 SGPropertyNode::set_string (const char * val)
448 if (_value.string_val->setValue(val)) {
449 firePropertyChange();
455 delete [] _local_val.string_val;
456 _local_val.string_val = copy_string(val);
457 firePropertyChange();
463 SGPropertyNode::clear_value ()
473 delete _value.bool_val;
476 _local_val.bool_val = SGRawValue<bool>::DefaultValue;
480 delete _value.int_val;
483 _local_val.int_val = SGRawValue<int>::DefaultValue;
487 delete _value.long_val;
488 _value.long_val = 0L;
490 _local_val.long_val = SGRawValue<long>::DefaultValue;
494 delete _value.float_val;
495 _value.float_val = 0;
497 _local_val.float_val = SGRawValue<float>::DefaultValue;
501 delete _value.double_val;
502 _value.double_val = 0;
504 _local_val.double_val = SGRawValue<double>::DefaultValue;
509 delete _value.string_val;
510 _value.string_val = 0;
512 delete [] _local_val.string_val;
514 _local_val.string_val = 0;
523 * Get the value as a string.
526 SGPropertyNode::make_string () const
528 if (!getAttribute(READ))
533 return _value.alias->getStringValue();
540 sprintf(_buffer, "%d", get_int());
543 sprintf(_buffer, "%ld", get_long());
546 sprintf(_buffer, "%f", get_float());
549 sprintf(_buffer, "%f", get_double());
561 * Trace a write access for a property.
564 SGPropertyNode::trace_write () const
567 cerr << "TRACE: Write node " << getPath () << ", value\""
568 << make_string() << '"' << endl;
570 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Write node " << getPath()
571 << ", value\"" << make_string() << '"');
576 * Trace a read access for a property.
579 SGPropertyNode::trace_read () const
582 cerr << "TRACE: Write node " << getPath () << ", value \""
583 << make_string() << '"' << endl;
585 SG_LOG(SG_GENERAL, SG_INFO, "TRACE: Read node " << getPath()
586 << ", value \"" << make_string() << '"');
591 * Increment reference counter
594 SGPropertyNode::incrementRef()
600 * Decrement reference counter
603 SGPropertyNode::decrementRef()
610 ////////////////////////////////////////////////////////////////////////
611 // Public methods from SGPropertyNode.
612 ////////////////////////////////////////////////////////////////////////
615 * Last used attribute
616 * Update as needed when enum Attribute is changed
618 const int SGPropertyNode::LAST_USED_ATTRIBUTE = TRACE_WRITE;
621 * Default constructor: always creates a root node.
623 SGPropertyNode::SGPropertyNode ()
624 : _name(copy_string("")),
634 _local_val.string_val = 0;
641 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
642 : _index(node._index),
643 _parent(0), // don't copy the parent
649 _listeners(0) // CHECK!!
651 _name = copy_string(node._name);
652 _local_val.string_val = 0;
657 _value.alias = node._value.alias;
663 _value.bool_val = node._value.bool_val->clone();
666 set_bool(node.get_bool());
672 _value.int_val = node._value.int_val->clone();
675 set_int(node.get_int());
681 _value.long_val = node._value.long_val->clone();
684 set_long(node.get_long());
690 _value.float_val = node._value.float_val->clone();
693 set_float(node.get_float());
699 _value.double_val = node._value.double_val->clone();
702 set_double(node.get_double());
709 _value.string_val = node._value.string_val->clone();
712 set_string(node.get_string());
720 * Convenience constructor.
722 SGPropertyNode::SGPropertyNode (const char * name,
724 SGPropertyNode * parent)
734 _name = copy_string(name);
735 _local_val.string_val = 0;
742 SGPropertyNode::~SGPropertyNode ()
752 * Alias to another node.
755 SGPropertyNode::alias (SGPropertyNode * target)
757 if (target == 0 || _type == ALIAS || _tied)
760 _value.alias = target;
767 * Alias to another node by path.
770 SGPropertyNode::alias (const char * path)
772 return alias(getNode(path, true));
780 SGPropertyNode::unalias ()
791 * Get the target of an alias.
794 SGPropertyNode::getAliasTarget ()
796 return (_type == ALIAS ? _value.alias : 0);
800 const SGPropertyNode *
801 SGPropertyNode::getAliasTarget () const
803 return (_type == ALIAS ? _value.alias : 0);
808 * Get a non-const child by index.
811 SGPropertyNode::getChild (int position)
813 if (position >= 0 && position < nChildren())
814 return _children[position];
821 * Get a const child by index.
823 const SGPropertyNode *
824 SGPropertyNode::getChild (int position) const
826 if (position >= 0 && position < nChildren())
827 return _children[position];
834 * Get a non-const child by name and index, creating if necessary.
837 SGPropertyNode::getChild (const char * name, int index, bool create)
839 int pos = find_child(name, index, _children);
841 return _children[pos];
843 SGPropertyNode_ptr node;
844 pos = find_child(name, index, _removedChildren);
846 std::vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
848 node = _removedChildren[pos];
849 _removedChildren.erase(it);
850 node->setAttribute(REMOVED, false);
852 node = new SGPropertyNode(name, index, this);
854 _children.push_back(node);
856 _parent->firePropertyChange();
857 return _children[_children.size()-1];
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);
916 _parent->firePropertyChange();
922 SGPropertyNode::getPath (bool simplify) const
927 string path = _parent->getPath(simplify);
930 if (_index != 0 || !simplify) {
932 sprintf(buffer, "[%d]", _index);
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);
1995 SGPropertyNode::firePropertyChange (SGPropertyNode * node)
1997 if (_listeners != 0) {
1998 for (int i = 0; i < _listeners->size(); i++) {
1999 (*_listeners)[i]->propertyChanged(node);
2003 _parent->firePropertyChange(node);
2008 ////////////////////////////////////////////////////////////////////////
2009 // Simplified hash table for caching paths.
2010 ////////////////////////////////////////////////////////////////////////
2012 #define HASH_TABLE_SIZE 199
2014 SGPropertyNode::hash_table::entry::entry ()
2020 SGPropertyNode::hash_table::entry::~entry ()
2022 // Don't delete the value; we don't own
2028 SGPropertyNode::hash_table::entry::set_key (const char * key)
2030 _key = copy_string(key);
2034 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2039 SGPropertyNode::hash_table::bucket::bucket ()
2045 SGPropertyNode::hash_table::bucket::~bucket ()
2047 for (int i = 0; i < _length; i++)
2051 SGPropertyNode::hash_table::entry *
2052 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2055 for (i = 0; i < _length; i++) {
2056 if (!strcmp(_entries[i]->get_key(), key))
2060 entry ** new_entries = new entry*[_length+1];
2061 for (i = 0; i < _length; i++) {
2062 new_entries[i] = _entries[i];
2065 _entries = new_entries;
2066 _entries[_length] = new entry;
2067 _entries[_length]->set_key(key);
2069 return _entries[_length - 1];
2076 SGPropertyNode::hash_table::hash_table ()
2082 SGPropertyNode::hash_table::~hash_table ()
2084 for (unsigned int i = 0; i < _data_length; i++)
2089 SGPropertyNode::hash_table::get (const char * key)
2091 if (_data_length == 0)
2093 unsigned int index = hashcode(key) % _data_length;
2094 if (_data[index] == 0)
2096 entry * e = _data[index]->get_entry(key);
2100 return e->get_value();
2104 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2106 if (_data_length == 0) {
2107 _data = new bucket*[HASH_TABLE_SIZE];
2108 _data_length = HASH_TABLE_SIZE;
2109 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2112 unsigned int index = hashcode(key) % _data_length;
2113 if (_data[index] == 0) {
2114 _data[index] = new bucket;
2116 entry * e = _data[index]->get_entry(key, true);
2117 e->set_value(value);
2121 SGPropertyNode::hash_table::hashcode (const char * key)
2123 unsigned int hash = 0;
2125 hash = 31 * hash + *key;
2134 * Default constructor
2136 SGPropertyNode_ptr::SGPropertyNode_ptr()
2144 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2148 _ptr->incrementRef();
2152 * Constructor from a pointer to a node
2154 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2158 _ptr->incrementRef();
2164 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2166 if (_ptr && _ptr->decrementRef() == 0)
2171 * Assignement operator
2173 SGPropertyNode_ptr &
2174 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2176 if (_ptr && _ptr->decrementRef() == 0)
2180 _ptr->incrementRef();
2186 * Pointer access operator
2189 SGPropertyNode_ptr::operator->()
2195 * Pointer access operator (const)
2197 const SGPropertyNode *
2198 SGPropertyNode_ptr::operator->() const
2204 * Conversion to SGPropertyNode * operator
2206 SGPropertyNode_ptr::operator SGPropertyNode *()
2212 * Conversion to const SGPropertyNode * operator
2214 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2223 SGPropertyNode_ptr::valid() const