101 lines
2.2 KiB
C++
101 lines
2.2 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: ExtendStlLikeSharedPtr.hpp
|
|
Date: 2022-1-25
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
namespace Aurora::Memory
|
|
{
|
|
template<class TType_t, class Base_t>
|
|
struct ExSharedPtr : public Base_t
|
|
{
|
|
bool cached {};
|
|
|
|
using element_type = Base_t::element_type;
|
|
using weak_type = Base_t::weak_type;
|
|
using base_type = Base_t;
|
|
using Base_t::Base_t;
|
|
|
|
ExSharedPtr(Base_t &&in) : Base_t(in)
|
|
{}
|
|
ExSharedPtr(const Base_t &in) : Base_t(in)
|
|
{}
|
|
|
|
operator Base_t() const noexcept
|
|
{
|
|
return *this;
|
|
}
|
|
|
|
ExSharedPtr &operator =(const Base_t &in) noexcept
|
|
{
|
|
Base_t::operator=(in);
|
|
return *this;
|
|
}
|
|
|
|
TType_t &operator*() const
|
|
{
|
|
throwif();
|
|
return Base_t::operator*();
|
|
}
|
|
|
|
TType_t *operator->() const
|
|
{
|
|
throwif();
|
|
return Base_t::operator->();
|
|
}
|
|
|
|
TType_t &operator*()
|
|
{
|
|
throwif();
|
|
return Base_t::operator*();
|
|
}
|
|
|
|
TType_t *operator->()
|
|
{
|
|
throwif();
|
|
return Base_t::operator->();
|
|
}
|
|
|
|
private:
|
|
|
|
auline void _cache()
|
|
{
|
|
cached = Base_t::operator bool();
|
|
}
|
|
|
|
auline void throwif() const
|
|
{
|
|
if (!cached) [[unlikely]]
|
|
{
|
|
if (!Base_t::operator bool()) [[likely]]
|
|
{
|
|
AU_THROW_STRING("ExSharedPointer Null Access Violation");
|
|
}
|
|
}
|
|
}
|
|
|
|
auline void throwif()
|
|
{
|
|
if (!cached) [[unlikely]]
|
|
{
|
|
cached = Base_t::operator bool();
|
|
if (!cached) [[likely]]
|
|
{
|
|
AU_THROW_STRING("ExSharedPointer Null Access Violation");
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
template<class TType_t, class Base_t>
|
|
struct ExSharedFromThis : Base_t
|
|
{
|
|
ExSharedPtr<TType_t, AURORA_RUNTIME_AU_SHARED_PTR<TType_t>> SharedFromThis()
|
|
{
|
|
return Base_t::shared_from_this();
|
|
}
|
|
};
|
|
} |