ADTF  3.18.2
delegate_decl.h
Go to the documentation of this file.
1 
16 #ifndef A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
17 #define A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
18 
19 namespace a_util {
25 namespace detail {
27 namespace delegates {
28 template <typename ReturnType>
29 class DelegateInterface {
30 public:
31  virtual ~DelegateInterface();
32  virtual ReturnType invoke() = 0;
33  virtual DelegateInterface<ReturnType>* clone() = 0;
34 };
35 
36 template <typename ReturnType>
37 class FunctionDelegate : public DelegateInterface<ReturnType> {
38  typedef ReturnType (*Function)();
39 
40 public:
41  FunctionDelegate(Function f);
42  virtual ReturnType invoke();
43  virtual DelegateInterface<ReturnType>* clone();
44 
45 private:
46  Function _func;
47 };
48 
49 template <typename ReturnType, typename Method, typename Instance>
50 class MethodDelegate : public DelegateInterface<ReturnType> {
51 public:
52  MethodDelegate(Method method, Instance& instance);
53  virtual ReturnType invoke();
54  virtual DelegateInterface<ReturnType>* clone();
55 
56 private:
57  Method _method;
58  Instance& _instance;
59 };
60 } // namespace delegates
61 
63 namespace functions {
64 template <typename ReturnType, typename ParamType>
65 class DelegateInterface {
66 public:
67  virtual ~DelegateInterface();
68  virtual ReturnType invoke(ParamType) = 0;
69  virtual DelegateInterface<ReturnType, ParamType>* clone() = 0;
70 };
71 
72 template <typename ReturnType, typename ParamType>
73 class FunctionDelegate : public DelegateInterface<ReturnType, ParamType> {
74  typedef ReturnType (*Function)(ParamType);
75 
76 public:
77  FunctionDelegate(Function f);
78  virtual ReturnType invoke(ParamType param);
79  virtual DelegateInterface<ReturnType, ParamType>* clone();
80 
81 private:
82  Function _func;
83 };
84 
85 template <typename ReturnType, typename ParamType, typename Method, typename Instance>
86 class MethodDelegate : public DelegateInterface<ReturnType, ParamType> {
87 public:
88  MethodDelegate(Method method, Instance& inst);
89  virtual ReturnType invoke(ParamType param);
90  virtual DelegateInterface<ReturnType, ParamType>* clone();
91 
92 private:
93  Method _method;
94  Instance& _instance;
95 };
96 
97 } // namespace functions
98 
100 template <typename DelegateType>
101 class DelegateBase {
102 protected: // construction/destruction only available through specialized class
103  ~DelegateBase();
104  DelegateBase();
105  DelegateBase(const DelegateBase& other);
106  DelegateBase& operator=(const DelegateBase& other);
107 
108  void setDelegate(DelegateType* delegate);
109  DelegateType* getDelegate();
110  const DelegateType* getDelegate() const;
111 
112 public:
113  operator bool() const;
114 
115 private:
116  DelegateType* _ptr_to_callee;
117 };
118 
119 } // namespace detail
122 namespace experimental {
127 template <typename ReturnType>
129  : public detail::DelegateBase<detail::delegates::DelegateInterface<ReturnType>> {
130 public:
135  NullaryDelegate(ReturnType (*Function)());
143  template <typename Method, typename Instance>
144  NullaryDelegate(Method method, Instance& instance);
145 
150  ReturnType operator()();
151 
152 private:
153  typedef detail::DelegateBase<detail::delegates::DelegateInterface<ReturnType>> Base;
154 };
155 
161 template <typename ReturnType, typename ParamType>
163  : public detail::DelegateBase<detail::functions::DelegateInterface<ReturnType, ParamType>> {
164 public:
169  UnaryDelegate(ReturnType (*Function)(ParamType));
170 
178  template <typename Method, typename Instance>
179  UnaryDelegate(Method method, Instance& instance);
180 
186  ReturnType operator()(ParamType param);
187 
188 private:
189  typedef detail::DelegateBase<detail::functions::DelegateInterface<ReturnType, ParamType>> Base;
190 }; //
192 
193 } // namespace experimental
194 } // namespace a_util
195 
200 #endif // A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
General delegate template: encapsulates a function or a method without a parameter.
NullaryDelegate(ReturnType(*Function)())
CTOR to delegate a function.
ReturnType operator()()
Invocation operator - invokes the delegated method or function.
General function template: encapsulates a function or a method with a single parameter.
ReturnType operator()(ParamType param)
Invocation operator - invokes the delegated method or function.
UnaryDelegate(ReturnType(*Function)(ParamType))
CTOR to delegate an unary function.
Private implementation for NullaryDelegate and UnaryDelegate types.
Serves as the root component, with common functionality documented in core functionality.
Definition: base.h:24