Advertisement
Ancurio

glibmm n-ary tree example

Mar 20th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1.  
  2. #include <glibmm.h>
  3. #include <iostream>
  4.  
  5. struct Vec2
  6. {
  7.     long x;
  8.     long y;
  9.  
  10.     Vec2(long x, long y)
  11.         : x(x), y(y)
  12.     {}
  13. };
  14.  
  15. struct Entity
  16. {
  17.     Vec2 position;
  18.     Vec2 velocity;
  19.     Vec2 acceleration;
  20.     const char *name;
  21.  
  22.     Entity(const char *name)
  23.         : position(0, 0),
  24.           velocity(0, 0),
  25.           acceleration(0, 0),
  26.           name(name)
  27.     {}
  28.  
  29.     ~Entity()
  30.     {
  31.         std::cout << "Entity " << name << " says goodbye\n";
  32.     }
  33. };
  34.  
  35. typedef Glib::NodeTree<Entity> ENode;
  36.  
  37. bool travFun(ENode &node)
  38. {
  39.     std::cout << "Traversing node: " << node.data().name << "\n";
  40.  
  41.     return false;
  42. }
  43.  
  44. int main(int, char**)
  45. {
  46.     Entity root("Root");
  47.     root.position = Vec2(10, 20);
  48.  
  49.     ENode tree(root);
  50.  
  51.     tree.append_data("child");
  52.  
  53.     tree.append_data("child2");
  54.  
  55.     tree.traverse(sigc::ptr_fun(&travFun), Glib::TRAVERSE_POST_ORDER);
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement