proxygen
folly::Singleton< T, Tag, VaultTag > Class Template Reference

#include <Singleton.h>

Public Types

typedef std::function< T *(void)> CreateFunc
 
typedef std::function< void(T *)> TeardownFunc
 

Public Member Functions

 Singleton (std::nullptr_t=nullptr, typename Singleton::TeardownFunc t=nullptr)
 
 Singleton (typename Singleton::CreateFunc c, typename Singleton::TeardownFunc t=nullptr)
 
SingletonshouldEagerInit ()
 

Static Public Member Functions

static Tget ()
 
static std::weak_ptr< Tget_weak ()
 
static std::shared_ptr< Ttry_get ()
 
static folly::ReadMostlySharedPtr< Ttry_get_fast ()
 
static void vivify ()
 
static void make_mock (std::nullptr_t=nullptr, typename Singleton< T >::TeardownFunc t=nullptr)
 
static void make_mock (CreateFunc c, typename Singleton< T >::TeardownFunc t=nullptr)
 

Static Private Member Functions

static detail::SingletonHolder< T > & getEntry ()
 
static detail::SingletonHolder< T >::TeardownFunc getTeardownFunc (TeardownFunc t)
 

Detailed Description

template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
class folly::Singleton< T, Tag, VaultTag >

Definition at line 571 of file Singleton.h.

Member Typedef Documentation

template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
typedef std::function<T*(void)> folly::Singleton< T, Tag, VaultTag >::CreateFunc

Definition at line 573 of file Singleton.h.

template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
typedef std::function<void(T*)> folly::Singleton< T, Tag, VaultTag >::TeardownFunc

Definition at line 574 of file Singleton.h.

Constructor & Destructor Documentation

template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
folly::Singleton< T, Tag, VaultTag >::Singleton ( std::nullptr_t  = nullptr,
typename Singleton< T, Tag, VaultTag >::TeardownFunc  t = nullptr 
)
inlineexplicit

Definition at line 611 of file Singleton.h.

614  : Singleton([]() { return new T; }, std::move(t)) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::std T
Singleton(std::nullptr_t=nullptr, typename Singleton::TeardownFunc t=nullptr)
Definition: Singleton.h:611
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
folly::Singleton< T, Tag, VaultTag >::Singleton ( typename Singleton< T, Tag, VaultTag >::CreateFunc  c,
typename Singleton< T, Tag, VaultTag >::TeardownFunc  t = nullptr 
)
inlineexplicit

Definition at line 616 of file Singleton.h.

618  {
619  if (c == nullptr) {
621  }
622 
623  auto vault = SingletonVault::singleton<VaultTag>();
624  getEntry().registerSingleton(std::move(c), getTeardownFunc(std::move(t)));
625  vault->registerSingleton(&getEntry());
626  }
static detail::SingletonHolder< T >::TeardownFunc getTeardownFunc(TeardownFunc t)
Definition: Singleton.h:684
void singletonThrowNullCreator(const std::type_info &type)
Definition: Singleton.cpp:174
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
folly::std T
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
char c

Member Function Documentation

template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static T* folly::Singleton< T, Tag, VaultTag >::get ( )
inlinestatic

Definition at line 579 of file Singleton.h.

Referenced by follySingletonRaw().

579  {
580  return getEntry().get();
581  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static std::weak_ptr<T> folly::Singleton< T, Tag, VaultTag >::get_weak ( )
inlinestatic

Definition at line 587 of file Singleton.h.

Referenced by TEST().

587  {
588  return getEntry().get_weak();
589  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static detail::SingletonHolder<T>& folly::Singleton< T, Tag, VaultTag >::getEntry ( )
inlinestaticprivate

Definition at line 679 of file Singleton.h.

679  {
680  return detail::SingletonHolder<T>::template singleton<Tag, VaultTag>();
681  }
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static detail::SingletonHolder<T>::TeardownFunc folly::Singleton< T, Tag, VaultTag >::getTeardownFunc ( TeardownFunc  t)
inlinestaticprivate

Definition at line 684 of file Singleton.h.

685  {
686  if (t == nullptr) {
687  return [](T* v) { delete v; };
688  } else {
689  return t;
690  }
691  }
auto v
folly::std T
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static void folly::Singleton< T, Tag, VaultTag >::make_mock ( std::nullptr_t  = nullptr,
typename Singleton< T >::TeardownFunc  t = nullptr 
)
inlinestatic

Construct and inject a mock singleton which should be used only from tests. Unlike regular singletons which are initialized once per process lifetime, mock singletons live for the duration of a test. This means that one process running multiple tests can initialize and register the same singleton multiple times. This functionality should be used only from tests since it relaxes validation and performance in order to be able to perform the injection. The returned mock singleton is functionality identical to regular singletons.

Definition at line 660 of file Singleton.h.

Referenced by TEST().

662  {
663  make_mock([]() { return new T; }, t);
664  }
static void make_mock(std::nullptr_t=nullptr, typename Singleton< T >::TeardownFunc t=nullptr)
Definition: Singleton.h:660
folly::std T
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static void folly::Singleton< T, Tag, VaultTag >::make_mock ( CreateFunc  c,
typename Singleton< T >::TeardownFunc  t = nullptr 
)
inlinestatic

Definition at line 666 of file Singleton.h.

668  {
669  if (c == nullptr) {
671  }
672 
673  auto& entry = getEntry();
674 
675  entry.registerSingletonMock(c, getTeardownFunc(t));
676  }
static detail::SingletonHolder< T >::TeardownFunc getTeardownFunc(TeardownFunc t)
Definition: Singleton.h:684
void singletonThrowNullCreator(const std::type_info &type)
Definition: Singleton.cpp:174
folly::std T
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
char c
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
Singleton& folly::Singleton< T, Tag, VaultTag >::shouldEagerInit ( )
inline

Should be instantiated as soon as "doEagerInit[Via]" is called. Singletons are usually lazy-loaded (built on-demand) but for those which are known to be needed, to avoid the potential lag for objects that take long to construct during runtime, there is an option to make sure these are built up-front.

Use like: Singleton<Foo> gFooInstance = Singleton<Foo>(...).shouldEagerInit();

Or alternately, define the singleton as usual, and say gFooInstance.shouldEagerInit();

at some point prior to calling registrationComplete(). Then doEagerInit() or doEagerInitVia(Executor*) can be called.

Definition at line 644 of file Singleton.h.

644  {
645  auto vault = SingletonVault::singleton<VaultTag>();
646  vault->addEagerInitSingleton(&getEntry());
647  return *this;
648  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static std::shared_ptr<T> folly::Singleton< T, Tag, VaultTag >::try_get ( )
inlinestatic

Definition at line 598 of file Singleton.h.

Referenced by follySingletonTryGet(), NeedySingleton::NeedySingleton(), SelfNeedySingleton::SelfNeedySingleton(), SlowpokeNeedySingleton::SlowpokeNeedySingleton(), TEST(), and folly::tryGetShutdownSocketSet().

598  {
599  return getEntry().try_get();
600  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static folly::ReadMostlySharedPtr<T> folly::Singleton< T, Tag, VaultTag >::try_get_fast ( )
inlinestatic

Definition at line 602 of file Singleton.h.

Referenced by follySingletonTryGetFast(), TEST(), and folly::tryGetShutdownSocketSetFast().

602  {
603  return getEntry().try_get_fast();
604  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679
template<typename T, typename Tag = detail::DefaultTag, typename VaultTag = detail::DefaultTag>
static void folly::Singleton< T, Tag, VaultTag >::vivify ( )
inlinestatic

Definition at line 607 of file Singleton.h.

607  {
608  getEntry().vivify();
609  }
static detail::SingletonHolder< T > & getEntry()
Definition: Singleton.h:679

The documentation for this class was generated from the following file: