cgv
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action.h
1#pragma once
2
3#include "group.h"
4#include <string>
5#include <cgv/type/cond/is_base_of.h>
6
7#include "lib_begin.h"
8
9namespace cgv {
10 namespace base {
11
12class CGV_API traverse_policy;
13
16class CGV_API action
17{
18protected:
19 bool default_result_begin;
20 bool default_result_end;
21public:
23 action(bool _default_result_begin, bool _default_result_end);
25 void set_default_results(bool _default_result);
27 void set_default_result_begin(bool _default_result_begin);
29 void set_default_result_end(bool _default_result_end);
31 virtual void select(base_ptr p);
33 virtual bool implements_action() const;
35 virtual traverse_policy* get_policy() const;
37 virtual bool begin();
39 virtual bool end();
41 virtual bool has_begin_only() const;
42};
43
44template <bool is_derived, class X>
46{
47 static void extract_policy(X*, traverse_policy* &tp) { tp = 0; }
48};
49
50template <class X>
52{
53 static void extract_policy(X* x, traverse_policy* &tp) { tp = static_cast<traverse_policy*>(x); }
54};
55
57template <class X>
59{
60protected:
62 X* x;
63public:
65 base_method_action(bool _default_result_begin, bool _default_result_end)
66 : action(_default_result_begin, _default_result_end), tp(0), x(0) {}
67
69 void select(base_ptr p) {
70 x = p->get_interface<X>();
72 }
74 bool implements_action() const {
75 return x != 0;
76 }
79 return tp;
80 }
81};
82
83
85template <class X, typename T1>
87{
88protected:
89 T1 v1;
90public:
92 method_action(T1 _v1, bool _default_result_begin, bool _default_result_end)
93 : base_method_action<X>(_default_result_begin, _default_result_end), v1(_v1) {}
95 void set_signature(T1 _v1) { v1 = _v1; }
97 bool call_method(void (X::*mp)(T1), bool default_result) {
98 if (this->x && mp)
99 (this->x->*mp)(v1);
100 return default_result;
101 }
103 bool call_method(bool (X::*mp)(T1), bool default_result) {
104 if (this->x && mp)
105 return (this->x->*mp)(v1);
106 return default_result;
107 }
108};
109
111template <class X, typename R, typename T1>
113{
114protected:
115 R (X::*on_begin)(T1);
116public:
118 single_method_action(T1 _v1, R (X::*_on_begin)(T1),
119 bool _default_result_begin = false, bool _default_result_end = false)
120 : method_action<X,T1>(_v1, _default_result_begin, _default_result_end), on_begin(_on_begin) {}
122 bool begin() { return this->call_method(on_begin, this->default_result_begin); }
124 bool has_begin_only() const { return true; }
125};
126
128template <class X, typename R1, typename R2, typename T1>
130{
131protected:
132 R1 (X::*on_begin)(T1);
133 R2 (X::*on_end)(T1);
134public:
136 matched_method_action(T1 _v1, R1 (X::*_on_begin)(T1), R2 (X::*_on_end)(T1), bool _default_result_begin, bool _default_result_end)
137 : method_action<X,T1>(_v1, _default_result_begin, _default_result_end), on_begin(_on_begin), on_end(_on_end) {}
139 bool begin() { return this->call_method(on_begin, this->default_result_begin); }
141 bool end() { return this->call_method(on_end, this->default_result_end); }
142};
143
145template <typename T1, class X, typename R>
146single_method_action<X,R,T1> make_action(T1 _v1, R (X::*_on_begin)(T1), bool _default_result_begin = false, bool _default_result_end = false) {
148}
149
151template <typename T1, class X, typename R1, typename R2>
155
156
158template <class X, typename T1, typename T2>
160{
161protected:
162 T1 v1;
163 T2 v2;
164public:
169 void set_signature(T1 _v1, T2 _v2) { v1 = _v1; v2 = _v2; }
171 bool call_method(void (X::*mp)(T1, T2), bool default_result) {
172 if (this->x && mp)
173 (this->x->*mp)(v1,v2);
174 return default_result;
175 }
177 bool call_method(bool (X::*mp)(T1, T2), bool default_result) {
178 if (this->x && mp)
179 return (this->x->*mp)(v1, v2);
180 return default_result;
181 }
182};
183
185template <class X, typename R, typename T1, typename T2>
187{
188protected:
189 R (X::*on_begin)(T1,T2);
190public:
196 bool begin() { return this->call_method(on_begin, this->default_result_begin); }
198 bool has_begin_only() const { return true; }
199};
200
202template <typename T1, typename T2, class X, typename R>
206
207 }
208}
209
210#include <cgv/config/lib_end.h>
The action class is used in tree traversals together with the traverser.
Definition action.h:17
base class for all actions that use methods of class X
Definition action.h:59
base_method_action(bool _default_result_begin, bool _default_result_end)
construct from default return values that are passed on to the base class
Definition action.h:65
traverse_policy * get_policy() const
simply return the stored pointer of type traverse_policy*
Definition action.h:78
bool implements_action() const
simply return whether the stored pointer of type X* is not 0
Definition action.h:74
void select(base_ptr p)
implement the select method and store pointers of type X* and traverse_policy*
Definition action.h:69
complete implementation of method actions that call a begin and end method when entering and leaving ...
Definition action.h:130
bool end()
uses call_method of base class method_action to call the method refered to by the stored method point...
Definition action.h:141
bool begin()
uses call_method of base class method_action to call the method refered to by the stored method point...
Definition action.h:139
matched_method_action(T1 _v1, R1(X::*_on_begin)(T1), R2(X::*_on_end)(T1), bool _default_result_begin, bool _default_result_end)
construct from signature, method pointers and default result values
Definition action.h:136
base class for all method actions that take a single argument of type T1 in their signature
Definition action.h:160
bool call_method(bool(X::*mp)(T1, T2), bool default_result)
call a bool method given a default return value
Definition action.h:177
method_action_2(T1 _v1, T2 _v2, bool _default_result_begin, bool _default_result_end)
construct action from signature and default return values
Definition action.h:166
void set_signature(T1 _v1, T2 _v2)
set a new signature with which the methods of the traversed nodes are called
Definition action.h:169
bool call_method(void(X::*mp)(T1, T2), bool default_result)
call a void method given a default return value
Definition action.h:171
base class for all method actions that take a single argument of type T1 in their signature
Definition action.h:87
bool call_method(bool(X::*mp)(T1), bool default_result)
call a bool method given a default return value
Definition action.h:103
bool call_method(void(X::*mp)(T1), bool default_result)
call a void method given a default return value
Definition action.h:97
void set_signature(T1 _v1)
set a new signature with which the methods of the traversed nodes are called
Definition action.h:95
method_action(T1 _v1, bool _default_result_begin, bool _default_result_end)
construct action from signature and default return values
Definition action.h:92
complete implementation of method actions that only call one method when entering a node
Definition action.h:187
single_method_action_2(T1 _v1, T2 _v2, R(X::*_on_begin)(T1, T2), bool _default_result_begin=false, bool _default_result_end=false)
construct from signature, method pointer and default result values
Definition action.h:192
bool has_begin_only() const
check whether the action has registered a single begin method or both begin and end methods
Definition action.h:198
bool begin()
uses call_method of base class method_action to call the method refered to by the stored method point...
Definition action.h:196
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
single_method_action(T1 _v1, R(X::*_on_begin)(T1), bool _default_result_begin=false, bool _default_result_end=false)
construct from signature, method pointer and default result values
Definition action.h:118
bool has_begin_only() const
check whether the action has registered a single begin method or both begin and end methods
Definition action.h:124
bool begin()
uses call_method of base class method_action to call the method refered to by the stored method point...
Definition action.h:122
nodes should inherit from this policy class to allow selective tree traversals
Definition traverser.h:24
single_method_action< X, R, T1 > make_action(T1 _v1, R(X::*_on_begin)(T1), bool _default_result_begin=false, bool _default_result_end=false)
helper function to construct an action from a signature and one method that is called when a node is ...
Definition action.h:146
single_method_action_2< X, R, T1, T2 > make_action_2(T1 _v1, T2 _v2, R(X::*_on_begin)(T1, T2), bool _default_result_begin=false, bool _default_result_end=false)
helper function to construct an action from a signature and one method that is called when a node is ...
Definition action.h:203
the cgv namespace
Definition print.h:11