glibmm r698 - in trunk: . glib/src tests/glibmm_tree



Author: murrayc
Date: Tue Jul 29 10:10:29 2008
New Revision: 698
URL: http://svn.gnome.org/viewvc/glibmm?rev=698&view=rev

Log:
2008-07-29  Murray Cumming  <murrayc murrayc com>

* Makefile.am: Build the docs at the end, after the tests, to 
save time when testing API changes.

* glib/src/tree.hg: Const corrections: Add const and non-const versions 
of many methods, instead of returning non-const objects from const methods.
find().
Changed max_height() to get_max_height() for consistency.
* tests/glibmm_tree/main.cc: Adapted to changed API.

Modified:
   trunk/ChangeLog
   trunk/Makefile.am
   trunk/glib/src/tree.hg
   trunk/tests/glibmm_tree/main.cc

Modified: trunk/Makefile.am
==============================================================================
--- trunk/Makefile.am	(original)
+++ trunk/Makefile.am	Tue Jul 29 10:10:29 2008
@@ -1,6 +1,6 @@
 ACLOCAL_AMFLAGS	= -I scripts
 
-SUBDIRS		= tools glib gio examples docs scripts tests MSVC_Net2003
+SUBDIRS		= tools glib gio examples scripts tests MSVC_Net2003 docs
 DIST_SUBDIRS	= $(SUBDIRS)
 
 EXTRA_DIST	= build_shared/Makefile_build.am_fragment \

Modified: trunk/glib/src/tree.hg
==============================================================================
--- trunk/glib/src/tree.hg	(original)
+++ trunk/glib/src/tree.hg	Tue Jul 29 10:10:29 2008
@@ -62,18 +62,19 @@
   typedef sigc::slot<void, Tree<T>&> ForeachFunc;
 
 private:
-  static Tree<T>* wrap(GNode *node)
+  static Tree<T>* wrap(GNode* node)
   {
     if (!node)
       return 0;
 
-    return reinterpret_cast<Tree<T> *>(node->data);
+    return reinterpret_cast<Tree<T>* >(node->data);
   }
 
 public:
   explicit Tree(T& data) :
     data_(data)
   {
+    //Store the this pointer in the GNode so we can discover this wrapper later:
     gobject_ = g_node_new(reinterpret_cast<gpointer>(this));
   };
   _IGNORE(g_node_new)
@@ -162,7 +163,7 @@
    */
   Tree<T>* insert_data(int position, T& data)
   {
-    Tree<T> *node = new Tree<T>(data);
+    Tree<T>* node = new Tree<T>(data);
     insert(position, *node);
     return node;
   }
@@ -176,7 +177,7 @@
    */
   Tree<T>* insert_data_before(Tree<T>& sibling, T& data)
   {
-    Tree<T> *node = new Tree<T>(data);
+    Tree<T>* node = new Tree<T>(data);
     insert_before(sibling, *node);
     return node;
   }
@@ -189,7 +190,7 @@
    */
   Tree<T>* append_data(T& data)
   {
-    Tree<T> *node = new Tree<T>(data);
+    Tree<T>* node = new Tree<T>(data);
     append(*node);
     return node;
   }
@@ -220,10 +221,16 @@
    *
    * @return A pointer to the root of the tree.
    */
-  Tree<T>* root() const
+  Tree<T>* get_root()
   {
     return wrap(g_node_get_root(gobj()));
   }
+
+  const Tree<T>* get_root() const
+  {
+    return wrap(g_node_get_root(gobj()));
+  }
+
   _IGNORE(g_node_get_root)
 
   /** Traverses a tree starting at the current node.
@@ -242,7 +249,7 @@
   void traverse(TraverseType order, TraverseFlags flags, int max_depth, const TraverseFunc &func)
   {
     TraverseFunc func_copy = func;
-    g_node_traverse(gobj(), (GTraverseType)order, (GTraverseFlags)flags, max_depth, wrap_traverse_slot, reinterpret_cast<gpointer>(&func_copy));
+    g_node_traverse(gobj(), (GTraverseType)order, (GTraverseFlags)flags, max_depth, c_callback_traverse, reinterpret_cast<gpointer>(&func_copy));
   }
   _IGNORE(g_node_traverse);
 
@@ -255,64 +262,87 @@
   void foreach(TraverseFlags flags, const ForeachFunc &func)
   {
     ForeachFunc func_copy = func;
-    g_node_children_foreach(gobj(), (GTraverseFlags)flags, wrap_foreach_slot, reinterpret_cast<gpointer>(&func_copy));
+    g_node_children_foreach(gobj(), (GTraverseFlags)flags, c_callback_foreach, reinterpret_cast<gpointer>(&func_copy));
   }
   _IGNORE(g_node_children_foreach)
 
   /** Finds the first child of a Tree with the given data.
    *
    * @param flags Which types of children are to be visited, one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
-   * @param data The data for which to search
+   * @param data The data for which to search.
    * @return the found child, or 0 if the data is not found
    */
-  Tree<T>* find_child(TraverseFlags flags, const T& data) const
+  Tree<T>* find_child(TraverseFlags flags, const T& data)
   {
     sigc::slot<void, GNode*, const T&, GNode*> real_slot = sigc::ptr_fun(on_compare_child);
 
     GNode* child = 0;
     type_foreach_gnode_slot bound_slot = sigc::bind(real_slot, data, child);
 
-    g_node_children_foreach(gobj(), (GTraverseFlags)flags, on_wrap_compare_child, reinterpret_cast<gpointer>(&bound_slot));
+    g_node_children_foreach(gobj(), (GTraverseFlags)flags, c_callback_foreach_compare_child, reinterpret_cast<gpointer>(&bound_slot));
     
     return wrap(child);
   }
+
+  /** Finds the first child of a Tree with the given data.
+   *
+   * @param flags Which types of children are to be visited, one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
+   * @param data The data for which to search.
+   * @return the found child, or 0 if the data is not found
+   */
+  const Tree<T>* find_child(TraverseFlags flags, const T& data) const
+  {
+    return const_cast<Tree<T>*>(this)->find_child(flags, data);
+  }
+
   _IGNORE(g_node_find_child)
 
   /** Finds a node in a tree.
    *
-   * @param order The order in which nodes are visited: 
-   * TRAVERSE_IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER
+   * @param order The order in which nodes are visited: TRAVERSE_IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER
    * @param flags Which types of children are to be visited: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
+   * @param data The data for which to search.
    * @return The found node, or 0 if the data is not found.
    */
-  Tree<T>* find(TraverseType order, TraverseFlags flags, const T& data) const
+  Tree<T>* find(TraverseType order, TraverseFlags flags, const T& data)
   {
     //We use a sigc::slot for the C callback, so we can bind some extra data.
     sigc::slot<gboolean, GNode*, const T&, GNode**> real_slot = sigc::ptr_fun(on_compare_node);
     GNode* child = 0;
     type_traverse_gnode_slot bound_slot = sigc::bind(real_slot, data, &child);
 
-    g_node_traverse(gobject_, (GTraverseType)order, (GTraverseFlags)flags, -1, on_wrap_compare_node, reinterpret_cast<gpointer>(&bound_slot));
+    g_node_traverse(gobject_, (GTraverseType)order, (GTraverseFlags)flags, -1, c_callback_traverse_compare_node, reinterpret_cast<gpointer>(&bound_slot));
 
     return wrap(child);
   }
+
+  /** Finds a node in a tree.
+   *
+   * @param order The order in which nodes are visited: TRAVERSE_IN_ORDER, TRAVERSE_PRE_ORDER, TRAVERSE_POST_ORDER, or TRAVERSE_LEVEL_ORDER
+   * @param flags Which types of children are to be visited: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES.
+   * @param data The data for which to search.
+   * @return The found node, or 0 if the data is not found.
+   */
+  const Tree<T>* find(TraverseType order, TraverseFlags flags, const T& data) const
+  {
+    return const_cast<Tree<T>*>(this)->find(order, flags, data);
+  }
   _IGNORE(g_node_find)
 
   /** Gets the position of the first child which contains the given data.
    *
    * @param data The data to find.
-   * @return The index of the child which contains data, 
-   * or -1 if the data is not found.
+   * @return The index of the child which contains data, or -1 if the data is not found.
    */
   int child_index(const T& data) const
   {
     int n = 0;
 
-    for(Tree<T>* i = first_child();
-      i != 0; i = i->next_sibling())
+    for(const Tree<T>* i = first_child();  i != 0; i = i->next_sibling())
     {
       if((i->data()) == data)
         return n;
+
       n++;
     }
 
@@ -337,69 +367,131 @@
    *
    * @return The first child, or 0 if the node has no children. 
    */
-  Tree<T>* first_child() const
+  Tree<T>* first_child()
   {
     return wrap(g_node_first_child(gobj()));
   }
+
+  /** Gets the first child.
+   *
+   * @return The first child, or 0 if the node has no children. 
+   */
+  const Tree<T>* first_child() const
+  {
+    return const_cast<Tree<T>*>(this)->first_child();
+  }
   _IGNORE(g_node_first_child)
 
   /** Gets the last child.
    *
    * @return The last child, or 0 if the node has no children.
    */
-  Tree<T>* last_child() const
+  Tree<T>* last_child()
   {
     return wrap(g_node_last_child(gobj()));
   }
+
+  /** Gets the last child.
+   *
+   * @return The last child, or 0 if the node has no children.
+   */
+  const Tree<T>* last_child() const
+  {
+    return const_cast<Tree<T>*>(this)->last_child();
+  }
   _IGNORE(g_node_last_child)
 
   /** Gets the nth child.
    *
    * @return The nth child, or 0 if n is too large.
    */
-  Tree<T>* nth_child(int n) const
+  Tree<T>* nth_child(int n) 
   {
     return wrap(g_node_nth_child(gobj(), n));
   }
+
+  /** Gets the nth child.
+   *
+   * @return The nth child, or 0 if n is too large.
+   */
+  const Tree<T>* nth_child(int n) const
+  {
+    return const_cast<Tree<T>*>(this)->nth_child(n);
+  }
   _IGNORE(g_node_nth_child)
   
   /** Gets the first sibling
    * @return The first sibling, or 0 if the node has no siblings.
    */
-  Tree<T>* first_sibling() const
+  Tree<T>* first_sibling()
   {
     return wrap(g_node_first_sibling(gobj()));
   }
+
+  /** Gets the first sibling
+   * @return The first sibling, or 0 if the node has no siblings.
+   */
+  const Tree<T>* first_sibling() const
+  {
+    return const_cast<Tree<T>*>(this)->first_sibling();
+  }
   _IGNORE(g_node_first_sibling)
 
   /** Gets the previous sibling.
    *
    * @return The previous sibling, or 0 if the node has no siblings.
    */
-  Tree<T>* prev_sibling() const
+  Tree<T>* prev_sibling()
   {
     return wrap(g_node_prev_sibling(gobj()));
   }
+
+  /** Gets the previous sibling.
+   *
+   * @return The previous sibling, or 0 if the node has no siblings.
+   */
+  const Tree<T>* prev_sibling() const
+  {
+    return const_cast<Tree<T>*>(this)->prev_sibling();
+  }
   _IGNORE(g_node_prev_sibling)
 
   /** Gets the next sibling
    *
    * @return The next sibling, or 0 if the node has no siblings.
    */
-  Tree<T>* next_sibling() const
+  Tree<T>* next_sibling()
   {
     return wrap(g_node_next_sibling(gobj()));
   }
+
+  /** Gets the next sibling
+   *
+   * @return The next sibling, or 0 if the node has no siblings.
+   */
+  const Tree<T>* next_sibling() const
+  {
+    return const_cast<Tree<T>*>(this)->next_sibling();
+  }
   _IGNORE(g_node_next_sibling)
 
   /** Gets the last sibling.
    *
    * @return The last sibling, or 0 if the node has no siblings.
    */
-  Tree<T>* last_sibling() const
+  Tree<T>* last_sibling()
   {
     return wrap(g_node_last_sibling(gobj()));
   }
+
+  /** Gets the last sibling.
+   *
+   * @return The last sibling, or 0 if the node has no siblings.
+   */
+  const Tree<T>* last_sibling() const
+  {
+    return const_cast<Tree<T>*>(this)->last_sibling();
+  }
   _IGNORE(g_node_last_sibling)
 
   /** Returns true if this is a leaf node.
@@ -426,7 +518,7 @@
    *
    * @return the depth of this node
    */
-  unsigned int depth() const
+  guint depth() const
   {
     return g_node_depth(gobj());
   }
@@ -437,7 +529,7 @@
    * @param flags Which types of children are to be counted: one of TRAVERSE_ALL, TRAVERSE_LEAVES and TRAVERSE_NON_LEAVES
    * @return The number of nodes in the tree.
    */
-  unsigned int node_count(TraverseFlags flags) const
+  guint node_count(TraverseFlags flags) const
   {
     return g_node_n_nodes(gobj(), (GTraverseFlags)flags);
   }
@@ -447,7 +539,7 @@
    *
    * @return The number of children.
    */
-  unsigned int child_count() const
+  guint child_count() const
   {
     return g_node_n_children(gobj());
   }
@@ -472,9 +564,9 @@
    *
    * @return The maximum height of all branches.
    */
-  unsigned int max_height() const
+  guint get_max_height() const
   {
-    return g_node_max_height(gobj());
+    return g_node_max_height(const_cast<GNode*>(gobj()));
   }
   _IGNORE(g_node_max_height)
 
@@ -487,7 +579,13 @@
   _IGNORE(g_node_unlink)
 
   /// Accessor for this node's data
-  T& data() const
+  T& data()
+  {
+    return data_;
+  }
+
+  /// Accessor for this node's data
+  const T& data() const
   {
     return data_;
   }
@@ -502,28 +600,36 @@
   }
 
 
-  /// For auto-wrapping
-  GNode* gobj() const
+  /// Provides access to the underlying C GObject.
+  GNode* gobj()
+  {
+    return gobject_;
+  }
+
+  /// Provides access to the underlying C GObject.
+  const GNode* gobj() const
   {
     return gobject_;
   }
+
   // Leaving these unimplemented for now
   _IGNORE(g_node_copy)
   _IGNORE(g_node_copy_deep)
 
 protected:
-  GNode *gobject_;
-  T &data_;
+  GNode* gobject_;
+  T& data_;
 
+private:
   /// Wrapper for invoking a TraverseFunc.
-  static gboolean wrap_traverse_slot(GNode* node, gpointer slot)
+  static gboolean c_callback_traverse(GNode* node, gpointer slot)
   {
     const TraverseFunc* tf = reinterpret_cast<const TraverseFunc*>(slot);
     return (*tf)(*wrap(node));
   }
 
   /// Wrapper for invoking a ForeachFunc.
-  static void wrap_foreach_slot(GNode* node, gpointer slot)
+  static void c_callback_foreach(GNode* node, gpointer slot)
   {
     const ForeachFunc* ff = reinterpret_cast<const ForeachFunc*>(slot);
     (*ff)(*wrap(node));
@@ -539,9 +645,9 @@
   }
 
   /// Wrapper for invoking a sigc::slot<void,GNode*> (Internal use).
-  static void on_wrap_compare_child(GNode* node, gpointer data)
+  static void c_callback_foreach_compare_child(GNode* node, gpointer data)
   {
-    const ForeachFunc *slot = reinterpret_cast<const ForeachFunc *>(data);
+    const ForeachFunc* slot = reinterpret_cast<const ForeachFunc*>(data);
     (*slot)(*wrap(node));
   }
 
@@ -557,9 +663,9 @@
   }
 
   /// Wrapper for invoking a sigc::slot<gboolean,GNode*> (Internal use).
-  static gboolean on_wrap_compare_node(GNode* node, gpointer data)
+  static gboolean c_callback_traverse_compare_node(GNode* node, gpointer data)
   {
-    const TraverseFunc *slot = reinterpret_cast<const TraverseFunc *>(data);
+    const TraverseFunc* slot = reinterpret_cast<const TraverseFunc*>(data);
     return (*slot)(*wrap(node));
   }
 

Modified: trunk/tests/glibmm_tree/main.cc
==============================================================================
--- trunk/tests/glibmm_tree/main.cc	(original)
+++ trunk/tests/glibmm_tree/main.cc	Tue Jul 29 10:10:29 2008
@@ -1,5 +1,4 @@
 #include <iostream>
-
 #include <glibmm.h>
 
 bool echo(Glib::Tree<std::string>& i)
@@ -63,41 +62,51 @@
   std::cout << std::endl;
 
   Glib::Tree<std::string> *tmp = ta.find(Glib::IN_ORDER, Glib::TRAVERSE_LEAVES | Glib::TRAVERSE_NON_LEAVES, e);
-  if(NULL == tmp){ std::cout << e << " not found" << std::endl; }
-  else{ std::cout << "Found " << (tmp->data()) << std::endl; }
+  if(!tmp)
+    std::cout << e << " not found" << std::endl;
+  else
+    std::cout << "Found " << (tmp->data()) << std::endl;
 
   tmp = ta.find(Glib::IN_ORDER, Glib::TRAVERSE_LEAVES | Glib::TRAVERSE_NON_LEAVES, a);
-  if(NULL == tmp){ std::cout << a << " not found" << std::endl; }
-  else{ std::cout << "Found " << (tmp->data()) << std::endl; }
+  if(!tmp)
+    std::cout << a << " not found" << std::endl;
+  else
+    std::cout << "Found " << (tmp->data()) << std::endl;
 
   tmp = ta.find(Glib::IN_ORDER, Glib::TRAVERSE_LEAVES | Glib::TRAVERSE_NON_LEAVES, "f");
-  if(NULL == tmp){ std::cout << a << " not found" << std::endl; }
-  else{ std::cout << "Found " << (tmp->data()) << std::endl; }
+  if(!tmp)
+    std::cout << a << " not found" << std::endl;
+  else
+    std::cout << "Found " << (tmp->data()) << std::endl;
 
   tmp = ta.find_child(Glib::TRAVERSE_LEAVES | Glib::TRAVERSE_NON_LEAVES, e);
-  if(NULL == tmp){ std::cout << e << " is not a child of " << (ta.data()) << std::endl; }
-  else{ std::cout << "Mistakenly found " << e << " in " << (ta.data()) << "'s children" << std::endl; }
+  if(!tmp)
+    std::cout << e << " is not a child of " << (ta.data()) << std::endl;
+  else
+    std::cout << "Mistakenly found " << e << " in " << (ta.data()) << "'s children" << std::endl;
 
   tmp = ta.find_child(Glib::TRAVERSE_LEAVES | Glib::TRAVERSE_NON_LEAVES, c);
-  if(NULL == tmp) { 
+  if(!tmp)
     std::cout << c << " is the number " << ta.child_index(c) << " child of " << (ta.data()) << std::endl;
-  }
-  else{ std::cout << "Mistakenly didn't find " << c << " in " << (ta.data()) << "'s children" << std::endl; }
+  else
+   std::cout << "Mistakenly didn't find " << c << " in " << (ta.data()) << "'s children" << std::endl;
 
   tmp = tc.next_sibling();
-  if(NULL == tmp){ std::cout << tc.data() << "'s next sibling is NULL" << std::endl; }
-  else{ std::cout << tc.data() << "'s next sibling is " << tmp->data() << std::endl; }
+  if(!tmp)
+    std::cout << tc.data() << "'s next sibling is NULL" << std::endl;
+  else
+    std::cout << tc.data() << "'s next sibling is " << tmp->data() << std::endl;
 
-  tmp = ta.root();
+  tmp = ta.get_root();
   std::cout << "Root is " << (tmp->data()) << std::endl;
-  std::cout << "Depth is " << tmp->max_height() << std::endl;
+  std::cout << "Depth is " << tmp->get_max_height() << std::endl;
 
   ta.unlink(tc);
-  std::cout << "New depth is " << tmp->max_height() << std::endl;
+  std::cout << "New depth is " << tmp->get_max_height() << std::endl;
 
-  tmp = tc.root();
+  tmp = tc.get_root();
   std::cout << "Pruned root is " << (tmp->data()) << std::endl;
-  std::cout << "Pruned depth is " << tmp->max_height() << std::endl;
+  std::cout << "Pruned depth is " << tmp->get_max_height() << std::endl;
 
   return 0;
 }



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