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("")),
645 _local_val.string_val = 0;
652 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
653 : _index(node._index),
654 _parent(0), // don't copy the parent
660 _listeners(0) // CHECK!!
662 _name = copy_string(node._name);
663 _local_val.string_val = 0;
668 _value.alias = node._value.alias;
674 _value.bool_val = node._value.bool_val->clone();
677 set_bool(node.get_bool());
683 _value.int_val = node._value.int_val->clone();
686 set_int(node.get_int());
692 _value.long_val = node._value.long_val->clone();
695 set_long(node.get_long());
701 _value.float_val = node._value.float_val->clone();
704 set_float(node.get_float());
710 _value.double_val = node._value.double_val->clone();
713 set_double(node.get_double());
720 _value.string_val = node._value.string_val->clone();
723 set_string(node.get_string());
731 * Convenience constructor.
733 SGPropertyNode::SGPropertyNode (const char * name,
735 SGPropertyNode * parent)
745 _name = copy_string(name);
746 _local_val.string_val = 0;
753 SGPropertyNode::~SGPropertyNode ()
763 * Alias to another node.
766 SGPropertyNode::alias (SGPropertyNode * target)
768 if (target == 0 || _type == ALIAS || _tied)
771 _value.alias = target;
778 * Alias to another node by path.
781 SGPropertyNode::alias (const char * path)
783 return alias(getNode(path, true));
791 SGPropertyNode::unalias ()
802 * Get the target of an alias.
805 SGPropertyNode::getAliasTarget ()
807 return (_type == ALIAS ? _value.alias : 0);
811 const SGPropertyNode *
812 SGPropertyNode::getAliasTarget () const
814 return (_type == ALIAS ? _value.alias : 0);
819 * Get a non-const child by index.
822 SGPropertyNode::getChild (int position)
824 if (position >= 0 && position < nChildren())
825 return _children[position];
832 * Get a const child by index.
834 const SGPropertyNode *
835 SGPropertyNode::getChild (int position) const
837 if (position >= 0 && position < nChildren())
838 return _children[position];
845 * Get a non-const child by name and index, creating if necessary.
848 SGPropertyNode::getChild (const char * name, int index, bool create)
850 int pos = find_child(name, index, _children);
852 return _children[pos];
854 SGPropertyNode_ptr node;
855 pos = find_child(name, index, _removedChildren);
857 vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
859 node = _removedChildren[pos];
860 _removedChildren.erase(it);
861 node->setAttribute(REMOVED, false);
863 node = new SGPropertyNode(name, index, this);
865 _children.push_back(node);
866 fireChildAdded(node);
875 * Get a const child by name and index.
877 const SGPropertyNode *
878 SGPropertyNode::getChild (const char * name, int index) const
880 int pos = find_child(name, index, _children);
882 return _children[pos];
889 * Get all children with the same name (but different indices).
891 vector<SGPropertyNode_ptr>
892 SGPropertyNode::getChildren (const char * name) const
894 vector<SGPropertyNode_ptr> children;
895 int max = _children.size();
897 for (int i = 0; i < max; i++)
898 if (compare_strings(_children[i]->getName(), name))
899 children.push_back(_children[i]);
901 sort(children.begin(), children.end(), CompareIndices());
907 * Remove a child node
910 SGPropertyNode::removeChild (const char * name, int index, bool keep)
912 SGPropertyNode_ptr ret;
913 int pos = find_child(name, index, _children);
915 vector<SGPropertyNode_ptr>::iterator it = _children.begin();
917 SGPropertyNode_ptr node = _children[pos];
920 _removedChildren.push_back(node);
922 node->setAttribute(REMOVED, true);
924 fireChildRemoved(node);
931 SGPropertyNode::getPath (bool simplify) const
936 string path = _parent->getPath(simplify);
939 if (_index != 0 || !simplify) {
941 sprintf(buffer, "[%d]", _index);
944 strncpy(_buffer, path.c_str(), MAX_STRING_LEN);
949 SGPropertyNode::getType () const
952 return _value.alias->getType();
959 SGPropertyNode::getBoolValue () const
961 // Shortcut for common case
962 if (_attr == (READ|WRITE) && _type == BOOL)
965 if (getAttribute(TRACE_READ))
967 if (!getAttribute(READ))
968 return SGRawValue<bool>::DefaultValue;
971 return _value.alias->getBoolValue();
975 return get_int() == 0 ? false : true;
977 return get_long() == 0L ? false : true;
979 return get_float() == 0.0 ? false : true;
981 return get_double() == 0.0L ? false : true;
984 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
987 return SGRawValue<bool>::DefaultValue;
992 SGPropertyNode::getIntValue () const
994 // Shortcut for common case
995 if (_attr == (READ|WRITE) && _type == INT)
998 if (getAttribute(TRACE_READ))
1000 if (!getAttribute(READ))
1001 return SGRawValue<int>::DefaultValue;
1004 return _value.alias->getIntValue();
1006 return int(get_bool());
1010 return int(get_long());
1012 return int(get_float());
1014 return int(get_double());
1017 return atoi(get_string());
1020 return SGRawValue<int>::DefaultValue;
1025 SGPropertyNode::getLongValue () const
1027 // Shortcut for common case
1028 if (_attr == (READ|WRITE) && _type == LONG)
1031 if (getAttribute(TRACE_READ))
1033 if (!getAttribute(READ))
1034 return SGRawValue<long>::DefaultValue;
1037 return _value.alias->getLongValue();
1039 return long(get_bool());
1041 return long(get_int());
1045 return long(get_float());
1047 return long(get_double());
1050 return strtol(get_string(), 0, 0);
1053 return SGRawValue<long>::DefaultValue;
1058 SGPropertyNode::getFloatValue () const
1060 // Shortcut for common case
1061 if (_attr == (READ|WRITE) && _type == FLOAT)
1064 if (getAttribute(TRACE_READ))
1066 if (!getAttribute(READ))
1067 return SGRawValue<float>::DefaultValue;
1070 return _value.alias->getFloatValue();
1072 return float(get_bool());
1074 return float(get_int());
1076 return float(get_long());
1080 return float(get_double());
1083 return atof(get_string());
1086 return SGRawValue<float>::DefaultValue;
1091 SGPropertyNode::getDoubleValue () const
1093 // Shortcut for common case
1094 if (_attr == (READ|WRITE) && _type == DOUBLE)
1095 return get_double();
1097 if (getAttribute(TRACE_READ))
1099 if (!getAttribute(READ))
1100 return SGRawValue<double>::DefaultValue;
1104 return _value.alias->getDoubleValue();
1106 return double(get_bool());
1108 return double(get_int());
1110 return double(get_long());
1112 return double(get_float());
1114 return get_double();
1117 return strtod(get_string(), 0);
1120 return SGRawValue<double>::DefaultValue;
1125 SGPropertyNode::getStringValue () const
1127 // Shortcut for common case
1128 if (_attr == (READ|WRITE) && _type == STRING)
1129 return get_string();
1131 if (getAttribute(TRACE_READ))
1133 if (!getAttribute(READ))
1134 return SGRawValue<const char *>::DefaultValue;
1135 return make_string();
1139 SGPropertyNode::setBoolValue (bool value)
1141 // Shortcut for common case
1142 if (_attr == (READ|WRITE) && _type == BOOL)
1143 return set_bool(value);
1145 bool result = false;
1147 if (_type == NONE || _type == UNSPECIFIED) {
1155 result = _value.alias->setBoolValue(value);
1158 result = set_bool(value);
1161 result = set_int(int(value));
1164 result = set_long(long(value));
1167 result = set_float(float(value));
1170 result = set_double(double(value));
1174 result = set_string(value ? "true" : "false");
1181 if (getAttribute(TRACE_WRITE))
1187 SGPropertyNode::setIntValue (int value)
1189 // Shortcut for common case
1190 if (_attr == (READ|WRITE) && _type == INT)
1191 return set_int(value);
1193 bool result = false;
1195 if (_type == NONE || _type == UNSPECIFIED) {
1198 _local_val.int_val = 0;
1203 result = _value.alias->setIntValue(value);
1206 result = set_bool(value == 0 ? false : true);
1209 result = set_int(value);
1212 result = set_long(long(value));
1215 result = set_float(float(value));
1218 result = set_double(double(value));
1223 sprintf(buf, "%d", value);
1224 result = set_string(buf);
1232 if (getAttribute(TRACE_WRITE))
1238 SGPropertyNode::setLongValue (long value)
1240 // Shortcut for common case
1241 if (_attr == (READ|WRITE) && _type == LONG)
1242 return set_long(value);
1244 bool result = false;
1246 if (_type == NONE || _type == UNSPECIFIED) {
1249 _local_val.long_val = 0L;
1254 result = _value.alias->setLongValue(value);
1257 result = set_bool(value == 0L ? false : true);
1260 result = set_int(int(value));
1263 result = set_long(value);
1266 result = set_float(float(value));
1269 result = set_double(double(value));
1274 sprintf(buf, "%ld", value);
1275 result = set_string(buf);
1283 if (getAttribute(TRACE_WRITE))
1289 SGPropertyNode::setFloatValue (float value)
1291 // Shortcut for common case
1292 if (_attr == (READ|WRITE) && _type == FLOAT)
1293 return set_float(value);
1295 bool result = false;
1297 if (_type == NONE || _type == UNSPECIFIED) {
1300 _local_val.float_val = 0;
1305 result = _value.alias->setFloatValue(value);
1308 result = set_bool(value == 0.0 ? false : true);
1311 result = set_int(int(value));
1314 result = set_long(long(value));
1317 result = set_float(value);
1320 result = set_double(double(value));
1325 sprintf(buf, "%f", value);
1326 result = set_string(buf);
1334 if (getAttribute(TRACE_WRITE))
1340 SGPropertyNode::setDoubleValue (double value)
1342 // Shortcut for common case
1343 if (_attr == (READ|WRITE) && _type == DOUBLE)
1344 return set_double(value);
1346 bool result = false;
1348 if (_type == NONE || _type == UNSPECIFIED) {
1350 _local_val.double_val = value;
1356 result = _value.alias->setDoubleValue(value);
1359 result = set_bool(value == 0.0L ? false : true);
1362 result = set_int(int(value));
1365 result = set_long(long(value));
1368 result = set_float(float(value));
1371 result = set_double(value);
1376 sprintf(buf, "%f", value);
1377 result = set_string(buf);
1385 if (getAttribute(TRACE_WRITE))
1391 SGPropertyNode::setStringValue (const char * value)
1393 // Shortcut for common case
1394 if (_attr == (READ|WRITE) && _type == STRING)
1395 return set_string(value);
1397 bool result = false;
1399 if (_type == NONE || _type == UNSPECIFIED) {
1406 result = _value.alias->setStringValue(value);
1409 result = set_bool((compare_strings(value, "true")
1410 || atoi(value)) ? true : false);
1413 result = set_int(atoi(value));
1416 result = set_long(strtol(value, 0, 0));
1419 result = set_float(atof(value));
1422 result = set_double(strtod(value, 0));
1426 result = set_string(value);
1433 if (getAttribute(TRACE_WRITE))
1439 SGPropertyNode::setUnspecifiedValue (const char * value)
1441 bool result = false;
1443 if (_type == NONE) {
1445 _type = UNSPECIFIED;
1450 result = _value.alias->setUnspecifiedValue(value);
1453 result = set_bool((compare_strings(value, "true")
1454 || atoi(value)) ? true : false);
1457 result = set_int(atoi(value));
1460 result = set_long(strtol(value, 0, 0));
1463 result = set_float(atof(value));
1466 result = set_double(strtod(value, 0));
1470 result = set_string(value);
1477 if (getAttribute(TRACE_WRITE))
1483 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1485 if (_type == ALIAS || _tied)
1488 useDefault = useDefault && hasValue();
1489 bool old_val = false;
1491 old_val = getBoolValue();
1496 _value.bool_val = rawValue.clone();
1499 setBoolValue(old_val);
1505 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1507 if (_type == ALIAS || _tied)
1510 useDefault = useDefault && hasValue();
1513 old_val = getIntValue();
1518 _value.int_val = rawValue.clone();
1521 setIntValue(old_val);
1527 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1529 if (_type == ALIAS || _tied)
1532 useDefault = useDefault && hasValue();
1535 old_val = getLongValue();
1540 _value.long_val = rawValue.clone();
1543 setLongValue(old_val);
1549 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1551 if (_type == ALIAS || _tied)
1554 useDefault = useDefault && hasValue();
1555 float old_val = 0.0;
1557 old_val = getFloatValue();
1562 _value.float_val = rawValue.clone();
1565 setFloatValue(old_val);
1571 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1573 if (_type == ALIAS || _tied)
1576 useDefault = useDefault && hasValue();
1577 double old_val = 0.0;
1579 old_val = getDoubleValue();
1584 _value.double_val = rawValue.clone();
1587 setDoubleValue(old_val);
1594 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1596 if (_type == ALIAS || _tied)
1599 useDefault = useDefault && hasValue();
1602 old_val = getStringValue();
1607 _value.string_val = rawValue.clone();
1610 setStringValue(old_val.c_str());
1616 SGPropertyNode::untie ()
1623 bool val = getBoolValue();
1626 _local_val.bool_val = val;
1630 int val = getIntValue();
1633 _local_val.int_val = val;
1637 long val = getLongValue();
1640 _local_val.long_val = val;
1644 float val = getFloatValue();
1647 _local_val.float_val = val;
1651 double val = getDoubleValue();
1654 _local_val.double_val = val;
1659 string val = getStringValue();
1662 _local_val.string_val = copy_string(val.c_str());
1675 SGPropertyNode::getRootNode ()
1680 return _parent->getRootNode();
1683 const SGPropertyNode *
1684 SGPropertyNode::getRootNode () const
1689 return _parent->getRootNode();
1693 SGPropertyNode::getNode (const char * relative_path, bool create)
1695 if (_path_cache == 0)
1696 _path_cache = new hash_table;
1698 SGPropertyNode * result = _path_cache->get(relative_path);
1700 vector<PathComponent> components;
1701 parse_path(relative_path, components);
1702 result = find_node(this, components, 0, create);
1704 _path_cache->put(relative_path, result);
1711 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1713 vector<PathComponent> components;
1714 parse_path(relative_path, components);
1715 if (components.size() > 0)
1716 components[components.size()-1].index = index;
1717 return find_node(this, components, 0, create);
1720 const SGPropertyNode *
1721 SGPropertyNode::getNode (const char * relative_path) const
1723 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1726 const SGPropertyNode *
1727 SGPropertyNode::getNode (const char * relative_path, int index) const
1729 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1733 ////////////////////////////////////////////////////////////////////////
1734 // Convenience methods using relative paths.
1735 ////////////////////////////////////////////////////////////////////////
1739 * Test whether another node has a value attached.
1742 SGPropertyNode::hasValue (const char * relative_path) const
1744 const SGPropertyNode * node = getNode(relative_path);
1745 return (node == 0 ? false : node->hasValue());
1750 * Get the value type for another node.
1752 SGPropertyNode::Type
1753 SGPropertyNode::getType (const char * relative_path) const
1755 const SGPropertyNode * node = getNode(relative_path);
1756 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1761 * Get a bool value for another node.
1764 SGPropertyNode::getBoolValue (const char * relative_path,
1765 bool defaultValue) const
1767 const SGPropertyNode * node = getNode(relative_path);
1768 return (node == 0 ? defaultValue : node->getBoolValue());
1773 * Get an int value for another node.
1776 SGPropertyNode::getIntValue (const char * relative_path,
1777 int defaultValue) const
1779 const SGPropertyNode * node = getNode(relative_path);
1780 return (node == 0 ? defaultValue : node->getIntValue());
1785 * Get a long value for another node.
1788 SGPropertyNode::getLongValue (const char * relative_path,
1789 long defaultValue) const
1791 const SGPropertyNode * node = getNode(relative_path);
1792 return (node == 0 ? defaultValue : node->getLongValue());
1797 * Get a float value for another node.
1800 SGPropertyNode::getFloatValue (const char * relative_path,
1801 float defaultValue) const
1803 const SGPropertyNode * node = getNode(relative_path);
1804 return (node == 0 ? defaultValue : node->getFloatValue());
1809 * Get a double value for another node.
1812 SGPropertyNode::getDoubleValue (const char * relative_path,
1813 double defaultValue) const
1815 const SGPropertyNode * node = getNode(relative_path);
1816 return (node == 0 ? defaultValue : node->getDoubleValue());
1821 * Get a string value for another node.
1824 SGPropertyNode::getStringValue (const char * relative_path,
1825 const char * defaultValue) const
1827 const SGPropertyNode * node = getNode(relative_path);
1828 return (node == 0 ? defaultValue : node->getStringValue());
1833 * Set a bool value for another node.
1836 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1838 return getNode(relative_path, true)->setBoolValue(value);
1843 * Set an int value for another node.
1846 SGPropertyNode::setIntValue (const char * relative_path, int value)
1848 return getNode(relative_path, true)->setIntValue(value);
1853 * Set a long value for another node.
1856 SGPropertyNode::setLongValue (const char * relative_path, long value)
1858 return getNode(relative_path, true)->setLongValue(value);
1863 * Set a float value for another node.
1866 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1868 return getNode(relative_path, true)->setFloatValue(value);
1873 * Set a double value for another node.
1876 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1878 return getNode(relative_path, true)->setDoubleValue(value);
1883 * Set a string value for another node.
1886 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1888 return getNode(relative_path, true)->setStringValue(value);
1893 * Set an unknown value for another node.
1896 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1899 return getNode(relative_path, true)->setUnspecifiedValue(value);
1904 * Test whether another node is tied.
1907 SGPropertyNode::isTied (const char * relative_path) const
1909 const SGPropertyNode * node = getNode(relative_path);
1910 return (node == 0 ? false : node->isTied());
1915 * Tie a node reached by a relative path, creating it if necessary.
1918 SGPropertyNode::tie (const char * relative_path,
1919 const SGRawValue<bool> &rawValue,
1922 return getNode(relative_path, true)->tie(rawValue, useDefault);
1927 * Tie a node reached by a relative path, creating it if necessary.
1930 SGPropertyNode::tie (const char * relative_path,
1931 const SGRawValue<int> &rawValue,
1934 return getNode(relative_path, true)->tie(rawValue, useDefault);
1939 * Tie a node reached by a relative path, creating it if necessary.
1942 SGPropertyNode::tie (const char * relative_path,
1943 const SGRawValue<long> &rawValue,
1946 return getNode(relative_path, true)->tie(rawValue, useDefault);
1951 * Tie a node reached by a relative path, creating it if necessary.
1954 SGPropertyNode::tie (const char * relative_path,
1955 const SGRawValue<float> &rawValue,
1958 return getNode(relative_path, true)->tie(rawValue, useDefault);
1963 * Tie a node reached by a relative path, creating it if necessary.
1966 SGPropertyNode::tie (const char * relative_path,
1967 const SGRawValue<double> &rawValue,
1970 return getNode(relative_path, true)->tie(rawValue, useDefault);
1975 * Tie a node reached by a relative path, creating it if necessary.
1978 SGPropertyNode::tie (const char * relative_path,
1979 const SGRawValue<const char *> &rawValue,
1982 return getNode(relative_path, true)->tie(rawValue, useDefault);
1987 * Attempt to untie another node reached by a relative path.
1990 SGPropertyNode::untie (const char * relative_path)
1992 SGPropertyNode * node = getNode(relative_path);
1993 return (node == 0 ? false : node->untie());
1997 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
1999 if (_listeners == 0)
2000 _listeners = new vector<SGPropertyChangeListener *>;
2001 _listeners->push_back(listener);
2002 listener->register_property(this);
2006 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2008 vector<SGPropertyChangeListener *>::iterator it =
2009 find(_listeners->begin(), _listeners->end(), listener);
2010 if (it != _listeners->end()) {
2011 _listeners->erase(it);
2012 listener->unregister_property(this);
2013 if (_listeners->empty()) {
2014 vector<SGPropertyChangeListener *> * tmp = _listeners;
2022 SGPropertyNode::fireValueChanged ()
2024 fireValueChanged(this);
2028 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2030 fireChildAdded(this, child);
2034 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2036 fireChildRemoved(this, child);
2040 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2042 if (_listeners != 0) {
2043 for (unsigned int i = 0; i < _listeners->size(); i++) {
2044 (*_listeners)[i]->valueChanged(node);
2048 _parent->fireValueChanged(node);
2052 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2053 SGPropertyNode * child)
2055 if (_listeners != 0) {
2056 for (unsigned int i = 0; i < _listeners->size(); i++) {
2057 (*_listeners)[i]->childAdded(parent, child);
2061 _parent->fireChildAdded(parent, child);
2065 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2066 SGPropertyNode * child)
2068 if (_listeners != 0) {
2069 for (unsigned int i = 0; i < _listeners->size(); i++) {
2070 (*_listeners)[i]->childRemoved(parent, child);
2074 _parent->fireChildRemoved(parent, child);
2079 ////////////////////////////////////////////////////////////////////////
2080 // Simplified hash table for caching paths.
2081 ////////////////////////////////////////////////////////////////////////
2083 #define HASH_TABLE_SIZE 199
2085 SGPropertyNode::hash_table::entry::entry ()
2091 SGPropertyNode::hash_table::entry::~entry ()
2093 // Don't delete the value; we don't own
2099 SGPropertyNode::hash_table::entry::set_key (const char * key)
2101 _key = copy_string(key);
2105 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2110 SGPropertyNode::hash_table::bucket::bucket ()
2116 SGPropertyNode::hash_table::bucket::~bucket ()
2118 for (int i = 0; i < _length; i++)
2122 SGPropertyNode::hash_table::entry *
2123 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2126 for (i = 0; i < _length; i++) {
2127 if (!strcmp(_entries[i]->get_key(), key))
2131 entry ** new_entries = new entry*[_length+1];
2132 for (i = 0; i < _length; i++) {
2133 new_entries[i] = _entries[i];
2136 _entries = new_entries;
2137 _entries[_length] = new entry;
2138 _entries[_length]->set_key(key);
2140 return _entries[_length - 1];
2147 SGPropertyNode::hash_table::hash_table ()
2153 SGPropertyNode::hash_table::~hash_table ()
2155 for (unsigned int i = 0; i < _data_length; i++)
2160 SGPropertyNode::hash_table::get (const char * key)
2162 if (_data_length == 0)
2164 unsigned int index = hashcode(key) % _data_length;
2165 if (_data[index] == 0)
2167 entry * e = _data[index]->get_entry(key);
2171 return e->get_value();
2175 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2177 if (_data_length == 0) {
2178 _data = new bucket*[HASH_TABLE_SIZE];
2179 _data_length = HASH_TABLE_SIZE;
2180 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2183 unsigned int index = hashcode(key) % _data_length;
2184 if (_data[index] == 0) {
2185 _data[index] = new bucket;
2187 entry * e = _data[index]->get_entry(key, true);
2188 e->set_value(value);
2192 SGPropertyNode::hash_table::hashcode (const char * key)
2194 unsigned int hash = 0;
2196 hash = 31 * hash + *key;
2205 * Default constructor
2207 SGPropertyNode_ptr::SGPropertyNode_ptr()
2215 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2219 _ptr->incrementRef();
2223 * Constructor from a pointer to a node
2225 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2229 _ptr->incrementRef();
2235 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2237 if (_ptr && _ptr->decrementRef() == 0)
2242 * Assignement operator
2244 SGPropertyNode_ptr &
2245 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2247 if (_ptr && _ptr->decrementRef() == 0)
2251 _ptr->incrementRef();
2257 * Pointer access operator
2260 SGPropertyNode_ptr::operator->()
2266 * Pointer access operator (const)
2268 const SGPropertyNode *
2269 SGPropertyNode_ptr::operator->() const
2275 * Conversion to SGPropertyNode * operator
2277 SGPropertyNode_ptr::operator SGPropertyNode *()
2283 * Conversion to const SGPropertyNode * operator
2285 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2294 SGPropertyNode_ptr::valid() const
2301 ////////////////////////////////////////////////////////////////////////
2302 // Implementation of SGPropertyChangeListener.
2303 ////////////////////////////////////////////////////////////////////////
2305 SGPropertyChangeListener::~SGPropertyChangeListener ()
2307 // This will come back and remove
2308 // the current item each time. Is
2310 vector<SGPropertyNode *>::iterator it;
2311 for (it = _properties.begin(); it != _properties.end(); it++)
2312 (*it)->removeChangeListener(this);
2316 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2322 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2323 SGPropertyNode * child)
2329 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2330 SGPropertyNode * child)
2336 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2338 _properties.push_back(node);
2342 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2344 vector<SGPropertyNode *>::iterator it =
2345 find(_properties.begin(), _properties.end(), node);
2346 if (it != _properties.end())
2347 _properties.erase(it);