glibmm r718 - in trunk: . glib/src tests/glibmm_nodetree



Author: murrayc
Date: Tue Aug 26 12:17:30 2008
New Revision: 718
URL: http://svn.gnome.org/viewvc/glibmm?rev=718&view=rev

Log:
2008-08-26  SzilÃrd Pfeiffer  <szilard pfeiffer gmail com>

* glib/src/nodetree.hg: Added a copy constructor. Therefore, take 
store the data by value instead of reference, taking it by const reference.
* tests/glibmm_nodetree/main.cc: Test the copy constructor.
Bug #547909.

Modified:
   trunk/ChangeLog
   trunk/glib/src/nodetree.hg
   trunk/tests/glibmm_nodetree/main.cc

Modified: trunk/glib/src/nodetree.hg
==============================================================================
--- trunk/glib/src/nodetree.hg	(original)
+++ trunk/glib/src/nodetree.hg	Tue Aug 26 12:17:30 2008
@@ -83,7 +83,7 @@
   }
 
 public:
-  explicit NodeTree(T& data) :
+  explicit NodeTree(const T& data) :
     data_(data)
   {
     //Store the this pointer in the GNode so we can discover this wrapper later:
@@ -96,10 +96,29 @@
    */
   ~NodeTree()
   {
-    g_node_destroy(gobj());
+    if(!is_root())
+      unlink();
+
+    //Free the children (not with g_node_destroy to avoid the leaking of C++ warpper objects).
+    while(NodeTree<T>* i = first_child())
+      delete i;
+
+    //Free the wrapped object.
+    g_slice_free(GNode, gobject_);
   };
   _IGNORE(g_node_destroy)
 
+  NodeTree(const NodeTree<T>& node) :
+    data_(node.data_)
+  {
+    //Store the this pointer in the GNode so we can discover the wrapped object later.
+    gobject_ = g_node_new(reinterpret_cast<gpointer>(this));
+
+    //Prepend the copied children of @node to the constructing node.
+    for(const NodeTree<T>* i = node.last_child();  i != 0; i = i->prev_sibling())
+      prepend(*(new NodeTree<T>(*i)));
+  }
+
   /// Provides access to the underlying C GObject.
   inline GNode* gobj()
   {
@@ -184,7 +203,7 @@
    * @param data the data for the new NodeTree
    * @return the new NodeTree
    */
-  NodeTree<T>* insert_data(int position, T& data)
+  NodeTree<T>* insert_data(int position, const T& data)
   {
     NodeTree<T>* node = new NodeTree<T>(data);
     insert(position, *node);
@@ -198,7 +217,7 @@
    * @param data the data for the new NodeTree
    * @return the new NodeTree
    */
-  NodeTree<T>* insert_data_before(NodeTree<T>& sibling, T& data)
+  NodeTree<T>* insert_data_before(NodeTree<T>& sibling, const T& data)
   {
     NodeTree<T>* node = new NodeTree<T>(data);
     insert_before(sibling, *node);
@@ -211,7 +230,7 @@
    * @param data the data for the new NodeTree
    * @return the new NodeTree
    */
-  NodeTree<T>* append_data(T& data)
+  NodeTree<T>* append_data(const T& data)
   {
     NodeTree<T>* node = new NodeTree<T>(data);
     append(*node);
@@ -224,7 +243,7 @@
    * @param data the data for the new NodeTree
    * @return the new NodeTree
    */
-  NodeTree<T>* prepend_data(T& data)
+  NodeTree<T>* prepend_data(const T& data)
   {
     NodeTree<T>* node = new NodeTree<T>(data);
     prepend(*node);
@@ -608,12 +627,25 @@
 
   /** Unlinks a node from a tree, resulting in two separate trees.
    */
-  void unlink(NodeTree<T>& child)
+  void unlink()
   {
-    g_node_unlink(child.gobj());
+    g_node_unlink(gobj());
   }
   _IGNORE(g_node_unlink)
 
+#if 0 //Commented-out because people can just use the copy constructor.
+  /** Recursively copies a node and it's data.
+   *
+   * Returns: a new node containing the copies of the data.
+   */
+  NodeTree<T>* copy_deep() const
+  {
+    //Use copy constructor instead of g_node_copy_deep to create C++ wrappers also not only the wrapped C objects.
+    return new NodeTree<T>(*this);
+  }
+#endif
+  _IGNORE(g_node_copy_deep)
+
   /// Accessor for this node's data
   T& data()
   {
@@ -637,13 +669,12 @@
 
   // Leaving these unimplemented for now
   _IGNORE(g_node_copy)
-  _IGNORE(g_node_copy_deep)
 
 
 private:
 
   GNode* gobject_;
-  T& data_;
+  T data_;
 
   /// Wrapper for invoking a TraverseFunc.
   static gboolean c_callback_traverse(GNode* node, gpointer slot)

Modified: trunk/tests/glibmm_nodetree/main.cc
==============================================================================
--- trunk/tests/glibmm_nodetree/main.cc	(original)
+++ trunk/tests/glibmm_nodetree/main.cc	Tue Aug 26 12:17:30 2008
@@ -118,8 +118,8 @@
   g_assert(tstring == "ABFEDCGKJIH");
   tstring.clear();
 
-  /* TODO:
-  node = root->copy();
+
+  node = root; //A deep copy.
   g_assert(root->node_count(type_nodetree_string::TRAVERSE_ALL) == node->node_count(type_nodetree_string::TRAVERSE_ALL));
   g_assert(root->get_max_height() == node->get_max_height());
   root->traverse(sigc::bind(sigc::ptr_fun(node_build_string), sigc::ref(tstring)), Glib::TRAVERSE_IN_ORDER, type_nodetree_string::TRAVERSE_ALL, -1);
@@ -127,7 +127,7 @@
   g_assert(tstring == cstring);
 
   delete node;
-  */
+
   delete root;
 
   /* allocation tests */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]