VR-Vantage
Loading...
Searching...
No Matches
delegate.h
Go to the documentation of this file.
1/*********************************************************************************
2** Copyright (c) 2014 MAK Technologies, Inc.
3** All rights reserved.
4*********************************************************************************/
5
6#pragma once
7
12
13namespace makRadarFx
14{
15
16
18 template <typename Ret, typename Param0>
20 {
21 public:
22 virtual Ret invoke(Param0 param0) = 0;
23 virtual Callback* clone() = 0;
24
25 virtual ~Callback() {};
26
27 virtual bool operator==(const Callback& rhs)=0;
28 virtual bool operator!=(const Callback& rhs)
29 {
30 return !(*this==rhs);
31 }
32 };
33
34
37 template <typename Ret, typename Param0>
38 class StaticFunctionCallback : public Callback<Ret, Param0>
39 {
40 private:
41 Ret (*myFunction)(Param0);
42
43 public:
44 StaticFunctionCallback(Ret (*func)(Param0))
45 : myFunction(func)
46 {}
47
52
58
59 virtual Ret invoke(Param0 param0)
60 {
61 return (*myFunction)(param0);
62 }
63
64 virtual bool operator==(const Callback<Ret, Param0>& rhs)
65 {
67 return myFunction == rhsp->myFunction;
68 }
69 };
70
71
74 template <typename Ret, typename Param0, typename T, typename Method>
75 class MethodCallback : public Callback<Ret, Param0>
76 {
77 private:
78 void* myObject;
79 Method myMethod;
80
81 public:
82 MethodCallback(void *object, Method method)
83 : myObject(object)
84 , myMethod(method)
85 {}
86
88 {
89 myObject=orig.myObject;
90 myMethod=orig.myMethod;
91 }
92
94 {
96 return nc;
97 }
98
99 virtual Ret invoke(Param0 param0)
100 {
101 T *obj = static_cast<T *>(myObject);
102 return (obj->*myMethod)(param0);
103 }
104
105 virtual bool operator==(const Callback<Ret, Param0>& rhs)
106 {
107 MethodCallback* rhsp= (MethodCallback*)&rhs;
108 return (myObject == rhsp->myObject) && (myMethod==rhsp->myMethod);
109 }
110 };
111
114 template <typename Ret, typename Param0>
116 {
117 private:
120
121 public:
124 :myCallback(0)
125 ,myIsStatic(false)
126 {}
127
128 DtDelegate(Ret (*func)(Param0))
129 :myCallback(new StaticFunctionCallback<Ret, Param0>(func))
130 ,myIsStatic(true)
131 {}
132
134 template <typename T, typename Method>
135 DtDelegate(T *object, Method method)
136 :myCallback(new MethodCallback<Ret, Param0, T, Method>(object, method))
137 ,myIsStatic(false)
138 {}
139
140
142 {
143 myCallback=orig.myCallback->clone();
145 }
146
148 {
149 if(this!=&orig)
150 {
151 myCallback=orig.myCallback->clone();
153 }
154
155 return *this;
156 }
157
159 virtual ~DtDelegate(void) { delete myCallback; }
160
162 Ret operator()(Param0 param0)
163 {
164#ifdef _DEBUG
165 if(myCallback==0)
166 throw "Missing callback";
167#endif
168
169 return myCallback->invoke(param0);
170 }
171
172 bool operator==(const DtDelegate& rhs) const
173 {
174 if(myIsStatic!=rhs.myIsStatic)
175 {
176 return false;
177 }
178
179 return (*myCallback)==(*(rhs.myCallback));
180 }
181
182 bool operator!=(const DtDelegate& rhs)
183 {
184 return !(*this==rhs);
185 }
186 };
187} //makRadarFx::
Callback template the base abstract callback class that is used internally to the delegate.
Definition delegate.h:20
virtual bool operator==(const Callback &rhs)=0
virtual Callback * clone()=0
virtual bool operator!=(const Callback &rhs)
Definition delegate.h:28
virtual Ret invoke(Param0 param0)=0
virtual ~Callback()
Definition delegate.h:25
DtDelegate(Ret(*func)(Param0))
constructor using a static function
Definition delegate.h:128
DtDelegate & operator=(const DtDelegate &orig)
Definition delegate.h:147
bool operator!=(const DtDelegate &rhs)
Definition delegate.h:182
Callback< void, DtRadarFxConnector * > * myCallback
Definition delegate.h:118
virtual ~DtDelegate(void)
destructor
Definition delegate.h:159
DtDelegate(const DtDelegate &orig)
Definition delegate.h:141
bool operator==(const DtDelegate &rhs) const
Definition delegate.h:172
Ret operator()(Param0 param0)
execute operator
Definition delegate.h:162
DtDelegate()
default empty constructor
Definition delegate.h:123
DtDelegate(T *object, Method method)
constructor using a class member functions
Definition delegate.h:135
Member function callback is used to bind class member functions The return type, single parameter,...
Definition delegate.h:76
virtual Ret invoke(Param0 param0)
Definition delegate.h:99
virtual bool operator==(const Callback< Ret, Param0 > &rhs)
Definition delegate.h:105
Method myMethod
Definition delegate.h:79
void * myObject
Definition delegate.h:78
MethodCallback(void *object, Method method)
Definition delegate.h:82
MethodCallback(const MethodCallback &orig)
Definition delegate.h:87
virtual MethodCallback * clone()
Definition delegate.h:93
Static member function (or global function) callback is used to bind global or static class functions...
Definition delegate.h:39
Ret(* myFunction)(Param0)
Definition delegate.h:41
virtual Ret invoke(Param0 param0)
Definition delegate.h:59
virtual bool operator==(const Callback< Ret, Param0 > &rhs)
Definition delegate.h:64
StaticFunctionCallback(Ret(*func)(Param0))
Definition delegate.h:44
virtual StaticFunctionCallback * clone()
Definition delegate.h:53
StaticFunctionCallback(const StaticFunctionCallback &orig)
Definition delegate.h:48
Definition radarFxConnector.h:28