cgv
Loading...
Searching...
No Matches
find_action.h
1#pragma once
2
3#include <cgv/base/action.h>
4
5namespace cgv {
6 namespace base {
7
9template <class X>
10class find_action : public action
11{
12 X* x;
13protected:
14 std::vector<X*>& result;
15public:
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
37template <class X>
38void find_interface(base_ptr start, std::vector<X*>& result) {
39 find_action<X> fa(result);
40 traverser(fa).traverse(start);
41}
42
44template <class X, typename T>
45bool ensure_by_find(X* start, T*& pointer, unsigned i = 0)
46{
47 if (pointer)
48 return true;
49
50 std::vector<T*> 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}
The action class is used in tree traversals together with the traverser.
Definition action.h:17
simple action implementation that adds nodes implementing X to a results vector
Definition find_action.h:11
void select(base_ptr p)
make the passed object current
Definition find_action.h:18
bool implements_action() const
check if the current object implements the interface needed for this action
Definition find_action.h:31
bool begin()
perform the enter part of the action on the current object
Definition find_action.h:21
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
class used to traverse a tree structure
Definition traverser.h:102
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...
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
data::ref_ptr< base, true > base_ptr
ref counted pointer to base
Definition base.h:37
void find_interface(base_ptr start, std::vector< X * > &result)
collect all nodes that implement interface X
Definition find_action.h:38
the cgv namespace
Definition print.h:11