aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/Private.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-29 16:23:52 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-29 16:23:52 +0100
commitc59d460f12e1393e0ddbaaa1c6f5522eb12f8ff9 (patch)
treee7d7ceabbb5b538b8fc57704750ccae9135be735 /src/crepe/util/Private.hpp
parentc2ef6a36532c8c078fd7836325d6be277b946cbf (diff)
more utility classes for Audio system
Diffstat (limited to 'src/crepe/util/Private.hpp')
-rw-r--r--src/crepe/util/Private.hpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/crepe/util/Private.hpp b/src/crepe/util/Private.hpp
new file mode 100644
index 0000000..30c8146
--- /dev/null
+++ b/src/crepe/util/Private.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <stdexcept>
+#include <format>
+
+#include "Private.h"
+
+namespace crepe {
+
+template <typename T, typename... Args>
+void Private::set(Args &&... args) {
+ T * instance = new T(std::forward<Args>(args)...);
+ this->instance = static_cast<void*>(instance);
+ this->destructor = [](void * instance) {
+ delete static_cast<T*>(instance);
+ };
+ this->type = typeid(T);
+}
+
+template <typename T>
+T & Private::get() {
+ using namespace std;
+ if (this->empty())
+ throw out_of_range("Private: get() called on empty object");
+ type_index requested_type = typeid(T);
+ if (this->type != requested_type)
+ throw logic_error(format("Private: get() called with [T = {}] (actual is [T = {}])", requested_type.name(), this->type.name()));
+ return *static_cast<T*>(this->instance);
+}
+
+}