1#ifndef SDL3PP_CALLBACK_WRAPPER_H_
2#define SDL3PP_CALLBACK_WRAPPER_H_
7#include <SDL3/SDL_assert.h>
31 constexpr void*
get()
const {
return id; }
34 constexpr operator bool()
const {
return id != 0; }
38template<
class Result,
class... Args>
51 static Result
Call(
void* handle, Args... args)
78template<
class Result,
class... Args>
99 static Result
CallOnce(
void* handle, Args... args)
101 auto f = release(handle);
108 auto f = release(handle);
121 if (handle ==
nullptr)
return {};
122 auto ptr =
static_cast<ValueType*
>(handle);
131 return release(handle.
get());
143template<
class KEY,
class VALUE,
size_t VARIANT = 0>
146 static_assert(
sizeof(KEY) <=
sizeof(
void*));
164 auto lockGuard = lock();
165 return &Values().insert_or_assign(key, std::move(value)).first->second;
171 auto lockGuard = lock();
172 return Values().contains(handle);
178 auto lockGuard = lock();
179 return Values().at(handle);
185 auto lockGuard = lock();
186 auto& values = Values();
187 auto it = values.find(key);
188 if (it == values.end())
return {};
202 auto lockGuard = lock();
203 return Values().erase(key);
207 static std::map<KeyType, ValueType>& Values()
209 static std::map<KeyType, ValueType> values;
213 static std::lock_guard<std::mutex> lock()
215 static std::mutex uniqueMutex;
216 return std::lock_guard{uniqueMutex};
221template<
class KEY,
class VALUE,
size_t VARIANT = 0>
225template<
class KEY,
class Result,
class... Args,
size_t VARIANT>
247template<
class Result,
class... Args>
264 auto lockGuard = lock();
266 v = std::move(value);
273 auto lockGuard = lock();
275 return bool(v) && &v == handle;
281 if (&get() == handle) {
290 auto lockGuard = lock();
297 auto lockGuard = lock();
312 auto lockGuard = lock();
317 static ValueType& Value()
319 static ValueType value;
323 static std::lock_guard<std::mutex> lock()
325 static std::mutex uniqueMutex;
326 return std::lock_guard{uniqueMutex};
A typesafe handle for callback.
Definition SDL3pp_callbackWrapper.h:21
constexpr void * get() const
Get Internal id.
Definition SDL3pp_callbackWrapper.h:31
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition SDL3pp_assert.h:374
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Base class for callback wrappers.
Definition SDL3pp_callbackWrapper.h:40
static Result CallSuffixed(Args... args, void *handle)
Call with suffix handle.
Definition SDL3pp_callbackWrapper.h:58
static Result Call(void *handle, Args... args)
Call.
Definition SDL3pp_callbackWrapper.h:51
std::function< Result(Args...)> ValueType
The wrapped std::function type.
Definition SDL3pp_callbackWrapper.h:42
static const ValueType & Unwrap(void *handle)
Return unwrapped value of handle.
Definition SDL3pp_callbackWrapper.h:45
static Result CallOnce(void *handle, Args... args)
Call once and release.
Definition SDL3pp_callbackWrapper.h:99
static ValueType release(void *handle)
Transfer ownership from the function and delete handle.
Definition SDL3pp_callbackWrapper.h:119
static Result CallOnceSuffixed(Args... args, void *handle)
Call once and release with suffix handle.
Definition SDL3pp_callbackWrapper.h:106
static ValueType * Wrap(ValueType &&cb)
Change the callback into a void* pointer.
Definition SDL3pp_callbackWrapper.h:93
static const ValueType release(CallbackHandle handle)
Return unwrapped value of handle.
Definition SDL3pp_callbackWrapper.h:129
std::function< Result(Args...)> ValueType
The wrapped std::function type.
Definition SDL3pp_callbackWrapper.h:85
Definition SDL3pp_callbackWrapper.h:66
std::function< Result(Args...)> ValueType
Wrapped type.
Definition SDL3pp_callbackWrapper.h:233
Store callbacks by key.
Definition SDL3pp_callbackWrapper.h:222
Wrapper key to value result callbacks.
Definition SDL3pp_callbackWrapper.h:145
static ValueType release(KeyType key)
Return unwrapped value associated by key and remove association.
Definition SDL3pp_callbackWrapper.h:183
KEY KeyType
Key type.
Definition SDL3pp_callbackWrapper.h:150
static bool erase(KeyType key)
Remove association.
Definition SDL3pp_callbackWrapper.h:200
static bool contains(KeyType handle)
True if handle is stored.
Definition SDL3pp_callbackWrapper.h:169
VALUE ValueType
Value type.
Definition SDL3pp_callbackWrapper.h:153
static ValueType * Wrap(KeyType key, ValueType &&value)
Change the value into a void* pointer held by key.
Definition SDL3pp_callbackWrapper.h:162
static const ValueType & at(KeyType handle)
Return unwrapped value of handle.
Definition SDL3pp_callbackWrapper.h:176
static const ValueType & get()
Return wrapped type, if handle is contained.
Definition SDL3pp_callbackWrapper.h:288
static void erase()
Erase value from store.
Definition SDL3pp_callbackWrapper.h:310
static ValueType release(void *handle)
Return wrapped type and erase it from store.
Definition SDL3pp_callbackWrapper.h:303
static bool contains(void *handle)
True if handle equals to wrapped value.
Definition SDL3pp_callbackWrapper.h:271
std::function< Result(Args...)> ValueType
Wrapped type.
Definition SDL3pp_callbackWrapper.h:254
static ValueType release()
Return wrapped type and erase it from store.
Definition SDL3pp_callbackWrapper.h:295
static ValueType at(void *handle)
Return wrapped type, if handle is contained.
Definition SDL3pp_callbackWrapper.h:279
static ValueType * Wrap(ValueType &&value)
Change the value into a void* pointer held uniquely by this type.
Definition SDL3pp_callbackWrapper.h:262
Stored Wrapper unique by type result callbacks.
Definition SDL3pp_callbackWrapper.h:242