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>
14 #include "PropertyInterpolationMgr.hxx"
15 #include "vectorPropTemplates.hxx"
27 #include <boost/algorithm/string/find_iterator.hpp>
28 #include <boost/algorithm/string/predicate.hpp>
29 #include <boost/algorithm/string/classification.hpp>
30 #include <boost/bind.hpp>
31 #include <boost/functional/hash.hpp>
32 #include <boost/range.hpp>
38 #include <simgear/compiler.h>
39 #include <simgear/debug/logstream.hxx>
41 #if ( _MSC_VER == 1200 )
42 // MSVC 6 is buggy, and needs something strange here
43 using std::vector<SGPropertyNode_ptr>;
44 using std::vector<SGPropertyChangeListener *>;
45 using std::vector<SGPropertyNode *>;
57 using std::stringstream;
59 using namespace simgear;
61 ////////////////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////////////////
66 * Comparator class for sorting by index.
71 int operator() (const SGPropertyNode_ptr n1, const SGPropertyNode_ptr n2) const {
72 return (n1->getIndex() < n2->getIndex());
77 ////////////////////////////////////////////////////////////////////////
78 // Convenience macros for value access.
79 ////////////////////////////////////////////////////////////////////////
81 #define TEST_READ(dflt) if (!getAttribute(READ)) return dflt
82 #define TEST_WRITE if (!getAttribute(WRITE)) return false
84 ////////////////////////////////////////////////////////////////////////
85 // Local path normalization code.
86 ////////////////////////////////////////////////////////////////////////
89 * Parse the name for a path component.
91 * Name: [_a-zA-Z][-._a-zA-Z0-9]*
94 template<typename Range>
96 parse_name (const SGPropertyNode *node, const Range &path)
98 typename Range::iterator i = path.begin();
99 typename Range::iterator max = path.end();
103 if (i != path.end() && *i == '.') {
106 if (i != max && *i != '/')
107 throw string("illegal character after . or ..");
108 } else if (isalpha(*i) || *i == '_') {
111 // The rules inside a name are a little
114 if (isalpha(*i) || isdigit(*i) || *i == '_' ||
115 *i == '-' || *i == '.') {
117 } else if (*i == '[' || *i == '/') {
122 err.append("' found in propertyname after '"+node->getNameString()+"'");
123 err.append("\nname may contain only ._- and alphanumeric characters");
131 if (path.begin() == i) {
134 err.append("' found in propertyname after '"+node->getNameString()+"'");
135 err.append("\nname must begin with alpha or '_'");
139 return Range(path.begin(), i);
142 // Validate the name of a single node
143 inline bool validateName(const string& name)
145 using namespace boost;
148 if (!isalpha(name[0]) && name[0] != '_')
150 return all(make_iterator_range(name.begin(), name.end()),
151 is_alnum() || is_any_of("_-."));
154 ////////////////////////////////////////////////////////////////////////
155 // Other static utility functions.
156 ////////////////////////////////////////////////////////////////////////
160 copy_string (const char * s)
162 size_t slen = strlen(s);
163 char * copy = new char[slen + 1];
165 // the source string length is known so no need to check for '\0'
166 // when copying every single character
167 memcpy(copy, s, slen);
168 *(copy + slen) = '\0';
173 compare_strings (const char * s1, const char * s2)
175 return !strncmp(s1, s2, SGPropertyNode::MAX_STRING_LEN);
179 * Locate a child node by name and index.
181 template<typename Itr>
183 find_child (Itr begin, Itr end, int index, const PropertyList& nodes)
185 size_t nNodes = nodes.size();
186 boost::iterator_range<Itr> name(begin, end);
187 for (size_t i = 0; i < nNodes; i++) {
188 SGPropertyNode * node = nodes[i];
190 // searching for a matching index is a lot less time consuming than
191 // comparing two strings so do that first.
192 if (node->getIndex() == index && boost::equals(node->getName(), name))
193 return static_cast<int>(i);
199 * Locate the child node with the highest index of the same name
202 find_last_child (const char * name, const PropertyList& nodes)
204 size_t nNodes = nodes.size();
207 for (size_t i = 0; i < nNodes; i++) {
208 SGPropertyNode * node = nodes[i];
209 if (compare_strings(node->getName(), name))
211 int idx = node->getIndex();
212 if (idx > index) index = idx;
219 * Get first unused index for child nodes with the given name
222 first_unused_index( const char * name,
223 const PropertyList& nodes,
226 const char* nameEnd = name + strlen(name);
228 for( int index = min_index; index < std::numeric_limits<int>::max(); ++index )
230 if( find_child(name, nameEnd, index, nodes) < 0 )
234 SG_LOG(SG_GENERAL, SG_ALERT, "Too many nodes: " << name);
238 template<typename Itr>
239 inline SGPropertyNode*
240 SGPropertyNode::getExistingChild (Itr begin, Itr end, int index)
242 int pos = find_child(begin, end, index, _children);
244 return _children[pos];
248 template<typename Itr>
250 SGPropertyNode::getChildImpl (Itr begin, Itr end, int index, bool create)
252 SGPropertyNode* node = getExistingChild(begin, end, index);
257 node = new SGPropertyNode(begin, end, index, this);
258 _children.push_back(node);
259 fireChildAdded(node);
266 template<typename SplitItr>
268 find_node_aux(SGPropertyNode * current, SplitItr& itr, bool create,
271 typedef typename SplitItr::value_type Range;
272 // Run off the end of the list
277 // Success! This is the one we want.
281 // Empty name at this point is empty, not root.
283 return find_node_aux(current, ++itr, create, last_index);
284 Range name = parse_name(current, token);
285 if (equals(name, "."))
286 return find_node_aux(current, ++itr, create, last_index);
287 if (equals(name, "..")) {
288 SGPropertyNode * parent = current->getParent();
290 throw string("attempt to move past root with '..'");
291 return find_node_aux(parent, ++itr, create, last_index);
294 if (last_index >= 0) {
295 // If we are at the last token and last_index is valid, use
296 // last_index as the index value
298 while (!(++itr).eof()) {
312 if (name.end() != token.end()) {
313 if (*name.end() == '[') {
314 typename Range::iterator i = name.end() + 1, end = token.end();
315 for (;i != end; ++i) {
317 index = (index * 10) + (*i - '0');
322 if (i == token.end() || *i != ']')
323 throw string("unterminated index (looking for ']')");
325 throw string("illegal characters in token: ")
326 + string(name.begin(), name.end());
330 return find_node_aux(current->getChildImpl(name.begin(), name.end(),
331 index, create), itr, create,
335 // Internal function for parsing property paths. last_index provides
336 // and index value for the last node name token, if supplied.
337 template<typename Range>
339 find_node (SGPropertyNode * current,
344 using namespace boost;
345 typedef split_iterator<typename range_result_iterator<Range>::type>
348 PathSplitIterator itr
349 = make_split_iterator(path, first_finder("/", is_equal()));
350 if (*path.begin() == '/')
351 return find_node_aux(current->getRootNode(), itr, create, last_index);
353 return find_node_aux(current, itr, create, last_index);
356 ////////////////////////////////////////////////////////////////////////
357 // Private methods from SGPropertyNode (may be inlined for speed).
358 ////////////////////////////////////////////////////////////////////////
361 SGPropertyNode::get_bool () const
364 return static_cast<SGRawValue<bool>*>(_value.val)->getValue();
366 return _local_val.bool_val;
370 SGPropertyNode::get_int () const
373 return (static_cast<SGRawValue<int>*>(_value.val))->getValue();
375 return _local_val.int_val;
379 SGPropertyNode::get_long () const
382 return static_cast<SGRawValue<long>*>(_value.val)->getValue();
384 return _local_val.long_val;
388 SGPropertyNode::get_float () const
391 return static_cast<SGRawValue<float>*>(_value.val)->getValue();
393 return _local_val.float_val;
397 SGPropertyNode::get_double () const
400 return static_cast<SGRawValue<double>*>(_value.val)->getValue();
402 return _local_val.double_val;
406 SGPropertyNode::get_string () const
409 return static_cast<SGRawValue<const char*>*>(_value.val)->getValue();
411 return _local_val.string_val;
415 SGPropertyNode::set_bool (bool val)
418 if (static_cast<SGRawValue<bool>*>(_value.val)->setValue(val)) {
425 _local_val.bool_val = val;
432 SGPropertyNode::set_int (int val)
435 if (static_cast<SGRawValue<int>*>(_value.val)->setValue(val)) {
442 _local_val.int_val = val;
449 SGPropertyNode::set_long (long val)
452 if (static_cast<SGRawValue<long>*>(_value.val)->setValue(val)) {
459 _local_val.long_val = val;
466 SGPropertyNode::set_float (float val)
469 if (static_cast<SGRawValue<float>*>(_value.val)->setValue(val)) {
476 _local_val.float_val = val;
483 SGPropertyNode::set_double (double val)
486 if (static_cast<SGRawValue<double>*>(_value.val)->setValue(val)) {
493 _local_val.double_val = val;
500 SGPropertyNode::set_string (const char * val)
503 if (static_cast<SGRawValue<const char*>*>(_value.val)->setValue(val)) {
510 delete [] _local_val.string_val;
511 _local_val.string_val = copy_string(val);
518 SGPropertyNode::clearValue ()
520 if (_type == props::ALIAS) {
523 } else if (_type != props::NONE) {
526 _local_val.bool_val = SGRawValue<bool>::DefaultValue();
529 _local_val.int_val = SGRawValue<int>::DefaultValue();
532 _local_val.long_val = SGRawValue<long>::DefaultValue();
535 _local_val.float_val = SGRawValue<float>::DefaultValue();
538 _local_val.double_val = SGRawValue<double>::DefaultValue();
541 case props::UNSPECIFIED:
543 delete [] _local_val.string_val;
545 _local_val.string_val = 0;
547 default: // avoid compiler warning
559 * Get the value as a string.
562 SGPropertyNode::make_string () const
564 if (!getAttribute(READ))
568 return _value.alias->getStringValue();
570 return get_bool() ? "true" : "false";
572 case props::UNSPECIFIED:
591 sstr << std::setprecision(10) << get_double();
593 case props::EXTENDED:
595 props::Type realType = _value.val->getType();
596 // Perhaps this should be done for all types?
597 if (realType == props::VEC3D || realType == props::VEC4D)
599 static_cast<SGRawExtended*>(_value.val)->printOn(sstr);
605 _buffer = sstr.str();
606 return _buffer.c_str();
610 * Trace a write access for a property.
613 SGPropertyNode::trace_write () const
616 cerr << "TRACE: Write node " << getPath () << ", value \""
617 << make_string() << '"' << endl;
619 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Write node " << getPath()
620 << ", value \"" << make_string() << '"');
625 * Trace a read access for a property.
628 SGPropertyNode::trace_read () const
631 cerr << "TRACE: Write node " << getPath () << ", value \""
632 << make_string() << '"' << endl;
634 SG_LOG(SG_GENERAL, SG_ALERT, "TRACE: Read node " << getPath()
635 << ", value \"" << make_string() << '"');
639 ////////////////////////////////////////////////////////////////////////
640 // Public methods from SGPropertyNode.
641 ////////////////////////////////////////////////////////////////////////
644 * Last used attribute
645 * Update as needed when enum Attribute is changed
647 const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE;
650 * Default constructor: always creates a root node.
652 SGPropertyNode::SGPropertyNode ()
660 _local_val.string_val = 0;
668 SGPropertyNode::SGPropertyNode (const SGPropertyNode &node)
669 : SGReferenced(node),
672 _parent(0), // don't copy the parent
676 _listeners(0) // CHECK!!
678 _local_val.string_val = 0;
680 if (_type == props::NONE)
682 if (_type == props::ALIAS) {
683 _value.alias = node._value.alias;
688 if (_tied || _type == props::EXTENDED) {
689 _value.val = node._value.val->clone();
694 set_bool(node.get_bool());
697 set_int(node.get_int());
700 set_long(node.get_long());
703 set_float(node.get_float());
706 set_double(node.get_double());
709 case props::UNSPECIFIED:
710 set_string(node.get_string());
719 * Convenience constructor.
721 template<typename Itr>
722 SGPropertyNode::SGPropertyNode (Itr begin, Itr end,
724 SGPropertyNode * parent)
733 _local_val.string_val = 0;
735 if (!validateName(_name))
736 throw string("plain name expected instead of '") + _name + '\'';
739 SGPropertyNode::SGPropertyNode (const string& name,
741 SGPropertyNode * parent)
750 _local_val.string_val = 0;
752 if (!validateName(name))
753 throw string("plain name expected instead of '") + _name + '\'';
759 SGPropertyNode::~SGPropertyNode ()
761 // zero out all parent pointers, else they might be dangling
762 for (unsigned i = 0; i < _children.size(); ++i)
763 _children[i]->_parent = 0;
767 vector<SGPropertyChangeListener*>::iterator it;
768 for (it = _listeners->begin(); it != _listeners->end(); ++it)
769 (*it)->unregister_property(this);
776 * Alias to another node.
779 SGPropertyNode::alias (SGPropertyNode * target)
781 if (target && (_type != props::ALIAS) && (!_tied))
785 _value.alias = target;
786 _type = props::ALIAS;
794 SG_LOG(SG_GENERAL, SG_ALERT,
795 "Failed to create alias for " << getPath() << ". "
796 "The target property does not exist.");
799 if (_type == props::ALIAS)
801 if (_value.alias == target)
802 return true; // ok, identical alias requested
803 SG_LOG(SG_GENERAL, SG_ALERT,
804 "Failed to create alias at " << target->getPath() << ". "
805 "Source "<< getPath() << " is already aliasing another property.");
810 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". "
811 "Source " << getPath() << " is a tied property.");
820 * Alias to another node by path.
823 SGPropertyNode::alias (const char * path)
825 return alias(getNode(path, true));
833 SGPropertyNode::unalias ()
835 if (_type != props::ALIAS)
843 * Get the target of an alias.
846 SGPropertyNode::getAliasTarget ()
848 return (_type == props::ALIAS ? _value.alias : 0);
852 const SGPropertyNode *
853 SGPropertyNode::getAliasTarget () const
855 return (_type == props::ALIAS ? _value.alias : 0);
859 * create a non-const child by name after the last node with the same name.
862 SGPropertyNode::addChild(const char * name, int min_index, bool append)
865 ? std::max(find_last_child(name, _children) + 1, min_index)
866 : first_unused_index(name, _children, min_index);
868 SGPropertyNode_ptr node;
869 node = new SGPropertyNode(name, name + strlen(name), pos, this);
870 _children.push_back(node);
871 fireChildAdded(node);
876 * Create multiple children with unused indices
878 simgear::PropertyList
879 SGPropertyNode::addChildren( const std::string& name,
884 simgear::PropertyList nodes;
885 std::set<int> used_indices;
889 // First grab all used indices. This saves us of testing every index if it
890 // is used for every element to be created
891 for( size_t i = 0; i < nodes.size(); i++ )
893 const SGPropertyNode* node = nodes[i];
895 if( node->getNameString() == name && node->getIndex() >= min_index )
896 used_indices.insert(node->getIndex());
901 // If we don't want to fill the holes just find last node
902 min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index);
905 for( int index = min_index;
906 index < std::numeric_limits<int>::max() && nodes.size() < count;
909 if( used_indices.find(index) == used_indices.end() )
911 SGPropertyNode_ptr node;
912 node = new SGPropertyNode(name, index, this);
913 _children.push_back(node);
914 fireChildAdded(node);
915 nodes.push_back(node);
923 * Get a non-const child by index.
926 SGPropertyNode::getChild (int position)
928 if (position >= 0 && position < nChildren())
929 return _children[position];
936 * Get a const child by index.
938 const SGPropertyNode *
939 SGPropertyNode::getChild (int position) const
941 if (position >= 0 && position < nChildren())
942 return _children[position];
949 * Get a non-const child by name and index, creating if necessary.
953 SGPropertyNode::getChild (const char * name, int index, bool create)
955 return getChildImpl(name, name + strlen(name), index, create);
959 SGPropertyNode::getChild (const string& name, int index, bool create)
961 SGPropertyNode* node = getExistingChild(name.begin(), name.end(), index);
965 node = new SGPropertyNode(name, index, this);
966 _children.push_back(node);
967 fireChildAdded(node);
975 * Get a const child by name and index.
977 const SGPropertyNode *
978 SGPropertyNode::getChild (const char * name, int index) const
980 int pos = find_child(name, name + strlen(name), index, _children);
982 return _children[pos];
989 * Get all children with the same name (but different indices).
992 SGPropertyNode::getChildren (const char * name) const
994 PropertyList children;
995 size_t max = _children.size();
997 for (size_t i = 0; i < max; i++)
998 if (compare_strings(_children[i]->getName(), name))
999 children.push_back(_children[i]);
1001 sort(children.begin(), children.end(), CompareIndices());
1005 //------------------------------------------------------------------------------
1006 bool SGPropertyNode::removeChild(SGPropertyNode* node)
1008 if( node->_parent != this )
1011 PropertyList::iterator it =
1012 std::find(_children.begin(), _children.end(), node);
1013 if( it == _children.end() )
1020 //------------------------------------------------------------------------------
1021 SGPropertyNode_ptr SGPropertyNode::removeChild(int pos)
1023 if (pos < 0 || pos >= (int)_children.size())
1024 return SGPropertyNode_ptr();
1026 return eraseChild(_children.begin() + pos);
1031 * Remove a child node
1034 SGPropertyNode::removeChild(const char * name, int index)
1036 SGPropertyNode_ptr ret;
1037 int pos = find_child(name, name + strlen(name), index, _children);
1039 ret = removeChild(pos);
1045 * Remove all children with the specified name.
1048 SGPropertyNode::removeChildren(const char * name)
1050 PropertyList children;
1052 for (int pos = static_cast<int>(_children.size() - 1); pos >= 0; pos--)
1053 if (compare_strings(_children[pos]->getName(), name))
1054 children.push_back(removeChild(pos));
1056 sort(children.begin(), children.end(), CompareIndices());
1061 SGPropertyNode::removeAllChildren()
1063 for(unsigned i = 0; i < _children.size(); ++i)
1065 SGPropertyNode_ptr& node = _children[i];
1067 node->setAttribute(REMOVED, true);
1069 fireChildRemoved(node);
1076 SGPropertyNode::getDisplayName (bool simplify) const
1078 string display_name = _name;
1079 if (_index != 0 || !simplify) {
1081 sstr << '[' << _index << ']';
1082 display_name += sstr.str();
1084 return display_name;
1088 SGPropertyNode::getPath (bool simplify) const
1090 typedef std::vector<SGConstPropertyNode_ptr> PList;
1092 for (const SGPropertyNode* node = this; node->_parent; node = node->_parent)
1093 pathList.push_back(node);
1095 for (PList::reverse_iterator itr = pathList.rbegin(),
1096 rend = pathList.rend();
1100 result += (*itr)->getDisplayName(simplify);
1106 SGPropertyNode::getType () const
1108 if (_type == props::ALIAS)
1109 return _value.alias->getType();
1110 else if (_type == props::EXTENDED)
1111 return _value.val->getType();
1118 SGPropertyNode::getBoolValue () const
1120 // Shortcut for common case
1121 if (_attr == (READ|WRITE) && _type == props::BOOL)
1124 if (getAttribute(TRACE_READ))
1126 if (!getAttribute(READ))
1127 return SGRawValue<bool>::DefaultValue();
1130 return _value.alias->getBoolValue();
1134 return get_int() == 0 ? false : true;
1136 return get_long() == 0L ? false : true;
1138 return get_float() == 0.0 ? false : true;
1140 return get_double() == 0.0L ? false : true;
1142 case props::UNSPECIFIED:
1143 return (compare_strings(get_string(), "true") || getDoubleValue() != 0.0L);
1146 return SGRawValue<bool>::DefaultValue();
1151 SGPropertyNode::getIntValue () const
1153 // Shortcut for common case
1154 if (_attr == (READ|WRITE) && _type == props::INT)
1157 if (getAttribute(TRACE_READ))
1159 if (!getAttribute(READ))
1160 return SGRawValue<int>::DefaultValue();
1163 return _value.alias->getIntValue();
1165 return int(get_bool());
1169 return int(get_long());
1171 return int(get_float());
1173 return int(get_double());
1175 case props::UNSPECIFIED:
1176 return atoi(get_string());
1179 return SGRawValue<int>::DefaultValue();
1184 SGPropertyNode::getLongValue () const
1186 // Shortcut for common case
1187 if (_attr == (READ|WRITE) && _type == props::LONG)
1190 if (getAttribute(TRACE_READ))
1192 if (!getAttribute(READ))
1193 return SGRawValue<long>::DefaultValue();
1196 return _value.alias->getLongValue();
1198 return long(get_bool());
1200 return long(get_int());
1204 return long(get_float());
1206 return long(get_double());
1208 case props::UNSPECIFIED:
1209 return strtol(get_string(), 0, 0);
1212 return SGRawValue<long>::DefaultValue();
1217 SGPropertyNode::getFloatValue () const
1219 // Shortcut for common case
1220 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1223 if (getAttribute(TRACE_READ))
1225 if (!getAttribute(READ))
1226 return SGRawValue<float>::DefaultValue();
1229 return _value.alias->getFloatValue();
1231 return float(get_bool());
1233 return float(get_int());
1235 return float(get_long());
1239 return float(get_double());
1241 case props::UNSPECIFIED:
1242 return atof(get_string());
1245 return SGRawValue<float>::DefaultValue();
1250 SGPropertyNode::getDoubleValue () const
1252 // Shortcut for common case
1253 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1254 return get_double();
1256 if (getAttribute(TRACE_READ))
1258 if (!getAttribute(READ))
1259 return SGRawValue<double>::DefaultValue();
1263 return _value.alias->getDoubleValue();
1265 return double(get_bool());
1267 return double(get_int());
1269 return double(get_long());
1271 return double(get_float());
1273 return get_double();
1275 case props::UNSPECIFIED:
1276 return strtod(get_string(), 0);
1279 return SGRawValue<double>::DefaultValue();
1284 SGPropertyNode::getStringValue () const
1286 // Shortcut for common case
1287 if (_attr == (READ|WRITE) && _type == props::STRING)
1288 return get_string();
1290 if (getAttribute(TRACE_READ))
1292 if (!getAttribute(READ))
1293 return SGRawValue<const char *>::DefaultValue();
1294 return make_string();
1298 SGPropertyNode::setBoolValue (bool value)
1300 // Shortcut for common case
1301 if (_attr == (READ|WRITE) && _type == props::BOOL)
1302 return set_bool(value);
1304 bool result = false;
1306 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1309 _type = props::BOOL;
1314 result = _value.alias->setBoolValue(value);
1317 result = set_bool(value);
1320 result = set_int(int(value));
1323 result = set_long(long(value));
1326 result = set_float(float(value));
1329 result = set_double(double(value));
1332 case props::UNSPECIFIED:
1333 result = set_string(value ? "true" : "false");
1340 if (getAttribute(TRACE_WRITE))
1346 SGPropertyNode::setIntValue (int value)
1348 // Shortcut for common case
1349 if (_attr == (READ|WRITE) && _type == props::INT)
1350 return set_int(value);
1352 bool result = false;
1354 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1357 _local_val.int_val = 0;
1362 result = _value.alias->setIntValue(value);
1365 result = set_bool(value == 0 ? false : true);
1368 result = set_int(value);
1371 result = set_long(long(value));
1374 result = set_float(float(value));
1377 result = set_double(double(value));
1380 case props::UNSPECIFIED: {
1382 sprintf(buf, "%d", value);
1383 result = set_string(buf);
1391 if (getAttribute(TRACE_WRITE))
1397 SGPropertyNode::setLongValue (long value)
1399 // Shortcut for common case
1400 if (_attr == (READ|WRITE) && _type == props::LONG)
1401 return set_long(value);
1403 bool result = false;
1405 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1407 _type = props::LONG;
1408 _local_val.long_val = 0L;
1413 result = _value.alias->setLongValue(value);
1416 result = set_bool(value == 0L ? false : true);
1419 result = set_int(int(value));
1422 result = set_long(value);
1425 result = set_float(float(value));
1428 result = set_double(double(value));
1431 case props::UNSPECIFIED: {
1433 sprintf(buf, "%ld", value);
1434 result = set_string(buf);
1442 if (getAttribute(TRACE_WRITE))
1448 SGPropertyNode::setFloatValue (float value)
1450 // Shortcut for common case
1451 if (_attr == (READ|WRITE) && _type == props::FLOAT)
1452 return set_float(value);
1454 bool result = false;
1456 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1458 _type = props::FLOAT;
1459 _local_val.float_val = 0;
1464 result = _value.alias->setFloatValue(value);
1467 result = set_bool(value == 0.0 ? false : true);
1470 result = set_int(int(value));
1473 result = set_long(long(value));
1476 result = set_float(value);
1479 result = set_double(double(value));
1482 case props::UNSPECIFIED: {
1484 sprintf(buf, "%f", value);
1485 result = set_string(buf);
1493 if (getAttribute(TRACE_WRITE))
1499 SGPropertyNode::setDoubleValue (double value)
1501 // Shortcut for common case
1502 if (_attr == (READ|WRITE) && _type == props::DOUBLE)
1503 return set_double(value);
1505 bool result = false;
1507 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1509 _local_val.double_val = value;
1510 _type = props::DOUBLE;
1515 result = _value.alias->setDoubleValue(value);
1518 result = set_bool(value == 0.0L ? false : true);
1521 result = set_int(int(value));
1524 result = set_long(long(value));
1527 result = set_float(float(value));
1530 result = set_double(value);
1533 case props::UNSPECIFIED: {
1535 sprintf(buf, "%f", value);
1536 result = set_string(buf);
1544 if (getAttribute(TRACE_WRITE))
1550 SGPropertyNode::setStringValue (const char * value)
1552 // Shortcut for common case
1553 if (_attr == (READ|WRITE) && _type == props::STRING)
1554 return set_string(value);
1556 bool result = false;
1558 if (_type == props::NONE || _type == props::UNSPECIFIED) {
1560 _type = props::STRING;
1565 result = _value.alias->setStringValue(value);
1568 result = set_bool((compare_strings(value, "true")
1569 || atoi(value)) ? true : false);
1572 result = set_int(atoi(value));
1575 result = set_long(strtol(value, 0, 0));
1578 result = set_float(atof(value));
1581 result = set_double(strtod(value, 0));
1584 case props::UNSPECIFIED:
1585 result = set_string(value);
1587 case props::EXTENDED:
1589 stringstream sstr(value);
1590 static_cast<SGRawExtended*>(_value.val)->readFrom(sstr);
1598 if (getAttribute(TRACE_WRITE))
1604 SGPropertyNode::setUnspecifiedValue (const char * value)
1606 bool result = false;
1608 if (_type == props::NONE) {
1610 _type = props::UNSPECIFIED;
1612 props::Type type = _type;
1613 if (type == props::EXTENDED)
1614 type = _value.val->getType();
1617 result = _value.alias->setUnspecifiedValue(value);
1620 result = set_bool((compare_strings(value, "true")
1621 || atoi(value)) ? true : false);
1624 result = set_int(atoi(value));
1627 result = set_long(strtol(value, 0, 0));
1630 result = set_float(atof(value));
1633 result = set_double(strtod(value, 0));
1636 case props::UNSPECIFIED:
1637 result = set_string(value);
1640 result = static_cast<SGRawValue<SGVec3d>*>(_value.val)->setValue(parseString<SGVec3d>(value));
1643 result = static_cast<SGRawValue<SGVec4d>*>(_value.val)->setValue(parseString<SGVec4d>(value));
1650 if (getAttribute(TRACE_WRITE))
1655 //------------------------------------------------------------------------------
1656 bool SGPropertyNode::interpolate( const std::string& type,
1657 const SGPropertyNode& target,
1659 const std::string& easing )
1661 if( !_interpolation_mgr )
1663 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1665 // no interpolation possible -> set to target immediately
1666 setUnspecifiedValue( target.getStringValue() );
1670 return _interpolation_mgr->interpolate(this, type, target, duration, easing);
1673 //------------------------------------------------------------------------------
1674 bool SGPropertyNode::interpolate( const std::string& type,
1675 const PropertyList& values,
1676 const double_list& deltas,
1677 const std::string& easing )
1679 if( !_interpolation_mgr )
1681 SG_LOG(SG_GENERAL, SG_WARN, "No property interpolator available");
1683 // no interpolation possible -> set to last value immediately
1684 if( !values.empty() )
1685 setUnspecifiedValue(values.back()->getStringValue());
1689 return _interpolation_mgr->interpolate(this, type, values, deltas, easing);
1692 //------------------------------------------------------------------------------
1693 void SGPropertyNode::setInterpolationMgr(simgear::PropertyInterpolationMgr* mgr)
1695 _interpolation_mgr = mgr;
1698 simgear::PropertyInterpolationMgr* SGPropertyNode::_interpolation_mgr = 0;
1700 //------------------------------------------------------------------------------
1701 std::ostream& SGPropertyNode::printOn(std::ostream& stream) const
1703 if (!getAttribute(READ))
1707 return _value.alias->printOn(stream);
1709 stream << (get_bool() ? "true" : "false");
1712 stream << get_int();
1715 stream << get_long();
1718 stream << get_float();
1721 stream << get_double();
1724 case props::UNSPECIFIED:
1725 stream << get_string();
1727 case props::EXTENDED:
1728 static_cast<SGRawExtended*>(_value.val)->printOn(stream);
1732 default: // avoid compiler warning
1739 bool SGPropertyNode::tie (const SGRawValue<const char *> &rawValue,
1742 if (_type == props::ALIAS || _tied)
1745 useDefault = useDefault && hasValue();
1746 std::string old_val;
1748 old_val = getStringValue();
1750 _type = props::STRING;
1752 _value.val = rawValue.clone();
1755 int save_attributes = getAttributes();
1756 setAttribute( WRITE, true );
1757 setStringValue(old_val.c_str());
1758 setAttributes( save_attributes );
1764 SGPropertyNode::untie ()
1771 bool val = getBoolValue();
1773 _type = props::BOOL;
1774 _local_val.bool_val = val;
1778 int val = getIntValue();
1781 _local_val.int_val = val;
1785 long val = getLongValue();
1787 _type = props::LONG;
1788 _local_val.long_val = val;
1791 case props::FLOAT: {
1792 float val = getFloatValue();
1794 _type = props::FLOAT;
1795 _local_val.float_val = val;
1798 case props::DOUBLE: {
1799 double val = getDoubleValue();
1801 _type = props::DOUBLE;
1802 _local_val.double_val = val;
1806 case props::UNSPECIFIED: {
1807 string val = getStringValue();
1809 _type = props::STRING;
1810 _local_val.string_val = copy_string(val.c_str());
1813 case props::EXTENDED: {
1814 SGRawExtended* val = static_cast<SGRawExtended*>(_value.val);
1815 _value.val = 0; // Prevent clearValue() from deleting
1817 _type = props::EXTENDED;
1818 _value.val = val->makeContainer();
1832 SGPropertyNode::getRootNode ()
1837 return _parent->getRootNode();
1840 const SGPropertyNode *
1841 SGPropertyNode::getRootNode () const
1846 return _parent->getRootNode();
1850 SGPropertyNode::getNode (const char * relative_path, bool create)
1852 using namespace boost;
1854 return find_node(this, make_iterator_range(relative_path, relative_path
1855 + strlen(relative_path)),
1860 SGPropertyNode::getNode (const char * relative_path, int index, bool create)
1862 using namespace boost;
1863 return find_node(this, make_iterator_range(relative_path, relative_path
1864 + strlen(relative_path)),
1868 const SGPropertyNode *
1869 SGPropertyNode::getNode (const char * relative_path) const
1871 return ((SGPropertyNode *)this)->getNode(relative_path, false);
1874 const SGPropertyNode *
1875 SGPropertyNode::getNode (const char * relative_path, int index) const
1877 return ((SGPropertyNode *)this)->getNode(relative_path, index, false);
1880 ////////////////////////////////////////////////////////////////////////
1881 // Convenience methods using relative paths.
1882 ////////////////////////////////////////////////////////////////////////
1886 * Test whether another node has a value attached.
1889 SGPropertyNode::hasValue (const char * relative_path) const
1891 const SGPropertyNode * node = getNode(relative_path);
1892 return (node == 0 ? false : node->hasValue());
1897 * Get the value type for another node.
1900 SGPropertyNode::getType (const char * relative_path) const
1902 const SGPropertyNode * node = getNode(relative_path);
1903 return (node == 0 ? props::UNSPECIFIED : node->getType());
1908 * Get a bool value for another node.
1911 SGPropertyNode::getBoolValue (const char * relative_path,
1912 bool defaultValue) const
1914 const SGPropertyNode * node = getNode(relative_path);
1915 return (node == 0 ? defaultValue : node->getBoolValue());
1920 * Get an int value for another node.
1923 SGPropertyNode::getIntValue (const char * relative_path,
1924 int defaultValue) const
1926 const SGPropertyNode * node = getNode(relative_path);
1927 return (node == 0 ? defaultValue : node->getIntValue());
1932 * Get a long value for another node.
1935 SGPropertyNode::getLongValue (const char * relative_path,
1936 long defaultValue) const
1938 const SGPropertyNode * node = getNode(relative_path);
1939 return (node == 0 ? defaultValue : node->getLongValue());
1944 * Get a float value for another node.
1947 SGPropertyNode::getFloatValue (const char * relative_path,
1948 float defaultValue) const
1950 const SGPropertyNode * node = getNode(relative_path);
1951 return (node == 0 ? defaultValue : node->getFloatValue());
1956 * Get a double value for another node.
1959 SGPropertyNode::getDoubleValue (const char * relative_path,
1960 double defaultValue) const
1962 const SGPropertyNode * node = getNode(relative_path);
1963 return (node == 0 ? defaultValue : node->getDoubleValue());
1968 * Get a string value for another node.
1971 SGPropertyNode::getStringValue (const char * relative_path,
1972 const char * defaultValue) const
1974 const SGPropertyNode * node = getNode(relative_path);
1975 return (node == 0 ? defaultValue : node->getStringValue());
1980 * Set a bool value for another node.
1983 SGPropertyNode::setBoolValue (const char * relative_path, bool value)
1985 return getNode(relative_path, true)->setBoolValue(value);
1990 * Set an int value for another node.
1993 SGPropertyNode::setIntValue (const char * relative_path, int value)
1995 return getNode(relative_path, true)->setIntValue(value);
2000 * Set a long value for another node.
2003 SGPropertyNode::setLongValue (const char * relative_path, long value)
2005 return getNode(relative_path, true)->setLongValue(value);
2010 * Set a float value for another node.
2013 SGPropertyNode::setFloatValue (const char * relative_path, float value)
2015 return getNode(relative_path, true)->setFloatValue(value);
2020 * Set a double value for another node.
2023 SGPropertyNode::setDoubleValue (const char * relative_path, double value)
2025 return getNode(relative_path, true)->setDoubleValue(value);
2030 * Set a string value for another node.
2033 SGPropertyNode::setStringValue (const char * relative_path, const char * value)
2035 return getNode(relative_path, true)->setStringValue(value);
2040 * Set an unknown value for another node.
2043 SGPropertyNode::setUnspecifiedValue (const char * relative_path,
2046 return getNode(relative_path, true)->setUnspecifiedValue(value);
2051 * Test whether another node is tied.
2054 SGPropertyNode::isTied (const char * relative_path) const
2056 const SGPropertyNode * node = getNode(relative_path);
2057 return (node == 0 ? false : node->isTied());
2062 * Tie a node reached by a relative path, creating it if necessary.
2065 SGPropertyNode::tie (const char * relative_path,
2066 const SGRawValue<bool> &rawValue,
2069 return getNode(relative_path, true)->tie(rawValue, useDefault);
2074 * Tie a node reached by a relative path, creating it if necessary.
2077 SGPropertyNode::tie (const char * relative_path,
2078 const SGRawValue<int> &rawValue,
2081 return getNode(relative_path, true)->tie(rawValue, useDefault);
2086 * Tie a node reached by a relative path, creating it if necessary.
2089 SGPropertyNode::tie (const char * relative_path,
2090 const SGRawValue<long> &rawValue,
2093 return getNode(relative_path, true)->tie(rawValue, useDefault);
2098 * Tie a node reached by a relative path, creating it if necessary.
2101 SGPropertyNode::tie (const char * relative_path,
2102 const SGRawValue<float> &rawValue,
2105 return getNode(relative_path, true)->tie(rawValue, useDefault);
2110 * Tie a node reached by a relative path, creating it if necessary.
2113 SGPropertyNode::tie (const char * relative_path,
2114 const SGRawValue<double> &rawValue,
2117 return getNode(relative_path, true)->tie(rawValue, useDefault);
2122 * Tie a node reached by a relative path, creating it if necessary.
2125 SGPropertyNode::tie (const char * relative_path,
2126 const SGRawValue<const char *> &rawValue,
2129 return getNode(relative_path, true)->tie(rawValue, useDefault);
2134 * Attempt to untie another node reached by a relative path.
2137 SGPropertyNode::untie (const char * relative_path)
2139 SGPropertyNode * node = getNode(relative_path);
2140 return (node == 0 ? false : node->untie());
2144 SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener,
2147 if (_listeners == 0)
2148 _listeners = new vector<SGPropertyChangeListener*>;
2149 _listeners->push_back(listener);
2150 listener->register_property(this);
2152 listener->valueChanged(this);
2156 SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener)
2158 if (_listeners == 0)
2160 vector<SGPropertyChangeListener*>::iterator it =
2161 find(_listeners->begin(), _listeners->end(), listener);
2162 if (it != _listeners->end()) {
2163 _listeners->erase(it);
2164 listener->unregister_property(this);
2165 if (_listeners->empty()) {
2166 vector<SGPropertyChangeListener*>* tmp = _listeners;
2174 SGPropertyNode::fireValueChanged ()
2176 fireValueChanged(this);
2180 SGPropertyNode::fireChildAdded (SGPropertyNode * child)
2182 fireChildAdded(this, child);
2186 SGPropertyNode::fireCreatedRecursive(bool fire_self)
2190 _parent->fireChildAdded(this);
2192 if( _children.empty() && getType() != simgear::props::NONE )
2193 return fireValueChanged();
2196 for(size_t i = 0; i < _children.size(); ++i)
2197 _children[i]->fireCreatedRecursive(true);
2201 SGPropertyNode::fireChildRemoved (SGPropertyNode * child)
2203 fireChildRemoved(this, child);
2207 SGPropertyNode::fireChildrenRemovedRecursive()
2209 for(size_t i = 0; i < _children.size(); ++i)
2211 SGPropertyNode* child = _children[i];
2212 fireChildRemoved(this, child);
2213 child->fireChildrenRemovedRecursive();
2218 SGPropertyNode::fireValueChanged (SGPropertyNode * node)
2220 if (_listeners != 0) {
2221 for (unsigned int i = 0; i < _listeners->size(); i++) {
2222 (*_listeners)[i]->valueChanged(node);
2226 _parent->fireValueChanged(node);
2230 SGPropertyNode::fireChildAdded (SGPropertyNode * parent,
2231 SGPropertyNode * child)
2233 if (_listeners != 0) {
2234 for (unsigned int i = 0; i < _listeners->size(); i++) {
2235 (*_listeners)[i]->childAdded(parent, child);
2239 _parent->fireChildAdded(parent, child);
2243 SGPropertyNode::fireChildRemoved (SGPropertyNode * parent,
2244 SGPropertyNode * child)
2246 if (_listeners != 0) {
2247 for (unsigned int i = 0; i < _listeners->size(); i++) {
2248 (*_listeners)[i]->childRemoved(parent, child);
2252 _parent->fireChildRemoved(parent, child);
2255 //------------------------------------------------------------------------------
2257 SGPropertyNode::eraseChild(simgear::PropertyList::iterator child)
2259 SGPropertyNode_ptr node = *child;
2260 node->setAttribute(REMOVED, true);
2262 fireChildRemoved(node);
2264 _children.erase(child);
2268 ////////////////////////////////////////////////////////////////////////
2269 // Implementation of SGPropertyChangeListener.
2270 ////////////////////////////////////////////////////////////////////////
2272 SGPropertyChangeListener::~SGPropertyChangeListener ()
2274 for (int i = static_cast<int>(_properties.size() - 1); i >= 0; i--)
2275 _properties[i]->removeChangeListener(this);
2279 SGPropertyChangeListener::valueChanged (SGPropertyNode * node)
2285 SGPropertyChangeListener::childAdded (SGPropertyNode * node,
2286 SGPropertyNode * child)
2292 SGPropertyChangeListener::childRemoved (SGPropertyNode * parent,
2293 SGPropertyNode * child)
2299 SGPropertyChangeListener::register_property (SGPropertyNode * node)
2301 _properties.push_back(node);
2305 SGPropertyChangeListener::unregister_property (SGPropertyNode * node)
2307 vector<SGPropertyNode *>::iterator it =
2308 find(_properties.begin(), _properties.end(), node);
2309 if (it != _properties.end())
2310 _properties.erase(it);
2314 std::ostream& SGRawBase<SGVec3d>::printOn(std::ostream& stream) const
2317 = static_cast<const SGRawValue<SGVec3d>*>(this)->getValue();
2318 for (int i = 0; i < 3; ++i) {
2329 std::istream& readFrom<SGVec3d>(std::istream& stream, SGVec3d& result)
2331 for (int i = 0; i < 3; ++i) {
2332 stream >> result[i];
2338 std::ostream& SGRawBase<SGVec4d>::printOn(std::ostream& stream) const
2341 = static_cast<const SGRawValue<SGVec4d>*>(this)->getValue();
2342 for (int i = 0; i < 4; ++i) {
2353 std::istream& readFrom<SGVec4d>(std::istream& stream, SGVec4d& result)
2355 for (int i = 0; i < 4; ++i) {
2356 stream >> result[i];
2363 bool compareNodeValue(const SGPropertyNode& lhs, const SGPropertyNode& rhs)
2365 props::Type ltype = lhs.getType();
2366 props::Type rtype = rhs.getType();
2373 return false; // XXX Should we look in aliases?
2375 return lhs.getValue<bool>() == rhs.getValue<bool>();
2377 return lhs.getValue<int>() == rhs.getValue<int>();
2379 return lhs.getValue<long>() == rhs.getValue<long>();
2381 return lhs.getValue<float>() == rhs.getValue<float>();
2383 return lhs.getValue<double>() == rhs.getValue<double>();
2385 case props::UNSPECIFIED:
2386 return !strcmp(lhs.getStringValue(), rhs.getStringValue());
2388 return lhs.getValue<SGVec3d>() == rhs.getValue<SGVec3d>();
2390 return lhs.getValue<SGVec4d>() == rhs.getValue<SGVec4d>();
2398 bool SGPropertyNode::compare(const SGPropertyNode& lhs,
2399 const SGPropertyNode& rhs)
2403 int lhsChildren = lhs.nChildren();
2404 int rhsChildren = rhs.nChildren();
2405 if (lhsChildren != rhsChildren)
2407 if (lhsChildren == 0)
2408 return compareNodeValue(lhs, rhs);
2409 for (size_t i = 0; i < lhs._children.size(); ++i) {
2410 const SGPropertyNode* lchild = lhs._children[i];
2411 const SGPropertyNode* rchild = rhs._children[i];
2412 // I'm guessing that the nodes will usually be in the same
2414 if (lchild->getIndex() != rchild->getIndex()
2415 || lchild->getNameString() != rchild->getNameString()) {
2417 for (PropertyList::const_iterator itr = rhs._children.begin(),
2418 end = rhs._children.end();
2421 if (lchild->getIndex() == (*itr)->getIndex()
2422 && lchild->getNameString() == (*itr)->getNameString()) {
2429 if (!compare(*lchild, *rchild))
2435 struct PropertyPlaceLess {
2436 typedef bool result_type;
2437 bool operator()(SGPropertyNode_ptr lhs, SGPropertyNode_ptr rhs) const
2439 int comp = lhs->getNameString().compare(rhs->getNameString());
2441 return lhs->getIndex() < rhs->getIndex();
2447 size_t hash_value(const SGPropertyNode& node)
2449 using namespace boost;
2451 if (node.nChildren() == 0) {
2452 switch (node.getType()) {
2457 return hash_value(node.getValue<bool>());
2459 return hash_value(node.getValue<int>());
2461 return hash_value(node.getValue<long>());
2463 return hash_value(node.getValue<float>());
2465 return hash_value(node.getValue<double>());
2467 case props::UNSPECIFIED:
2469 const char *val = node.getStringValue();
2470 return hash_range(val, val + strlen(val));
2474 const SGVec3d val = node.getValue<SGVec3d>();
2475 return hash_range(&val[0], &val[3]);
2479 const SGVec4d val = node.getValue<SGVec4d>();
2480 return hash_range(&val[0], &val[4]);
2482 case props::ALIAS: // XXX Should we look in aliases?
2488 PropertyList children(node._children.begin(), node._children.end());
2489 sort(children.begin(), children.end(), PropertyPlaceLess());
2490 for (PropertyList::const_iterator itr = children.begin(),
2491 end = children.end();
2494 hash_combine(seed, (*itr)->_name);
2495 hash_combine(seed, (*itr)->_index);
2496 hash_combine(seed, hash_value(**itr));