VR-Vantage 3.2 API Documentation
 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) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************************/
5 
6 #pragma once
7 
12 
13 namespace makRadarFx
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& 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 
49  {
51  }
52 
54  {
56  return nc;
57  }
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 
93  virtual MethodCallback* clone()
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  {}
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 
141  DtDelegate(const DtDelegate& orig)
142  {
143  myCallback=orig.myCallback->clone();
144  myIsStatic=orig.myIsStatic;
145  }
146 
148  {
149  if(this!=&orig)
150  {
151  myCallback=orig.myCallback->clone();
152  myIsStatic=orig.myIsStatic;
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::


Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)