cgv
find_action.h
1 #pragma once
2 
3 #include <cgv/base/action.h>
4 
5 namespace cgv {
6  namespace base {
7 
9 template <class X>
10 class find_action : public action
11 {
12  X* x;
13 protected:
14  std::vector<X*>& result;
15 public:
16  find_action(std::vector<X*>& _result) : action(true,true), result(_result) {}
18  void select(base_ptr p) {
19  x = p->get_interface<X>();
20  }
21  bool begin()
22  {
23  if (x) {
24  result.push_back(x);
25  return true;
26  }
27  else
28  return false;
29  }
31  bool implements_action() const {
32  return x != 0;
33  }
34 };
35 
37 template <class X>
38 void find_interface(base_ptr start, std::vector<X*>& result) {
39  find_action<X> fa(result);
40  traverser(fa).traverse(start);
41 }
42 
44 template <class X, typename T>
45 bool ensure_by_find(X* start, T*& pointer, unsigned i = 0)
46 {
47  if (pointer)
48  return true;
49 
50  std::vector<T*> instances;
51  cgv::base::find_interface<T>(base_ptr(start), instances);
52  if (instances.empty())
53  return false;
54  pointer = instances[i < instances.size() ? i : instances.size() - 1];
55  return true;
56 }
57 
58  }
59 }
cgv::base::traverser
class used to traverse a tree structure
Definition: traverser.h:102
cgv::base::ensure_by_find
bool ensure_by_find(X *start, T *&pointer, unsigned i=0)
traverse the hierarchy to find the i-th instance of type T and set pointer to it but only in case poi...
Definition: find_action.h:45
cgv::data::ref_ptr< base, true >
cgv::base::action::action
action(bool _default_result_begin, bool _default_result_end)
construct with default return values for the begin and end method
Definition: action.cxx:6
cgv::base::base_ptr
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
Definition: base.h:37
cgv::base::find_action::begin
bool begin()
perform the enter part of the action on the current object
Definition: find_action.h:21
cgv::base::find_action
simple action implementation that adds nodes implementing X to a results vector
Definition: find_action.h:11
cgv::base::find_interface
void find_interface(base_ptr start, std::vector< X * > &result)
collect all nodes that implement interface X
Definition: find_action.h:38
cgv::base::action
Definition: action.h:17
cgv::base::find_action::select
void select(base_ptr p)
make the passed object current
Definition: find_action.h:18
cgv::base::traverser::traverse
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...
Definition: traverser.cxx:373
cgv::base::find_action::implements_action
bool implements_action() const
check if the current object implements the interface needed for this action
Definition: find_action.h:31
cgv
the cgv namespace
Definition: vr_calib.cxx:9