axmol/thirdparty/yasio/core/singleton.hpp

162 lines
5.0 KiB
C++
Raw Normal View History

2020-11-16 14:47:43 +08:00
//////////////////////////////////////////////////////////////////////////////////////////
2023-05-14 17:06:43 +08:00
// A multi-platform support c++11 library with focus on asynchronous socket I/O for any
2021-01-14 15:30:18 +08:00
// client application.
2020-11-16 14:47:43 +08:00
//////////////////////////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
2023-01-01 17:36:38 +08:00
Copyright (c) 2012-2023 HALX99
2020-11-16 14:47:43 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
2023-05-15 16:57:02 +08:00
// core/singleton.hpp: A common use thread safe singleton class template, support non-delayed or delayed init with variadic args.
//
// refer to:
// https://www.youtube.com/watch?v=c1gO9aB9nbs&t=1120s
//
// remark:
// Singletons make it hard to determine the lifetime of an object, which can
// lead to buggy code and spurious crashes.
//
// Instead of adding another singleton into the mix, try to identify either:
// a) An existing singleton that can manage your object's lifetime
// b) Locations where you can deterministically create the object and pass
// into other objects
//
// If you absolutely need a singleton, please keep them as trivial as possible
// and ideally a leaf dependency. Singletons get problematic when they attempt
// to do too much in their destructor or have circular dependencies.
2020-11-16 14:47:43 +08:00
#ifndef YASIO__SINGLETON_HPP
#define YASIO__SINGLETON_HPP
#include <new>
#include <memory>
#include <functional>
2023-05-15 16:57:02 +08:00
#include <mutex>
#include <atomic>
2020-11-16 14:47:43 +08:00
2023-05-14 22:39:05 +08:00
#include "yasio/config.hpp"
2020-11-16 14:47:43 +08:00
namespace yasio
{
2023-05-14 17:06:43 +08:00
template <typename _Ty, bool delay = false>
class singleton_constructor {
2020-11-16 14:47:43 +08:00
public:
2023-05-14 17:06:43 +08:00
template <typename... _Types>
static _Ty* construct(_Types&&... args)
2020-11-16 14:47:43 +08:00
{
2021-12-08 00:27:52 +08:00
return new _Ty(std::forward<_Types>(args)...);
2020-11-16 14:47:43 +08:00
}
};
2023-05-14 17:06:43 +08:00
template <typename _Ty>
class singleton_constructor<_Ty, true> {
2020-11-16 14:47:43 +08:00
public:
2023-05-14 17:06:43 +08:00
template <typename... _Types>
static _Ty* construct(_Types&&... args)
2020-11-16 14:47:43 +08:00
{
2021-12-08 00:27:52 +08:00
auto inst = new _Ty();
delay_init(inst, std::forward<_Types>(args)...);
2020-11-16 14:47:43 +08:00
return inst;
}
private:
template <typename _Fty, typename... _Types>
static void delay_init(_Ty* inst, _Fty&& memf, _Types&&... args)
{ // init use specific member func with more than 1 args
std::mem_fn(memf)(inst, std::forward<_Types>(args)...);
}
2023-05-14 17:06:43 +08:00
template <typename _Fty, typename _Arg>
static void delay_init(_Ty* inst, _Fty&& memf, _Arg&& arg)
2020-11-16 14:47:43 +08:00
{ // init use specific member func with 1 arg
std::mem_fn(memf)(inst, std::forward<_Arg>(arg));
}
2023-05-14 17:06:43 +08:00
template <typename _Fty>
static void delay_init(_Ty* inst, _Fty&& memf)
2020-11-16 14:47:43 +08:00
{ // init use specific member func without arg
std::mem_fn(memf)(inst);
}
static void delay_init(_Ty* inst)
{ // dummy init without delay init member func, same as no delay constructor
}
};
2023-05-14 17:06:43 +08:00
template <typename _Ty>
class singleton {
2020-11-16 14:47:43 +08:00
typedef singleton<_Ty> _Myt;
typedef _Ty* pointer;
public:
// Return the singleton instance
2023-05-15 19:50:23 +08:00
template <typename... _Types>
2023-05-14 17:06:43 +08:00
static pointer instance(_Types&&... args)
2020-11-16 14:47:43 +08:00
{
2023-05-15 19:50:23 +08:00
return get_instance<false>(std::forward<_Types>(args)...);
}
2020-11-16 14:47:43 +08:00
2023-05-15 19:50:23 +08:00
template <typename... _Types>
static pointer instance1(_Types&&... args)
{
return get_instance<true>(std::forward<_Types>(args)...);
2020-11-16 14:47:43 +08:00
}
2023-05-15 16:57:02 +08:00
static void destroy(void)
2020-11-16 14:47:43 +08:00
{
2023-05-15 19:50:23 +08:00
if (auto inst = _Myt::__single__.exchange(nullptr, std::memory_order_acq_rel))
delete inst;
2020-11-16 14:47:43 +08:00
}
// Peek the singleton instance
static pointer peek() { return _Myt::__single__; }
private:
2023-05-15 19:50:23 +08:00
template <bool delay, typename... _Types>
static pointer get_instance(_Types&&... args)
{
auto& inst = _Myt::__single__;
if (inst.load(std::memory_order_acquire))
return inst;
{
std::lock_guard<std::mutex> lck(__mutex__);
if (!inst.load(std::memory_order_relaxed))
inst.store(singleton_constructor<_Ty, delay>::construct(std::forward<_Types>(args)...), std::memory_order_release);
}
return inst;
}
2020-11-16 14:47:43 +08:00
static std::atomic<_Ty*> __single__;
static std::mutex __mutex__;
2023-05-15 16:57:02 +08:00
2020-11-16 14:47:43 +08:00
private:
// disable construct, assign operation, copy construct also not allowed.
singleton(void) = delete;
};
2023-05-14 17:06:43 +08:00
template <typename _Ty>
std::atomic<_Ty*> singleton<_Ty>::__single__;
template <typename _Ty>
std::mutex singleton<_Ty>::__mutex__;
2023-05-15 16:57:02 +08:00
2020-11-16 14:47:43 +08:00
} // namespace yasio
#endif