From 785293f4bf1beec65d23be0612e545e4c26ec366 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 19:27:53 +0100 Subject: b --- compat/safe-ptr.hpp | 61 +++++++++++++++++++++++++++++++++++++++++++ compat/shared-ptr-wrapper.hpp | 12 +++++++++ 2 files changed, 73 insertions(+) create mode 100644 compat/safe-ptr.hpp create mode 100644 compat/shared-ptr-wrapper.hpp (limited to 'compat') diff --git a/compat/safe-ptr.hpp b/compat/safe-ptr.hpp new file mode 100644 index 00000000..f6b0b260 --- /dev/null +++ b/compat/safe-ptr.hpp @@ -0,0 +1,61 @@ +#pragma once +#include "compat/assert.hpp" +#include "compat/defs.hpp" +#include +#include +#include + +namespace floormat { + +template +class safe_ptr final +{ + T* ptr; + +public: + template + requires requires (Ts&&... xs) { + new T{Utility::forward(xs)...}; + } + safe_ptr(InPlaceInitT, Ts&&... args) noexcept: + ptr{new T{Utility::forward(args)...}} + {} + + explicit safe_ptr(T*&& ptr) noexcept: ptr{ptr} + { + fm_assert(ptr != nullptr); + } + + ~safe_ptr() noexcept + { + if (ptr) + delete ptr; + ptr = (T*)-0xbadbabe; + } + + explicit safe_ptr(safe_ptr&& other) noexcept: ptr{other.ptr} + { + other.ptr = nullptr; + } + + safe_ptr& operator=(safe_ptr&& other) noexcept + { + fm_assert(this != &other); + if (ptr) + delete ptr; + ptr = other.ptr; + other.ptr = nullptr; + return *this; + } + + fm_DECLARE_DELETED_COPY_ASSIGNMENT(safe_ptr); + + //explicit operator bool() const noexcept { return ptr != nullptr; } + + const T& operator*() const noexcept { return *ptr; } + T& operator*() noexcept { return *ptr; } + const T* operator->() const noexcept { return ptr; } + T* operator->() noexcept { return ptr; } +}; + +} // namespace floormat diff --git a/compat/shared-ptr-wrapper.hpp b/compat/shared-ptr-wrapper.hpp new file mode 100644 index 00000000..8315d674 --- /dev/null +++ b/compat/shared-ptr-wrapper.hpp @@ -0,0 +1,12 @@ +#pragma once +#include + +namespace floormat { + +template +struct shared_ptr_wrapper final +{ + std::shared_ptr ptr; +}; + +} // namespace floormat -- cgit v1.2.3