Dev Essential  1.6.3
Loading...
Searching...
No Matches
delegate_decl.h
Go to the documentation of this file.
1
15
16#ifndef A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
17#define A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
18
19namespace a_util {
25namespace detail {
27namespace delegates {
28template <typename ReturnType>
29class DelegateInterface {
30public:
31 virtual ~DelegateInterface();
32 virtual ReturnType invoke() = 0;
33 virtual DelegateInterface<ReturnType>* clone() = 0;
34};
35
36template <typename ReturnType>
37class FunctionDelegate : public DelegateInterface<ReturnType> {
38 typedef ReturnType (*Function)();
39
40public:
41 FunctionDelegate(Function f);
42 virtual ReturnType invoke();
43 virtual DelegateInterface<ReturnType>* clone();
44
45private:
46 Function _func;
47};
48
49template <typename ReturnType, typename Method, typename Instance>
50class MethodDelegate : public DelegateInterface<ReturnType> {
51public:
52 MethodDelegate(Method method, Instance& instance);
53 virtual ReturnType invoke();
54 virtual DelegateInterface<ReturnType>* clone();
55
56private:
57 Method _method;
58 Instance& _instance;
59};
60} // namespace delegates
61
63namespace functions {
64template <typename ReturnType, typename ParamType>
65class DelegateInterface {
66public:
67 virtual ~DelegateInterface();
68 virtual ReturnType invoke(ParamType) = 0;
69 virtual DelegateInterface<ReturnType, ParamType>* clone() = 0;
70};
71
72template <typename ReturnType, typename ParamType>
73class FunctionDelegate : public DelegateInterface<ReturnType, ParamType> {
74 typedef ReturnType (*Function)(ParamType);
75
76public:
77 FunctionDelegate(Function f);
78 virtual ReturnType invoke(ParamType param);
79 virtual DelegateInterface<ReturnType, ParamType>* clone();
80
81private:
82 Function _func;
83};
84
85template <typename ReturnType, typename ParamType, typename Method, typename Instance>
86class MethodDelegate : public DelegateInterface<ReturnType, ParamType> {
87public:
88 MethodDelegate(Method method, Instance& inst);
89 virtual ReturnType invoke(ParamType param);
90 virtual DelegateInterface<ReturnType, ParamType>* clone();
91
92private:
93 Method _method;
94 Instance& _instance;
95};
96
97} // namespace functions
98
100template <typename DelegateType>
101class DelegateBase {
102protected: // 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
112public:
113 operator bool() const;
114
115private:
116 DelegateType* _ptr_to_callee;
117};
118
119} // namespace detail
121
122namespace experimental {
127template <typename ReturnType>
129 : public detail::DelegateBase<detail::delegates::DelegateInterface<ReturnType>> {
130public:
135 NullaryDelegate(ReturnType (*Function)());
143 template <typename Method, typename Instance>
144 NullaryDelegate(Method method, Instance& instance);
145
150 ReturnType operator()();
151
152private:
153 typedef detail::DelegateBase<detail::delegates::DelegateInterface<ReturnType>> Base;
154};
155
161template <typename ReturnType, typename ParamType>
163 : public detail::DelegateBase<detail::functions::DelegateInterface<ReturnType, ParamType>> {
164public:
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
188private:
189 typedef detail::DelegateBase<detail::functions::DelegateInterface<ReturnType, ParamType>> Base;
190};
191 //
192
193} // namespace experimental
194} // namespace a_util
195
199
200#endif // A_UTIL_UTIL_DELEGATE_DECL_HEADER_INCLUDED
ReturnType operator()()
Definition delegate_impl.h:194
NullaryDelegate(ReturnType(*Function)())
Definition delegate_impl.h:175
UnaryDelegate(Method method, Instance &instance)
Definition delegate_impl.h:212
ReturnType operator()(ParamType param)
Definition delegate_impl.h:222
UnaryDelegate(ReturnType(*Function)(ParamType))
Definition delegate_impl.h:202
Serves as component for features which are considered experimental.
Definition base.h:26
Serves as the root component, with common functionality documented in core functionality.
Definition base.h:24