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>
33 #if ( _MSC_VER == 1200 )
34 // MSVC 6 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 template<> const bool SGRawValue<bool>::DefaultValue = false;
75 template<> const int SGRawValue<int>::DefaultValue = 0;
76 template<> const long SGRawValue<long>::DefaultValue = 0L;
77 template<> const float SGRawValue<float>::DefaultValue = 0.0;
78 template<> const double SGRawValue<double>::DefaultValue = 0.0L;
79 template<> 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("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()) {
278 return (current->getAttribute(SGPropertyNode::REMOVED) ? 0 : current);
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::clearValue ()
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("")),
647 _local_val.string_val = 0;
654 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
657 _parent(0), // don't copy the parent
664 _listeners(0) // CHECK!!
666 _name = copy_string(node._name);
667 _local_val.string_val = 0;
672 _value.alias = node._value.alias;
678 _value.bool_val = node._value.bool_val->clone();
681 set_bool(node.get_bool());
687 _value.int_val = node._value.int_val->clone();
690 set_int(node.get_int());
696 _value.long_val = node._value.long_val->clone();
699 set_long(node.get_long());
705 _value.float_val = node._value.float_val->clone();
708 set_float(node.get_float());
714 _value.double_val = node._value.double_val->clone();
717 set_double(node.get_double());
724 _value.string_val = node._value.string_val->clone();
727 set_string(node.get_string());
735 * Convenience constructor.
737 SGPropertyNode::SGPropertyNode (const char * name,
739 SGPropertyNode * parent)
751 _name = copy_string(name);
752 _local_val.string_val = 0;
759 SGPropertyNode::~SGPropertyNode ()
762 delete [] _display_name;
771 * Alias to another node.
774 SGPropertyNode::alias (SGPropertyNode * target)
776 if (target == 0 || _type == ALIAS || _tied)
779 _value.alias = target;
781 _value.alias->incrementRef();
787 * Alias to another node by path.
790 SGPropertyNode::alias (const char * path)
792 return alias(getNode(path, true));
800 SGPropertyNode::unalias ()
805 _value.alias->decrementRef();
812 * Get the target of an alias.
815 SGPropertyNode::getAliasTarget ()
817 return (_type == ALIAS ? _value.alias : 0);
821 const SGPropertyNode *
822 SGPropertyNode::getAliasTarget () const
824 return (_type == ALIAS ? _value.alias : 0);
829 * Get a non-const child by index.
832 SGPropertyNode::getChild (int position)
834 if (position >= 0 && position < nChildren())
835 return _children[position];
842 * Get a const child by index.
844 const SGPropertyNode *
845 SGPropertyNode::getChild (int position) const
847 if (position >= 0 && position < nChildren())
848 return _children[position];
855 * Get a non-const child by name and index, creating if necessary.
858 SGPropertyNode::getChild (const char * name, int index, bool create)
860 int pos = find_child(name, index, _children);
862 return _children[pos];
864 SGPropertyNode_ptr node;
865 pos = find_child(name, index, _removedChildren);
867 vector<SGPropertyNode_ptr>::iterator it = _removedChildren.begin();
869 node = _removedChildren[pos];
870 _removedChildren.erase(it);
871 node->setAttribute(REMOVED, false);
873 node = new SGPropertyNode(name, index, this);
875 _children.push_back(node);
876 fireChildAdded(node);
885 * Get a const child by name and index.
887 const SGPropertyNode *
888 SGPropertyNode::getChild (const char * name, int index) const
890 int pos = find_child(name, index, _children);
892 return _children[pos];
899 * Get all children with the same name (but different indices).
901 vector<SGPropertyNode_ptr>
902 SGPropertyNode::getChildren (const char * name) const
904 vector<SGPropertyNode_ptr> children;
905 int max = _children.size();
907 for (int i = 0; i < max; i++)
908 if (compare_strings(_children[i]->getName(), name))
909 children.push_back(_children[i]);
911 sort(children.begin(), children.end(), CompareIndices());
917 * Remove a child node, and all children that aren't referenced.
918 * "keep" does only apply to the outmost item. Returns "true" if
919 * not all nodes could be removed.
922 SGPropertyNode::removeChild (const char * name, int index, bool keep)
925 int pos = find_child(name, index, _children);
927 vector<SGPropertyNode_ptr>::iterator it = _children.begin();
929 SGPropertyNode *node = _children[pos];
930 if (node->nChildren() && node->removeChildren())
933 if (node->isTied() || node->_count != 1 || node->nChildren())
937 _removedChildren.push_back(node);
940 _path_cache->erase(name); // EMH - TODO: Take "index" into account!
942 node->setAttribute(REMOVED, true);
944 fireChildRemoved(node);
952 * Remove all children nodes, or all with a given name. Returns
953 * "true" if not all nodes could be removed.
956 SGPropertyNode::removeChildren(const char *name)
959 vector<SGPropertyNode_ptr>::iterator it = _children.end();
960 vector<SGPropertyNode_ptr>::iterator begin = _children.begin();
961 while (it-- != begin) {
962 SGPropertyNode *node = *it;
963 if (name && !compare_strings(node->getName(), name))
966 if (node->nChildren() && node->removeChildren())
969 if (node->isTied() || node->_count != 1 || node->nChildren())
973 _path_cache->erase(node->getName());
975 node->setAttribute(REMOVED, true);
977 fireChildRemoved(node);
986 SGPropertyNode::getDisplayName (bool simplify) const
988 string display = _name;
989 if (_index != 0 || !simplify) {
991 sprintf(buffer, "[%d]", _index);
994 _display_name = copy_string(display.c_str());
995 return _display_name;
1000 SGPropertyNode::getPath (bool simplify) const
1002 // Calculate the complete path only once.
1008 path = _parent->getPath(simplify);
1010 path += getDisplayName(simplify);
1012 _path = copy_string(path.c_str());
1018 SGPropertyNode::Type
1019 SGPropertyNode::getType () const
1022 return _value.alias->getType();
1029 SGPropertyNode::getBoolValue () const
1031 // Shortcut for common case
1032 if (_attr == (READ|WRITE) && _type == BOOL)
1035 if (getAttribute(TRACE_READ))
1037 if (!getAttribute(READ))
1038 return SGRawValue<bool>::DefaultValue;
1041 return _value.alias->getBoolValue();
1045 return get_int() == 0 ? false : true;
1047 return get_long() == 0L ? false : true;
1049 return get_float() == 0.0 ? false : true;
1051 return get_double() == 0.0L ? false : true;
1054 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1057 return SGRawValue<bool>::DefaultValue;
1062 SGPropertyNode::getIntValue () const
1064 // Shortcut for common case
1065 if (_attr == (READ|WRITE) && _type == INT)
1068 if (getAttribute(TRACE_READ))
1070 if (!getAttribute(READ))
1071 return SGRawValue<int>::DefaultValue;
1074 return _value.alias->getIntValue();
1076 return int(get_bool());
1080 return int(get_long());
1082 return int(get_float());
1084 return int(get_double());
1087 return atoi(get_string());
1090 return SGRawValue<int>::DefaultValue;
1095 SGPropertyNode::getLongValue () const
1097 // Shortcut for common case
1098 if (_attr == (READ|WRITE) && _type == LONG)
1101 if (getAttribute(TRACE_READ))
1103 if (!getAttribute(READ))
1104 return SGRawValue<long>::DefaultValue;
1107 return _value.alias->getLongValue();
1109 return long(get_bool());
1111 return long(get_int());
1115 return long(get_float());
1117 return long(get_double());
1120 return strtol(get_string(), 0, 0);
1123 return SGRawValue<long>::DefaultValue;
1128 SGPropertyNode::getFloatValue () const
1130 // Shortcut for common case
1131 if (_attr == (READ|WRITE) && _type == FLOAT)
1134 if (getAttribute(TRACE_READ))
1136 if (!getAttribute(READ))
1137 return SGRawValue<float>::DefaultValue;
1140 return _value.alias->getFloatValue();
1142 return float(get_bool());
1144 return float(get_int());
1146 return float(get_long());
1150 return float(get_double());
1153 return atof(get_string());
1156 return SGRawValue<float>::DefaultValue;
1161 SGPropertyNode::getDoubleValue () const
1163 // Shortcut for common case
1164 if (_attr == (READ|WRITE) && _type == DOUBLE)
1165 return get_double();
1167 if (getAttribute(TRACE_READ))
1169 if (!getAttribute(READ))
1170 return SGRawValue<double>::DefaultValue;
1174 return _value.alias->getDoubleValue();
1176 return double(get_bool());
1178 return double(get_int());
1180 return double(get_long());
1182 return double(get_float());
1184 return get_double();
1187 return strtod(get_string(), 0);
1190 return SGRawValue<double>::DefaultValue;
1195 SGPropertyNode::getStringValue () const
1197 // Shortcut for common case
1198 if (_attr == (READ|WRITE) && _type == STRING)
1199 return get_string();
1201 if (getAttribute(TRACE_READ))
1203 if (!getAttribute(READ))
1204 return SGRawValue<const char *>::DefaultValue;
1205 return make_string();
1209 SGPropertyNode::setBoolValue (bool value)
1211 // Shortcut for common case
1212 if (_attr == (READ|WRITE) && _type == BOOL)
1213 return set_bool(value);
1215 bool result = false;
1217 if (_type == NONE || _type == UNSPECIFIED) {
1225 result = _value.alias->setBoolValue(value);
1228 result = set_bool(value);
1231 result = set_int(int(value));
1234 result = set_long(long(value));
1237 result = set_float(float(value));
1240 result = set_double(double(value));
1244 result = set_string(value ? "true" : "false");
1251 if (getAttribute(TRACE_WRITE))
1257 SGPropertyNode::setIntValue (int value)
1259 // Shortcut for common case
1260 if (_attr == (READ|WRITE) && _type == INT)
1261 return set_int(value);
1263 bool result = false;
1265 if (_type == NONE || _type == UNSPECIFIED) {
1268 _local_val.int_val = 0;
1273 result = _value.alias->setIntValue(value);
1276 result = set_bool(value == 0 ? false : true);
1279 result = set_int(value);
1282 result = set_long(long(value));
1285 result = set_float(float(value));
1288 result = set_double(double(value));
1293 sprintf(buf, "%d", value);
1294 result = set_string(buf);
1302 if (getAttribute(TRACE_WRITE))
1308 SGPropertyNode::setLongValue (long value)
1310 // Shortcut for common case
1311 if (_attr == (READ|WRITE) && _type == LONG)
1312 return set_long(value);
1314 bool result = false;
1316 if (_type == NONE || _type == UNSPECIFIED) {
1319 _local_val.long_val = 0L;
1324 result = _value.alias->setLongValue(value);
1327 result = set_bool(value == 0L ? false : true);
1330 result = set_int(int(value));
1333 result = set_long(value);
1336 result = set_float(float(value));
1339 result = set_double(double(value));
1344 sprintf(buf, "%ld", value);
1345 result = set_string(buf);
1353 if (getAttribute(TRACE_WRITE))
1359 SGPropertyNode::setFloatValue (float value)
1361 // Shortcut for common case
1362 if (_attr == (READ|WRITE) && _type == FLOAT)
1363 return set_float(value);
1365 bool result = false;
1367 if (_type == NONE || _type == UNSPECIFIED) {
1370 _local_val.float_val = 0;
1375 result = _value.alias->setFloatValue(value);
1378 result = set_bool(value == 0.0 ? false : true);
1381 result = set_int(int(value));
1384 result = set_long(long(value));
1387 result = set_float(value);
1390 result = set_double(double(value));
1395 sprintf(buf, "%f", value);
1396 result = set_string(buf);
1404 if (getAttribute(TRACE_WRITE))
1410 SGPropertyNode::setDoubleValue (double value)
1412 // Shortcut for common case
1413 if (_attr == (READ|WRITE) && _type == DOUBLE)
1414 return set_double(value);
1416 bool result = false;
1418 if (_type == NONE || _type == UNSPECIFIED) {
1420 _local_val.double_val = value;
1426 result = _value.alias->setDoubleValue(value);
1429 result = set_bool(value == 0.0L ? false : true);
1432 result = set_int(int(value));
1435 result = set_long(long(value));
1438 result = set_float(float(value));
1441 result = set_double(value);
1446 sprintf(buf, "%f", value);
1447 result = set_string(buf);
1455 if (getAttribute(TRACE_WRITE))
1461 SGPropertyNode::setStringValue (const char * value)
1463 // Shortcut for common case
1464 if (_attr == (READ|WRITE) && _type == STRING)
1465 return set_string(value);
1467 bool result = false;
1469 if (_type == NONE || _type == UNSPECIFIED) {
1476 result = _value.alias->setStringValue(value);
1479 result = set_bool((compare_strings(value, "true")
1480 || atoi(value)) ? true : false);
1483 result = set_int(atoi(value));
1486 result = set_long(strtol(value, 0, 0));
1489 result = set_float(atof(value));
1492 result = set_double(strtod(value, 0));
1496 result = set_string(value);
1503 if (getAttribute(TRACE_WRITE))
1509 SGPropertyNode::setUnspecifiedValue (const char * value)
1511 bool result = false;
1513 if (_type == NONE) {
1515 _type = UNSPECIFIED;
1520 result = _value.alias->setUnspecifiedValue(value);
1523 result = set_bool((compare_strings(value, "true")
1524 || atoi(value)) ? true : false);
1527 result = set_int(atoi(value));
1530 result = set_long(strtol(value, 0, 0));
1533 result = set_float(atof(value));
1536 result = set_double(strtod(value, 0));
1540 result = set_string(value);
1547 if (getAttribute(TRACE_WRITE))
1553 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1555 if (_type == ALIAS || _tied)
1558 useDefault = useDefault && hasValue();
1559 bool old_val = false;
1561 old_val = getBoolValue();
1566 _value.bool_val = rawValue.clone();
1569 setBoolValue(old_val);
1575 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1577 if (_type == ALIAS || _tied)
1580 useDefault = useDefault && hasValue();
1583 old_val = getIntValue();
1588 _value.int_val = rawValue.clone();
1591 setIntValue(old_val);
1597 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1599 if (_type == ALIAS || _tied)
1602 useDefault = useDefault && hasValue();
1605 old_val = getLongValue();
1610 _value.long_val = rawValue.clone();
1613 setLongValue(old_val);
1619 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1621 if (_type == ALIAS || _tied)
1624 useDefault = useDefault && hasValue();
1625 float old_val = 0.0;
1627 old_val = getFloatValue();
1632 _value.float_val = rawValue.clone();
1635 setFloatValue(old_val);
1641 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1643 if (_type == ALIAS || _tied)
1646 useDefault = useDefault && hasValue();
1647 double old_val = 0.0;
1649 old_val = getDoubleValue();
1654 _value.double_val = rawValue.clone();
1657 setDoubleValue(old_val);
1664 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1666 if (_type == ALIAS || _tied)
1669 useDefault = useDefault && hasValue();
1672 old_val = getStringValue();
1677 _value.string_val = rawValue.clone();
1680 setStringValue(old_val.c_str());
1686 SGPropertyNode::untie ()
1693 bool val = getBoolValue();
1696 _local_val.bool_val = val;
1700 int val = getIntValue();
1703 _local_val.int_val = val;
1707 long val = getLongValue();
1710 _local_val.long_val = val;
1714 float val = getFloatValue();
1717 _local_val.float_val = val;
1721 double val = getDoubleValue();
1724 _local_val.double_val = val;
1729 string val = getStringValue();
1732 _local_val.string_val = copy_string(val.c_str());
1745 SGPropertyNode::getRootNode ()
1750 return _parent->getRootNode();
1753 const SGPropertyNode *
1754 SGPropertyNode::getRootNode () const
1759 return _parent->getRootNode();
1763 SGPropertyNode::getNode (const char * relative_path, bool create)
1765 if (_path_cache == 0)
1766 _path_cache = new hash_table;
1768 SGPropertyNode * result = _path_cache->get(relative_path);
1770 vector<PathComponent> components;
1771 parse_path(relative_path, components);
1772 result = find_node(this, components, 0, create);
1774 _path_cache->put(relative_path, result);
1781 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1783 vector<PathComponent> components;
1784 parse_path(relative_path, components);
1785 if (components.size() > 0)
1786 components.back().index = index;
1787 return find_node(this, components, 0, create);
1790 const SGPropertyNode *
1791 SGPropertyNode::getNode (const char * relative_path) const
1793 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1796 const SGPropertyNode *
1797 SGPropertyNode::getNode (const char * relative_path, int index) const
1799 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1803 ////////////////////////////////////////////////////////////////////////
1804 // Convenience methods using relative paths.
1805 ////////////////////////////////////////////////////////////////////////
1809 * Test whether another node has a value attached.
1812 SGPropertyNode::hasValue (const char * relative_path) const
1814 const SGPropertyNode * node = getNode(relative_path);
1815 return (node == 0 ? false : node->hasValue());
1820 * Get the value type for another node.
1822 SGPropertyNode::Type
1823 SGPropertyNode::getType (const char * relative_path) const
1825 const SGPropertyNode * node = getNode(relative_path);
1826 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1831 * Get a bool value for another node.
1834 SGPropertyNode::getBoolValue (const char * relative_path,
1835 bool defaultValue) const
1837 const SGPropertyNode * node = getNode(relative_path);
1838 return (node == 0 ? defaultValue : node->getBoolValue());
1843 * Get an int value for another node.
1846 SGPropertyNode::getIntValue (const char * relative_path,
1847 int defaultValue) const
1849 const SGPropertyNode * node = getNode(relative_path);
1850 return (node == 0 ? defaultValue : node->getIntValue());
1855 * Get a long value for another node.
1858 SGPropertyNode::getLongValue (const char * relative_path,
1859 long defaultValue) const
1861 const SGPropertyNode * node = getNode(relative_path);
1862 return (node == 0 ? defaultValue : node->getLongValue());
1867 * Get a float value for another node.
1870 SGPropertyNode::getFloatValue (const char * relative_path,
1871 float defaultValue) const
1873 const SGPropertyNode * node = getNode(relative_path);
1874 return (node == 0 ? defaultValue : node->getFloatValue());
1879 * Get a double value for another node.
1882 SGPropertyNode::getDoubleValue (const char * relative_path,
1883 double defaultValue) const
1885 const SGPropertyNode * node = getNode(relative_path);
1886 return (node == 0 ? defaultValue : node->getDoubleValue());
1891 * Get a string value for another node.
1894 SGPropertyNode::getStringValue (const char * relative_path,
1895 const char * defaultValue) const
1897 const SGPropertyNode * node = getNode(relative_path);
1898 return (node == 0 ? defaultValue : node->getStringValue());
1903 * Set a bool value for another node.
1906 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1908 return getNode(relative_path, true)->setBoolValue(value);
1913 * Set an int value for another node.
1916 SGPropertyNode::setIntValue (const char * relative_path, int value)
1918 return getNode(relative_path, true)->setIntValue(value);
1923 * Set a long value for another node.
1926 SGPropertyNode::setLongValue (const char * relative_path, long value)
1928 return getNode(relative_path, true)->setLongValue(value);
1933 * Set a float value for another node.
1936 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1938 return getNode(relative_path, true)->setFloatValue(value);
1943 * Set a double value for another node.
1946 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1948 return getNode(relative_path, true)->setDoubleValue(value);
1953 * Set a string value for another node.
1956 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1958 return getNode(relative_path, true)->setStringValue(value);
1963 * Set an unknown value for another node.
1966 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1969 return getNode(relative_path, true)->setUnspecifiedValue(value);
1974 * Test whether another node is tied.
1977 SGPropertyNode::isTied (const char * relative_path) const
1979 const SGPropertyNode * node = getNode(relative_path);
1980 return (node == 0 ? false : node->isTied());
1985 * Tie a node reached by a relative path, creating it if necessary.
1988 SGPropertyNode::tie (const char * relative_path,
1989 const SGRawValue<bool> &rawValue,
1992 return getNode(relative_path, true)->tie(rawValue, useDefault);
1997 * Tie a node reached by a relative path, creating it if necessary.
2000 SGPropertyNode::tie (const char * relative_path,
2001 const SGRawValue<int> &rawValue,
2004 return getNode(relative_path, true)->tie(rawValue, useDefault);
2009 * Tie a node reached by a relative path, creating it if necessary.
2012 SGPropertyNode::tie (const char * relative_path,
2013 const SGRawValue<long> &rawValue,
2016 return getNode(relative_path, true)->tie(rawValue, useDefault);
2021 * Tie a node reached by a relative path, creating it if necessary.
2024 SGPropertyNode::tie (const char * relative_path,
2025 const SGRawValue<float> &rawValue,
2028 return getNode(relative_path, true)->tie(rawValue, useDefault);
2033 * Tie a node reached by a relative path, creating it if necessary.
2036 SGPropertyNode::tie (const char * relative_path,
2037 const SGRawValue<double> &rawValue,
2040 return getNode(relative_path, true)->tie(rawValue, useDefault);
2045 * Tie a node reached by a relative path, creating it if necessary.
2048 SGPropertyNode::tie (const char * relative_path,
2049 const SGRawValue<const char *> &rawValue,
2052 return getNode(relative_path, true)->tie(rawValue, useDefault);
2057 * Attempt to untie another node reached by a relative path.
2060 SGPropertyNode::untie (const char * relative_path)
2062 SGPropertyNode * node = getNode(relative_path);
2063 return (node == 0 ? false : node->untie());
2067 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
2069 if (_listeners == 0)
2070 _listeners = new vector<SGPropertyChangeListener *>;
2071 _listeners->push_back(listener);
2072 listener->register_property(this);
2076 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2078 vector<SGPropertyChangeListener *>::iterator it =
2079 find(_listeners->begin(), _listeners->end(), listener);
2080 if (it != _listeners->end()) {
2081 _listeners->erase(it);
2082 listener->unregister_property(this);
2083 if (_listeners->empty()) {
2084 vector<SGPropertyChangeListener *> * tmp = _listeners;
2092 SGPropertyNode::fireValueChanged ()
2094 fireValueChanged(this);
2098 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2100 fireChildAdded(this, child);
2104 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2106 fireChildRemoved(this, child);
2110 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2112 if (_listeners != 0) {
2113 for (unsigned int i = 0; i < _listeners->size(); i++) {
2114 (*_listeners)[i]->valueChanged(node);
2118 _parent->fireValueChanged(node);
2122 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2123 SGPropertyNode * child)
2125 if (_listeners != 0) {
2126 for (unsigned int i = 0; i < _listeners->size(); i++) {
2127 (*_listeners)[i]->childAdded(parent, child);
2131 _parent->fireChildAdded(parent, child);
2135 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2136 SGPropertyNode * child)
2138 if (_listeners != 0) {
2139 for (unsigned int i = 0; i < _listeners->size(); i++) {
2140 (*_listeners)[i]->childRemoved(parent, child);
2144 _parent->fireChildRemoved(parent, child);
2149 ////////////////////////////////////////////////////////////////////////
2150 // Simplified hash table for caching paths.
2151 ////////////////////////////////////////////////////////////////////////
2153 #define HASH_TABLE_SIZE 199
2155 SGPropertyNode::hash_table::entry::entry ()
2161 SGPropertyNode::hash_table::entry::~entry ()
2163 // Don't delete the value; we don't own
2169 SGPropertyNode::hash_table::entry::set_key (const char * key)
2171 _key = copy_string(key);
2175 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2180 SGPropertyNode::hash_table::bucket::bucket ()
2186 SGPropertyNode::hash_table::bucket::~bucket ()
2188 for (int i = 0; i < _length; i++)
2192 SGPropertyNode::hash_table::entry *
2193 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2196 for (i = 0; i < _length; i++) {
2197 if (!strcmp(_entries[i]->get_key(), key))
2201 entry ** new_entries = new entry*[_length+1];
2202 for (i = 0; i < _length; i++) {
2203 new_entries[i] = _entries[i];
2206 _entries = new_entries;
2207 _entries[_length] = new entry;
2208 _entries[_length]->set_key(key);
2210 return _entries[_length - 1];
2217 SGPropertyNode::hash_table::bucket::erase (const char * key)
2220 for (i = 0; i < _length; i++) {
2221 if (!strcmp(_entries[i]->get_key(), key))
2226 for (++i; i < _length; i++) {
2227 _entries[i-1] = _entries[i];
2234 SGPropertyNode::hash_table::hash_table ()
2240 SGPropertyNode::hash_table::~hash_table ()
2242 for (unsigned int i = 0; i < _data_length; i++)
2247 SGPropertyNode::hash_table::get (const char * key)
2249 if (_data_length == 0)
2251 unsigned int index = hashcode(key) % _data_length;
2252 if (_data[index] == 0)
2254 entry * e = _data[index]->get_entry(key);
2258 return e->get_value();
2262 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2264 if (_data_length == 0) {
2265 _data = new bucket*[HASH_TABLE_SIZE];
2266 _data_length = HASH_TABLE_SIZE;
2267 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2270 unsigned int index = hashcode(key) % _data_length;
2271 if (_data[index] == 0) {
2272 _data[index] = new bucket;
2274 entry * e = _data[index]->get_entry(key, true);
2275 e->set_value(value);
2279 SGPropertyNode::hash_table::erase (const char * key)
2281 if (_data_length == 0)
2283 unsigned int index = hashcode(key) % _data_length;
2284 if (_data[index] == 0)
2286 _data[index]->erase(key);
2290 SGPropertyNode::hash_table::hashcode (const char * key)
2292 unsigned int hash = 0;
2294 hash = 31 * hash + *key;
2303 * Default constructor
2305 SGPropertyNode_ptr::SGPropertyNode_ptr()
2313 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2317 _ptr->incrementRef();
2321 * Constructor from a pointer to a node
2323 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2327 _ptr->incrementRef();
2333 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2335 if (_ptr && _ptr->decrementRef() == 0)
2340 * Assignement operator
2342 SGPropertyNode_ptr &
2343 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2345 if (_ptr && _ptr->decrementRef() == 0)
2349 _ptr->incrementRef();
2355 * Pointer access operator
2358 SGPropertyNode_ptr::operator->()
2364 * Pointer access operator (const)
2366 const SGPropertyNode *
2367 SGPropertyNode_ptr::operator->() const
2373 * Conversion to SGPropertyNode * operator
2375 SGPropertyNode_ptr::operator SGPropertyNode *()
2381 * Conversion to const SGPropertyNode * operator
2383 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2392 SGPropertyNode_ptr::valid() const
2399 ////////////////////////////////////////////////////////////////////////
2400 // Implementation of SGPropertyChangeListener.
2401 ////////////////////////////////////////////////////////////////////////
2403 SGPropertyChangeListener::~SGPropertyChangeListener ()
2405 // This will come back and remove
2406 // the current item each time. Is
2408 vector<SGPropertyNode *>::iterator it;
2409 for (it = _properties.begin(); it != _properties.end(); it++)
2410 (*it)->removeChangeListener(this);
2414 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2420 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2421 SGPropertyNode * child)
2427 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2428 SGPropertyNode * child)
2434 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2436 _properties.push_back(node);
2440 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2442 vector<SGPropertyNode *>::iterator it =
2443 find(_properties.begin(), _properties.end(), node);
2444 if (it != _properties.end())
2445 _properties.erase(it);