X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fprops%2Fprops.cxx;h=09b71fb44c6b91a158fdefd0a86707d65f665cd7;hb=e24e3c06122dcba0d15a3d01be8957bb6b96d739;hp=3d63758e143707fe38b100728e8ac6f7534f2e99;hpb=284a9e061280535de01b0e3f36a9b85ef8178e61;p=simgear.git diff --git a/simgear/props/props.cxx b/simgear/props/props.cxx index 3d63758e..09b71fb4 100644 --- a/simgear/props/props.cxx +++ b/simgear/props/props.cxx @@ -11,9 +11,12 @@ #endif #include "props.hxx" +#include "vectorPropTemplates.hxx" #include +#include +#include #include #include #include @@ -27,8 +30,6 @@ #include #include -#include - #if PROPS_STANDALONE #include #else @@ -217,6 +218,26 @@ find_last_child (const char * name, const PropertyList& nodes) return index; } +/** + * Get first unused index for child nodes with the given name + */ +static int +first_unused_index( const char * name, + const PropertyList& nodes, + int min_index ) +{ + const char* nameEnd = name + strlen(name); + + for( int index = min_index; index < std::numeric_limits::max(); ++index ) + { + if( find_child(name, nameEnd, index, nodes) < 0 ) + return index; + } + + SG_LOG(SG_GENERAL, SG_ALERT, "Too much nodes: " << name); + return -1; +} + template inline SGPropertyNode* SGPropertyNode::getExistingChild (Itr begin, Itr end, int index, bool create) @@ -642,7 +663,7 @@ SGPropertyNode::trace_read () const * Last used attribute * Update as needed when enum Attribute is changed */ -const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE; +const int SGPropertyNode::LAST_USED_ATTRIBUTE = PRESERVE; /** * Default constructor: always creates a root node. @@ -650,7 +671,6 @@ const int SGPropertyNode::LAST_USED_ATTRIBUTE = USERARCHIVE; SGPropertyNode::SGPropertyNode () : _index(0), _parent(0), - _path_cache(0), _type(props::NONE), _tied(false), _attr(READ|WRITE), @@ -665,10 +685,10 @@ SGPropertyNode::SGPropertyNode () * Copy constructor. */ SGPropertyNode::SGPropertyNode (const SGPropertyNode &node) - : _index(node._index), + : SGReferenced(node), + _index(node._index), _name(node._name), _parent(0), // don't copy the parent - _path_cache(0), _type(node._type), _tied(node._tied), _attr(node._attr), @@ -724,7 +744,6 @@ SGPropertyNode::SGPropertyNode (Itr begin, Itr end, : _index(index), _name(begin, end), _parent(parent), - _path_cache(0), _type(props::NONE), _tied(false), _attr(READ|WRITE), @@ -742,7 +761,6 @@ SGPropertyNode::SGPropertyNode (const string& name, : _index(index), _name(name), _parent(parent), - _path_cache(0), _type(props::NONE), _tied(false), _attr(READ|WRITE), @@ -764,7 +782,6 @@ SGPropertyNode::~SGPropertyNode () _children[i]->_parent = 0; for (unsigned i = 0; i < _removedChildren.size(); ++i) _removedChildren[i]->_parent = 0; - delete _path_cache; clearValue(); if (_listeners) { @@ -782,13 +799,41 @@ SGPropertyNode::~SGPropertyNode () bool SGPropertyNode::alias (SGPropertyNode * target) { - if (target == 0 || _type == props::ALIAS || _tied) - return false; - clearValue(); - get(target); - _value.alias = target; - _type = props::ALIAS; - return true; + if (target && (_type != props::ALIAS) && (!_tied)) + { + clearValue(); + get(target); + _value.alias = target; + _type = props::ALIAS; + return true; + } + +#if PROPS_STANDALONE +#else + if (!target) + { + SG_LOG(SG_GENERAL, SG_ALERT, + "Failed to create alias for " << getPath() << ". " + "The target property does not exist."); + } + else + if (_type == props::ALIAS) + { + if (_value.alias == target) + return true; // ok, identical alias requested + SG_LOG(SG_GENERAL, SG_ALERT, + "Failed to create alias at " << target->getPath() << ". " + "Source "<< getPath() << " is already aliasing another property."); + } + else + if (_tied) + { + SG_LOG(SG_GENERAL, SG_ALERT, "Failed to create alias at " << target->getPath() << ". " + "Source " << getPath() << " is a tied property."); + } +#endif + + return false; } @@ -835,9 +880,11 @@ SGPropertyNode::getAliasTarget () const * create a non-const child by name after the last node with the same name. */ SGPropertyNode * -SGPropertyNode::addChild (const char * name) +SGPropertyNode::addChild(const char * name, int min_index, bool append) { - int pos = find_last_child(name, _children)+1; + int pos = append + ? std::max(find_last_child(name, _children) + 1, min_index) + : first_unused_index(name, _children, min_index); SGPropertyNode_ptr node; node = new SGPropertyNode(name, name + strlen(name), pos, this); @@ -846,6 +893,52 @@ SGPropertyNode::addChild (const char * name) return node; } +/** + * Create multiple children with unused indices + */ +simgear::PropertyList +SGPropertyNode::addChildren( const std::string& name, + size_t count, + int min_index, + bool append ) +{ + simgear::PropertyList nodes; + std::set used_indices; + + if( !append ) + { + // First grab all used indices. This saves us of testing every index if it + // is used for every element to be created + for( size_t i = 0; i < nodes.size(); i++ ) + { + const SGPropertyNode* node = nodes[i]; + + if( node->getNameString() == name && node->getIndex() >= min_index ) + used_indices.insert(node->getIndex()); + } + } + else + { + // If we don't want to fill the holes just find last node + min_index = std::max(find_last_child(name.c_str(), _children) + 1, min_index); + } + + for( int index = min_index; + index < std::numeric_limits::max() && nodes.size() < count; + ++index ) + { + if( used_indices.find(index) == used_indices.end() ) + { + SGPropertyNode_ptr node; + node = new SGPropertyNode(name, index, this); + _children.push_back(node); + fireChildAdded(node); + nodes.push_back(node); + } + } + + return nodes; +} /** * Get a non-const child by index. @@ -932,22 +1025,6 @@ SGPropertyNode::getChildren (const char * name) const } -/** - * Remove this node and all children from nodes that link to them - * in their path cache. - */ -void -SGPropertyNode::remove_from_path_caches () -{ - for (unsigned int i = 0; i < _children.size(); ++i) - _children[i]->remove_from_path_caches(); - - for (unsigned int i = 0; i < _linkedNodes.size(); i++) - _linkedNodes[i]->erase(this); - _linkedNodes.clear(); -} - - /** * Remove child by position. */ @@ -966,7 +1043,6 @@ SGPropertyNode::removeChild (int pos, bool keep) _removedChildren.push_back(node); } - node->remove_from_path_caches(); node->setAttribute(REMOVED, true); node->clearValue(); fireChildRemoved(node); @@ -1004,25 +1080,6 @@ SGPropertyNode::removeChildren (const char * name, bool keep) return children; } - -/** - * Remove a linked node. - */ -bool -SGPropertyNode::remove_linked_node (hash_table * node) -{ - for (unsigned int i = 0; i < _linkedNodes.size(); i++) { - if (_linkedNodes[i] == node) { - vector::iterator it = _linkedNodes.begin(); - it += i; - _linkedNodes.erase(it); - return true; - } - } - return false; -} - - string SGPropertyNode::getDisplayName (bool simplify) const { @@ -1035,7 +1092,6 @@ SGPropertyNode::getDisplayName (bool simplify) const return display_name; } - string SGPropertyNode::getPath (bool simplify) const { @@ -1657,8 +1713,12 @@ bool SGPropertyNode::tie (const SGRawValue &rawValue, _tied = true; _value.val = rawValue.clone(); - if (useDefault) + if (useDefault) { + int save_attributes = getAttributes(); + setAttribute( WRITE, true ); setStringValue(old_val.c_str()); + setAttributes( save_attributes ); + } return true; } @@ -1752,20 +1812,10 @@ SGPropertyNode * SGPropertyNode::getNode (const char * relative_path, bool create) { using namespace boost; - if (_path_cache == 0) - _path_cache = new hash_table; - SGPropertyNode * result = _path_cache->get(relative_path); - if (result == 0) { - result = find_node(this, - make_iterator_range(relative_path, relative_path - + strlen(relative_path)), - create); - if (result != 0) - _path_cache->put(relative_path, result); - } - - return result; + return find_node(this, make_iterator_range(relative_path, relative_path + + strlen(relative_path)), + create); } SGPropertyNode * @@ -2068,6 +2118,8 @@ SGPropertyNode::addChangeListener (SGPropertyChangeListener * listener, void SGPropertyNode::removeChangeListener (SGPropertyChangeListener * listener) { + if (_listeners == 0) + return; vector::iterator it = find(_listeners->begin(), _listeners->end(), listener); if (it != _listeners->end()) { @@ -2137,172 +2189,6 @@ SGPropertyNode::fireChildRemoved (SGPropertyNode * parent, _parent->fireChildRemoved(parent, child); } - - -//////////////////////////////////////////////////////////////////////// -// Simplified hash table for caching paths. -//////////////////////////////////////////////////////////////////////// - -#define HASH_TABLE_SIZE 199 - -SGPropertyNode::hash_table::entry::entry () - : _value(0) -{ -} - -SGPropertyNode::hash_table::entry::~entry () -{ - // Don't delete the value; we don't own - // the pointer. -} - -void -SGPropertyNode::hash_table::entry::set_key (const char * key) -{ - _key = key; -} - -void -SGPropertyNode::hash_table::entry::set_value (SGPropertyNode * value) -{ - _value = value; -} - -SGPropertyNode::hash_table::bucket::bucket () - : _length(0), - _entries(0) -{ -} - -SGPropertyNode::hash_table::bucket::~bucket () -{ - for (int i = 0; i < _length; i++) - delete _entries[i]; - delete [] _entries; -} - -SGPropertyNode::hash_table::entry * -SGPropertyNode::hash_table::bucket::get_entry (const char * key, bool create) -{ - int i; - for (i = 0; i < _length; i++) { - if (!strcmp(_entries[i]->get_key(), key)) - return _entries[i]; - } - if (create) { - entry ** new_entries = new entry*[_length+1]; - for (i = 0; i < _length; i++) { - new_entries[i] = _entries[i]; - } - delete [] _entries; - _entries = new_entries; - _entries[_length] = new entry; - _entries[_length]->set_key(key); - _length++; - return _entries[_length - 1]; - } else { - return 0; - } -} - -bool -SGPropertyNode::hash_table::bucket::erase (SGPropertyNode * node) -{ - for (int i = 0; i < _length; i++) { - if (_entries[i]->get_value() == node) { - delete _entries[i]; - for (++i; i < _length; i++) { - _entries[i-1] = _entries[i]; - } - _length--; - return true; - } - } - return false; -} - -void -SGPropertyNode::hash_table::bucket::clear (SGPropertyNode::hash_table * owner) -{ - for (int i = 0; i < _length; i++) { - SGPropertyNode * node = _entries[i]->get_value(); - if (node) - node->remove_linked_node(owner); - } -} - -SGPropertyNode::hash_table::hash_table () - : _data_length(0), - _data(0) -{ -} - -SGPropertyNode::hash_table::~hash_table () -{ - for (unsigned int i = 0; i < _data_length; i++) { - if (_data[i]) { - _data[i]->clear(this); - delete _data[i]; - } - } - delete [] _data; -} - -SGPropertyNode * -SGPropertyNode::hash_table::get (const char * key) -{ - if (_data_length == 0) - return 0; - unsigned int index = hashcode(key) % _data_length; - if (_data[index] == 0) - return 0; - entry * e = _data[index]->get_entry(key); - if (e == 0) - return 0; - else - return e->get_value(); -} - -void -SGPropertyNode::hash_table::put (const char * key, SGPropertyNode * value) -{ - if (_data_length == 0) { - _data = new bucket*[HASH_TABLE_SIZE]; - _data_length = HASH_TABLE_SIZE; - for (unsigned int i = 0; i < HASH_TABLE_SIZE; i++) - _data[i] = 0; - } - unsigned int index = hashcode(key) % _data_length; - if (_data[index] == 0) { - _data[index] = new bucket; - } - entry * e = _data[index]->get_entry(key, true); - e->set_value(value); - value->add_linked_node(this); -} - -bool -SGPropertyNode::hash_table::erase (SGPropertyNode * node) -{ - for (unsigned int i = 0; i < _data_length; i++) - if (_data[i] && _data[i]->erase(node)) - return true; - - return false; -} - -unsigned int -SGPropertyNode::hash_table::hashcode (const char * key) -{ - unsigned int hash = 0; - while (*key != 0) { - hash = 31 * hash + *key; - key++; - } - return hash; -} - - //////////////////////////////////////////////////////////////////////// // Implementation of SGPropertyChangeListener.