source: XIOS/trunk/src/visitable.hpp @ 622

Last change on this file since 622 was 619, checked in by mhnguyen, 9 years ago

Implementing the first prototype of filter

+) Create new class filter
+) Implement class for specific algorithm
+) Implement inversing algorithm

Test
+) On Curie
+) Grid with one axis: passed

File size: 3.5 KB
Line 
1#ifndef __XIOS_VISITABLE_HPP
2#define __XIOS_VISITABLE_HPP
3
4#include "visitor.hpp"
5#include <vector>
6
7namespace xios {
8
9template <typename R, typename ParamType>
10struct DefaultCatchAll
11{
12  static R OnUnknownVisitor(ParamType&, CBaseVisitor&)
13  { return R(); }
14};
15
16////////////////////////////////////////////////////////////////////////////////
17// class template CBaseVisitable
18////////////////////////////////////////////////////////////////////////////////
19template
20<
21  typename R = void,
22  template <typename, class> class CatchAll = DefaultCatchAll,
23  bool ConstVisitable = false
24>
25class CBaseVisitable;
26
27template<typename R,template <typename, class> class CatchAll>
28class CBaseVisitable<R, CatchAll, false>
29{
30public:
31  typedef R ReturnType;
32  virtual ~CBaseVisitable() {}
33  virtual ReturnType apply(CBaseVisitor&) = 0;
34  virtual void apply(std::vector<CBaseVisitor*>&) = 0;
35
36protected: // 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
49template<typename R,template <typename, class> class CatchAll>
50class CBaseVisitable<R, CatchAll, true>
51{
52public:
53  typedef R ReturnType;
54  virtual ~CBaseVisitable() {}
55  virtual ReturnType apply(CBaseVisitor&) const = 0;
56  virtual void apply(std::vector<CBaseVisitor*>&) const = 0;
57
58protected: // 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
96class CGenericTransformation : public virtual CBaseVisitable<>
97{
98public:
99  DEFINE_VISITABLE()
100};
101
102}
103#endif // __XIOS_VISITABLE_HPP
Note: See TracBrowser for help on using the repository browser.