VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
delegate.h
Go to the documentation of this file.
1 /*********************************************************************************
2 ** Copyright (c) 2019 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************************/
5 
6 #pragma once
7 
12 
13 namespace makVrf
14 {
15 
16 
18  template <typename Ret, typename Param0>
19  class Callback
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<Ret, Param0>& rhs)=0;
28  virtual bool operator!=(const Callback<Ret, Param0>& 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 
49  {
51  }
52 
53  virtual StaticFunctionCallback* clone() override
54  {
56  return nc;
57  }
58 
59  virtual Ret invoke(Param0 param0) override {
60  return (*myFunction)(param0);
61  }
62 
63  virtual bool operator==(const Callback<Ret, Param0>& rhs) override
64  {
66  return myFunction == rhsp->myFunction;
67  }
68  };
69 
70 
73  template <typename Ret, typename Param0, typename T, typename Method>
74  class MethodCallback : public Callback<Ret, Param0>
75  {
76  private:
77  void* myObject;
78  Method myMethod;
79 
80  public:
81  MethodCallback(void *object, Method method)
82  : myObject(object)
83  , myMethod(method)
84  {}
85 
87  {
88  myObject=orig.myObject;
89  myMethod=orig.myMethod;
90  }
91 
92  virtual MethodCallback* clone() override
93  {
95  return nc;
96  }
97 
98  virtual Ret invoke(Param0 param0) override
99  {
100  T *obj = static_cast<T *>(myObject);
101  return (obj->*myMethod)(param0);
102  }
103 
104  virtual bool operator==(const Callback<Ret, Param0>& rhs) override
105  {
106  MethodCallback* rhsp= (MethodCallback*)&rhs;
107  return (myObject == rhsp->myObject) && (myMethod==rhsp->myMethod);
108  }
109  };
110 
113  template <typename Ret, typename Param0>
115  {
116  private:
119 
120  public:
123  :myCallback(0)
124  ,myIsStatic(false)
125  {}
127  DtDelegate(Ret (*func)(Param0))
128  :myCallback(new StaticFunctionCallback<Ret, Param0>(func))
129  ,myIsStatic(true)
130  {}
131 
133  template <typename T, typename Method>
134  DtDelegate(T *object, Method method)
135  :myCallback(new MethodCallback<Ret, Param0, T, Method>(object, method))
136  ,myIsStatic(false)
137  {}
138 
139 
140  DtDelegate(const DtDelegate& orig)
141  {
142  myCallback=orig.myCallback->clone();
143  myIsStatic=orig.myIsStatic;
144  }
145 
147  {
148  if(this!=&orig)
149  {
150  myCallback=orig.myCallback->clone();
151  myIsStatic=orig.myIsStatic;
152  }
153 
154  return *this;
155  }
156 
158  virtual ~DtDelegate(void) { delete myCallback; }
159 
161  Ret operator()(Param0 param0)
162  {
163 #ifdef _DEBUG
164  if(myCallback==0)
165  throw "Missing callback";
166 #endif
167 
168  return myCallback->invoke(param0);
169  }
170 
171  bool operator==(const DtDelegate& rhs) const
172  {
173  if(myIsStatic!=rhs.myIsStatic)
174  {
175  return false;
176  }
177 
178  return (*myCallback)==(*(rhs.myCallback));
179  }
180 
181  bool operator!=(const DtDelegate& rhs)
182  {
183  return !(*this==rhs);
184  }
185  };
186 } //makVrf::
StaticFunctionCallback(const StaticFunctionCallback &orig)
Definition: delegate.h:48
virtual MethodCallback * clone() override
Definition: delegate.h:92
Method myMethod
Definition: delegate.h:78
virtual Ret invoke(Param0 param0) override
Definition: delegate.h:59
virtual Callback * clone()=0
virtual Ret invoke(Param0 param0) override
Definition: delegate.h:98
DtDelegate(Ret(*func)(Param0))
constructor using a static function
Definition: delegate.h:127
Ret(* myFunction)(Param0)
Definition: delegate.h:41
virtual Ret invoke(Param0 param0)=0
DtDelegate & operator=(const DtDelegate &orig)
Definition: delegate.h:146
DtDelegate(T *object, Method method)
constructor using a class member functions
Definition: delegate.h:134
virtual StaticFunctionCallback * clone() override
Definition: delegate.h:53
MethodCallback(void *object, Method method)
Definition: delegate.h:81
Delegate template for binding static or member functions for callback at later time The return type a...
Definition: delegate.h:114
bool operator!=(const DtDelegate &rhs)
Definition: delegate.h:181
DtDelegate()
default empty constructor
Definition: delegate.h:122
virtual bool operator==(const Callback< Ret, Param0 > &rhs) override
Definition: delegate.h:63
Callback< Ret, Param0 > * myCallback
Definition: delegate.h:117
bool myIsStatic
Definition: delegate.h:118
void * myObject
Definition: delegate.h:77
Ret operator()(Param0 param0)
execute operator
Definition: delegate.h:161
virtual ~Callback()
Definition: delegate.h:25
StaticFunctionCallback(Ret(*func)(Param0))
Definition: delegate.h:44
DtDelegate(const DtDelegate &orig)
Definition: delegate.h:140
Callback template the base abstract callback class that is used internally to the delegate...
Definition: delegate.h:19
MethodCallback(const MethodCallback &orig)
Definition: delegate.h:86
Member function callback is used to bind class member functions The return type, single parameter...
Definition: delegate.h:74
bool operator==(const DtDelegate &rhs) const
Definition: delegate.h:171
virtual bool operator==(const Callback< Ret, Param0 > &rhs)=0
Static member function (or global function) callback is used to bind global or static class functions...
Definition: delegate.h:38
virtual ~DtDelegate(void)
destructor
Definition: delegate.h:158
virtual bool operator==(const Callback< Ret, Param0 > &rhs) override
Definition: delegate.h:104
virtual bool operator!=(const Callback< Ret, Param0 > &rhs)
Definition: delegate.h:28

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)