25 #include "common/scummsys.h" 26 #include "common/noncopyable.h" 27 #include "common/safe-bool.h" 28 #include "common/types.h" 55 if (--_weakRefCount == 0)
64 if (--_strongRefCount == 0) {
70 bool isAlive()
const {
71 return _strongRefCount > 0;
74 RefValue getStrongCount()
const {
75 return _strongRefCount;
79 virtual void destructObject() = 0;
82 RefValue _weakRefCount;
83 RefValue _strongRefCount;
92 void destructObject()
override {
93 STATIC_ASSERT(
sizeof(T) > 0, SharedPtr_cannot_delete_incomplete_type);
100 template<
class T,
class DL>
106 void destructObject()
override {
166 typedef T *PointerType;
167 typedef T &ReferenceType;
168 typedef BasePtrTrackerInternal::RefValue RefValue;
170 SharedPtr() : _pointer(
nullptr), _tracker(
nullptr) {
173 SharedPtr(std::nullptr_t) : _pointer(
nullptr), _tracker(
nullptr) {
178 _tracker->decStrong();
185 template<
class T2,
class DL>
191 _tracker->incStrong();
197 _tracker->incStrong();
202 if (r._tracker && r._tracker->isAlive()) {
203 _pointer = r._pointer;
204 _tracker = r._tracker;
205 _tracker->incStrong();
220 T &operator*()
const { assert(_pointer);
return *_pointer; }
221 T *operator->()
const { assert(_pointer);
return _pointer; }
229 PointerType
get()
const {
return _pointer; }
233 return _pointer == r.
get();
238 return _pointer != r.
get();
241 bool operator==(std::nullptr_t)
const {
242 return _pointer ==
nullptr;
245 bool operator!=(std::nullptr_t)
const {
246 return _pointer !=
nullptr;
254 return _pointer !=
nullptr;
261 if (_tracker ==
nullptr)
263 return _tracker->getStrongCount();
272 return refCount() == 1;
280 _tracker->decStrong();
292 _pointer = r._pointer;
293 _tracker = r._tracker;
296 _tracker->incStrong();
298 oldTracker->decStrong();
308 if (r._tracker && r._tracker->isAlive()) {
309 _tracker = r._tracker;
310 _pointer = r._pointer;
311 _tracker->incStrong();
318 oldTracker->decStrong();
326 _tracker->decStrong();
345 return SharedPtr<T2>(
dynamic_cast<T2 *
>(_pointer), _tracker);
361 return SharedPtr<T2>(
reinterpret_cast<T2 *
>(_pointer), _tracker);
367 tracker->incStrong();
387 WeakPtr() : _pointer(
nullptr), _tracker(
nullptr) {
390 WeakPtr(std::nullptr_t) : _pointer(
nullptr), _tracker(
nullptr) {
420 return WeakPtr<T2>(expired() ? nullptr :
static_cast<T2 *
>(_pointer), _tracker);
428 return WeakPtr<T2>(expired() ? nullptr :
dynamic_cast<T2 *
>(_pointer), _tracker);
436 return WeakPtr<T2>(expired() ? nullptr :
const_cast<T2 *
>(_pointer), _tracker);
444 return WeakPtr<T2>(expired() ? nullptr :
reinterpret_cast<T2 *
>(_pointer), _tracker);
458 if (_tracker ==
nullptr)
460 return _tracker->getStrongCount();
467 return _tracker ==
nullptr || _tracker->getStrongCount() == 0;
475 return _tracker < other._tracker;
483 return _tracker < other._tracker;
520 _pointer = r._pointer;
521 _tracker = r._tracker;
526 oldTracker->decWeak();
536 _pointer = r._pointer;
537 _tracker = r._tracker;
542 oldTracker->decWeak();
555 template <
typename T>
557 inline void operator()(T *
object) {
558 STATIC_ASSERT(
sizeof(T) > 0, cannot_delete_incomplete_type);
563 template <
typename T>
565 inline void operator()(T *
object) {
566 STATIC_ASSERT(
sizeof(T) > 0, cannot_delete_incomplete_type);
571 template<
typename T,
class DL = DefaultDeleter<T> >
573 template<
class T2,
class DL2>
577 typedef T *PointerType;
578 typedef T &ReferenceType;
580 explicit ScopedPtr(PointerType o =
nullptr) : _pointer(o) {}
581 ScopedPtr(std::nullptr_t) : _pointer(
nullptr) {}
588 o._pointer =
nullptr;
591 ReferenceType operator*()
const {
return *_pointer; }
592 PointerType operator->()
const {
return _pointer; }
607 void reset(PointerType o =
nullptr) {
624 PointerType oldPointer = _pointer;
625 _pointer = other._pointer;
626 other._pointer =
nullptr;
636 PointerType
get()
const {
return _pointer; }
645 PointerType r = _pointer;
651 PointerType _pointer;
654 template<
typename T,
class DL = DefaultDeleter<T> >
658 typedef T *PointerType;
659 typedef T &ReferenceType;
661 explicit DisposablePtr(PointerType o, DisposeAfterUse::Flag dispose) : _pointer(o), _dispose(dispose), _shared() {}
664 o._pointer =
nullptr;
665 o._dispose = DisposeAfterUse::NO;
670 if (_dispose) DL()(_pointer);
673 ReferenceType operator*()
const {
return *_pointer; }
674 PointerType operator->()
const {
return _pointer; }
685 void reset(PointerType o, DisposeAfterUse::Flag dispose) {
686 if (_dispose) DL()(_pointer);
696 reset(
nullptr, DisposeAfterUse::NO);
700 bool isDynamicallyCastable() {
701 return dynamic_cast<T2 *
>(_pointer) !=
nullptr;
707 template <
class T2,
class DL2 = DefaultDeleter<T2> >
710 ret._pointer =
dynamic_cast<T2 *
>(_pointer);
711 ret._dispose = _dispose;
712 ret._shared = _shared.template dynamicCast<T2>();
714 _dispose = DisposeAfterUse::NO;
724 PointerType
get()
const {
return _pointer; }
726 template <
class T2,
class DL2>
730 DisposablePtr() : _pointer(
nullptr), _dispose(DisposeAfterUse::NO), _shared() {}
731 PointerType _pointer;
732 DisposeAfterUse::Flag _dispose;
743 #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)) 752 void store(
const T &value)
const;
785 void store(
const T &value)
const;
802 memcpy(&result, _ptr,
sizeof(T));
808 memcpy(_ptr, &value,
sizeof(T));
bool expired() const
Definition: ptr.h:466
WeakPtr< T2 > dynamicCast() const
Definition: ptr.h:427
void reset(const WeakPtr< T2 > &r)
Definition: ptr.h:533
bool unique() const
Definition: ptr.h:271
void reset(T *ptr)
Definition: ptr.h:324
ScopedPtr & operator=(std::nullptr_t)
Definition: ptr.h:615
SharedPtr< T2 > reinterpretCast() const
Definition: ptr.h:360
void reset(const SharedPtr< T2 > &r)
Definition: ptr.h:289
ScopedPtr(ScopedPtr< T2 > &&o)
Definition: ptr.h:587
SharedPtr< T2 > staticCast() const
Definition: ptr.h:336
bool owner_before(const WeakPtr< T2 > &other) const
Definition: ptr.h:474
bool operator_bool() const
Definition: ptr.h:253
int refCount() const
Definition: ptr.h:260
SharedPtr< T > lock() const
Definition: ptr.h:450
void reset()
Definition: ptr.h:278
void reset(const SharedPtr< T2 > &r)
Definition: ptr.h:517
WeakPtr< T2 > constCast() const
Definition: ptr.h:435
Definition: noncopyable.h:39
PointerType release()
Definition: ptr.h:644
PointerType get() const
Definition: ptr.h:229
WeakPtr< T2 > staticCast() const
Definition: ptr.h:419
bool operator_bool() const
Definition: ptr.h:598
Definition: algorithm.h:29
bool operator_bool() const
Definition: ptr.h:680
bool owner_before(const SharedPtr< T2 > &other) const
Definition: ptr.h:482
void reset(PointerType o=nullptr)
Definition: ptr.h:607
ScopedPtr & operator=(ScopedPtr< T2 > &&other)
Definition: ptr.h:623
void reset()
Definition: ptr.h:695
SharedPtr< T2 > dynamicCast() const
Definition: ptr.h:344
void reset(PointerType o, DisposeAfterUse::Flag dispose)
Definition: ptr.h:685
void reset(const WeakPtr< T2 > &r)
Definition: ptr.h:305
void reset()
Definition: ptr.h:506
Definition: safe-bool.h:56
SharedPtr< T2 > constCast() const
Definition: ptr.h:352
WeakPtr< T2 > reinterpretCast() const
Definition: ptr.h:443
int refCount() const
Definition: ptr.h:457