Files

43 lines
629 B
C++
Raw Permalink Normal View History

2024-10-09 02:01:23 +02:00
#pragma once
#include <libremidi/config.hpp>
2024-10-09 02:01:23 +02:00
#include <memory>
#include <mutex>
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;
}
}
}