cgv
traverser.h
1 #pragma once
2 
3 #include "group.h"
4 #include "action.h"
5 #include <string>
6 
7 #include "lib_begin.h"
8 
9 namespace cgv {
10  namespace base {
11 
14  TP_ALL,
19  TP_STOP_ON_FAILURE = 16
20 };
21 
23 class CGV_API traverse_policy
24 {
25 protected:
26  TraversePolicy policy;
27  bool active;
28  int focus;
29 public:
31  traverse_policy(int _policy = TP_ALL+TP_STOP_ON_SUCCESS, bool _active = true, int _focus = -1);
33  int get_policy() const;
35  bool stop_on_success() const;
37  bool stop_on_failure() const;
39  void set_policy(int _policy);
41  int get_focused_child() const;
43  void set_focused_child(int _focused_child);
45  bool get_active() const;
47  void set_active(bool _active);
48 };
49 
51 template <class X>
52 bool grab_focus(X* instance) {
53  node* n = dynamic_cast<node*>(instance);
54  if (!n)
55  return true;
56  base_ptr p = n->get_parent();
57  if (p.empty())
58  return true;
59  group_ptr g = p->get_group();
60  if (g.empty())
61  return true;
62  X* pinstance = p->get_interface<X>();
63  if (!pinstance)
64  return true;
65  for (unsigned int i=0; i<g->get_nr_children(); ++i) {
66  if (g->get_child(i)->get_interface<X>() == instance) {
67  pinstance->set_focused_child(i);
68  return grab_focus(pinstance);
69  }
70  }
71  // at this point we did not find the instance in the children of its parent
72  return false;
73 }
74 
77 {
78 public:
80  virtual bool on_enter_node(base_ptr b);
82  virtual bool on_leave_node(base_ptr b);
84  virtual bool on_enter_children(group_ptr g);
86  virtual bool on_leave_children(group_ptr g);
88  virtual bool on_enter_parent(node_ptr n);
90  virtual bool on_leave_parent(node_ptr n);
91 };
92 
95 {
96  TS_DEPTH_FIRST,
97  TS_BREADTH_FIRST
98 };
99 
101 class CGV_API traverser
102 {
103 protected:
104  action& a;
105  TraverseStrategy strategy;
106  std::string visit_order;
107  bool stop_if_not_implemented;
109  template <typename TCH>
110  bool traverse_tmp_1(base_ptr dest, base_ptr src, bool& force_termination, TCH& tch);
112  template <typename TCH>
113  bool traverse_tmp_2(base_ptr p, base_ptr dest, base_ptr src, traverse_policy* tp, bool& force_termination, TCH& tch);
114 public:
116  traverser(action& _a,
117  const std::string& _visit_order = "pnc",
118  TraverseStrategy _strategy = TS_DEPTH_FIRST,
119  bool _stop_if_not_implemented = false);
121  traverser& set_visit_order(const std::string& _visit_order);
125  bool traverse(base_ptr start, traverse_callback_handler* tch = 0);
126 };
127 
128  }
129 }
130 
131 #include <cgv/config/lib_end.h>
cgv::base::group::get_nr_children
unsigned int get_nr_children() const
return the number of children
Definition: group.cxx:31
cgv::base::base::get_group
virtual data::ref_ptr< group, true > get_group()
cast upward to group
Definition: base.cxx:55
cgv::base::TP_STOP_ON_FAILURE
@ TP_STOP_ON_FAILURE
this is an optional flag for traversals with methods that return a bool. If the returned bool is true...
Definition: traverser.h:19
cgv::base::traverser::set_strategy
traverser & set_strategy(TraverseStrategy _strategy)
set a different traverse strategy
cgv::base::traverser
class used to traverse a tree structure
Definition: traverser.h:102
cgv::base::traverser::set_visit_order
traverser & set_visit_order(const std::string &_visit_order)
set a different visiting order of node, children and parent
cgv::base::traverse_policy
nodes should inherit from this policy class to allow selective tree traversals
Definition: traverser.h:24
cgv::data::ref_ptr< base, true >
cgv::base::TP_FIRST_FOCUS
@ TP_FIRST_FOCUS
traverse only the focused child
Definition: traverser.h:16
cgv::base::grab_focus
bool grab_focus(X *instance)
try to grab the focus in the path of this node to the root of the tree
Definition: traverser.h:52
cgv::data::ref_ptr::empty
bool empty() const
check if pointer is not yet set
Definition: ref_ptr.h:230
cgv::base::TraverseStrategy
TraverseStrategy
not yet implemented
Definition: traverser.h:95
cgv::base::node::get_parent
node_ptr get_parent() const
return the parent node
Definition: node.cxx:13
cgv::base::action
Definition: action.h:17
cgv::base::TP_STOP_ON_SUCCESS
@ TP_STOP_ON_SUCCESS
like previous but change focus to the child, where traversal succeeded
Definition: traverser.h:18
cgv::base::group::get_child
base_ptr get_child(unsigned int i) const
return the i-th child
Definition: group.cxx:37
cgv::base::traverse_callback_handler
interface of a handler for traverse callbacks
Definition: traverser.h:77
cgv::base::base::get_interface
T * get_interface()
use dynamic type cast to check for the given interface
Definition: base.h:95
cgv::base::TraversePolicy
TraversePolicy
different traversal policies
Definition: traverser.h:13
cgv::base::TP_ONLY_FOCUS
@ TP_ONLY_FOCUS
traverse all children
Definition: traverser.h:15
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::base::node
Definition: node.h:17
cgv::base::TP_AUTO_FOCUS
@ TP_AUTO_FOCUS
first traverse focused and then the remaining children
Definition: traverser.h:17