TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Pointer.h
Go to the documentation of this file.
1 
12 #ifndef G3D_Pointer_h
13 #define G3D_Pointer_h
14 
15 #include "G3D/debugAssert.h"
16 #include "G3D/ReferenceCount.h"
17 
18 namespace G3D {
19 
62 template<typename ValueType>
63 class Pointer {
64 private:
65 
66  class Interface {
67  public:
68  virtual ~Interface() {};
69  virtual void set(ValueType b) = 0;
70  virtual ValueType get() const = 0;
71  virtual Interface* clone() const = 0;
72  virtual bool isNull() const = 0;
73  };
74 
75  class Memory : public Interface {
76  private:
77 
78  ValueType* value;
79 
80  public:
81 
82  Memory(ValueType* value) : value(value) {}
83 
84  virtual void set(ValueType v) {
85  *value = v;
86  }
87 
88  virtual ValueType get() const {
89  return *value;
90  }
91 
92  virtual Interface* clone() const {
93  return new Memory(value);
94  }
95 
96  virtual bool isNull() const {
97  return value == NULL;
98  }
99  };
100 
101  template<typename GetMethod, typename SetMethod>
102  class FcnAccessor : public Interface {
103  private:
104 
105  GetMethod getMethod;
106  SetMethod setMethod;
107 
108  public:
109 
110  FcnAccessor(GetMethod getMethod, SetMethod setMethod) : getMethod(getMethod), setMethod(setMethod) {
111  }
112 
113  virtual void set(ValueType v) {
114  if (setMethod) {
115  (*setMethod)(v);
116  }
117  }
118 
119  virtual ValueType get() const {
120  return (*getMethod)();
121  }
122 
123  virtual Interface* clone() const {
124  return new FcnAccessor(getMethod, setMethod);
125  }
126 
127  virtual bool isNull() const { return false; }
128  };
129 
130  template<class T, typename GetMethod, typename SetMethod>
131  class Accessor : public Interface {
132  private:
133 
134  T* object;
135  GetMethod getMethod;
136  SetMethod setMethod;
137 
138  public:
139 
140  Accessor(T* object,
141  GetMethod getMethod,
142  SetMethod setMethod) : object(object), getMethod(getMethod), setMethod(setMethod) {
143  debugAssert(object != NULL);
144  }
145 
146  virtual void set(ValueType v) {
147  if (setMethod) {
148  (object->*setMethod)(v);
149  }
150  }
151 
152  virtual ValueType get() const {
153  return (object->*getMethod)();
154  }
155 
156  virtual Interface* clone() const {
157  return new Accessor(object, getMethod, setMethod);
158  }
159 
160  virtual bool isNull() const {
161  return object == NULL;
162  }
163  };
164 
165 
166  template<class T, typename GetMethod, typename SetMethod>
167  class SharedAccessor : public Interface {
168  private:
169 
170  shared_ptr<T> object;
171  GetMethod getMethod;
172  SetMethod setMethod;
173 
174  public:
175 
177  (const shared_ptr<T>& object,
178  GetMethod getMethod,
179  SetMethod setMethod) : object(object), getMethod(getMethod), setMethod(setMethod) {
180 
181  debugAssert(object != NULL);
182  }
183 
184  virtual void set(ValueType v) {
185  if (setMethod) {
186  (object.get()->*setMethod)(v);
187  }
188  }
189 
190  virtual ValueType get() const {
191  return (object.get()->*getMethod)();
192  }
193 
194  virtual Interface* clone() const {
195  return new SharedAccessor(object, getMethod, setMethod);
196  }
197 
198  virtual bool isNull() const {
199  return (bool)object;
200  }
201  };
202 
204 
205 public:
206 
207  Pointer() : m_interface(NULL) {};
208 
210  Pointer(ValueType* v) : m_interface(new Memory(v)) {}
211 
212  inline bool isNull() const {
213  return (m_interface == NULL) || m_interface->isNull();
214  }
215 
216  // Assignment
217  inline Pointer& operator=(const Pointer& r) {
218  delete m_interface;
219  if (r.m_interface != NULL) {
220  m_interface = r.m_interface->clone();
221  } else {
222  m_interface = NULL;
223  }
224  return this[0];
225  }
226 
227  Pointer(const Pointer& p) : m_interface(NULL) {
228  this[0] = p;
229  }
230 
231 
233  template<class Class>
234  Pointer(const shared_ptr<Class>& object,
235  ValueType (Class::*getMethod)() const,
236  void (Class::*setMethod)(ValueType)) :
237  m_interface(new SharedAccessor<Class, ValueType (Class::*)() const, void (Class::*)(ValueType)>(object, getMethod, setMethod)) {}
238 
240  template<class Class>
241  Pointer(const shared_ptr<Class>& object,
242  const ValueType& (Class::*getMethod)() const,
243  void (Class::*setMethod)(ValueType)) :
244  m_interface(new SharedAccessor<Class, const ValueType& (Class::*)() const, void (Class::*)(ValueType)>(object, getMethod, setMethod)) {}
245 
246 
248  Pointer(ValueType (*getMethod)(),
249  void (*setMethod)(ValueType)) :
250  m_interface(new FcnAccessor<ValueType (*)(), void (*)(ValueType)>(getMethod, setMethod)) {}
251 
253  Pointer(const ValueType& (*getMethod)(),
254  void (*setMethod)(ValueType)) :
255  m_interface(new FcnAccessor<const ValueType& (*)(), void (*)(ValueType)>(getMethod, setMethod)) {}
256 
257 
259  template<class Class>
260  Pointer(const shared_ptr<Class>& object,
261  ValueType (Class::*getMethod)() const,
262  void (Class::*setMethod)(const ValueType&)) :
263  m_interface(new SharedAccessor<Class, ValueType (Class::*)() const, void (Class::*)(const ValueType&)>(object, getMethod, setMethod)) {}
264 
266  template<class Class>
267  Pointer(const shared_ptr<Class>& object,
268  const ValueType& (Class::*getMethod)() const,
269  void (Class::*setMethod)(const ValueType&)) :
270  m_interface(new SharedAccessor<Class, const ValueType& (Class::*)() const, void (Class::*)(const ValueType&)>(object, getMethod, setMethod)) {}
271 
273  template<class Class>
274  Pointer(Class* object,
275  const ValueType& (Class::*getMethod)() const,
276  void (Class::*setMethod)(const ValueType&)) :
277  m_interface(new Accessor<Class, const ValueType& (Class::*)() const, void (Class::*)(const ValueType&)>(object, getMethod, setMethod)) {}
278 
280  template<class Class>
281  Pointer(Class* object,
282  ValueType (Class::*getMethod)() const,
283  void (Class::*setMethod)(const ValueType&)) :
284  m_interface(new Accessor<Class, ValueType (Class::*)() const, void (Class::*)(const ValueType&)>(object, getMethod, setMethod)) {}
285 
287  template<class Class>
288  Pointer(Class* object,
289  const ValueType& (Class::*getMethod)() const,
290  void (Class::*setMethod)(ValueType)) :
291  m_interface(new Accessor<Class, const ValueType& (Class::*)() const, void (Class::*)(ValueType)>(object, getMethod, setMethod)) {}
292 
294  template<class Class>
295  Pointer(Class* object,
296  ValueType (Class::*getMethod)() const,
297  void (Class::*setMethod)(ValueType)) :
298  m_interface(new Accessor<Class, ValueType (Class::*)() const, void (Class::*)(ValueType)>(object, getMethod, setMethod)) {}
299 
301  delete m_interface;
302  }
303 
304  inline const ValueType getValue() const {
305  debugAssert(m_interface != NULL);
306  return m_interface->get();
307  }
308 
311  inline void setValue(const ValueType& v) {
312  debugAssert(m_interface != NULL);
313  m_interface->set(v);
314  }
315 
317  private:
318 
319  friend class Pointer;
321  IndirectValue(Pointer* p) : pointer(p) {}
322 
323  public:
324 
325  void operator=(const ValueType& v) {
326  pointer->setValue(v);
327  }
328 
329  operator ValueType() const {
330  return pointer->getValue();
331  }
332 
333  };
334 
336  return IndirectValue(this);
337  }
338 
339  inline const ValueType operator*() const {
340  return getValue();
341  }
342 };
343 
344 template<class T>
345 bool isNull(const Pointer<T>& p) {
346  return p.isNull();
347 }
348 
349 template<class T>
350 bool notNull(const Pointer<T>& p) {
351  return ! p.isNull();
352 }
353 
354 }
355 
356 #endif
Pointer(Class *object, const ValueType &(Class::*getMethod)() const, void(Class::*setMethod)(ValueType))
Definition: Pointer.h:288
const ValueType getValue() const
Definition: Pointer.h:304
SetMethod setMethod
Definition: Pointer.h:172
Pointer(const shared_ptr< Class > &object, const ValueType &(Class::*getMethod)() const, void(Class::*setMethod)(const ValueType &))
Definition: Pointer.h:267
IndirectValue operator*()
Definition: Pointer.h:335
Definition: Pointer.h:63
Definition: Pointer.h:316
Definition: Pointer.h:102
GetMethod getMethod
Definition: Pointer.h:135
bool notNull(const Pointer< T > &p)
Definition: Pointer.h:350
Pointer(ValueType *v)
Definition: Pointer.h:210
Definition: Pointer.h:131
virtual ~Interface()
Definition: Pointer.h:68
Pointer(const shared_ptr< Class > &object, ValueType(Class::*getMethod)() const, void(Class::*setMethod)(ValueType))
Definition: Pointer.h:234
SharedAccessor(const shared_ptr< T > &object, GetMethod getMethod, SetMethod setMethod)
Definition: Pointer.h:177
virtual bool isNull() const
Definition: Pointer.h:198
SetMethod setMethod
Definition: Pointer.h:106
virtual Interface * clone() const =0
Definition: AABox.h:25
Accessor(T *object, GetMethod getMethod, SetMethod setMethod)
Definition: Pointer.h:140
SetMethod setMethod
Definition: Pointer.h:136
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: Pointer.h:167
const ValueType operator*() const
Definition: Pointer.h:339
Definition: Pointer.h:75
virtual Interface * clone() const
Definition: Pointer.h:92
virtual bool isNull() const =0
virtual ValueType get() const =0
virtual Interface * clone() const
Definition: Pointer.h:194
ValueType * value
Definition: Pointer.h:78
Definition: Pointer.h:66
IndirectValue(Pointer *p)
Definition: Pointer.h:321
#define debugAssert(exp)
Definition: debugAssert.h:160
virtual void set(ValueType v)
Definition: Pointer.h:184
void setValue(const ValueType &v)
Assign a value to the referenced element. If this Pointer was initialized with a NULL setMethod...
Definition: Pointer.h:311
Pointer(Class *object, ValueType(Class::*getMethod)() const, void(Class::*setMethod)(ValueType))
Definition: Pointer.h:295
Pointer(const ValueType &(*getMethod)(), void(*setMethod)(ValueType))
Definition: Pointer.h:253
Pointer(Class *object, const ValueType &(Class::*getMethod)() const, void(Class::*setMethod)(const ValueType &))
Definition: Pointer.h:274
Pointer()
Definition: Pointer.h:207
Pointer(const shared_ptr< Class > &object, ValueType(Class::*getMethod)() const, void(Class::*setMethod)(const ValueType &))
Definition: Pointer.h:260
Pointer(ValueType(*getMethod)(), void(*setMethod)(ValueType))
Definition: Pointer.h:248
virtual bool isNull() const
Definition: Pointer.h:160
GetMethod getMethod
Definition: Pointer.h:171
virtual bool isNull() const
Definition: Pointer.h:127
Pointer(Class *object, ValueType(Class::*getMethod)() const, void(Class::*setMethod)(const ValueType &))
Definition: Pointer.h:281
shared_ptr< T > object
Definition: Pointer.h:170
Interface * m_interface
Definition: Pointer.h:203
bool isNull(const Pointer< T > &p)
Definition: Pointer.h:345
Pointer * pointer
Definition: Pointer.h:320
Pointer & operator=(const Pointer &r)
Definition: Pointer.h:217
virtual void set(ValueType v)
Definition: Pointer.h:84
Pointer(const Pointer &p)
Definition: Pointer.h:227
void operator=(const ValueType &v)
Definition: Pointer.h:325
virtual void set(ValueType v)
Definition: Pointer.h:146
Pointer(const shared_ptr< Class > &object, const ValueType &(Class::*getMethod)() const, void(Class::*setMethod)(ValueType))
Definition: Pointer.h:241
~Pointer()
Definition: Pointer.h:300
FcnAccessor(GetMethod getMethod, SetMethod setMethod)
Definition: Pointer.h:110
virtual Interface * clone() const
Definition: Pointer.h:123
#define const
Definition: zconf.h:217
virtual void set(ValueType v)
Definition: Pointer.h:113
virtual Interface * clone() const
Definition: Pointer.h:156
T * object
Definition: Pointer.h:134
virtual void set(ValueType b)=0
bool isNull() const
Definition: Pointer.h:212
Memory(ValueType *value)
Definition: Pointer.h:82
GetMethod getMethod
Definition: Pointer.h:105
virtual bool isNull() const
Definition: Pointer.h:96