2024-10-09 02:01:23 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2026-06-23 00:05:41 +02:00
|
|
|
#include <libremidi/config.hpp>
|
|
|
|
|
|
2024-10-09 02:01:23 +02:00
|
|
|
#include <memory>
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
2026-06-23 00:05:41 +02:00
|
|
|
NAMESPACE_LIBREMIDI
|
2024-10-09 02:01:23 +02:00
|
|
|
{
|
|
|
|
|
template <auto func>
|
|
|
|
|
struct deleter
|
|
|
|
|
{
|
|
|
|
|
template <typename U>
|
|
|
|
|
void operator()(U* x) noexcept(noexcept(func(x)))
|
|
|
|
|
{
|
|
|
|
|
func(x);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T, auto func>
|
|
|
|
|
using unique_handle = std::unique_ptr<T, deleter<func>>;
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
std::shared_ptr<T> instance()
|
|
|
|
|
{
|
|
|
|
|
static std::mutex mut;
|
|
|
|
|
static std::weak_ptr<T> cache;
|
|
|
|
|
|
|
|
|
|
std::lock_guard _{mut};
|
|
|
|
|
|
|
|
|
|
if (auto ptr = cache.lock())
|
|
|
|
|
{
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto shared = std::make_shared<T>();
|
|
|
|
|
cache = shared;
|
|
|
|
|
return shared;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|