VMware GemFire Native C++ Reference  9.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SharedPtr.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef GEODE_SHAREDPTR_H_
4 #define GEODE_SHAREDPTR_H_
5 
6 /*
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "SharedBase.hpp"
24 #include "Assert.hpp"
25 #include "TypeHelper.hpp"
26 #include <typeinfo>
27 #include "SharedPtrHelper.hpp"
28 
32 namespace apache {
33 namespace geode {
34 namespace client {
35 
36 #if GF_DEVEL_ASSERTS == 1
37 #define GF_CHECK_NPE(x) \
38  if (x != NULL) { \
39  } else \
40  apache::geode::client::SPEHelper::throwNullPointerException( \
41  typeid(*this).name())
42 #else
43 #define GF_CHECK_NPE(x)
44 #endif
45 
46 class MapEntry;
47 class MapEntryImpl;
48 
49 template <class Target>
52 class SharedPtr {
53  public:
55  inline SharedPtr() : m_ptr(NULL) {}
56 
58  inline SharedPtr(const NullSharedBase* ptr) : m_ptr(NULL) {}
59 
65  inline explicit SharedPtr(const Target* ptr)
66  : m_ptr(const_cast<Target*>(ptr)) {
67  if (NULL != m_ptr) getSB(m_ptr)->preserveSB();
68  }
69 
71  inline SharedPtr(const SharedPtr& other) : m_ptr(other.m_ptr) {
72  if (NULL != m_ptr) getSB(m_ptr)->preserveSB();
73  }
74 
80  template <class Other>
81  inline SharedPtr(const SharedPtr<Other>& other)
82  : m_ptr(getTarget<Target>(other.ptr())) {
83  if (NULL != m_ptr) getSB(m_ptr)->preserveSB();
84  }
85 
87  inline ~SharedPtr() {
88  if (NULL != m_ptr) getSB(m_ptr)->releaseSB();
89 
90  m_ptr = NULL;
91  }
92 
93  inline Target* operator->() const {
94  GF_CHECK_NPE(m_ptr);
95  GF_DEV_ASSERT(getSB(m_ptr)->refCount() > 0);
96 
97  return m_ptr;
98  }
99 
100  inline Target& operator*() const {
101  GF_CHECK_NPE(m_ptr);
102  return *m_ptr;
103  }
104 
110  inline SharedPtr& operator=(Target* other) {
111  if (NULL != other) getSB(other)->preserveSB();
112 
113  if (NULL != m_ptr) getSB(m_ptr)->releaseSB();
114 
115  m_ptr = other;
116 
117  return *this;
118  }
119 
120  inline SharedPtr& operator=(const SharedPtr& other) {
121  Target* otherPtr = other.m_ptr;
122 
123  if (NULL != otherPtr) {
124  getSB(otherPtr)->preserveSB();
125  }
126  if (NULL != m_ptr) {
127  getSB(m_ptr)->releaseSB();
128  }
129  m_ptr = otherPtr;
130 
131  GF_DEV_ASSERT(otherPtr == other.m_ptr);
132 
133  return *this;
134  }
135 
142  template <class Other>
143  inline SharedPtr& operator=(const SharedPtr<Other>& other) {
144  Other* otherPtr = other.ptr();
145 
146  Target* otherTargetPtr = getTarget<Target>(otherPtr);
147 
148  if (NULL != otherPtr) {
149  getSB(otherPtr)->preserveSB();
150  }
151  if (NULL != m_ptr) {
152  getSB(m_ptr)->releaseSB();
153  }
154  m_ptr = otherTargetPtr;
155 
156  GF_DEV_ASSERT(otherPtr == other.ptr());
157 
158  return *this;
159  }
160 
161  inline SharedPtr& operator=(const NullSharedBase* nullOther) {
162  if (m_ptr != NULL) {
163  getSB(m_ptr)->releaseSB();
164  }
165  m_ptr = NULL;
166  return *this;
167  }
168 
177  template <class Other>
178  inline SharedPtr& operator=(Other* other) {
179  Target* otherTargetPtr = getTarget<Target>(other);
180 
181  if (NULL != other) {
182  getSB(other)->preserveSB();
183  }
184  if (NULL != m_ptr) {
185  getSB(m_ptr)->releaseSB();
186  }
187  m_ptr = otherTargetPtr;
188 
189  return *this;
190  }
191 
192  inline bool operator==(const Target* other) const { return m_ptr == other; }
193 
194  inline bool operator!=(const Target* other) const { return m_ptr != other; }
195 
196  inline bool operator==(const NullSharedBase* nullOther) const {
197  return m_ptr == NULL;
198  }
199 
200  inline bool operator!=(const NullSharedBase* nullOther) const {
201  return m_ptr != NULL;
202  }
203 
204  inline bool operator==(const SharedPtr& other) const {
205  return m_ptr == other.m_ptr;
206  }
207 
208  inline bool operator!=(const SharedPtr& other) const {
209  return m_ptr != other.m_ptr;
210  }
211 
212  template <class Other>
213  inline bool operator==(const SharedPtr<Other>& other) {
214  return ((const void*)m_ptr) == ((const void*)other.ptr());
215  }
216 
217  template <class Other>
218  inline bool operator!=(const SharedPtr<Other>& other) {
219  return !operator==(other);
220  }
221 
222  inline Target* ptr() const { return m_ptr; }
223 
224  private:
226  inline explicit SharedPtr(bool noInit) {}
227 
228  Target* m_ptr;
229 
230  friend class MapEntry;
231  friend class MapEntryImpl;
232 };
233 
235 
247 template <class TargetSP, class Other>
248 TargetSP staticCast(const SharedPtr<Other>& other) {
249  GF_D_ASSERT((other.ptr() == NULL) ||
250  (dynamic_cast<GF_UNWRAP_SP(TargetSP)*>(other.ptr()) != NULL));
251 
252  return TargetSP(static_cast<GF_UNWRAP_SP(TargetSP)*>(other.ptr()));
253 }
254 
258 template <class TargetSP, class Other>
259 TargetSP dynCast(const SharedPtr<Other>& other) {
260  GF_UNWRAP_SP(TargetSP) * otherPtr;
261 
262  if ((other.ptr() == NULL)) {
263  return NULLPTR;
264  } else if ((otherPtr = dynamic_cast<GF_UNWRAP_SP(TargetSP)*>(other.ptr())) !=
265  NULL) {
266  return TargetSP(otherPtr);
267  } else {
268  SPEHelper::throwClassCastException(
269  "dynCast: cast failed", typeid(other).name(), typeid(TargetSP).name());
270  return NULLPTR;
271  }
272 }
273 
277 template <class TargetSP, class Other>
278 bool instanceOf(const SharedPtr<Other>& other) {
279  return (dynamic_cast<GF_UNWRAP_SP(TargetSP)*>(other.ptr()) != NULL);
280 }
281 } // namespace client
282 } // namespace geode
283 } // namespace apache
284 
285 #endif // GEODE_SHAREDPTR_H_
#define GF_DEV_ASSERT(x)
Throws the given assertion if GF_DEVEL_ASSERTS is true.
Definition: Assert.hpp:97
Each enum represents a predefined RegionAttributes in a Cache.
Definition: Assert.hpp:31
SharedPtr & operator=(const SharedPtr< Other > &other)
Assigns a pointer of type Other from a SharedPtr object.
Definition: SharedPtr.hpp:143
void releaseSB() const
Atomically decrement reference count, the SharedBase object is automatically deleted when its referen...
SharedPtr(const NullSharedBase *ptr)
Constructor for the NULL pointer.
Definition: SharedPtr.hpp:58
SharedPtr & operator=(Other *other)
Assigns a pointer of type Other.
Definition: SharedPtr.hpp:178
SharedPtr(const Target *ptr)
Explicit copy constructor, given a pointer.
Definition: SharedPtr.hpp:65
Class encapsulating a NULL SharedBase smart pointer.
Definition: SharedBase.hpp:73
void preserveSB() const
Atomically increment reference count.
~SharedPtr()
Destructor.
Definition: SharedPtr.hpp:87
SharedPtr(const SharedPtr< Other > &other)
Constructor, given another SharedPtr.
Definition: SharedPtr.hpp:81
#define GF_D_ASSERT(x)
Throws the given assertion if GF_DEBUG_ASSERTS is true.
Definition: Assert.hpp:89
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:259
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:52
SharedPtr & operator=(Target *other)
Assigns a pointer.
Definition: SharedPtr.hpp:110
Assertion functions for debugging.
bool instanceOf(const SharedPtr< Other > &other)
Dynamically check if the underlying pointer is of the given SharedPtr type.
Definition: SharedPtr.hpp:278
SharedPtr(const SharedPtr &other)
Constructor, given another SharedPtr.
Definition: SharedPtr.hpp:71
TargetSP staticCast(const SharedPtr< Other > &other)
Statically cast the underlying pointer to the given type.
Definition: SharedPtr.hpp:248
SharedPtr()
Constructor.
Definition: SharedPtr.hpp:55
This namespace contains all the Geode C++ API classes, enumerations and globals.

Pivotal GemFire C++ Cache API Documentation