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 * Detach a child node from the tree and return a guarded pointer to it.
920 SGPropertyNode::detachChild (const char * name, int index, bool keep)
922 SGPropertyNode_ptr ret;
923 int pos = find_child(name, index, _children);
925 vector<SGPropertyNode_ptr>::iterator it = _children.begin();
927 SGPropertyNode_ptr node = _children[pos];
930 _removedChildren.push_back(node);
933 _path_cache->erase(name); // EMH - TODO: Take "index" into account!
934 node->setAttribute(REMOVED, true);
937 fireChildRemoved(node);
944 * Remove a child node, and all children that aren't referenced.
945 * Returns "true" if the node and all subnodes could be removed.
948 SGPropertyNode::removeChild (const char * name, int index)
950 int pos = find_child(name, index, _children);
952 vector<SGPropertyNode_ptr>::iterator it = _children.begin();
954 SGPropertyNode *node = _children[pos];
955 if (node->_count != 1 || node->isTied() || !node->removeChildren())
959 _path_cache->erase(name); // EMH - TODO: Take "index" into account!
961 node->setAttribute(REMOVED, true);
963 fireChildRemoved(node);
971 * Remove all children nodes, or all with a given name.
972 * Returns "true" if the node and all subnodes could be removed.
975 SGPropertyNode::removeChildren(const char *name)
978 vector<SGPropertyNode_ptr>::iterator it = _children.end();
979 vector<SGPropertyNode_ptr>::iterator begin = _children.begin();
980 while (it-- != begin) {
981 SGPropertyNode *node = *it;
982 if (name && !compare_strings(node->getName(), name))
985 if (node->_count != 1 || node->isTied() || !node->removeChildren())
989 _path_cache->erase(node->getName());
991 node->setAttribute(REMOVED, true);
993 fireChildRemoved(node);
1002 SGPropertyNode::getDisplayName (bool simplify) const
1004 string display = _name;
1005 if (_index != 0 || !simplify) {
1007 sprintf(buffer, "[%d]", _index);
1010 _display_name = copy_string(display.c_str());
1011 return _display_name;
1016 SGPropertyNode::getPath (bool simplify) const
1018 // Calculate the complete path only once.
1024 path = _parent->getPath(simplify);
1026 path += getDisplayName(simplify);
1028 _path = copy_string(path.c_str());
1034 SGPropertyNode::Type
1035 SGPropertyNode::getType () const
1038 return _value.alias->getType();
1045 SGPropertyNode::getBoolValue () const
1047 // Shortcut for common case
1048 if (_attr == (READ|WRITE) && _type == BOOL)
1051 if (getAttribute(TRACE_READ))
1053 if (!getAttribute(READ))
1054 return SGRawValue<bool>::DefaultValue;
1057 return _value.alias->getBoolValue();
1061 return get_int() == 0 ? false : true;
1063 return get_long() == 0L ? false : true;
1065 return get_float() == 0.0 ? false : true;
1067 return get_double() == 0.0L ? false : true;
1070 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1073 return SGRawValue<bool>::DefaultValue;
1078 SGPropertyNode::getIntValue () const
1080 // Shortcut for common case
1081 if (_attr == (READ|WRITE) && _type == INT)
1084 if (getAttribute(TRACE_READ))
1086 if (!getAttribute(READ))
1087 return SGRawValue<int>::DefaultValue;
1090 return _value.alias->getIntValue();
1092 return int(get_bool());
1096 return int(get_long());
1098 return int(get_float());
1100 return int(get_double());
1103 return atoi(get_string());
1106 return SGRawValue<int>::DefaultValue;
1111 SGPropertyNode::getLongValue () const
1113 // Shortcut for common case
1114 if (_attr == (READ|WRITE) && _type == LONG)
1117 if (getAttribute(TRACE_READ))
1119 if (!getAttribute(READ))
1120 return SGRawValue<long>::DefaultValue;
1123 return _value.alias->getLongValue();
1125 return long(get_bool());
1127 return long(get_int());
1131 return long(get_float());
1133 return long(get_double());
1136 return strtol(get_string(), 0, 0);
1139 return SGRawValue<long>::DefaultValue;
1144 SGPropertyNode::getFloatValue () const
1146 // Shortcut for common case
1147 if (_attr == (READ|WRITE) && _type == FLOAT)
1150 if (getAttribute(TRACE_READ))
1152 if (!getAttribute(READ))
1153 return SGRawValue<float>::DefaultValue;
1156 return _value.alias->getFloatValue();
1158 return float(get_bool());
1160 return float(get_int());
1162 return float(get_long());
1166 return float(get_double());
1169 return atof(get_string());
1172 return SGRawValue<float>::DefaultValue;
1177 SGPropertyNode::getDoubleValue () const
1179 // Shortcut for common case
1180 if (_attr == (READ|WRITE) && _type == DOUBLE)
1181 return get_double();
1183 if (getAttribute(TRACE_READ))
1185 if (!getAttribute(READ))
1186 return SGRawValue<double>::DefaultValue;
1190 return _value.alias->getDoubleValue();
1192 return double(get_bool());
1194 return double(get_int());
1196 return double(get_long());
1198 return double(get_float());
1200 return get_double();
1203 return strtod(get_string(), 0);
1206 return SGRawValue<double>::DefaultValue;
1211 SGPropertyNode::getStringValue () const
1213 // Shortcut for common case
1214 if (_attr == (READ|WRITE) && _type == STRING)
1215 return get_string();
1217 if (getAttribute(TRACE_READ))
1219 if (!getAttribute(READ))
1220 return SGRawValue<const char *>::DefaultValue;
1221 return make_string();
1225 SGPropertyNode::setBoolValue (bool value)
1227 // Shortcut for common case
1228 if (_attr == (READ|WRITE) && _type == BOOL)
1229 return set_bool(value);
1231 bool result = false;
1233 if (_type == NONE || _type == UNSPECIFIED) {
1241 result = _value.alias->setBoolValue(value);
1244 result = set_bool(value);
1247 result = set_int(int(value));
1250 result = set_long(long(value));
1253 result = set_float(float(value));
1256 result = set_double(double(value));
1260 result = set_string(value ? "true" : "false");
1267 if (getAttribute(TRACE_WRITE))
1273 SGPropertyNode::setIntValue (int value)
1275 // Shortcut for common case
1276 if (_attr == (READ|WRITE) && _type == INT)
1277 return set_int(value);
1279 bool result = false;
1281 if (_type == NONE || _type == UNSPECIFIED) {
1284 _local_val.int_val = 0;
1289 result = _value.alias->setIntValue(value);
1292 result = set_bool(value == 0 ? false : true);
1295 result = set_int(value);
1298 result = set_long(long(value));
1301 result = set_float(float(value));
1304 result = set_double(double(value));
1309 sprintf(buf, "%d", value);
1310 result = set_string(buf);
1318 if (getAttribute(TRACE_WRITE))
1324 SGPropertyNode::setLongValue (long value)
1326 // Shortcut for common case
1327 if (_attr == (READ|WRITE) && _type == LONG)
1328 return set_long(value);
1330 bool result = false;
1332 if (_type == NONE || _type == UNSPECIFIED) {
1335 _local_val.long_val = 0L;
1340 result = _value.alias->setLongValue(value);
1343 result = set_bool(value == 0L ? false : true);
1346 result = set_int(int(value));
1349 result = set_long(value);
1352 result = set_float(float(value));
1355 result = set_double(double(value));
1360 sprintf(buf, "%ld", value);
1361 result = set_string(buf);
1369 if (getAttribute(TRACE_WRITE))
1375 SGPropertyNode::setFloatValue (float value)
1377 // Shortcut for common case
1378 if (_attr == (READ|WRITE) && _type == FLOAT)
1379 return set_float(value);
1381 bool result = false;
1383 if (_type == NONE || _type == UNSPECIFIED) {
1386 _local_val.float_val = 0;
1391 result = _value.alias->setFloatValue(value);
1394 result = set_bool(value == 0.0 ? false : true);
1397 result = set_int(int(value));
1400 result = set_long(long(value));
1403 result = set_float(value);
1406 result = set_double(double(value));
1411 sprintf(buf, "%f", value);
1412 result = set_string(buf);
1420 if (getAttribute(TRACE_WRITE))
1426 SGPropertyNode::setDoubleValue (double value)
1428 // Shortcut for common case
1429 if (_attr == (READ|WRITE) && _type == DOUBLE)
1430 return set_double(value);
1432 bool result = false;
1434 if (_type == NONE || _type == UNSPECIFIED) {
1436 _local_val.double_val = value;
1442 result = _value.alias->setDoubleValue(value);
1445 result = set_bool(value == 0.0L ? false : true);
1448 result = set_int(int(value));
1451 result = set_long(long(value));
1454 result = set_float(float(value));
1457 result = set_double(value);
1462 sprintf(buf, "%f", value);
1463 result = set_string(buf);
1471 if (getAttribute(TRACE_WRITE))
1477 SGPropertyNode::setStringValue (const char * value)
1479 // Shortcut for common case
1480 if (_attr == (READ|WRITE) && _type == STRING)
1481 return set_string(value);
1483 bool result = false;
1485 if (_type == NONE || _type == UNSPECIFIED) {
1492 result = _value.alias->setStringValue(value);
1495 result = set_bool((compare_strings(value, "true")
1496 || atoi(value)) ? true : false);
1499 result = set_int(atoi(value));
1502 result = set_long(strtol(value, 0, 0));
1505 result = set_float(atof(value));
1508 result = set_double(strtod(value, 0));
1512 result = set_string(value);
1519 if (getAttribute(TRACE_WRITE))
1525 SGPropertyNode::setUnspecifiedValue (const char * value)
1527 bool result = false;
1529 if (_type == NONE) {
1531 _type = UNSPECIFIED;
1536 result = _value.alias->setUnspecifiedValue(value);
1539 result = set_bool((compare_strings(value, "true")
1540 || atoi(value)) ? true : false);
1543 result = set_int(atoi(value));
1546 result = set_long(strtol(value, 0, 0));
1549 result = set_float(atof(value));
1552 result = set_double(strtod(value, 0));
1556 result = set_string(value);
1563 if (getAttribute(TRACE_WRITE))
1569 SGPropertyNode::tie (const SGRawValue<bool> &rawValue, bool useDefault)
1571 if (_type == ALIAS || _tied)
1574 useDefault = useDefault && hasValue();
1575 bool old_val = false;
1577 old_val = getBoolValue();
1582 _value.bool_val = rawValue.clone();
1585 setBoolValue(old_val);
1591 SGPropertyNode::tie (const SGRawValue<int> &rawValue, bool useDefault)
1593 if (_type == ALIAS || _tied)
1596 useDefault = useDefault && hasValue();
1599 old_val = getIntValue();
1604 _value.int_val = rawValue.clone();
1607 setIntValue(old_val);
1613 SGPropertyNode::tie (const SGRawValue<long> &rawValue, bool useDefault)
1615 if (_type == ALIAS || _tied)
1618 useDefault = useDefault && hasValue();
1621 old_val = getLongValue();
1626 _value.long_val = rawValue.clone();
1629 setLongValue(old_val);
1635 SGPropertyNode::tie (const SGRawValue<float> &rawValue, bool useDefault)
1637 if (_type == ALIAS || _tied)
1640 useDefault = useDefault && hasValue();
1641 float old_val = 0.0;
1643 old_val = getFloatValue();
1648 _value.float_val = rawValue.clone();
1651 setFloatValue(old_val);
1657 SGPropertyNode::tie (const SGRawValue<double> &rawValue, bool useDefault)
1659 if (_type == ALIAS || _tied)
1662 useDefault = useDefault && hasValue();
1663 double old_val = 0.0;
1665 old_val = getDoubleValue();
1670 _value.double_val = rawValue.clone();
1673 setDoubleValue(old_val);
1680 SGPropertyNode::tie (const SGRawValue<const char *> &rawValue, bool useDefault)
1682 if (_type == ALIAS || _tied)
1685 useDefault = useDefault && hasValue();
1688 old_val = getStringValue();
1693 _value.string_val = rawValue.clone();
1696 setStringValue(old_val.c_str());
1702 SGPropertyNode::untie ()
1709 bool val = getBoolValue();
1712 _local_val.bool_val = val;
1716 int val = getIntValue();
1719 _local_val.int_val = val;
1723 long val = getLongValue();
1726 _local_val.long_val = val;
1730 float val = getFloatValue();
1733 _local_val.float_val = val;
1737 double val = getDoubleValue();
1740 _local_val.double_val = val;
1745 string val = getStringValue();
1748 _local_val.string_val = copy_string(val.c_str());
1761 SGPropertyNode::getRootNode ()
1766 return _parent->getRootNode();
1769 const SGPropertyNode *
1770 SGPropertyNode::getRootNode () const
1775 return _parent->getRootNode();
1779 SGPropertyNode::getNode (const char * relative_path, bool create)
1781 if (_path_cache == 0)
1782 _path_cache = new hash_table;
1784 SGPropertyNode * result = _path_cache->get(relative_path);
1786 vector<PathComponent> components;
1787 parse_path(relative_path, components);
1788 result = find_node(this, components, 0, create);
1790 _path_cache->put(relative_path, result);
1797 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1799 vector<PathComponent> components;
1800 parse_path(relative_path, components);
1801 if (components.size() > 0)
1802 components.back().index = index;
1803 return find_node(this, components, 0, create);
1806 const SGPropertyNode *
1807 SGPropertyNode::getNode (const char * relative_path) const
1809 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1812 const SGPropertyNode *
1813 SGPropertyNode::getNode (const char * relative_path, int index) const
1815 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1819 ////////////////////////////////////////////////////////////////////////
1820 // Convenience methods using relative paths.
1821 ////////////////////////////////////////////////////////////////////////
1825 * Test whether another node has a value attached.
1828 SGPropertyNode::hasValue (const char * relative_path) const
1830 const SGPropertyNode * node = getNode(relative_path);
1831 return (node == 0 ? false : node->hasValue());
1836 * Get the value type for another node.
1838 SGPropertyNode::Type
1839 SGPropertyNode::getType (const char * relative_path) const
1841 const SGPropertyNode * node = getNode(relative_path);
1842 return (node == 0 ? UNSPECIFIED : (Type)(node->getType()));
1847 * Get a bool value for another node.
1850 SGPropertyNode::getBoolValue (const char * relative_path,
1851 bool defaultValue) const
1853 const SGPropertyNode * node = getNode(relative_path);
1854 return (node == 0 ? defaultValue : node->getBoolValue());
1859 * Get an int value for another node.
1862 SGPropertyNode::getIntValue (const char * relative_path,
1863 int defaultValue) const
1865 const SGPropertyNode * node = getNode(relative_path);
1866 return (node == 0 ? defaultValue : node->getIntValue());
1871 * Get a long value for another node.
1874 SGPropertyNode::getLongValue (const char * relative_path,
1875 long defaultValue) const
1877 const SGPropertyNode * node = getNode(relative_path);
1878 return (node == 0 ? defaultValue : node->getLongValue());
1883 * Get a float value for another node.
1886 SGPropertyNode::getFloatValue (const char * relative_path,
1887 float defaultValue) const
1889 const SGPropertyNode * node = getNode(relative_path);
1890 return (node == 0 ? defaultValue : node->getFloatValue());
1895 * Get a double value for another node.
1898 SGPropertyNode::getDoubleValue (const char * relative_path,
1899 double defaultValue) const
1901 const SGPropertyNode * node = getNode(relative_path);
1902 return (node == 0 ? defaultValue : node->getDoubleValue());
1907 * Get a string value for another node.
1910 SGPropertyNode::getStringValue (const char * relative_path,
1911 const char * defaultValue) const
1913 const SGPropertyNode * node = getNode(relative_path);
1914 return (node == 0 ? defaultValue : node->getStringValue());
1919 * Set a bool value for another node.
1922 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1924 return getNode(relative_path, true)->setBoolValue(value);
1929 * Set an int value for another node.
1932 SGPropertyNode::setIntValue (const char * relative_path, int value)
1934 return getNode(relative_path, true)->setIntValue(value);
1939 * Set a long value for another node.
1942 SGPropertyNode::setLongValue (const char * relative_path, long value)
1944 return getNode(relative_path, true)->setLongValue(value);
1949 * Set a float value for another node.
1952 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1954 return getNode(relative_path, true)->setFloatValue(value);
1959 * Set a double value for another node.
1962 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1964 return getNode(relative_path, true)->setDoubleValue(value);
1969 * Set a string value for another node.
1972 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1974 return getNode(relative_path, true)->setStringValue(value);
1979 * Set an unknown value for another node.
1982 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1985 return getNode(relative_path, true)->setUnspecifiedValue(value);
1990 * Test whether another node is tied.
1993 SGPropertyNode::isTied (const char * relative_path) const
1995 const SGPropertyNode * node = getNode(relative_path);
1996 return (node == 0 ? false : node->isTied());
2001 * Tie a node reached by a relative path, creating it if necessary.
2004 SGPropertyNode::tie (const char * relative_path,
2005 const SGRawValue<bool> &rawValue,
2008 return getNode(relative_path, true)->tie(rawValue, useDefault);
2013 * Tie a node reached by a relative path, creating it if necessary.
2016 SGPropertyNode::tie (const char * relative_path,
2017 const SGRawValue<int> &rawValue,
2020 return getNode(relative_path, true)->tie(rawValue, useDefault);
2025 * Tie a node reached by a relative path, creating it if necessary.
2028 SGPropertyNode::tie (const char * relative_path,
2029 const SGRawValue<long> &rawValue,
2032 return getNode(relative_path, true)->tie(rawValue, useDefault);
2037 * Tie a node reached by a relative path, creating it if necessary.
2040 SGPropertyNode::tie (const char * relative_path,
2041 const SGRawValue<float> &rawValue,
2044 return getNode(relative_path, true)->tie(rawValue, useDefault);
2049 * Tie a node reached by a relative path, creating it if necessary.
2052 SGPropertyNode::tie (const char * relative_path,
2053 const SGRawValue<double> &rawValue,
2056 return getNode(relative_path, true)->tie(rawValue, useDefault);
2061 * Tie a node reached by a relative path, creating it if necessary.
2064 SGPropertyNode::tie (const char * relative_path,
2065 const SGRawValue<const char *> &rawValue,
2068 return getNode(relative_path, true)->tie(rawValue, useDefault);
2073 * Attempt to untie another node reached by a relative path.
2076 SGPropertyNode::untie (const char * relative_path)
2078 SGPropertyNode * node = getNode(relative_path);
2079 return (node == 0 ? false : node->untie());
2083 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener)
2085 if (_listeners == 0)
2086 _listeners = new vector<SGPropertyChangeListener *>;
2087 _listeners->push_back(listener);
2088 listener->register_property(this);
2092 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2094 vector<SGPropertyChangeListener *>::iterator it =
2095 find(_listeners->begin(), _listeners->end(), listener);
2096 if (it != _listeners->end()) {
2097 _listeners->erase(it);
2098 listener->unregister_property(this);
2099 if (_listeners->empty()) {
2100 vector<SGPropertyChangeListener *> * tmp = _listeners;
2108 SGPropertyNode::fireValueChanged ()
2110 fireValueChanged(this);
2114 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2116 fireChildAdded(this, child);
2120 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2122 fireChildRemoved(this, child);
2126 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2128 if (_listeners != 0) {
2129 for (unsigned int i = 0; i < _listeners->size(); i++) {
2130 (*_listeners)[i]->valueChanged(node);
2134 _parent->fireValueChanged(node);
2138 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2139 SGPropertyNode * child)
2141 if (_listeners != 0) {
2142 for (unsigned int i = 0; i < _listeners->size(); i++) {
2143 (*_listeners)[i]->childAdded(parent, child);
2147 _parent->fireChildAdded(parent, child);
2151 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2152 SGPropertyNode * child)
2154 if (_listeners != 0) {
2155 for (unsigned int i = 0; i < _listeners->size(); i++) {
2156 (*_listeners)[i]->childRemoved(parent, child);
2160 _parent->fireChildRemoved(parent, child);
2165 ////////////////////////////////////////////////////////////////////////
2166 // Simplified hash table for caching paths.
2167 ////////////////////////////////////////////////////////////////////////
2169 #define HASH_TABLE_SIZE 199
2171 SGPropertyNode::hash_table::entry::entry ()
2177 SGPropertyNode::hash_table::entry::~entry ()
2179 // Don't delete the value; we don't own
2185 SGPropertyNode::hash_table::entry::set_key (const char * key)
2187 _key = copy_string(key);
2191 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2196 SGPropertyNode::hash_table::bucket::bucket ()
2202 SGPropertyNode::hash_table::bucket::~bucket ()
2204 for (int i = 0; i < _length; i++)
2208 SGPropertyNode::hash_table::entry *
2209 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2212 for (i = 0; i < _length; i++) {
2213 if (!strcmp(_entries[i]->get_key(), key))
2217 entry ** new_entries = new entry*[_length+1];
2218 for (i = 0; i < _length; i++) {
2219 new_entries[i] = _entries[i];
2222 _entries = new_entries;
2223 _entries[_length] = new entry;
2224 _entries[_length]->set_key(key);
2226 return _entries[_length - 1];
2233 SGPropertyNode::hash_table::bucket::erase (const char * key)
2236 for (i = 0; i < _length; i++) {
2237 if (!strcmp(_entries[i]->get_key(), key))
2242 for (++i; i < _length; i++) {
2243 _entries[i-1] = _entries[i];
2250 SGPropertyNode::hash_table::hash_table ()
2256 SGPropertyNode::hash_table::~hash_table ()
2258 for (unsigned int i = 0; i < _data_length; i++)
2263 SGPropertyNode::hash_table::get (const char * key)
2265 if (_data_length == 0)
2267 unsigned int index = hashcode(key) % _data_length;
2268 if (_data[index] == 0)
2270 entry * e = _data[index]->get_entry(key);
2274 return e->get_value();
2278 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2280 if (_data_length == 0) {
2281 _data = new bucket*[HASH_TABLE_SIZE];
2282 _data_length = HASH_TABLE_SIZE;
2283 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2286 unsigned int index = hashcode(key) % _data_length;
2287 if (_data[index] == 0) {
2288 _data[index] = new bucket;
2290 entry * e = _data[index]->get_entry(key, true);
2291 e->set_value(value);
2295 SGPropertyNode::hash_table::erase (const char * key)
2297 if (_data_length == 0)
2299 unsigned int index = hashcode(key) % _data_length;
2300 if (_data[index] == 0)
2302 _data[index]->erase(key);
2306 SGPropertyNode::hash_table::hashcode (const char * key)
2308 unsigned int hash = 0;
2310 hash = 31 * hash + *key;
2319 * Default constructor
2321 SGPropertyNode_ptr::SGPropertyNode_ptr()
2329 SGPropertyNode_ptr::SGPropertyNode_ptr( const SGPropertyNode_ptr &r )
2333 _ptr->incrementRef();
2337 * Constructor from a pointer to a node
2339 SGPropertyNode_ptr::SGPropertyNode_ptr( SGPropertyNode *p )
2343 _ptr->incrementRef();
2349 SGPropertyNode_ptr::~SGPropertyNode_ptr()
2351 if (_ptr && _ptr->decrementRef() == 0)
2356 * Assignement operator
2358 SGPropertyNode_ptr &
2359 SGPropertyNode_ptr::operator=( const SGPropertyNode_ptr &r )
2361 if (_ptr && _ptr->decrementRef() == 0)
2365 _ptr->incrementRef();
2371 * Pointer access operator
2374 SGPropertyNode_ptr::operator->()
2380 * Pointer access operator (const)
2382 const SGPropertyNode *
2383 SGPropertyNode_ptr::operator->() const
2389 * Conversion to SGPropertyNode * operator
2391 SGPropertyNode_ptr::operator SGPropertyNode *()
2397 * Conversion to const SGPropertyNode * operator
2399 SGPropertyNode_ptr::operator const SGPropertyNode *() const
2408 SGPropertyNode_ptr::valid() const
2415 ////////////////////////////////////////////////////////////////////////
2416 // Implementation of SGPropertyChangeListener.
2417 ////////////////////////////////////////////////////////////////////////
2419 SGPropertyChangeListener::~SGPropertyChangeListener ()
2421 // This will come back and remove
2422 // the current item each time. Is
2424 vector<SGPropertyNode *>::iterator it;
2425 for (it = _properties.begin(); it != _properties.end(); it++)
2426 (*it)->removeChangeListener(this);
2430 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2436 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2437 SGPropertyNode * child)
2443 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2444 SGPropertyNode * child)
2450 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2452 _properties.push_back(node);
2456 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2458 vector<SGPropertyNode *>::iterator it =
2459 find(_properties.begin(), _properties.end(), node);
2460 if (it != _properties.end())
2461 _properties.erase(it);