]> git.mxchange.org Git - simgear.git/blobdiff - simgear/misc/props.hxx
Patch from Frederic Bouvier:
[simgear.git] / simgear / misc / props.hxx
index beeffa657c69c6e65ddff28bb010e0d50904eff9..a03b20066f217f29b96e11d1233a4678d5f5efd1 100644 (file)
@@ -17,7 +17,6 @@
 #endif
 
 #include <vector>
-#include <map>
 
 #if PROPS_STANDALONE
 
@@ -26,7 +25,6 @@
 
 using std::string;
 using std::vector;
-using std::map;
 using std::istream;
 using std::ostream;
 
@@ -38,7 +36,6 @@ using std::ostream;
 #include STL_IOSTREAM
 SG_USING_STD(string);
 SG_USING_STD(vector);
-SG_USING_STD(map);
 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
 SG_USING_STD(istream);
 SG_USING_STD(ostream);
@@ -452,6 +449,69 @@ private:
   setter_t _setter;
 };
 
+\f
+/**
+ * The smart pointer that manage reference counting
+ */
+class SGPropertyNode;
+class SGPropertyNode_ptr
+{
+public:
+
+  /**
+   * Default constructor
+   */
+  SGPropertyNode_ptr();
+
+  /**
+   * Copy constructor
+   */
+  SGPropertyNode_ptr( const SGPropertyNode_ptr &r );
+
+  /**
+   * Constructor from a pointer to a node
+   */
+  SGPropertyNode_ptr( SGPropertyNode *p );
+
+  /**
+   * Destructor
+   */
+  ~SGPropertyNode_ptr();
+
+  /**
+   * Assignement operator
+   */
+  SGPropertyNode_ptr &operator=( const SGPropertyNode_ptr &r );
+
+  /**
+   * Pointer access operator
+   */
+  SGPropertyNode *operator->();
+
+  /**
+   * Pointer access operator (const)
+   */
+  const SGPropertyNode *operator->() const;
+
+  /**
+   * Conversion to SGPropertyNode * operator
+   */
+  operator SGPropertyNode *();
+
+  /**
+   * Conversion to const SGPropertyNode * operator
+   */
+  operator const SGPropertyNode *() const;
+
+  /**
+   * Validity test
+   */
+  bool valid() const;
+
+private:
+
+  SGPropertyNode *_ptr;
+};
 
 \f
 /**
@@ -494,11 +554,19 @@ public:
     READ = 1,
     WRITE = 2,
     ARCHIVE = 4,
-    TRACE_READ = 8,
-    TRACE_WRITE = 16
+    REMOVED = 8,
+    TRACE_READ = 16,
+    TRACE_WRITE = 32
   };
 
 
+  /**
+   * Last used attribute
+   * Update as needed when enum Attribute is changed
+   */
+  static const int LAST_USED_ATTRIBUTE;
+
+
   /**
    * Default constructor.
    */
@@ -537,7 +605,7 @@ public:
   /**
    * Get the node's integer index.
    */
-  const int getIndex () const { return _index; }
+  int getIndex () const { return _index; }
 
 
   /**
@@ -560,7 +628,7 @@ public:
   /**
    * Get the number of child nodes.
    */
-  const int nChildren () const { return _children.size(); }
+  int nChildren () const { return _children.size(); }
 
 
   /**
@@ -591,13 +659,14 @@ public:
   /**
    * Get a vector of all children with the specified name.
    */
-  vector<SGPropertyNode *> getChildren (const char * name);
+  vector<SGPropertyNode_ptr> getChildren (const char * name) const;
 
 
   /**
-   * Get a vector all all children (const) with the specified name.
+   * Remove a child node
    */
-  vector<const SGPropertyNode *> getChildren (const char * name) const;
+  SGPropertyNode_ptr removeChild (const char * name, int index = 0,
+                                  bool keep = true);
 
 
   //
@@ -1080,17 +1149,34 @@ private:
    */
   void trace_write () const;
 
+
+  /**
+   * Increment reference counter
+   */
+  void incrementRef();
+
+  /**
+   * Decrement reference counter
+   */
+  int decrementRef();
+
+  friend class SGPropertyNode_ptr;
+
+
   mutable char _buffer[MAX_STRING_LEN+1];
 
-  const char * _name;
+  class hash_table;
+
+  char * _name;
   int _index;
   SGPropertyNode * _parent;
-  vector<SGPropertyNode *> _children;
-  typedef map<const string,SGPropertyNode *> cache_map;
-  cache_map * _path_cache;
+  vector<SGPropertyNode_ptr> _children;
+  vector<SGPropertyNode_ptr> _removedChildren;
+  hash_table * _path_cache;
   Type _type;
   bool _tied;
   int _attr;
+  int _count;
 
                                // The right kind of pointer...
   union {
@@ -1109,10 +1195,60 @@ private:
     long long_val;
     float float_val;
     double double_val;
-    const char * string_val;
+    char * string_val;
   } _local_val;
 
 
+\f
+  /**
+   * A very simple hash table with no remove functionality.
+   */
+  class hash_table {
+  public:
+
+    /**
+     * An entry in a bucket in a hash table.
+     */
+    class entry {
+    public:
+      entry ();
+      virtual ~entry ();
+      virtual const char * get_key () { return _key; }
+      virtual void set_key (const char * key);
+      virtual SGPropertyNode * get_value () { return _value; }
+      virtual void set_value (SGPropertyNode * value);
+    private:
+      char * _key;
+      SGPropertyNode * _value;
+    };
+
+
+    /**
+     * A bucket in a hash table.
+     */
+    class bucket {
+    public:
+      bucket ();
+      virtual ~bucket ();
+      virtual entry * get_entry (const char * key, bool create = false);
+    private:
+      int _length;
+      entry ** _entries;
+    };
+
+    friend class bucket;
+
+    hash_table ();
+    virtual ~hash_table ();
+    virtual SGPropertyNode * get (const char * key);
+    virtual void put (const char * key, SGPropertyNode * value);
+
+  private:
+    unsigned int hashcode (const char * key);
+    unsigned int _data_length;
+    bucket ** _data;
+  };
+
 };
 
 #endif // __PROPS_HXX