cgv
Loading...
Searching...
No Matches
traverser.h
1#pragma once
2
3#include "group.h"
4#include "action.h"
5#include <string>
6
7#include "lib_begin.h"
8
9namespace cgv {
10 namespace base {
11
21
23class CGV_API traverse_policy
24{
25protected:
26 TraversePolicy policy;
27 bool active;
28 int focus;
29public:
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
51template <class X>
52bool 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{
78public:
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
101class CGV_API traverser
102{
103protected:
104 action& a;
105 TraverseStrategy strategy;
106 std::string visit_order;
107 bool stop_if_not_implemented;
111 template <typename TCH>
112 bool traverse_tmp_1(base_ptr dest, base_ptr src, bool& force_termination, TCH& tch);
114 template <typename TCH>
115 bool traverse_tmp_2(base_ptr p, base_ptr dest, base_ptr src, traverse_policy* tp, bool& force_termination, TCH& tch);
116public:
118 traverser(action& _a,
119 const std::string& _visit_order = "pnc",
120 TraverseStrategy _strategy = TS_DEPTH_FIRST,
121 bool _stop_if_not_implemented = false, bool _ignore_activation_state = false);
123 traverser& set_visit_order(const std::string& _visit_order);
127 bool traverse(base_ptr start, traverse_callback_handler* tch = 0);
128};
129
130 }
131}
132
133#include <cgv/config/lib_end.h>
The action class is used in tree traversals together with the traverser.
Definition action.h:17
T * get_interface()
use dynamic type cast to check for the given interface
Definition base.h:128
virtual data::ref_ptr< group, true > get_group()
perform downcast to group
Definition base.cxx:45
unsigned int get_nr_children() const
return the number of children
Definition group.cxx:29
base_ptr get_child(unsigned int i) const
return the i-th child
Definition group.cxx:33
The node class keeps a pointer to its parent.
Definition node.h:19
node_ptr get_parent() const
return the parent node
Definition node.cxx:9
interface of a handler for traverse callbacks
Definition traverser.h:77
virtual bool on_enter_children(group_ptr g)
called before the children of a group node g are processed, return whether these should be skipped....
Definition traverser.cxx:60
virtual bool on_enter_parent(node_ptr n)
called before the parent of a node n is processed, return whether this should be skipped....
Definition traverser.cxx:70
virtual bool on_enter_node(base_ptr b)
called before a node b is processed, return, whether to skip this node. If the node is skipped,...
Definition traverser.cxx:50
virtual bool on_leave_node(base_ptr b)
called when a node b is left, return whether to terminate traversal
Definition traverser.cxx:55
virtual bool on_leave_parent(node_ptr n)
called when the parent of a node n has been left, return whether to terminate traversal
Definition traverser.cxx:75
virtual bool on_leave_children(group_ptr g)
called when the children of a group node g have been left, return whether to terminate traversal
Definition traverser.cxx:65
nodes should inherit from this policy class to allow selective tree traversals
Definition traverser.h:24
bool get_active() const
return whether the current node is active
Definition traverser.cxx:39
int get_policy() const
return the policy without the stop on success flag
Definition traverser.cxx:11
void set_policy(int _policy)
set a new policy, always add stop on success flag if needed
Definition traverser.cxx:27
bool stop_on_failure() const
return whether to stop on failure
Definition traverser.cxx:22
void set_focused_child(int _focused_child)
set the focused child
Definition traverser.cxx:35
void set_active(bool _active)
set the active flag of the current node
Definition traverser.cxx:43
bool stop_on_success() const
return whether to stop on success
Definition traverser.cxx:16
int get_focused_child() const
return the focused child or -1 if none is focused
Definition traverser.cxx:31
traverse_policy(int _policy=TP_ALL+TP_STOP_ON_SUCCESS, bool _active=true, int _focus=-1)
construct default traverse policy that visits everything
Definition traverser.cxx:7
bool traverse_tmp_1(base_ptr dest, base_ptr src, bool &force_termination, TCH &tch)
traverse a single object, template over a static traverse callback handler interface
traverser(action &_a, const std::string &_visit_order="pnc", TraverseStrategy _strategy=TS_DEPTH_FIRST, bool _stop_if_not_implemented=false, bool _ignore_activation_state=false)
construct from reference on action and traversal order string
bool ignore_activation_state
whether to ignore the active flag of the traverse policy of the traversed object
Definition traverser.h:109
bool traverse(base_ptr start, traverse_callback_handler *tch=0)
traverse a tree starting at given node according to set strategy, order and dest and previously comin...
traverser & set_visit_order(const std::string &_visit_order)
set a different visiting order of node, children and parent
traverser & set_strategy(TraverseStrategy _strategy)
set a different traverse strategy
bool traverse_tmp_2(base_ptr p, base_ptr dest, base_ptr src, traverse_policy *tp, bool &force_termination, TCH &tch)
helper method encapsulating common functionality
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
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
TraverseStrategy
not yet implemented
Definition traverser.h:95
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
Definition base.h:37
TraversePolicy
different traversal policies
Definition traverser.h:13
@ TP_AUTO_FOCUS
first traverse focused and then the remaining children
Definition traverser.h:17
@ 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
@ TP_ONLY_FOCUS
traverse all children
Definition traverser.h:15
@ TP_FIRST_FOCUS
traverse only the focused child
Definition traverser.h:16
@ TP_STOP_ON_SUCCESS
like previous but change focus to the child, where traversal succeeded
Definition traverser.h:18
data::ref_ptr< node, true > node_ptr
ref counted pointer to a node
Definition node.h:13
data::ref_ptr< group, true > group_ptr
ref counted pointer to a node
Definition group.h:14
this header is dependency free
Definition print.h:11