mirror of
https://github.com/ZDoom/Raze.git
synced 2025-05-29 16:31:43 +00:00
- started reorganizing SW's memory management.
Need to get rid of all those unmanaged allocations and present game data in an easily serializable form. This adds a managed TPointer class that replicates the useful parts of std::unique_pointer but steers clear of its properties that often render it useless.
This commit is contained in:
parent
588fa5ffe2
commit
dcb393bc44
5 changed files with 175 additions and 17 deletions
|
@ -1368,6 +1368,128 @@ protected:
|
|||
};
|
||||
|
||||
|
||||
// Pointer wrapper without the unpleasant side effects of std::unique_ptr, mainly the inability to copy it.
|
||||
// This class owns the object with no means to release it, and copying the pointer copies the object.
|
||||
template <class T>
|
||||
class TPointer
|
||||
{
|
||||
public:
|
||||
|
||||
////////
|
||||
TPointer()
|
||||
{
|
||||
Ptr = nullptr;
|
||||
}
|
||||
TPointer(const T& other)
|
||||
{
|
||||
Alloc();
|
||||
*Ptr = other;
|
||||
}
|
||||
TPointer(T&& other)
|
||||
{
|
||||
Alloc();
|
||||
*Ptr = other;
|
||||
}
|
||||
TPointer(const TPointer<T>& other)
|
||||
{
|
||||
DoCopy(other);
|
||||
}
|
||||
TPointer(TPointer<T>&& other)
|
||||
{
|
||||
Ptr = other.Ptr;
|
||||
other.Ptr = nullptr;
|
||||
}
|
||||
TPointer<T>& operator= (const T& other)
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
Alloc();
|
||||
*Ptr = other;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
TPointer<T>& operator= (const TPointer<T>& other)
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
DoCopy(other);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
TPointer<T>& operator= (TPointer<T>&& other)
|
||||
{
|
||||
if (&other != this)
|
||||
{
|
||||
if (Ptr) delete Ptr;
|
||||
Ptr = other.Ptr;
|
||||
other.Ptr = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
~TPointer()
|
||||
{
|
||||
if (Ptr) delete Ptr;
|
||||
Ptr = nullptr;
|
||||
}
|
||||
// Check equality of two pointers
|
||||
bool operator==(const TPointer<T>& other) const
|
||||
{
|
||||
return *Ptr == *other.Ptr;
|
||||
}
|
||||
|
||||
T& operator* () const
|
||||
{
|
||||
assert(Ptr);
|
||||
return *Ptr;
|
||||
}
|
||||
|
||||
T* operator->() { return Ptr; }
|
||||
|
||||
// returns raw pointer
|
||||
T* Data() const
|
||||
{
|
||||
return Ptr;
|
||||
}
|
||||
|
||||
operator T* () const
|
||||
{
|
||||
return Ptr;
|
||||
}
|
||||
|
||||
void Alloc()
|
||||
{
|
||||
if (!Ptr) Ptr = new T;
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
if (Ptr) delete Ptr;
|
||||
Ptr = nullptr;
|
||||
}
|
||||
|
||||
void Swap(TPointer<T>& other)
|
||||
{
|
||||
std::swap(Ptr, other.Ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
T* Ptr;
|
||||
|
||||
void DoCopy(const TPointer<T>& other)
|
||||
{
|
||||
if (other.Ptr == nullptr)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
Alloc();
|
||||
*Ptr = *other.Ptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue