135 lines
3.1 KiB
C++
135 lines
3.1 KiB
C++
/***
|
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: auMemoryModel.hpp
|
|
Date: 2022-2-1
|
|
File: AuroraUtils.hpp
|
|
File: auROXTLUtils.hpp
|
|
Date: 2021-6-9
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
#include "auTypes.hpp"
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_SHARED_PTR)
|
|
#define AURORA_RUNTIME_AU_SHARED_PTR std::shared_ptr
|
|
#endif
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_WEAK_PTR)
|
|
#define AURORA_RUNTIME_AU_WEAK_PTR std::weak_ptr
|
|
#endif
|
|
|
|
template<typename T>
|
|
using AuWPtr = AURORA_RUNTIME_AU_WEAK_PTR<T>;
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_UNIQUE_PTR)
|
|
#define AURORA_RUNTIME_AU_UNIQUE_PTR std::unique_ptr
|
|
#endif
|
|
|
|
#if !defined(AURORA_RUNTIME_AU_DEFAULT_DELETER)
|
|
#define AURORA_RUNTIME_AU_DEFAULT_DELETER std::default_delete
|
|
#endif
|
|
|
|
template<typename T>
|
|
using AuDefaultDeleter = AURORA_RUNTIME_AU_DEFAULT_DELETER<T>;
|
|
|
|
#include "Shims/ExtendStlLikeSharedPtr.hpp"
|
|
|
|
template<typename T>
|
|
using AuSPtr = typename Aurora::Memory::ExSharedPtr<T, AURORA_RUNTIME_AU_SHARED_PTR<T>>;
|
|
|
|
template<typename T, typename Deleter_t>
|
|
using AuUPtr = AURORA_RUNTIME_AU_UNIQUE_PTR<T, Deleter_t>;
|
|
|
|
#if !defined(AU_AuEnableSharedFromThis)
|
|
#define AU_AuEnableSharedFromThis
|
|
|
|
template<class X>
|
|
struct AuEnableSharedFromThis : Aurora::Memory::ExSharedFromThis<X, std::enable_shared_from_this<X>>
|
|
{};
|
|
|
|
#endif
|
|
|
|
#if !defined(AURORA_RUNTIME_MAKE_SHARED)
|
|
#define AURORA_RUNTIME_MAKE_SHARED std::make_shared
|
|
#endif
|
|
|
|
template<typename T, typename... Args>
|
|
static auline AuSPtr<T> AuMakeShared(Args&&... args)
|
|
{
|
|
try
|
|
{
|
|
return AURORA_RUNTIME_MAKE_SHARED<T>(AuForward<Args>(args)...);
|
|
}
|
|
catch (...)
|
|
{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static auline AuSPtr<T> AuMakeSharedArray(AuUInt count)
|
|
{
|
|
try
|
|
{
|
|
#if defined(AU_LANG_CPP_20) && 0
|
|
return AURORA_RUNTIME_AU_SHARED_PTR<T[]>(count);
|
|
#else
|
|
return AURORA_RUNTIME_AU_SHARED_PTR<T>(new T[count], AuDefaultDeleter<T[]>());
|
|
#endif
|
|
}
|
|
catch (...)
|
|
{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static AuSPtr<T> AuUnsafeRaiiToShared(T *in)
|
|
{
|
|
return AuSPtr<T>(in, [](T *)
|
|
{});
|
|
}
|
|
|
|
template<typename T, class Z>
|
|
static AuSPtr<T> AuUnsafeRaiiToShared(const AuUPtr<T, Z> &in)
|
|
{
|
|
return AuSPtr<T>(in.get(), [](T *)
|
|
{});
|
|
}
|
|
|
|
template<typename T, AuUInt Z>
|
|
static constexpr int AuArraySize(const T(&array)[Z])
|
|
{
|
|
return Z;
|
|
}
|
|
|
|
#if defined(DEBUG) || defined(STAGING)
|
|
|
|
template<typename ... T>
|
|
static auline void AU_NORETURN SysPanic(T... args);
|
|
|
|
template<typename Z, typename T>
|
|
static void auline AuSafeDelete(T *in)
|
|
{
|
|
static_assert(AuIsBaseOf_v<T, AuRemovePointer_t<Z>>, "Couldn't not safe delete from type T because it is not derived from Z");
|
|
auto cast = dynamic_cast<Z>(in);
|
|
if (cast == nullptr)
|
|
{
|
|
Z re;
|
|
SysPanic("Tried to free: 0x{:x}, type \"{}\" was not inherited from \"{}\"", AuUInt(in), typeid(in).name(), typeid(re).name());
|
|
}
|
|
delete cast;
|
|
}
|
|
|
|
#else
|
|
|
|
template<typename Z, typename T>
|
|
static void auline AuSafeDelete(T *in)
|
|
{
|
|
static_assert(AuIsBaseOf_v<T, AuRemovePointer_t<Z>>, "Couldn't not safe delete from type T because it is not derived from Z");
|
|
delete static_cast<Z>(in);
|
|
}
|
|
|
|
#endif |