1 | #ifndef __XIOS_VISITABLE_HPP |
---|
2 | #define __XIOS_VISITABLE_HPP |
---|
3 | |
---|
4 | #include "visitor.hpp" |
---|
5 | #include <vector> |
---|
6 | |
---|
7 | namespace xios { |
---|
8 | |
---|
9 | template <typename R, typename ParamType> |
---|
10 | struct DefaultCatchAll |
---|
11 | { |
---|
12 | static R OnUnknownVisitor(ParamType&, CBaseVisitor&) |
---|
13 | { return R(); } |
---|
14 | }; |
---|
15 | |
---|
16 | //////////////////////////////////////////////////////////////////////////////// |
---|
17 | // class template CBaseVisitable |
---|
18 | //////////////////////////////////////////////////////////////////////////////// |
---|
19 | template |
---|
20 | < |
---|
21 | typename R = void, |
---|
22 | template <typename, class> class CatchAll = DefaultCatchAll, |
---|
23 | bool ConstVisitable = false |
---|
24 | > |
---|
25 | class CBaseVisitable; |
---|
26 | |
---|
27 | template<typename R,template <typename, class> class CatchAll> |
---|
28 | class CBaseVisitable<R, CatchAll, false> |
---|
29 | { |
---|
30 | public: |
---|
31 | typedef R ReturnType; |
---|
32 | virtual ~CBaseVisitable() {} |
---|
33 | virtual ReturnType apply(CBaseVisitor&) = 0; |
---|
34 | virtual void apply(std::vector<CBaseVisitor*>&) = 0; |
---|
35 | |
---|
36 | protected: // give access only to the hierarchy |
---|
37 | template <class T> |
---|
38 | static ReturnType applyImpl(T& visited, CBaseVisitor& guest) |
---|
39 | { |
---|
40 | // Apply the CVisitor |
---|
41 | if (CVisitor<T,R>* p = dynamic_cast<CVisitor<T,R>*>(&guest)) |
---|
42 | { |
---|
43 | return p->operate(visited); |
---|
44 | } |
---|
45 | return CatchAll<R, T>::OnUnknownVisitor(visited, guest); |
---|
46 | } |
---|
47 | }; |
---|
48 | |
---|
49 | template<typename R,template <typename, class> class CatchAll> |
---|
50 | class CBaseVisitable<R, CatchAll, true> |
---|
51 | { |
---|
52 | public: |
---|
53 | typedef R ReturnType; |
---|
54 | virtual ~CBaseVisitable() {} |
---|
55 | virtual ReturnType apply(CBaseVisitor&) const = 0; |
---|
56 | virtual void apply(std::vector<CBaseVisitor*>&) const = 0; |
---|
57 | |
---|
58 | protected: // give access only to the hierarchy |
---|
59 | template <class T> |
---|
60 | static ReturnType applyImpl(const T& visited, CBaseVisitor& guest) |
---|
61 | { |
---|
62 | // Apply the CVisitor |
---|
63 | if (CVisitor<T,R>* p = dynamic_cast<CVisitor<T,R, true>*>(&guest)) |
---|
64 | { |
---|
65 | return p->operate(visited); |
---|
66 | } |
---|
67 | return CatchAll<R, T>::OnUnknownVisitor(const_cast<T&>(visited), guest); |
---|
68 | } |
---|
69 | }; |
---|
70 | |
---|
71 | //////////////////////////////////////////////////////////////////////////////// |
---|
72 | /// Put it in every class that you want to make guest |
---|
73 | /// functions (in addition to deriving it from CBaseVisitable<R>) |
---|
74 | //////////////////////////////////////////////////////////////////////////////// |
---|
75 | #define DEFINE_VISITABLE() \ |
---|
76 | virtual ReturnType apply(::xios::CBaseVisitor& guest) \ |
---|
77 | { return applyImpl(*this, guest); } \ |
---|
78 | virtual void apply(std::vector<CBaseVisitor*>& guests) \ |
---|
79 | { \ |
---|
80 | for (std::vector<CBaseVisitor*>::iterator it = guests.begin(); it != guests.end(); ++it) \ |
---|
81 | apply(*(*it)); \ |
---|
82 | } |
---|
83 | //////////////////////////////////////////////////////////////////////////////// |
---|
84 | /// Put it in every class that you want to make guest by const member |
---|
85 | /// functions (in addition to deriving it from CBaseVisitable<R>) |
---|
86 | //////////////////////////////////////////////////////////////////////////////// |
---|
87 | #define DEFINE_CONST_VISITABLE() \ |
---|
88 | virtual ReturnType apply(::xios::CBaseVisitor& guest) const \ |
---|
89 | { return applyImpl(*this, guest); } \ |
---|
90 | virtual void apply(std::vector<CBaseVisitor*>& guests) const \ |
---|
91 | { \ |
---|
92 | for (std::vector<CBaseVisitor*>::iterator it = guests.begin(); it != guests.end(); ++it) \ |
---|
93 | apply(*(*it)); \ |
---|
94 | } |
---|
95 | |
---|
96 | class CGenericTransformation : public virtual CBaseVisitable<> |
---|
97 | { |
---|
98 | public: |
---|
99 | DEFINE_VISITABLE() |
---|
100 | }; |
---|
101 | |
---|
102 | } |
---|
103 | #endif // __XIOS_VISITABLE_HPP |
---|