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.
|
|
|
|
*/
|
|
|
|
#ifndef YASIO__SINGLETON_HPP
|
|
|
|
#define YASIO__SINGLETON_HPP
|
|
|
|
#include <new>
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
|
2023-05-14 22:39:05 +08:00
|
|
|
#include "yasio/config.hpp"
|
2020-11-16 14:47:43 +08:00
|
|
|
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
|
|
|
# include <mutex>
|
|
|
|
# include <atomic>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// CLASS TEMPLATE singleton, support non-delayed or delayed init with variadic args
|
|
|
|
/// the managed singleton object will be destructed after main function.
|
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-14 17:06:43 +08:00
|
|
|
template <typename... _Types>
|
|
|
|
static pointer instance(_Types&&... args)
|
2020-11-16 14:47:43 +08:00
|
|
|
{
|
|
|
|
if (_Myt::__single__)
|
|
|
|
return _Myt::__single__;
|
|
|
|
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
|
|
|
std::lock_guard<std::mutex> lck(__mutex__);
|
|
|
|
if (_Myt::__single__)
|
|
|
|
return _Myt::__single__;
|
|
|
|
#endif
|
2023-05-14 17:06:43 +08:00
|
|
|
return (_Myt::__single__ = singleton_constructor<_Ty>::construct(std::forward<_Types>(args)...));
|
2020-11-16 14:47:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the singleton instance with delayed init func
|
2023-05-14 17:06:43 +08:00
|
|
|
template <typename... _Types>
|
|
|
|
static pointer delayed(_Types&&... args)
|
2020-11-16 14:47:43 +08:00
|
|
|
{
|
|
|
|
if (_Myt::__single__)
|
|
|
|
return _Myt::__single__;
|
|
|
|
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
|
|
|
std::lock_guard<std::mutex> lck(__mutex__);
|
|
|
|
if (_Myt::__single__)
|
|
|
|
return _Myt::__single__;
|
|
|
|
#endif
|
2023-05-14 17:06:43 +08:00
|
|
|
return (_Myt::__single__ = singleton_constructor<_Ty, true>::construct(std::forward<_Types>(args)...));
|
2020-11-16 14:47:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peek the singleton instance
|
|
|
|
static pointer peek() { return _Myt::__single__; }
|
|
|
|
|
|
|
|
static void destroy(void)
|
|
|
|
{
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
|
|
|
std::lock_guard<std::mutex> lck(__mutex__);
|
|
|
|
#endif
|
|
|
|
if (_Myt::__single__)
|
|
|
|
{
|
|
|
|
delete static_cast<_Ty*>(_Myt::__single__);
|
|
|
|
_Myt::__single__ = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
|
|
|
static std::atomic<_Ty*> __single__;
|
|
|
|
static std::mutex __mutex__;
|
|
|
|
#else
|
|
|
|
static _Ty* __single__;
|
|
|
|
#endif
|
|
|
|
private:
|
|
|
|
// disable construct, assign operation, copy construct also not allowed.
|
|
|
|
singleton(void) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if !defined(YASIO_DISABLE_CONCURRENT_SINGLETON)
|
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__;
|
2020-11-16 14:47:43 +08:00
|
|
|
#else
|
2023-05-14 17:06:43 +08:00
|
|
|
template <typename _Ty>
|
|
|
|
_Ty* singleton<_Ty>::__single__;
|
2020-11-16 14:47:43 +08:00
|
|
|
#endif
|
|
|
|
} // namespace yasio
|
|
|
|
|
|
|
|
#endif
|