Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sharedptr.h
Go to the documentation of this file.
1 
18 #ifndef ION_BASE_SHAREDPTR_H_
19 #define ION_BASE_SHAREDPTR_H_
20 
21 #include <algorithm>
22 
23 #include "base/macros.h"
24 #include "ion/base/logging.h"
25 #include "ion/base/shareable.h"
26 
27 namespace ion {
28 namespace base {
29 
60 template <typename T> class SharedPtr {
61  public:
63  SharedPtr() : ptr_(NULL), shr_(NULL) {}
64 
66  explicit SharedPtr(T* shared) : ptr_(shared), shr_(shared) {
67  AddReference();
68  }
69 
72  template <typename U> SharedPtr(const SharedPtr<U>& p)
73  : ptr_(p.Get()), shr_(p.Get()) {
74  AddReference();
75  }
76 
78  SharedPtr(const SharedPtr<T>& p) : ptr_(p.Get()), shr_(p.shr_) {
79  AddReference();
80  }
81 
83  RemoveReference();
84  ptr_ = NULL;
85  shr_ = NULL;
86  }
87 
89  T* Get() const { return ptr_; }
90 
92  void Reset(T* new_shared) {
93  if (new_shared != ptr_) {
94  RemoveReference();
95  ptr_ = new_shared;
96  shr_ = new_shared;
97  AddReference();
98  }
99  }
100 
102  void Reset() {
103  RemoveReference();
104  ptr_ = NULL;
105  shr_ = NULL;
106  }
107 
109  SharedPtr<T>& operator=(T* new_shared) {
110  Reset(new_shared);
111  return *this;
112  }
113 
115  template <typename U> SharedPtr<T>& operator=(U* new_shared) {
116  Reset(static_cast<T*>(new_shared));
117  return *this;
118  }
119 
124  if (p.Get() != ptr_) {
125  RemoveReference();
126  ptr_ = p.Get();
127  shr_ = p.shr_;
128  AddReference();
129  }
130  return *this;
131  }
132 
134  template <typename U> SharedPtr<T>& operator=(const SharedPtr<U>& p) {
135  Reset(p.Get());
136  return *this;
137  }
138 
140  T* operator->() const {
141  DCHECK(ptr_);
142  return ptr_;
143  }
144 
146  T& operator*() const {
147  DCHECK(ptr_);
148  return *ptr_;
149  }
150 
152  bool operator==(const SharedPtr<T>& p) const {
153  return p.ptr_ == ptr_;
154  }
155 
157  bool operator!=(const SharedPtr<T>& p) const {
158  return p.ptr_ != ptr_;
159  }
160 
163  void swap(SharedPtr<T>& p) {
164  std::swap(ptr_, p.ptr_);
165  std::swap(shr_, p.shr_);
166  }
167 
168  private:
170  void AddReference() {
171  if (shr_)
172  shr_->IncrementRefCount();
173  }
174 
176  void RemoveReference() {
177  if (shr_)
178  shr_->DecrementRefCount();
179  }
180 
181  T* ptr_;
186  const Shareable* shr_;
187 };
188 
190 template <typename To, typename From>
192  return SharedPtr<To>(dynamic_cast<To*>(orig.Get()));
193 }
194 
200 
201 } // namespace base
202 } // namespace ion
203 
204 #endif // ION_BASE_SHAREDPTR_H_
T & operator*() const
The * operator returns the underlying instance.
Definition: sharedptr.h:146
SharedPtr(const SharedPtr< U > &p)
Constructor that allows sharing of a pointer to a type that is compatible with T*.
Definition: sharedptr.h:72
SharedPtr< T > & operator=(U *new_shared)
Allow assignment to a compatible raw pointer.
Definition: sharedptr.h:115
T * operator->() const
The -> operator forwards to the raw pointer.
Definition: sharedptr.h:140
#define DCHECK(expr)
Definition: logging.h:331
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
Definition: sharedptr.h:89
SharedPtr< T > & operator=(const SharedPtr< T > &p)
Allow assignment to a SharedPtr of the same type.
Definition: sharedptr.h:123
bool operator==(const SharedPtr< T > &p) const
The equality operator returns true if the raw pointers are the same.
Definition: sharedptr.h:152
SharedPtr(const SharedPtr< T > &p)
The copy constructor shares the instance from the other pointer.
Definition: sharedptr.h:78
SharedPtr(T *shared)
Constructor that takes a raw shared pointer.
Definition: sharedptr.h:66
SharedPtr< To > DynamicPtrCast(const SharedPtr< From > &orig)
Allows casting SharedPtrs down a type hierarchy.
Definition: sharedptr.h:191
SharedPtr< T > & operator=(const SharedPtr< U > &p)
Allow assignment to a compatible SharedPtr.
Definition: sharedptr.h:134
SharedPtr()
The default constructor initializes the pointer to NULL.
Definition: sharedptr.h:63
Copyright 2016 Google Inc.
void Reset()
Make the SharedPtr point to NULL.
Definition: sharedptr.h:102
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
Definition: sharedptr.h:92
SharedPtr< T > & operator=(T *new_shared)
Assignment to a raw pointer is the same as Reset().
Definition: sharedptr.h:109
bool operator!=(const SharedPtr< T > &p) const
The inequality operator returns true if the raw pointers differ.
Definition: sharedptr.h:157
void swap(SharedPtr< T > &p)
STL-style function to swap the raw pointer with another SharedPtr without the need for copying...
Definition: sharedptr.h:163
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...
Definition: sharedptr.h:60