16#ifndef AEONGAMES_UNIQUEANYPTR_H
17#define AEONGAMES_UNIQUEANYPTR_H
33 const std::type_info& ( *mManager ) (
void* aPointer )
43 std::swap ( mPointer, aUniqueAnyPtr.mPointer );
44 std::swap ( mManager, aUniqueAnyPtr.mManager );
65 aUniqueAnyPtr.Swap ( *
this );
74 mPointer{ aUniquePointer.release() }, mManager{Manager<T>} {}
82 mPointer{
reinterpret_cast<void*
> ( aPointer ) }, mManager{Manager<T>} {}
90 mPointer{
new T ( std::move ( aValue ) ) }, mManager{Manager<T>} {}
97 mManager ( mPointer );
110 aUniqueAnyPtr.Swap ( *
this );
120 UniqueAnyPtr& operator= ( std::unique_ptr<T>&& aUniquePointer )
noexcept
124 mManager ( mPointer );
126 mPointer = aUniquePointer.release();
127 mManager = Manager<T>;
141 mManager ( mPointer );
144 mManager = Manager<T>;
162 template<
class T> T*
Get()
const
170 std::ostringstream stream;
171 stream <<
"Unique Any Pointer of different type, Requested: " <<
typeid ( T ).name() <<
" Contained: " <<
GetTypeInfo().name();
172 throw std::runtime_error ( stream.str().c_str() );
174 return reinterpret_cast<T*
> ( mPointer );
182 template<
class T> T*
Get()
185 return const_cast<T*
> (
static_cast<const UniqueAnyPtr*
> ( this )->Get<T>() );
193 void* pointer = mPointer;
208 std::ostringstream stream;
209 stream <<
"Unique Any Pointer of different type, Requested: " <<
typeid ( T ).name() <<
" Contained: " <<
GetTypeInfo().name();
210 throw std::runtime_error ( stream.str().c_str() );
229 unique_any_ptr.
Swap ( *
this );
239 return mManager (
nullptr );
241 return typeid ( nullptr );
251 return GetTypeInfo().hash_code() ==
typeid ( T ).hash_code();
255 template<
typename T>
static const std::type_info& Manager (
void* aPointer )
259 delete reinterpret_cast<T*
> ( aPointer );
271 template<
typename T,
typename... Ts>
274 return UniqueAnyPtr (
new T ( ::std::forward<Ts> ( params )... ) );
A type-erased owning smart pointer with unique ownership semantics.
T * Get()
Returns a typed pointer to the owned object (non-const overload).
UniqueAnyPtr(T &&aValue) noexcept
Constructs by moving a value into a new heap allocation.
UniqueAnyPtr() noexcept=default
Default constructor.
const void * GetRaw() const
Returns a const void pointer to the owned object.
std::unique_ptr< T > UniquePointer()
Releases ownership and returns a std::unique_ptr.
T * Release()
Releases ownership and returns a typed pointer.
UniqueAnyPtr(UniqueAnyPtr &&aUniqueAnyPtr) noexcept
Move constructor.
UniqueAnyPtr(std::unique_ptr< T > &&aUniquePointer) noexcept
Constructs from a std::unique_ptr, taking ownership.
void * ReleaseRaw()
Releases ownership and returns the raw void pointer.
void Swap(UniqueAnyPtr &aUniqueAnyPtr) noexcept
Swaps the contents of this pointer with another.
const std::type_info & GetTypeInfo() const
Returns the std::type_info of the stored type.
T * Get() const
Returns a typed pointer to the owned object (const overload).
UniqueAnyPtr(T *aPointer) noexcept
Constructs from a raw pointer, taking ownership.
void Reset()
Destroys the owned object and resets to empty state.
~UniqueAnyPtr()
Destructor.
bool HasType() const
Checks whether the stored object is of type T.
<- This is here just for the literals
UniqueAnyPtr MakeUniqueAny(Ts &&... params)
Creates a UniqueAnyPtr owning a new instance of T.