EHS/include/ehs/UniPtr.h

85 lines
952 B
C
Raw Permalink Normal View History

2024-02-05 22:25:30 -08:00
#pragma once
namespace ehs
{
template<typename T>
class UniPtr
{
private:
T* data;
public:
~UniPtr()
{
delete data;
}
UniPtr()
: data(nullptr)
{
}
UniPtr(T* data)
: data(data)
{
}
UniPtr(UniPtr&& uniPtr) noexcept
: data(uniPtr.data)
{
uniPtr.data = nullptr;
}
UniPtr(const UniPtr& uniPtr)
: data(nullptr)
{
}
UniPtr& operator=(UniPtr&& uniPtr) noexcept
{
if (this == &uniPtr)
return *this;
delete data;
data = uniPtr.data;
uniPtr.data = nullptr;
return *this;
}
UniPtr& operator=(const UniPtr& uniPtr) noexcept
{
if (this == &uniPtr)
return *this;
delete data;
data = nullptr;
return *this;
}
bool operator==(T* inPtr) const
{
return data == inPtr;
}
bool operator!=(T* inPtr) const
{
return data != inPtr;
}
void Release()
{
delete data;
data = nullptr;
}
T* GetData()
{
return data;
}
};
}