mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-28 04:30:53 +00:00
49 lines
1.0 KiB
C++
49 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <type_traits>
|
|
|
|
namespace ext {
|
|
|
|
template <class T, typename DefaultConstructable = void>
|
|
class Singleton;
|
|
|
|
/// For default-constructable type we don't need to implement |create()|
|
|
/// and may use "arrow" operator immediately.
|
|
template <class T>
|
|
class Singleton<T, std::enable_if_t<std::is_default_constructible_v<T>>>
|
|
{
|
|
public:
|
|
T * operator->()
|
|
{
|
|
static T instance;
|
|
return &instance;
|
|
}
|
|
};
|
|
|
|
/// For custom-constructed type we have to construct |Singleton| object with non-default constructor once
|
|
/// before any use of "arrow" operator.
|
|
template <class T>
|
|
class Singleton<T, std::enable_if_t<!std::is_default_constructible_v<T>>>
|
|
{
|
|
public:
|
|
Singleton() = default;
|
|
|
|
template <typename ... Args>
|
|
Singleton(const Args & ... args)
|
|
{
|
|
instance.reset(new T(args...));
|
|
/// TODO: throw exception on double-creation.
|
|
}
|
|
|
|
T * operator->()
|
|
{
|
|
return instance.get();
|
|
}
|
|
|
|
private:
|
|
inline static std::unique_ptr<T> instance{};
|
|
};
|
|
|
|
}
|