Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

glibmm n-ary tree example

By: Ancurio on Mar 20th, 2013  |  syntax: C++  |  size: 0.83 KB  |  views: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data