Plasma Engine  2.0
Loading...
Searching...
No Matches
SharedPtr.h
1#pragma once
2
3#include <Foundation/Types/RefCounted.h>
4#include <Foundation/Types/UniquePtr.h>
5
8template <typename T>
10{
11public:
12 PL_DECLARE_MEM_RELOCATABLE_TYPE();
13
16
18 template <typename U>
20
23 template <typename U>
24 plSharedPtr(U* pInstance, plAllocator* pAllocator);
25
27 plSharedPtr(const plSharedPtr<T>& other);
28
30 template <typename U>
32
34 template <typename U>
36
38 template <typename U>
40
42 plSharedPtr(std::nullptr_t);
43
46
48 template <typename U>
50
53
55 template <typename U>
57
59 template <typename U>
61
63 template <typename U>
65
67 plSharedPtr<T>& operator=(std::nullptr_t);
68
70 T* Borrow() const;
71
73 void Clear();
74
76 T& operator*() const;
77
79 T* operator->() const;
80
82 operator const T*() const;
83
85 operator T*();
86
88 explicit operator bool() const;
89
91 bool operator==(const plSharedPtr<T>& rhs) const;
92 bool operator!=(const plSharedPtr<T>& rhs) const;
93 bool operator<(const plSharedPtr<T>& rhs) const;
94 bool operator<=(const plSharedPtr<T>& rhs) const;
95 bool operator>(const plSharedPtr<T>& rhs) const;
96 bool operator>=(const plSharedPtr<T>& rhs) const;
97
99 bool operator==(std::nullptr_t) const;
100 bool operator!=(std::nullptr_t) const;
101 bool operator<(std::nullptr_t) const;
102 bool operator<=(std::nullptr_t) const;
103 bool operator>(std::nullptr_t) const;
104 bool operator>=(std::nullptr_t) const;
105
109 template <typename DERIVED>
111 {
112 return plSharedPtr<DERIVED>(static_cast<DERIVED*>(m_pInstance), m_pAllocator);
113 }
114
115private:
116 template <typename U>
117 friend class plSharedPtr;
118
119 void AddReferenceIfValid();
120 void ReleaseReferenceIfValid();
121
122 T* m_pInstance;
123 plAllocator* m_pAllocator;
124};
125
126#include <Foundation/Types/Implementation/SharedPtr_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
plSharedPtr< DERIVED > Downcast() const
Returns a copy of this, as an plSharedPtr<DERIVED>. Downcasts the stored pointer (using static_cast).
Definition SharedPtr.h:110
void Clear()
Destroys the managed object if no one else references it anymore and resets the shared ptr.
Definition SharedPtr_inl.h:174
plSharedPtr< T > & operator=(plUniquePtr< U > &&other)
Move assigns a shared ptr from a unique ptr. The unique ptr will be empty afterwards.
plSharedPtr(const plSharedPtr< U > &other)
Copy constructs a shared ptr from another. Both will hold a reference to the managed object afterward...
T * operator->() const
Provides access to the managed object.
Definition SharedPtr_inl.h:186
plSharedPtr< T > & operator=(const plInternal::NewInstance< U > &instance)
Sets the shared ptr from a freshly created instance through PL_NEW or PL_DEFAULT_NEW.
bool operator==(const plSharedPtr< T > &rhs) const
Compares the shared ptr against another shared ptr.
Definition SharedPtr_inl.h:210
T * Borrow() const
Borrows the managed object. The shared ptr stays unmodified.
Definition SharedPtr_inl.h:168
plSharedPtr(const plInternal::NewInstance< U > &instance)
Creates a shared ptr from a freshly created instance through PL_NEW or PL_DEFAULT_NEW.
plSharedPtr(U *pInstance, plAllocator *pAllocator)
Creates a shared ptr from a pointer and an allocator. The passed allocator will be used to destroy th...
T & operator*() const
Provides access to the managed object.
Definition SharedPtr_inl.h:180
~plSharedPtr()
Destroys the managed object using the stored allocator if no one else references it anymore.
Definition SharedPtr_inl.h:76
plSharedPtr(plUniquePtr< U > &&other)
Move constructs a shared ptr from a unique ptr. The unique ptr will be empty afterwards.
plSharedPtr< T > & operator=(plSharedPtr< U > &&other)
Move assigns a shared ptr from another. The other shared ptr will be empty afterwards.
plSharedPtr< T > & operator=(const plSharedPtr< U > &other)
Sets the shared ptr from another. Both will hold a reference to the managed object afterwards.
plSharedPtr(plSharedPtr< U > &&other)
Move constructs a shared ptr from another. The other shared ptr will be empty afterwards.
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
Definition Allocator_inl.h:18