4#pragma clang diagnostic push
8#pragma clang diagnostic ignored "-Wdelete-non-abstract-non-virtual-dtor"
19 virtual void call(
void *) {}
22 template<
typename T,
typename DeleterFunction>
23 struct Destructor :
public DestructorBase
25 static_assert(std::is_trivially_destructible<DeleterFunction>::value,
"DeleterFunction must be trivially destructible.");
27 Destructor(DeleterFunction t) : t(t) {}
29 void call(
void * ptr)
override { t(
static_cast<T *
>(ptr)); }
79 template<
typename DeleterFunction>
80 Pointer(T * ptr, DeleterFunction deleter) : ptr(ptr)
82 static_assert(
sizeof(Destructor<T, DeleterFunction>) <=
sizeof(storage),
"Deleter function too large (check capture list).");
83 static_assert(std::is_trivially_destructible<Destructor<T, DeleterFunction>>::value,
"Deleter must be trivially destructible.");
85 new (storage) Destructor<T, DeleterFunction>(deleter);
92 std::memcpy(storage, other.storage,
sizeof(storage));
100 std::memcpy(storage, other.storage,
sizeof(storage));
116 auto destructor = (DestructorBase *)storage;
117 destructor->call(ptr);
118 destructor->~DestructorBase();
135 operator bool()
const {
return ptr !=
nullptr; }
141 const T *
get()
const {
return ptr; }
151 uint8_t storage[32 -
sizeof(T *)] = {};
156 static_assert(
sizeof(Pointer<int>) == 32,
"Internal size check failed.");
171 template<
typename T,
typename DeleterFunction>
179#pragma clang diagnostic pop
Contains all Cogs related functionality.
Pointer< T > makePointer(T *t, DeleterFunction deleterFunc)
Construct a Pointer<T> instance from the given pointer and deleter function.
Provides scoped storage for a raw pointer and corresponding deletion logic.
Pointer & operator=(Pointer &other)=delete
Copy assignment disabled.
const T * get() const
Gets the raw pointer stored in this instance.
const T * operator->() const
Access the raw pointer in this instance.
T * operator->()
Access the raw pointer in this instance.
Pointer()=default
Default construct an empty instance.
Pointer(T *ptr, DeleterFunction deleter)
Constructs the instance from the given pointer and deleter function.
Pointer(Pointer &other)=delete
Copy construction disabled.
T * get()
Gets the raw pointer stored in this instance.
~Pointer()
Destructs the instance, invoking cleanup logic if the instance is non-empty.
Pointer & operator=(Pointer &&other) noexcept
Move assign an instance from other.
Pointer(Pointer &&other) noexcept
Move construct an instance from other.