cgv
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 
9 namespace cgv {
10  namespace base {
11 
12 class CGV_API traverse_policy;
13 
16 class CGV_API action
17 {
18 protected:
19  bool default_result_begin;
20  bool default_result_end;
21 public:
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 
44 template <bool is_derived, class X>
45 struct extract_policy_struct
46 {
47  static void extract_policy(X*, traverse_policy* &tp) { tp = 0; }
48 };
49 
50 template <class X>
51 struct extract_policy_struct<true,X>
52 {
53  static void extract_policy(X* x, traverse_policy* &tp) { tp = static_cast<traverse_policy*>(x); }
54 };
55 
57 template <class X>
58 class base_method_action : public action
59 {
60 protected:
61  traverse_policy* tp;
62  X* x;
63 public:
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>();
71  extract_policy_struct<type::cond::is_base_of<traverse_policy, X>::value,X>::extract_policy(x, tp);
72  }
74  bool implements_action() const {
75  return x != 0;
76  }
79  return tp;
80  }
81 };
82 
83 
85 template <class X, typename T1>
87 {
88 protected:
89  T1 v1;
90 public:
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 
111 template <class X, typename R, typename T1>
113 {
114 protected:
115  R (X::*on_begin)(T1);
116 public:
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 
128 template <class X, typename R1, typename R2, typename T1>
130 {
131 protected:
132  R1 (X::*on_begin)(T1);
133  R2 (X::*on_end)(T1);
134 public:
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 
145 template <typename T1, class X, typename R>
146 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) {
147  return single_method_action<X,R,T1>(_v1, _on_begin, _default_result_begin, _default_result_end);
148 }
149 
151 template <typename T1, class X, typename R1, typename R2>
152 matched_method_action<X,R1,R2,T1> make_action(T1 _v1, R1 (X::*_on_begin)(T1), R2 (X::*_on_end)(T1), bool _default_result_begin = false, bool _default_result_end = false) {
153  return matched_method_action<X,R1,R2,T1>(_v1, _on_begin, _on_end, _default_result_begin, _default_result_end);
154 }
155 
156 
158 template <class X, typename T1, typename T2>
160 {
161 protected:
162  T1 v1;
163  T2 v2;
164 public:
166  method_action_2(T1 _v1, T2 _v2, bool _default_result_begin, bool _default_result_end)
167  : base_method_action<X>(_default_result_begin, _default_result_end), v1(_v1), v2(_v2) {}
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 
185 template <class X, typename R, typename T1, typename T2>
187 {
188 protected:
189  R (X::*on_begin)(T1,T2);
190 public:
192  single_method_action_2(T1 _v1, T2 _v2, R (X::*_on_begin)(T1,T2),
193  bool _default_result_begin = false, bool _default_result_end = false)
194  : method_action_2<X,T1,T2>(_v1, _v2, _default_result_begin, _default_result_end), on_begin(_on_begin) {}
196  bool begin() { return this->call_method(on_begin, this->default_result_begin); }
198  bool has_begin_only() const { return true; }
199 };
200 
202 template <typename T1, typename T2, class X, typename R>
203 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) {
204  return single_method_action_2<X,R,T1,T2>(_v1, _v2, _on_begin, _default_result_begin, _default_result_end);
205 }
206 
207  }
208 }
209 
210 #include <cgv/config/lib_end.h>
cgv::base::base_method_action::implements_action
bool implements_action() const
simply return whether the stored pointer of type X* is not 0
Definition: action.h:74
cgv::base::single_method_action::begin
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
cgv::base::matched_method_action::begin
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
cgv::base::single_method_action
Definition: action.h:113
cgv::base::method_action_2::set_signature
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
cgv::base::make_action_2
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
cgv::base::method_action::call_method
bool call_method(void(X::*mp)(T1), bool default_result)
call a void method given a default return value
Definition: action.h:97
cgv::base::matched_method_action::end
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
cgv::base::base_method_action::select
void select(base_ptr p)
implement the select method and store pointers of type X* and traverse_policy*
Definition: action.h:69
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::method_action_2::method_action_2
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
cgv::base::base_method_action::base_method_action
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
cgv::base::single_method_action_2
Definition: action.h:187
cgv::base::method_action::call_method
bool call_method(bool(X::*mp)(T1), bool default_result)
call a bool method given a default return value
Definition: action.h:103
cgv::base::matched_method_action
Definition: action.h:130
cgv::base::method_action_2
Definition: action.h:160
cgv::base::base_method_action::get_policy
traverse_policy * get_policy() const
simply return the stored pointer of type traverse_policy*
Definition: action.h:78
cgv::base::base_method_action
Definition: action.h:59
cgv::base::method_action
Definition: action.h:87
cgv::base::method_action_2::call_method
bool call_method(void(X::*mp)(T1, T2), bool default_result)
call a void method given a default return value
Definition: action.h:171
cgv::base::action
Definition: action.h:17
cgv::base::method_action::set_signature
void set_signature(T1 _v1)
set a new signature with which the methods of the traversed nodes are called
Definition: action.h:95
cgv::base::make_action
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
cgv::base::single_method_action::single_method_action
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
cgv::base::method_action_2::call_method
bool call_method(bool(X::*mp)(T1, T2), bool default_result)
call a bool method given a default return value
Definition: action.h:177
cgv::base::single_method_action_2::single_method_action_2
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
cgv::base::method_action::method_action
method_action(T1 _v1, bool _default_result_begin, bool _default_result_end)
construct action from signature and default return values
Definition: action.h:92
cgv::base::single_method_action_2::begin
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
cgv::base::single_method_action::has_begin_only
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
cgv::base::single_method_action_2::has_begin_only
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
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::base::matched_method_action::matched_method_action
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