X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FSingleton.h;h=921c443ab3eeca0ff200894cedb82a8720aba1aa;hp=2c699e4793d7224c54a45b6acc5dbb9cdeec0962;hb=9b2b633c860eebda93c28ae387c50f1f81e29211;hpb=897366619be60e27083c747f2caf8635f0b530cf diff --git a/folly/Singleton.h b/folly/Singleton.h index 2c699e47..921c443a 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -121,16 +121,16 @@ // should call reenableInstances. #pragma once -#include #include #include #include -#include #include #include #include #include #include +#include +#include #include #include @@ -273,11 +273,11 @@ struct SingletonHolder : public SingletonHolderBase { void registerSingleton(CreateFunc c, TeardownFunc t); void registerSingletonMock(CreateFunc c, TeardownFunc t); - virtual bool hasLiveInstance() override; - virtual void createInstance() override; - virtual bool creationStarted() override; - virtual void preDestroyInstance(ReadMostlyMainPtrDeleter<>&) override; - virtual void destroyInstance() override; + bool hasLiveInstance() override; + void createInstance() override; + bool creationStarted() override; + void preDestroyInstance(ReadMostlyMainPtrDeleter<>&) override; + void destroyInstance() override; private: SingletonHolder(TypeDescriptor type, SingletonVault& vault); @@ -328,7 +328,7 @@ struct SingletonHolder : public SingletonHolderBase { SingletonHolder(SingletonHolder&&) = delete; }; -} +} // namespace detail class SingletonVault { public: @@ -358,7 +358,9 @@ class SingletonVault { } }; - explicit SingletonVault(Type type = Type::Strict) : type_(type) {} + static Type defaultVaultType(); + + explicit SingletonVault(Type type = defaultVaultType()) : type_(type) {} // Destructor is only called by unit tests to check destroyInstances. ~SingletonVault(); @@ -403,10 +405,10 @@ class SingletonVault { * * Sample usage: * - * wangle::IOThreadPoolExecutor executor(max_concurrency_level); + * folly::IOThreadPoolExecutor executor(max_concurrency_level); * folly::Baton<> done; * doEagerInitVia(executor, &done); - * done.wait(); // or 'timed_wait', or spin with 'try_wait' + * done.wait(); // or 'try_wait_for', etc. * */ void doEagerInitVia(Executor& exe, folly::Baton<>* done = nullptr); @@ -530,9 +532,10 @@ class SingletonVault { // singletons. Create instances of this class in the global scope of // type Singleton to register your singleton for later access via // Singleton::try_get(). -template +template < + typename T, + typename Tag = detail::DefaultTag, + typename VaultTag = detail::DefaultTag /* for testing */> class Singleton { public: typedef std::function CreateFunc; @@ -667,6 +670,21 @@ class LeakySingleton { static T& get() { return instance(); } + static void make_mock(std::nullptr_t /* c */ = nullptr) { + make_mock([]() { return new T; }); + } + + static void make_mock(CreateFunc createFunc) { + auto& entry = entryInstance(); + if (createFunc == nullptr) { + throw std::logic_error( + "nullptr_t should be passed if you want T to be default constructed"); + } + + entry.createFunc = createFunc; + entry.state = State::Dead; + } + private: enum class State { NotRegistered, Dead, Living }; @@ -716,6 +734,6 @@ class LeakySingleton { entry.state = State::Living; } }; -} +} // namespace folly #include