diff options
author | Szilárd Pfeiffer <szilard.pfeiffer@gmail.com> | 2008-08-26 12:17:30 +0000 |
---|---|---|
committer | Murray Cumming <murrayc@src.gnome.org> | 2008-08-26 12:17:30 +0000 |
commit | 84f356bb00e326151ecab8ba11aebbdeeb1e6199 (patch) | |
tree | 088285283a50bfe6ba50f6acd2db9dd40d3f2481 /glib/src/nodetree.hg | |
parent | 39182710355b53403196b36ad15793511dcd21a0 (diff) | |
download | glibmm-84f356bb00e326151ecab8ba11aebbdeeb1e6199.tar.gz |
Added a copy constructor. Therefore, take store the data by value instead
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.
svn path=/trunk/; revision=718
Diffstat (limited to 'glib/src/nodetree.hg')
-rw-r--r-- | glib/src/nodetree.hg | 51 |
1 files changed, 41 insertions, 10 deletions
diff --git a/glib/src/nodetree.hg b/glib/src/nodetree.hg index 09679b9b..eb99a624 100644 --- a/glib/src/nodetree.hg +++ b/glib/src/nodetree.hg @@ -83,7 +83,7 @@ private: } 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 @@ public: */ ~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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: * @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 @@ public: /** 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 @@ public: // 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) |