18 #ifndef ION_BASE_SHAREDPTR_H_
19 #define ION_BASE_SHAREDPTR_H_
23 #include "base/macros.h"
66 explicit SharedPtr(
T* shared) : ptr_(shared), shr_(shared) {
73 : ptr_(p.
Get()), shr_(p.
Get()) {
89 T*
Get()
const {
return ptr_; }
93 if (new_shared != ptr_) {
116 Reset(static_cast<T*>(new_shared));
124 if (p.
Get() != ptr_) {
153 return p.ptr_ == ptr_;
158 return p.ptr_ != ptr_;
164 std::swap(ptr_, p.ptr_);
165 std::swap(shr_, p.shr_);
170 void AddReference() {
172 shr_->IncrementRefCount();
176 void RemoveReference() {
178 shr_->DecrementRefCount();
186 const Shareable* shr_;
190 template <
typename To,
typename From>
204 #endif // ION_BASE_SHAREDPTR_H_
T & operator*() const
The * operator returns the underlying instance.
SharedPtr(const SharedPtr< U > &p)
Constructor that allows sharing of a pointer to a type that is compatible with T*.
SharedPtr< T > & operator=(U *new_shared)
Allow assignment to a compatible raw pointer.
T * operator->() const
The -> operator forwards to the raw pointer.
T * Get() const
Returns a raw pointer to the instance, which may be NULL.
SharedPtr< T > & operator=(const SharedPtr< T > &p)
Allow assignment to a SharedPtr of the same type.
bool operator==(const SharedPtr< T > &p) const
The equality operator returns true if the raw pointers are the same.
SharedPtr(const SharedPtr< T > &p)
The copy constructor shares the instance from the other pointer.
SharedPtr(T *shared)
Constructor that takes a raw shared pointer.
SharedPtr< To > DynamicPtrCast(const SharedPtr< From > &orig)
Allows casting SharedPtrs down a type hierarchy.
SharedPtr< T > & operator=(const SharedPtr< U > &p)
Allow assignment to a compatible SharedPtr.
SharedPtr()
The default constructor initializes the pointer to NULL.
Copyright 2016 Google Inc.
void Reset()
Make the SharedPtr point to NULL.
void Reset(T *new_shared)
Changes the pointer to point to the given shared, which may be NULL.
SharedPtr< T > & operator=(T *new_shared)
Assignment to a raw pointer is the same as Reset().
bool operator!=(const SharedPtr< T > &p) const
The inequality operator returns true if the raw pointers differ.
void swap(SharedPtr< T > &p)
STL-style function to swap the raw pointer with another SharedPtr without the need for copying...
A SharedPtr is a smart shared pointer to an instance of some class that implements reference counting...