activemq-cpp-3.8.2
Pointer.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef _DECAF_LANG_POINTER_H_
19 #define _DECAF_LANG_POINTER_H_
20 
21 #include <decaf/util/Config.h>
25 #include <decaf/util/Comparator.h>
26 #include <memory>
27 #include <typeinfo>
28 #include <algorithm>
29 #include <functional>
30 
31 namespace decaf {
32 namespace lang {
33 
34  // Used internally in Pointer.
35  struct STATIC_CAST_TOKEN {};
36  struct DYNAMIC_CAST_TOKEN {};
37 
52  template<typename T, typename REFCOUNTER = decaf::util::concurrent::atomic::AtomicRefCounter>
53  class Pointer : public REFCOUNTER {
54  private:
55 
56  typedef void (*deletionFuncPtr)(T* p);
57 
58  private:
59 
60  T* value;
61 
62  // Pointer to our internal delete function.
63  deletionFuncPtr onDelete;
64 
65  public:
66 
67  typedef T* PointerType; // type returned by operator->
68  typedef T& ReferenceType; // type returned by operator*
69  typedef REFCOUNTER CounterType; // Type of the Reference Counter
70 
71  public:
72 
79  Pointer() : REFCOUNTER(), value(NULL), onDelete(onDeleteFunc) {}
80 
88  explicit Pointer(const PointerType value) : REFCOUNTER(), value(value), onDelete(onDeleteFunc) {}
89 
97  Pointer(const Pointer& value) : REFCOUNTER(value), value(value.value), onDelete(onDeleteFunc) {}
98 
106  template<typename T1, typename R1>
107  Pointer(const Pointer<T1, R1>& value) : REFCOUNTER(value), value(value.get()), onDelete(onDeleteFunc) {}
108 
117  template<typename T1, typename R1>
118  Pointer(const Pointer<T1, R1>& value, const STATIC_CAST_TOKEN&) :
119  REFCOUNTER(value), value(static_cast<T*> (value.get())), onDelete(onDeleteFunc) {}
120 
132  template<typename T1, typename R1>
134  REFCOUNTER(value), value(dynamic_cast<T*> (value.get())), onDelete(onDeleteFunc) {
135 
136  if (this->value == NULL) {
137  // Remove the reference we took in the Reference Counter's ctor since we
138  // didn't actually create one as the dynamic cast failed.
139  REFCOUNTER::release();
141  __FILE__, __LINE__, "Failed to cast source pointer of type %s to this type: %s.",
142  typeid(T1).name(), typeid(T).name());
143  }
144  }
145 
146  virtual ~Pointer() {
147  if (REFCOUNTER::release() == true) {
148  onDelete(this->value);
149  }
150  }
151 
161  void reset(T* value = NULL) {
162  Pointer(value).swap(*this);
163  }
164 
174  T* release() {
175  T* temp = this->value;
176  this->value = NULL;
177  return temp;
178  }
179 
188  PointerType get() const {
189  return this->value;
190  }
191 
198  void swap(Pointer& value) {
199  std::swap(this->value, value.value);
200  REFCOUNTER::swap(value);
201  }
202 
207  Pointer& operator=(const Pointer& right) {
208  if (this == (void*) &right) {
209  return *this;
210  }
211 
212  Pointer temp(right);
213  temp.swap(*this);
214  return *this;
215  }
216  template<typename T1, typename R1>
218  if (this == (void*) &right) {
219  return *this;
220  }
221 
222  Pointer temp(right);
223  temp.swap(*this);
224  return *this;
225  }
226 
234  ReferenceType operator*() {
235  if (this->value == NULL) {
237  __FILE__, __LINE__, "Pointer operator& - Pointee is NULL.");
238  }
239 
240  return *(this->value);
241  }
242  ReferenceType operator*() const {
243  if (this->value == NULL) {
245  __FILE__, __LINE__, "Pointer operator& - Pointee is NULL.");
246  }
247 
248  return *(this->value);
249  }
250 
258  PointerType operator->() {
259  if (this->value == NULL) {
261  __FILE__, __LINE__, "Pointer operator-> - Pointee is NULL.");
262  }
263  return this->value;
264  }
265  PointerType operator->() const {
266  if (this->value == NULL) {
268  __FILE__, __LINE__, "Pointer operator-> - Pointee is NULL.");
269  }
270  return this->value;
271  }
272 
273  bool operator!() const {
274  return this->value == NULL;
275  }
276 
277  inline friend bool operator==(const Pointer& left, const T* right) {
278  return left.get() == right;
279  }
280 
281  inline friend bool operator==(const T* left, const Pointer& right) {
282  return left == right.get();
283  }
284 
285  inline friend bool operator!=(const Pointer& left, const T* right) {
286  return left.get() != right;
287  }
288 
289  inline friend bool operator!=(const T* left, const Pointer& right) {
290  return left != right.get();
291  }
292 
293  template<typename T1, typename R1>
294  bool operator==(const Pointer<T1, R1>& right) const {
295  return this->value == right.get();
296  }
297 
298  template<typename T1, typename R1>
299  bool operator!=(const Pointer<T1, R1>& right) const {
300  return !(this->value == right.get());
301  }
302 
303  template<typename T1>
306  }
307 
308  template<typename T1>
311  }
312 
313  private:
314 
315  // Internal Static deletion function.
316  static void onDeleteFunc(T* value) {
317  delete value;
318  }
319 
320  };
321 
323  template<typename T, typename R, typename U>
324  inline bool operator==(const Pointer<T, R>& left, const U* right) {
325  return left.get() == right;
326  }
327 
329  template<typename T, typename R, typename U>
330  inline bool operator==(const U* left, const Pointer<T, R>& right) {
331  return right.get() == left;
332  }
333 
335  template<typename T, typename R, typename U>
336  inline bool operator!=(const Pointer<T, R>& left, const U* right) {
337  return !(left.get() == right);
338  }
339 
341  template<typename T, typename R, typename U>
342  inline bool operator!=(const U* left, const Pointer<T, R>& right) {
343  return right.get() != left;
344  }
345 
347  template<typename T, typename R>
348  std::ostream& operator<<(std::ostream &out, const Pointer<T, R>& pointer) {
349  out << pointer.get();
350  return out;
351  }
352 
365  template<typename T, typename R = decaf::util::concurrent::atomic::AtomicRefCounter>
366  class PointerComparator: public decaf::util::Comparator<Pointer<T, R> > {
367  public:
368 
369  virtual ~PointerComparator() {}
370 
371  // Allows for operator less on types that implement Comparable or provide
372  // a workable operator <
373  virtual bool operator()(const Pointer<T, R>& left, const Pointer<T, R>& right) const {
374  return *left < *right;
375  }
376 
377  // Requires that the type in the pointer is an instance of a Comparable.
378  virtual int compare(const Pointer<T, R>& left, const Pointer<T, R>& right) const {
379  return *left < *right ? -1 : *right < *left ? 1 : 0;
380  }
381 
382  };
383 
384 }}
385 
387 namespace std {
388 
393  template<typename T>
394  struct less<decaf::lang::Pointer<T> > { //: public binary_function<decaf::lang::Pointer<T>, decaf::lang::Pointer<T>, bool> {
395 
398  typedef bool result_type;
399 
400  bool operator()(const decaf::lang::Pointer<T>& left, const decaf::lang::Pointer<T>& right) const {
401  return less<T*> ()(left.get(), right.get());
402  }
403  };
404 }
405 
406 #endif /*_DECAF_LANG_POINTER_H_*/
void reset(T *value=NULL)
Resets the Pointer to hold the new value.
Definition: Pointer.h:161
PointerType operator->() const
Definition: Pointer.h:265
Pointer(const Pointer< T1, R1 > &value)
Copy constructor.
Definition: Pointer.h:107
Pointer(const Pointer &value)
Copy constructor.
Definition: Pointer.h:97
Definition: Pointer.h:35
friend bool operator==(const Pointer &left, const T *right)
Definition: Pointer.h:277
friend bool operator==(const T *left, const Pointer &right)
Definition: Pointer.h:281
T & ReferenceType
Definition: Pointer.h:68
Pointer(const Pointer< T1, R1 > &value, const DYNAMIC_CAST_TOKEN &)
Dynamic Cast constructor.
Definition: Pointer.h:133
Pointer(const PointerType value)
Explicit Constructor, creates a Pointer that contains value with a single reference.
Definition: Pointer.h:88
A comparison function, which imposes a total ordering on some collection of objects.
Definition: Comparator.h:40
bool operator!() const
Definition: Pointer.h:273
ReferenceType operator*() const
Definition: Pointer.h:242
#define NULL
Definition: Config.h:33
virtual ~Pointer()
Definition: Pointer.h:146
bool operator==(const Pointer< T1, R1 > &right) const
Definition: Pointer.h:294
Definition: ArrayPointer.h:432
friend bool operator!=(const Pointer &left, const T *right)
Definition: Pointer.h:285
Definition: Pointer.h:36
Pointer()
Default Constructor.
Definition: Pointer.h:79
REFCOUNTER CounterType
Definition: Pointer.h:69
Pointer< T1, CounterType > dynamicCast() const
Definition: Pointer.h:304
T * release()
Releases the Pointer held and resets the internal pointer value to Null.
Definition: Pointer.h:174
Pointer(const Pointer< T1, R1 > &value, const STATIC_CAST_TOKEN &)
Static Cast constructor.
Definition: Pointer.h:118
bool result_type
Definition: Pointer.h:398
friend bool operator!=(const T *left, const Pointer &right)
Definition: Pointer.h:289
Pointer & operator=(const Pointer &right)
Assigns the value of right to this Pointer and increments the reference Count.
Definition: Pointer.h:207
decaf::lang::Pointer< T > first_argument_type
Definition: Pointer.h:396
virtual int compare(const Pointer< T, R > &left, const Pointer< T, R > &right) const
Definition: Pointer.h:378
bool operator()(const decaf::lang::Pointer< T > &left, const decaf::lang::Pointer< T > &right) const
Definition: Pointer.h:400
T * PointerType
Definition: Pointer.h:67
ReferenceType operator*()
Dereference Operator, returns a reference to the Contained value.
Definition: Pointer.h:234
PointerType get() const
Gets the real pointer that is contained within this Pointer.
Definition: Pointer.h:188
Definition: NullPointerException.h:32
Pointer< T1, CounterType > staticCast() const
Definition: Pointer.h:309
virtual ~PointerComparator()
Definition: Pointer.h:369
PointerType operator->()
Indirection Operator, returns a pointer to the Contained value.
Definition: Pointer.h:258
bool operator!=(const ArrayPointer< T > &left, const U *right)
Definition: ArrayPointer.h:391
virtual bool operator()(const Pointer< T, R > &left, const Pointer< T, R > &right) const
Definition: Pointer.h:373
bool operator==(const ArrayPointer< T > &left, const U *right)
Definition: ArrayPointer.h:379
This implementation of Comparator is designed to allows objects in a Collection to be sorted or teste...
Definition: Pointer.h:366
Definition: ClassCastException.h:31
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
bool operator!=(const Pointer< T1, R1 > &right) const
Definition: Pointer.h:299
void swap(Pointer &value)
Exception Safe Swap Function.
Definition: Pointer.h:198
decaf::lang::Pointer< T > second_argument_type
Definition: Pointer.h:397
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25
Pointer & operator=(const Pointer< T1, R1 > &right)
Definition: Pointer.h:217