From d7c2a8029c4aefc295719174a863129645d6ab99 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 20 Jun 2026 22:23:48 +0200 Subject: initial commit --- utilities.hpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 utilities.hpp (limited to 'utilities.hpp') diff --git a/utilities.hpp b/utilities.hpp new file mode 100644 index 0000000..819c170 --- /dev/null +++ b/utilities.hpp @@ -0,0 +1,67 @@ +#ifndef THROTTLE_QUADRANT_UTILITIES_HPP +#define THROTTLE_QUADRANT_UTILITIES_HPP + +#include + +void* operator new(size_t size, void* ptr); + +namespace tq { + +template +struct optional { + + optional() + : m_engaged{ false } { + } + + optional(optional const& other) + : m_engaged{ other.has_value() } { + if (m_engaged) { + construct_from(other.value()); + } + } + + explicit optional(ValueType const& value) + : m_engaged{ true } { + construct_from(value); + } + + ~optional() { + destroy(); + } + + auto operator=(optional const& other) -> optional& { + destroy(); + m_engaged = other.m_engaged; + if (m_engaged) { + construct_from(other.value()); + } + } + + auto has_value() const -> bool { + return m_engaged; + } + + auto value() const -> ValueType const& { + return *(reinterpret_cast(m_storage)); + } + +private: + auto construct_from(ValueType const& value) -> void { + new (static_cast(m_storage)) ValueType{ value }; + } + + auto destroy() -> void { + if (has_value()) { + (reinterpret_cast(m_storage))->~ValueType(); + } + } + + alignas(ValueType) byte m_storage[sizeof(ValueType)]; + bool m_engaged; +}; + + +} + +#endif \ No newline at end of file -- cgit v1.2.3