Pivotal GemFire Native Client Cache Reference  9.0.6
SharedPtr.hpp
Go to the documentation of this file.
1 #ifndef _GEMFIRE_SHAREDPTR_HPP_
2 #define _GEMFIRE_SHAREDPTR_HPP_
3 
4 /*=========================================================================
5  * Copyright (c) 2004-2014 Pivotal Software, Inc. All Rights Reserved.
6  * This product is protected by U.S. and international copyright
7  * and intellectual property laws. Pivotal products are covered by
8  * more patents listed at http://www.pivotal.io/patents.
9  *=========================================================================
10  */
11 
12 #include "SharedBase.hpp"
13 #include "Assert.hpp"
14 #include "TypeHelper.hpp"
15 #include <typeinfo>
16 #include "SharedPtrHelper.hpp"
17 
21 namespace gemfire {
22 
23 #if GF_DEVEL_ASSERTS == 1
24 #define GF_CHECK_NPE(x) if ( x != NULL ) { } else gemfire::SPEHelper::throwNullPointerException( typeid( *this ).name() )
25 #else
26 #define GF_CHECK_NPE(x)
27 #endif
28 
29 class MapEntry;
30 class MapEntryImpl;
31 
32 template<class Target>
35 class SharedPtr
36 {
37 
38 public:
40  inline SharedPtr()
41  : m_ptr(NULL)
42  {}
43 
45  inline SharedPtr(const NullSharedBase* ptr)
46  : m_ptr(NULL)
47  {}
48 
52  inline explicit SharedPtr(const Target* ptr)
53  : m_ptr(const_cast<Target *>(ptr))
54  {
55  if (NULL != m_ptr)
56  getSB( m_ptr )->preserveSB();
57  }
58 
60  inline SharedPtr(const SharedPtr& other)
61  : m_ptr(other.m_ptr)
62  {
63  if (NULL != m_ptr)
64  getSB( m_ptr )->preserveSB();
65  }
66 
70  template<class Other>
71  inline SharedPtr(const SharedPtr<Other>& other)
72  : m_ptr( getTarget< Target >( other.ptr( ) ) )
73  {
74  if (NULL != m_ptr)
75  getSB( m_ptr )->preserveSB();
76  }
77 
79  inline ~SharedPtr()
80  {
81  if (NULL != m_ptr)
82  getSB( m_ptr )->releaseSB();
83 
84  m_ptr = NULL;
85  }
86 
87  inline Target* operator -> () const
88  {
89  GF_CHECK_NPE( m_ptr );
90  GF_DEV_ASSERT( getSB( m_ptr )->refCount( ) > 0 );
91 
92  return m_ptr;
93  }
94 
95  inline Target& operator * () const
96  {
97  GF_CHECK_NPE( m_ptr );
98  return *m_ptr;
99  }
100 
104  inline SharedPtr& operator = (Target * other)
105  {
106  if (NULL != other)
107  getSB( other )->preserveSB();
108 
109  if (NULL != m_ptr)
110  getSB( m_ptr )->releaseSB();
111 
112  m_ptr = other;
113 
114  return *this;
115  }
116 
117  inline SharedPtr& operator = (const SharedPtr& other)
118  {
119  Target* otherPtr = other.m_ptr;
120 
121  if ( NULL != otherPtr ) {
122  getSB( otherPtr )->preserveSB( );
123  }
124  if ( NULL != m_ptr ) {
125  getSB( m_ptr )->releaseSB( );
126  }
127  m_ptr = otherPtr;
128 
129  GF_DEV_ASSERT( otherPtr == other.m_ptr );
130 
131  return *this;
132  }
133 
137  template<class Other>
138  inline SharedPtr& operator = (const SharedPtr<Other>& other)
139  {
140  Other* otherPtr = other.ptr( );
141 
142  Target* otherTargetPtr = getTarget< Target >( otherPtr );
143 
144  if ( NULL != otherPtr ) {
145  getSB( otherPtr )->preserveSB( );
146  }
147  if ( NULL != m_ptr ) {
148  getSB( m_ptr )->releaseSB( );
149  }
150  m_ptr = otherTargetPtr;
151 
152  GF_DEV_ASSERT( otherPtr == other.ptr( ) );
153 
154  return *this;
155  }
156 
157  inline SharedPtr& operator = (const NullSharedBase* nullOther)
158  {
159  if (m_ptr != NULL) {
160  getSB( m_ptr )->releaseSB();
161  }
162  m_ptr = NULL;
163  return *this;
164  }
165 
170  template<class Other>
171  inline SharedPtr& operator = (Other* other)
172  {
173  Target* otherTargetPtr = getTarget< Target >( other );
174 
175  if ( NULL != other ) {
176  getSB( other )->preserveSB( );
177  }
178  if ( NULL != m_ptr ) {
179  getSB( m_ptr )->releaseSB( );
180  }
181  m_ptr = otherTargetPtr;
182 
183  return *this;
184  }
185 
186  inline bool operator == (const Target* other) const
187  { return m_ptr == other; }
188 
189  inline bool operator != (const Target* other) const
190  { return m_ptr != other; }
191 
192  inline bool operator == (const NullSharedBase* nullOther) const
193  { return m_ptr == NULL; }
194 
195  inline bool operator != (const NullSharedBase* nullOther) const
196  { return m_ptr != NULL; }
197 
198  inline bool operator == (const SharedPtr& other) const
199  { return m_ptr == other.m_ptr; }
200 
201  inline bool operator != (const SharedPtr& other) const
202  { return m_ptr != other.m_ptr; }
203 
204  template<class Other>
205  inline bool operator == (const SharedPtr<Other>& other)
206  {
207  return ((const void*)m_ptr) == ((const void*) other.ptr() );
208  }
209 
210  template<class Other>
211  inline bool operator != (const SharedPtr<Other>& other)
212  { return ! operator == (other); }
213 
214  inline Target* ptr() const
215  {
216  return m_ptr;
217  }
218 
219 
220 private:
221 
223  inline explicit SharedPtr(bool noInit)
224  {}
225 
226  Target* m_ptr;
227 
228 friend class MapEntry;
229 friend class MapEntryImpl;
230 
231 };
232 
234 
235 
247 template <class TargetSP, class Other>
248 TargetSP staticCast( const SharedPtr<Other>& other )
249 {
250  GF_D_ASSERT( ( other.ptr( ) == NULL ) ||
251  ( dynamic_cast<GF_UNWRAP_SP( TargetSP )*>( other.ptr( ) ) != NULL ) );
252 
253  return TargetSP( static_cast<GF_UNWRAP_SP( TargetSP )*>( other.ptr( ) ) );
254 }
255 
259 template <class TargetSP, class Other>
260 TargetSP dynCast( const SharedPtr<Other>& other )
261 {
262  GF_UNWRAP_SP( TargetSP )* otherPtr;
263 
264  if ( ( other.ptr( ) == NULL ) ) {
265  return NULLPTR;
266  } else if ( ( otherPtr = dynamic_cast<GF_UNWRAP_SP( TargetSP )*>
267  ( other.ptr( ) ) ) != NULL ) {
268  return TargetSP( otherPtr );
269  } else {
270  SPEHelper::throwClassCastException( "dynCast: cast failed",
271  typeid( other ).name( ), typeid( TargetSP ).name( ) );
272  return NULLPTR;
273  }
274 }
275 
279 template<class TargetSP, class Other>
280 bool instanceOf(const SharedPtr<Other>& other)
281 {
282  return (dynamic_cast<GF_UNWRAP_SP(TargetSP)*> (other.ptr()) != NULL);
283 }
284 
285 }
286 
287 #endif
TargetSP staticCast(const SharedPtr< Other > &other)
Statically cast the underlying pointer to the given type.
Definition: SharedPtr.hpp:248
SharedPtr()
Constructor.
Definition: SharedPtr.hpp:40
Class encapsulating a NULL SharedBase smart pointer.
Definition: SharedBase.hpp:78
bool instanceOf(const SharedPtr< Other > &other)
Dynamically check if the underlying pointer is of the given SharedPtr type.
Definition: SharedPtr.hpp:280
SharedPtr(const Target *ptr)
Explicit copy constructor, given a pointer.
Definition: SharedPtr.hpp:52
SharedPtr(const SharedPtr< Other > &other)
Constructor, given another SharedPtr.
Definition: SharedPtr.hpp:71
#define GF_D_ASSERT(x)
Throws the given assertion if GF_DEBUG_ASSERTS is true.
Definition: Assert.hpp:73
~SharedPtr()
Destructor.
Definition: SharedPtr.hpp:79
SharedPtr & operator=(Target *other)
Assigns a pointer.
Definition: SharedPtr.hpp:104
This namespace contains all the GemFire C++ API classes, enumerations and globals.
Definition: Assert.hpp:19
SharedPtr(const SharedPtr &other)
Constructor, given another SharedPtr.
Definition: SharedPtr.hpp:60
Assertion functions for debugging.
SharedPtr(const NullSharedBase *ptr)
Constructor for the NULL pointer.
Definition: SharedPtr.hpp:45
TargetSP dynCast(const SharedPtr< Other > &other)
Dynamically cast the underlying pointer to the given type and throw ClassCastException if the cast fa...
Definition: SharedPtr.hpp:260
void preserveSB() const
Atomically increment reference count.
void releaseSB() const
Atomically decrement reference count, the SharedBase object is automatically deleted when its referen...
#define GF_DEV_ASSERT(x)
Throws the given assertion if GF_DEVEL_ASSERTS is true.
Definition: Assert.hpp:80
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:35

GemFire C++ Cache API Documentation