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].
10 # include <simgear_config.h>
22 #include <simgear/math/SGMath.hxx>
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
31 #if ( _MSC_VER == 1200 )
32 // MSVC 6 is buggy, and needs something strange here
33 using std::vector<SGPropertyNode_ptr>;
34 using std::vector<SGPropertyChangeListener *>;
35 using std::vector<SGPropertyNode *>;
47 using std::stringstream;
49 using namespace simgear;
52 ////////////////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
57 * Comparator class for sorting by index.
62 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
63 return (n1->getIndex() < n2->getIndex());
69 ////////////////////////////////////////////////////////////////////////
70 // Convenience macros for value access.
71 ////////////////////////////////////////////////////////////////////////
73 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
74 #define TEST_WRITE if (!getAttribute(WRITE)) return false
77 ////////////////////////////////////////////////////////////////////////
78 // Local path normalization code.
79 ////////////////////////////////////////////////////////////////////////
82 * A component in a path.
91 * Parse the name for a path component.
93 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
95 static inline const string
96 parse_name (const string &path, int &i)
99 int max = path.size();
101 if (path[i] == '.') {
103 if (i < max && path[i] == '.') {
109 if (i < max && path[i] != '/')
110 throw string("illegal character after " + name);
113 else if (isalpha(path[i]) || path[i] == '_') {
117 // The rules inside a name are a little
120 if (isalpha(path[i]) || isdigit(path[i]) || path[i] == '_' ||
121 path[i] == '-' || path[i] == '.') {
123 } else if (path[i] == '[' || path[i] == '/') {
126 throw string("name may contain only ._- and alphanumeric characters");
133 if (name.size() == 0)
134 throw string("name must begin with alpha or '_'");
142 * Parse the optional integer index for a path component.
144 * Index: "[" [0-9]+ "]"
147 parse_index (const string &path, int &i)
156 for (int max = path.size(); i < max; i++) {
157 if (isdigit(path[i])) {
158 index = (index * 10) + (path[i] - '0');
159 } else if (path[i] == ']') {
167 throw string("unterminated index (looking for ']')");
172 * Parse a single path component.
174 * Component: Name Index?
176 static inline PathComponent
177 parse_component (const string &path, int &i)
179 PathComponent component;
180 component.name = parse_name(path, i);
181 if (component.name[0] != '.')
182 component.index = parse_index(path, i);
184 component.index = -1;
190 * Parse a path into its components.
193 parse_path (const string &path, vector<PathComponent> &components)
196 int max = path.size();
198 // Check for initial '/'
199 if (path[pos] == '/') {
203 components.push_back(root);
205 while (pos < max && path[pos] == '/')
210 components.push_back(parse_component(path, pos));
211 while (pos < max && path[pos] == '/')
218 ////////////////////////////////////////////////////////////////////////
219 // Other static utility functions.
220 ////////////////////////////////////////////////////////////////////////
224 copy_string (const char * s)
226 unsigned long int slen = strlen(s);
227 char * copy = new char[slen + 1];
229 // the source string length is known so no need to check for '\0'
230 // when copying every single character
231 memcpy(copy, s, slen);
232 *(copy + slen) = '\0';
237 compare_strings (const char * s1, const char * s2)
239 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
243 * Locate a child node by name and index.
246 find_child (const char * name, int index, const PropertyList& nodes)
248 int nNodes = nodes.size();
249 for (int i = 0; i < nNodes; i++) {
250 SGPropertyNode * node = nodes[i];
252 // searching for a mathing index is a lot less time consuming than
253 // comparing two strings so do that first.
254 if (node->getIndex() == index && compare_strings(node->getName(), name))
261 * Locate the child node with the highest index of the same name
264 find_last_child (const char * name, const PropertyList& nodes)
266 int nNodes = nodes.size();
269 for (int i = 0; i < nNodes; i++) {
270 SGPropertyNode * node = nodes[i];
271 if (compare_strings(node->getName(), name))
273 int idx = node->getIndex();
274 if (idx > index) index = idx;
282 * Locate another node, given a relative path.
284 static SGPropertyNode *
285 find_node (SGPropertyNode * current,
286 const vector<PathComponent> &components,
290 // Run off the end of the list
295 // Success! This is the one we want.
296 else if (position >= (int)components.size()) {
297 return (current->getAttribute(SGPropertyNode::REMOVED) ? 0 : current);
300 // Empty component means root.
301 else if (components[position].name == "") {
302 return find_node(current->getRootNode(), components, position + 1, create);
305 // . means current directory
306 else if (components[position].name == ".") {
307 return find_node(current, components, position + 1, create);
310 // .. means parent directory
311 else if (components[position].name == "..") {
312 SGPropertyNode * parent = current->getParent();
314 throw string("attempt to move past root with '..'");
316 return find_node(parent, components, position + 1, create);
319 // Otherwise, a child name
321 SGPropertyNode * child =
322 current->getChild(components[position].name.c_str(),
323 components[position].index,
325 return find_node(child, components, position + 1, create);
331 ////////////////////////////////////////////////////////////////////////
332 // Private methods from SGPropertyNode (may be inlined for speed).
333 ////////////////////////////////////////////////////////////////////////
336 SGPropertyNode::get_bool () const
339 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
341 return _local_val.bool_val;
345 SGPropertyNode::get_int () const
348 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
350 return _local_val.int_val;
354 SGPropertyNode::get_long () const
357 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
359 return _local_val.long_val;
363 SGPropertyNode::get_float () const
366 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
368 return _local_val.float_val;
372 SGPropertyNode::get_double () const
375 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
377 return _local_val.double_val;
381 SGPropertyNode::get_string () const
384 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
386 return _local_val.string_val;
390 SGPropertyNode::set_bool (bool val)
393 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
400 _local_val.bool_val = val;
407 SGPropertyNode::set_int (int val)
410 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
417 _local_val.int_val = val;
424 SGPropertyNode::set_long (long val)
427 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
434 _local_val.long_val = val;
441 SGPropertyNode::set_float (float val)
444 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
451 _local_val.float_val = val;
458 SGPropertyNode::set_double (double val)
461 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
468 _local_val.double_val = val;
475 SGPropertyNode::set_string (const char * val)
478 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
485 delete [] _local_val.string_val;
486 _local_val.string_val = copy_string(val);
493 SGPropertyNode::clearValue ()
495 if (_type == props::ALIAS) {
498 } else if (_type != props::NONE) {
501 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
504 _local_val.int_val = SGRawValue<int>::DefaultValue();
507 _local_val.long_val = SGRawValue<long>::DefaultValue();
510 _local_val.float_val = SGRawValue<float>::DefaultValue();
513 _local_val.double_val = SGRawValue<double>::DefaultValue();
516 case props::UNSPECIFIED:
518 delete [] _local_val.string_val;
520 _local_val.string_val = 0;
532 * Get the value as a string.
535 SGPropertyNode::make_string () const
537 if (!getAttribute(READ))
541 return _value.alias->getStringValue();
543 return get_bool() ? "true" : "false";
545 case props::UNSPECIFIED:
564 sstr << std::setprecision(10) << get_double();
566 case props::EXTENDED:
568 props::Type realType = _value.val->getType();
569 // Perhaps this should be done for all types?
570 if (realType == props::VEC3D || realType == props::VEC4D)
572 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
578 _buffer = sstr.str();
579 return _buffer.c_str();
583 * Trace a write access for a property.
586 SGPropertyNode::trace_write () const
589 cerr << "TRACE: Write node " << getPath () << ", value \""
590 << make_string() << '"' << endl;
592 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
593 << ", value \"" << make_string() << '"');
598 * Trace a read access for a property.
601 SGPropertyNode::trace_read () const
604 cerr << "TRACE: Write node " << getPath () << ", value \""
605 << make_string() << '"' << endl;
607 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
608 << ", value \"" << make_string() << '"');
613 ////////////////////////////////////////////////////////////////////////
614 // Public methods from SGPropertyNode.
615 ////////////////////////////////////////////////////////////////////////
618 * Last used attribute
619 * Update as needed when enum Attribute is changed
621 const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE;
624 * Default constructor: always creates a root node.
626 SGPropertyNode::SGPropertyNode ()
635 _local_val.string_val = 0;
643 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
644 : _index(node._index),
646 _parent(0), // don't copy the parent
651 _listeners(0) // CHECK!!
653 _local_val.string_val = 0;
655 if (_type == props::NONE)
657 if (_type == props::ALIAS) {
658 _value.alias = node._value.alias;
663 if (_tied || _type == props::EXTENDED) {
664 _value.val = node._value.val->clone();
669 set_bool(node.get_bool());
672 set_int(node.get_int());
675 set_long(node.get_long());
678 set_float(node.get_float());
681 set_double(node.get_double());
684 case props::UNSPECIFIED:
685 set_string(node.get_string());
694 * Convenience constructor.
696 SGPropertyNode::SGPropertyNode (const char * name,
698 SGPropertyNode * parent)
708 _local_val.string_val = 0;
710 _name = parse_name(name, i);
711 if (i != int(strlen(name)) || name[0] == '.')
712 throw string("plain name expected instead of '") + name + '\'';
719 SGPropertyNode::~SGPropertyNode ()
721 // zero out all parent pointers, else they might be dangling
722 for (unsigned i = 0; i < _children.size(); ++i)
723 _children[i]->_parent = 0;
724 for (unsigned i = 0; i < _removedChildren.size(); ++i)
725 _removedChildren[i]->_parent = 0;
730 vector<SGPropertyChangeListener*>::iterator it;
731 for (it = _listeners->begin(); it != _listeners->end(); ++it)
732 (*it)->unregister_property(this);
739 * Alias to another node.
742 SGPropertyNode::alias (SGPropertyNode * target)
744 if (target == 0 || _type == props::ALIAS || _tied)
748 _value.alias = target;
749 _type = props::ALIAS;
755 * Alias to another node by path.
758 SGPropertyNode::alias (const char * path)
760 return alias(getNode(path, true));
768 SGPropertyNode::unalias ()
770 if (_type != props::ALIAS)
778 * Get the target of an alias.
781 SGPropertyNode::getAliasTarget ()
783 return (_type == props::ALIAS ? _value.alias : 0);
787 const SGPropertyNode *
788 SGPropertyNode::getAliasTarget () const
790 return (_type == props::ALIAS ? _value.alias : 0);
794 * create a non-const child by name after the last node with the same name.
797 SGPropertyNode::addChild (const char * name)
799 int pos = find_last_child(name, _children)+1;
801 SGPropertyNode_ptr node;
802 node = new SGPropertyNode(name, pos, this);
803 _children.push_back(node);
804 fireChildAdded(node);
810 * Get a non-const child by index.
813 SGPropertyNode::getChild (int position)
815 if (position >= 0 && position < nChildren())
816 return _children[position];
823 * Get a const child by index.
825 const SGPropertyNode *
826 SGPropertyNode::getChild (int position) const
828 if (position >= 0 && position < nChildren())
829 return _children[position];
836 * Get a non-const child by name and index, creating if necessary.
839 SGPropertyNode::getChild (const char * name, int index, bool create)
841 int pos = find_child(name, index, _children);
843 return _children[pos];
845 SGPropertyNode_ptr node;
846 pos = find_child(name, index, _removedChildren);
848 PropertyList::iterator it = _removedChildren.begin();
850 node = _removedChildren[pos];
851 _removedChildren.erase(it);
852 node->setAttribute(REMOVED, false);
854 node = new SGPropertyNode(name, index, this);
856 _children.push_back(node);
857 fireChildAdded(node);
866 * Get a const child by name and index.
868 const SGPropertyNode *
869 SGPropertyNode::getChild (const char * name, int index) const
871 int pos = find_child(name, index, _children);
873 return _children[pos];
880 * Get all children with the same name (but different indices).
883 SGPropertyNode::getChildren (const char * name) const
885 PropertyList children;
886 int max = _children.size();
888 for (int i = 0; i < max; i++)
889 if (compare_strings(_children[i]->getName(), name))
890 children.push_back(_children[i]);
892 sort(children.begin(), children.end(), CompareIndices());
898 * Remove this node and all children from nodes that link to them
899 * in their path cache.
902 SGPropertyNode::remove_from_path_caches ()
904 for (unsigned int i = 0; i < _children.size(); ++i)
905 _children[i]->remove_from_path_caches();
907 for (unsigned int i = 0; i < _linkedNodes.size(); i++)
908 _linkedNodes[i]->erase(this);
909 _linkedNodes.clear();
914 * Remove child by position.
917 SGPropertyNode::removeChild (int pos, bool keep)
919 SGPropertyNode_ptr node;
920 if (pos < 0 || pos >= (int)_children.size())
923 PropertyList::iterator it = _children.begin();
925 node = _children[pos];
928 _removedChildren.push_back(node);
931 node->remove_from_path_caches();
932 node->setAttribute(REMOVED, true);
934 fireChildRemoved(node);
940 * Remove a child node
943 SGPropertyNode::removeChild (const char * name, int index, bool keep)
945 SGPropertyNode_ptr ret;
946 int pos = find_child(name, index, _children);
948 ret = removeChild(pos, keep);
954 * Remove all children with the specified name.
957 SGPropertyNode::removeChildren (const char * name, bool keep)
959 PropertyList children;
961 for (int pos = _children.size() - 1; pos >= 0; pos--)
962 if (compare_strings(_children[pos]->getName(), name))
963 children.push_back(removeChild(pos, keep));
965 sort(children.begin(), children.end(), CompareIndices());
971 * Remove a linked node.
974 SGPropertyNode::remove_linked_node (hash_table * node)
976 for (unsigned int i = 0; i < _linkedNodes.size(); i++) {
977 if (_linkedNodes[i] == node) {
978 vector<hash_table *>::iterator it = _linkedNodes.begin();
980 _linkedNodes.erase(it);
989 SGPropertyNode::getDisplayName (bool simplify) const
991 string display_name = _name;
992 if (_index != 0 || !simplify) {
994 sstr << '[' << _index << ']';
995 display_name += sstr.str();
1002 SGPropertyNode::getPath (bool simplify) const
1004 // Calculate the complete path only once.
1005 if (_parent != 0 && _path.empty()) {
1006 _path = _parent->getPath(simplify);
1008 _path += getDisplayName(simplify);
1011 return _path.c_str();
1015 SGPropertyNode::getType () const
1017 if (_type == props::ALIAS)
1018 return _value.alias->getType();
1019 else if (_type == props::EXTENDED)
1020 return _value.val->getType();
1027 SGPropertyNode::getBoolValue () const
1029 // Shortcut for common case
1030 if (_attr == (READ|WRITE) && _type == props::BOOL)
1033 if (getAttribute(TRACE_READ))
1035 if (!getAttribute(READ))
1036 return SGRawValue<bool>::DefaultValue();
1039 return _value.alias->getBoolValue();
1043 return get_int() == 0 ? false : true;
1045 return get_long() == 0L ? false : true;
1047 return get_float() == 0.0 ? false : true;
1049 return get_double() == 0.0L ? false : true;
1051 case props::UNSPECIFIED:
1052 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1055 return SGRawValue<bool>::DefaultValue();
1060 SGPropertyNode::getIntValue () const
1062 // Shortcut for common case
1063 if (_attr == (READ|WRITE) && _type == props::INT)
1066 if (getAttribute(TRACE_READ))
1068 if (!getAttribute(READ))
1069 return SGRawValue<int>::DefaultValue();
1072 return _value.alias->getIntValue();
1074 return int(get_bool());
1078 return int(get_long());
1080 return int(get_float());
1082 return int(get_double());
1084 case props::UNSPECIFIED:
1085 return atoi(get_string());
1088 return SGRawValue<int>::DefaultValue();
1093 SGPropertyNode::getLongValue () const
1095 // Shortcut for common case
1096 if (_attr == (READ|WRITE) && _type == props::LONG)
1099 if (getAttribute(TRACE_READ))
1101 if (!getAttribute(READ))
1102 return SGRawValue<long>::DefaultValue();
1105 return _value.alias->getLongValue();
1107 return long(get_bool());
1109 return long(get_int());
1113 return long(get_float());
1115 return long(get_double());
1117 case props::UNSPECIFIED:
1118 return strtol(get_string(), 0, 0);
1121 return SGRawValue<long>::DefaultValue();
1126 SGPropertyNode::getFloatValue () const
1128 // Shortcut for common case
1129 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1132 if (getAttribute(TRACE_READ))
1134 if (!getAttribute(READ))
1135 return SGRawValue<float>::DefaultValue();
1138 return _value.alias->getFloatValue();
1140 return float(get_bool());
1142 return float(get_int());
1144 return float(get_long());
1148 return float(get_double());
1150 case props::UNSPECIFIED:
1151 return atof(get_string());
1154 return SGRawValue<float>::DefaultValue();
1159 SGPropertyNode::getDoubleValue () const
1161 // Shortcut for common case
1162 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1163 return get_double();
1165 if (getAttribute(TRACE_READ))
1167 if (!getAttribute(READ))
1168 return SGRawValue<double>::DefaultValue();
1172 return _value.alias->getDoubleValue();
1174 return double(get_bool());
1176 return double(get_int());
1178 return double(get_long());
1180 return double(get_float());
1182 return get_double();
1184 case props::UNSPECIFIED:
1185 return strtod(get_string(), 0);
1188 return SGRawValue<double>::DefaultValue();
1193 SGPropertyNode::getStringValue () const
1195 // Shortcut for common case
1196 if (_attr == (READ|WRITE) && _type == props::STRING)
1197 return get_string();
1199 if (getAttribute(TRACE_READ))
1201 if (!getAttribute(READ))
1202 return SGRawValue<const char *>::DefaultValue();
1203 return make_string();
1207 SGPropertyNode::setBoolValue (bool value)
1209 // Shortcut for common case
1210 if (_attr == (READ|WRITE) && _type == props::BOOL)
1211 return set_bool(value);
1213 bool result = false;
1215 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1218 _type = props::BOOL;
1223 result = _value.alias->setBoolValue(value);
1226 result = set_bool(value);
1229 result = set_int(int(value));
1232 result = set_long(long(value));
1235 result = set_float(float(value));
1238 result = set_double(double(value));
1241 case props::UNSPECIFIED:
1242 result = set_string(value ? "true" : "false");
1249 if (getAttribute(TRACE_WRITE))
1255 SGPropertyNode::setIntValue (int value)
1257 // Shortcut for common case
1258 if (_attr == (READ|WRITE) && _type == props::INT)
1259 return set_int(value);
1261 bool result = false;
1263 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1266 _local_val.int_val = 0;
1271 result = _value.alias->setIntValue(value);
1274 result = set_bool(value == 0 ? false : true);
1277 result = set_int(value);
1280 result = set_long(long(value));
1283 result = set_float(float(value));
1286 result = set_double(double(value));
1289 case props::UNSPECIFIED: {
1291 sprintf(buf, "%d", value);
1292 result = set_string(buf);
1300 if (getAttribute(TRACE_WRITE))
1306 SGPropertyNode::setLongValue (long value)
1308 // Shortcut for common case
1309 if (_attr == (READ|WRITE) && _type == props::LONG)
1310 return set_long(value);
1312 bool result = false;
1314 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1316 _type = props::LONG;
1317 _local_val.long_val = 0L;
1322 result = _value.alias->setLongValue(value);
1325 result = set_bool(value == 0L ? false : true);
1328 result = set_int(int(value));
1331 result = set_long(value);
1334 result = set_float(float(value));
1337 result = set_double(double(value));
1340 case props::UNSPECIFIED: {
1342 sprintf(buf, "%ld", value);
1343 result = set_string(buf);
1351 if (getAttribute(TRACE_WRITE))
1357 SGPropertyNode::setFloatValue (float value)
1359 // Shortcut for common case
1360 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1361 return set_float(value);
1363 bool result = false;
1365 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1367 _type = props::FLOAT;
1368 _local_val.float_val = 0;
1373 result = _value.alias->setFloatValue(value);
1376 result = set_bool(value == 0.0 ? false : true);
1379 result = set_int(int(value));
1382 result = set_long(long(value));
1385 result = set_float(value);
1388 result = set_double(double(value));
1391 case props::UNSPECIFIED: {
1393 sprintf(buf, "%f", value);
1394 result = set_string(buf);
1402 if (getAttribute(TRACE_WRITE))
1408 SGPropertyNode::setDoubleValue (double value)
1410 // Shortcut for common case
1411 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1412 return set_double(value);
1414 bool result = false;
1416 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1418 _local_val.double_val = value;
1419 _type = props::DOUBLE;
1424 result = _value.alias->setDoubleValue(value);
1427 result = set_bool(value == 0.0L ? false : true);
1430 result = set_int(int(value));
1433 result = set_long(long(value));
1436 result = set_float(float(value));
1439 result = set_double(value);
1442 case props::UNSPECIFIED: {
1444 sprintf(buf, "%f", value);
1445 result = set_string(buf);
1453 if (getAttribute(TRACE_WRITE))
1459 SGPropertyNode::setStringValue (const char * value)
1461 // Shortcut for common case
1462 if (_attr == (READ|WRITE) && _type == props::STRING)
1463 return set_string(value);
1465 bool result = false;
1467 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1469 _type = props::STRING;
1474 result = _value.alias->setStringValue(value);
1477 result = set_bool((compare_strings(value, "true")
1478 || atoi(value)) ? true : false);
1481 result = set_int(atoi(value));
1484 result = set_long(strtol(value, 0, 0));
1487 result = set_float(atof(value));
1490 result = set_double(strtod(value, 0));
1493 case props::UNSPECIFIED:
1494 result = set_string(value);
1496 case props::EXTENDED:
1498 stringstream sstr(value);
1499 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1507 if (getAttribute(TRACE_WRITE))
1513 SGPropertyNode::setUnspecifiedValue (const char * value)
1515 bool result = false;
1517 if (_type == props::NONE) {
1519 _type = props::UNSPECIFIED;
1521 props::Type type = _type;
1522 if (type == props::EXTENDED)
1523 type = _value.val->getType();
1526 result = _value.alias->setUnspecifiedValue(value);
1529 result = set_bool((compare_strings(value, "true")
1530 || atoi(value)) ? true : false);
1533 result = set_int(atoi(value));
1536 result = set_long(strtol(value, 0, 0));
1539 result = set_float(atof(value));
1542 result = set_double(strtod(value, 0));
1545 case props::UNSPECIFIED:
1546 result = set_string(value);
1549 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1552 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1559 if (getAttribute(TRACE_WRITE))
1564 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1566 if (!getAttribute(READ))
1570 return _value.alias->printOn(stream);
1572 stream << (get_bool() ? "true" : "false");
1575 stream << get_int();
1578 stream << get_long();
1581 stream << get_float();
1584 stream << get_double();
1587 case props::UNSPECIFIED:
1588 stream << get_string();
1590 case props::EXTENDED:
1591 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1600 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1603 if (_type == props::ALIAS || _tied)
1606 useDefault = useDefault && hasValue();
1607 std::string old_val;
1609 old_val = getStringValue();
1611 _type = props::STRING;
1613 _value.val = rawValue.clone();
1616 setStringValue(old_val.c_str());
1621 SGPropertyNode::untie ()
1628 bool val = getBoolValue();
1630 _type = props::BOOL;
1631 _local_val.bool_val = val;
1635 int val = getIntValue();
1638 _local_val.int_val = val;
1642 long val = getLongValue();
1644 _type = props::LONG;
1645 _local_val.long_val = val;
1648 case props::FLOAT: {
1649 float val = getFloatValue();
1651 _type = props::FLOAT;
1652 _local_val.float_val = val;
1655 case props::DOUBLE: {
1656 double val = getDoubleValue();
1658 _type = props::DOUBLE;
1659 _local_val.double_val = val;
1663 case props::UNSPECIFIED: {
1664 string val = getStringValue();
1666 _type = props::STRING;
1667 _local_val.string_val = copy_string(val.c_str());
1670 case props::EXTENDED: {
1671 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1672 _value.val = 0; // Prevent clearValue() from deleting
1674 _type = props::EXTENDED;
1675 _value.val = val->makeContainer();
1689 SGPropertyNode::getRootNode ()
1694 return _parent->getRootNode();
1697 const SGPropertyNode *
1698 SGPropertyNode::getRootNode () const
1703 return _parent->getRootNode();
1707 SGPropertyNode::getNode (const char * relative_path, bool create)
1709 if (_path_cache == 0)
1710 _path_cache = new hash_table;
1712 SGPropertyNode * result = _path_cache->get(relative_path);
1714 vector<PathComponent> components;
1715 parse_path(relative_path, components);
1716 result = find_node(this, components, 0, create);
1718 _path_cache->put(relative_path, result);
1725 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1727 vector<PathComponent> components;
1728 parse_path(relative_path, components);
1729 if (components.size() > 0)
1730 components.back().index = index;
1731 return find_node(this, components, 0, create);
1734 const SGPropertyNode *
1735 SGPropertyNode::getNode (const char * relative_path) const
1737 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1740 const SGPropertyNode *
1741 SGPropertyNode::getNode (const char * relative_path, int index) const
1743 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1747 ////////////////////////////////////////////////////////////////////////
1748 // Convenience methods using relative paths.
1749 ////////////////////////////////////////////////////////////////////////
1753 * Test whether another node has a value attached.
1756 SGPropertyNode::hasValue (const char * relative_path) const
1758 const SGPropertyNode * node = getNode(relative_path);
1759 return (node == 0 ? false : node->hasValue());
1764 * Get the value type for another node.
1767 SGPropertyNode::getType (const char * relative_path) const
1769 const SGPropertyNode * node = getNode(relative_path);
1770 return (node == 0 ? props::UNSPECIFIED : node->getType());
1775 * Get a bool value for another node.
1778 SGPropertyNode::getBoolValue (const char * relative_path,
1779 bool defaultValue) const
1781 const SGPropertyNode * node = getNode(relative_path);
1782 return (node == 0 ? defaultValue : node->getBoolValue());
1787 * Get an int value for another node.
1790 SGPropertyNode::getIntValue (const char * relative_path,
1791 int defaultValue) const
1793 const SGPropertyNode * node = getNode(relative_path);
1794 return (node == 0 ? defaultValue : node->getIntValue());
1799 * Get a long value for another node.
1802 SGPropertyNode::getLongValue (const char * relative_path,
1803 long defaultValue) const
1805 const SGPropertyNode * node = getNode(relative_path);
1806 return (node == 0 ? defaultValue : node->getLongValue());
1811 * Get a float value for another node.
1814 SGPropertyNode::getFloatValue (const char * relative_path,
1815 float defaultValue) const
1817 const SGPropertyNode * node = getNode(relative_path);
1818 return (node == 0 ? defaultValue : node->getFloatValue());
1823 * Get a double value for another node.
1826 SGPropertyNode::getDoubleValue (const char * relative_path,
1827 double defaultValue) const
1829 const SGPropertyNode * node = getNode(relative_path);
1830 return (node == 0 ? defaultValue : node->getDoubleValue());
1835 * Get a string value for another node.
1838 SGPropertyNode::getStringValue (const char * relative_path,
1839 const char * defaultValue) const
1841 const SGPropertyNode * node = getNode(relative_path);
1842 return (node == 0 ? defaultValue : node->getStringValue());
1847 * Set a bool value for another node.
1850 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1852 return getNode(relative_path, true)->setBoolValue(value);
1857 * Set an int value for another node.
1860 SGPropertyNode::setIntValue (const char * relative_path, int value)
1862 return getNode(relative_path, true)->setIntValue(value);
1867 * Set a long value for another node.
1870 SGPropertyNode::setLongValue (const char * relative_path, long value)
1872 return getNode(relative_path, true)->setLongValue(value);
1877 * Set a float value for another node.
1880 SGPropertyNode::setFloatValue (const char * relative_path, float value)
1882 return getNode(relative_path, true)->setFloatValue(value);
1887 * Set a double value for another node.
1890 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
1892 return getNode(relative_path, true)->setDoubleValue(value);
1897 * Set a string value for another node.
1900 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
1902 return getNode(relative_path, true)->setStringValue(value);
1907 * Set an unknown value for another node.
1910 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
1913 return getNode(relative_path, true)->setUnspecifiedValue(value);
1918 * Test whether another node is tied.
1921 SGPropertyNode::isTied (const char * relative_path) const
1923 const SGPropertyNode * node = getNode(relative_path);
1924 return (node == 0 ? false : node->isTied());
1929 * Tie a node reached by a relative path, creating it if necessary.
1932 SGPropertyNode::tie (const char * relative_path,
1933 const SGRawValue<bool> &rawValue,
1936 return getNode(relative_path, true)->tie(rawValue, useDefault);
1941 * Tie a node reached by a relative path, creating it if necessary.
1944 SGPropertyNode::tie (const char * relative_path,
1945 const SGRawValue<int> &rawValue,
1948 return getNode(relative_path, true)->tie(rawValue, useDefault);
1953 * Tie a node reached by a relative path, creating it if necessary.
1956 SGPropertyNode::tie (const char * relative_path,
1957 const SGRawValue<long> &rawValue,
1960 return getNode(relative_path, true)->tie(rawValue, useDefault);
1965 * Tie a node reached by a relative path, creating it if necessary.
1968 SGPropertyNode::tie (const char * relative_path,
1969 const SGRawValue<float> &rawValue,
1972 return getNode(relative_path, true)->tie(rawValue, useDefault);
1977 * Tie a node reached by a relative path, creating it if necessary.
1980 SGPropertyNode::tie (const char * relative_path,
1981 const SGRawValue<double> &rawValue,
1984 return getNode(relative_path, true)->tie(rawValue, useDefault);
1989 * Tie a node reached by a relative path, creating it if necessary.
1992 SGPropertyNode::tie (const char * relative_path,
1993 const SGRawValue<const char *> &rawValue,
1996 return getNode(relative_path, true)->tie(rawValue, useDefault);
2001 * Attempt to untie another node reached by a relative path.
2004 SGPropertyNode::untie (const char * relative_path)
2006 SGPropertyNode * node = getNode(relative_path);
2007 return (node == 0 ? false : node->untie());
2011 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2014 if (_listeners == 0)
2015 _listeners = new vector<SGPropertyChangeListener*>;
2016 _listeners->push_back(listener);
2017 listener->register_property(this);
2019 listener->valueChanged(this);
2023 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2025 vector<SGPropertyChangeListener*>::iterator it =
2026 find(_listeners->begin(), _listeners->end(), listener);
2027 if (it != _listeners->end()) {
2028 _listeners->erase(it);
2029 listener->unregister_property(this);
2030 if (_listeners->empty()) {
2031 vector<SGPropertyChangeListener*>* tmp = _listeners;
2039 SGPropertyNode::fireValueChanged ()
2041 fireValueChanged(this);
2045 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2047 fireChildAdded(this, child);
2051 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2053 fireChildRemoved(this, child);
2057 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2059 if (_listeners != 0) {
2060 for (unsigned int i = 0; i < _listeners->size(); i++) {
2061 (*_listeners)[i]->valueChanged(node);
2065 _parent->fireValueChanged(node);
2069 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2070 SGPropertyNode * child)
2072 if (_listeners != 0) {
2073 for (unsigned int i = 0; i < _listeners->size(); i++) {
2074 (*_listeners)[i]->childAdded(parent, child);
2078 _parent->fireChildAdded(parent, child);
2082 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2083 SGPropertyNode * child)
2085 if (_listeners != 0) {
2086 for (unsigned int i = 0; i < _listeners->size(); i++) {
2087 (*_listeners)[i]->childRemoved(parent, child);
2091 _parent->fireChildRemoved(parent, child);
2096 ////////////////////////////////////////////////////////////////////////
2097 // Simplified hash table for caching paths.
2098 ////////////////////////////////////////////////////////////////////////
2100 #define HASH_TABLE_SIZE 199
2102 SGPropertyNode::hash_table::entry::entry ()
2107 SGPropertyNode::hash_table::entry::~entry ()
2109 // Don't delete the value; we don't own
2114 SGPropertyNode::hash_table::entry::set_key (const char * key)
2120 SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value)
2125 SGPropertyNode::hash_table::bucket::bucket ()
2131 SGPropertyNode::hash_table::bucket::~bucket ()
2133 for (int i = 0; i < _length; i++)
2138 SGPropertyNode::hash_table::entry *
2139 SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create)
2142 for (i = 0; i < _length; i++) {
2143 if (!strcmp(_entries[i]->get_key(), key))
2147 entry ** new_entries = new entry*[_length+1];
2148 for (i = 0; i < _length; i++) {
2149 new_entries[i] = _entries[i];
2152 _entries = new_entries;
2153 _entries[_length] = new entry;
2154 _entries[_length]->set_key(key);
2156 return _entries[_length - 1];
2163 SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node)
2165 for (int i = 0; i < _length; i++) {
2166 if (_entries[i]->get_value() == node) {
2168 for (++i; i < _length; i++) {
2169 _entries[i-1] = _entries[i];
2179 SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner)
2181 for (int i = 0; i < _length; i++) {
2182 SGPropertyNode * node = _entries[i]->get_value();
2184 node->remove_linked_node(owner);
2188 SGPropertyNode::hash_table::hash_table ()
2194 SGPropertyNode::hash_table::~hash_table ()
2196 for (unsigned int i = 0; i < _data_length; i++) {
2198 _data[i]->clear(this);
2206 SGPropertyNode::hash_table::get (const char * key)
2208 if (_data_length == 0)
2210 unsigned int index = hashcode(key) % _data_length;
2211 if (_data[index] == 0)
2213 entry * e = _data[index]->get_entry(key);
2217 return e->get_value();
2221 SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value)
2223 if (_data_length == 0) {
2224 _data = new bucket*[HASH_TABLE_SIZE];
2225 _data_length = HASH_TABLE_SIZE;
2226 for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++)
2229 unsigned int index = hashcode(key) % _data_length;
2230 if (_data[index] == 0) {
2231 _data[index] = new bucket;
2233 entry * e = _data[index]->get_entry(key, true);
2234 e->set_value(value);
2235 value->add_linked_node(this);
2239 SGPropertyNode::hash_table::erase (SGPropertyNode * node)
2241 for (unsigned int i = 0; i < _data_length; i++)
2242 if (_data[i] && _data[i]->erase(node))
2249 SGPropertyNode::hash_table::hashcode (const char * key)
2251 unsigned int hash = 0;
2253 hash = 31 * hash + *key;
2261 ////////////////////////////////////////////////////////////////////////
2262 // Implementation of SGPropertyChangeListener.
2263 ////////////////////////////////////////////////////////////////////////
2265 SGPropertyChangeListener::~SGPropertyChangeListener ()
2267 for (int i = _properties.size() - 1; i >= 0; i--)
2268 _properties[i]->removeChangeListener(this);
2272 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2278 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2279 SGPropertyNode * child)
2285 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2286 SGPropertyNode * child)
2292 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2294 _properties.push_back(node);
2298 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2300 vector<SGPropertyNode *>::iterator it =
2301 find(_properties.begin(), _properties.end(), node);
2302 if (it != _properties.end())
2303 _properties.erase(it);
2307 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2310 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2311 for (int i = 0; i < 3; ++i) {
2322 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2324 for (int i = 0; i < 3; ++i) {
2325 stream >> result[i];
2331 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2334 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2335 for (int i = 0; i < 4; ++i) {
2346 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2348 for (int i = 0; i < 4; ++i) {
2349 stream >> result[i];