proxygen
folly::LeakySingleton< T, Tag > Class Template Reference

#include <Singleton.h>

Classes

struct  Entry
 

Public Types

using CreateFunc = std::function< T *()>
 

Public Member Functions

 LeakySingleton ()
 
 LeakySingleton (CreateFunc createFunc)
 

Static Public Member Functions

static Tget ()
 
static void make_mock (std::nullptr_t=nullptr)
 
static void make_mock (CreateFunc createFunc)
 

Private Types

enum  State { State::NotRegistered, State::Dead, State::Living }
 

Static Private Member Functions

static EntryentryInstance ()
 
static Tinstance ()
 
static void createInstance ()
 

Detailed Description

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

Definition at line 695 of file Singleton.h.

Member Typedef Documentation

template<typename T , typename Tag = detail::DefaultTag>
using folly::LeakySingleton< T, Tag >::CreateFunc = std::function<T*()>

Definition at line 697 of file Singleton.h.

Member Enumeration Documentation

template<typename T , typename Tag = detail::DefaultTag>
enum folly::LeakySingleton::State
strongprivate
Enumerator
NotRegistered 
Dead 
Living 

Definition at line 733 of file Singleton.h.

733 { NotRegistered, Dead, Living };

Constructor & Destructor Documentation

template<typename T , typename Tag = detail::DefaultTag>
folly::LeakySingleton< T, Tag >::LeakySingleton ( )
inline

Definition at line 699 of file Singleton.h.

References folly::T.

699 : LeakySingleton([] { return new T(); }) {}
folly::std T
template<typename T , typename Tag = detail::DefaultTag>
folly::LeakySingleton< T, Tag >::LeakySingleton ( CreateFunc  createFunc)
inlineexplicit

Definition at line 701 of file Singleton.h.

References folly::detail::singletonWarnLeakyDoubleRegistrationAndAbort().

701  {
702  auto& entry = entryInstance();
703  if (entry.state != State::NotRegistered) {
705  }
706  entry.createFunc = createFunc;
707  entry.state = State::Dead;
708  }
static Entry & entryInstance()
Definition: Singleton.h:748
void singletonWarnLeakyDoubleRegistrationAndAbort(const TypeDescriptor &type)
Definition: Singleton.cpp:88

Member Function Documentation

template<typename T , typename Tag = detail::DefaultTag>
static void folly::LeakySingleton< T, Tag >::createInstance ( )
inlinestaticprivate

Definition at line 762 of file Singleton.h.

References folly::detail::singletonWarnLeakyInstantiatingNotRegisteredAndAbort().

762  {
763  auto& entry = entryInstance();
764 
765  std::lock_guard<std::mutex> lg(entry.mutex);
766  if (entry.state == State::Living) {
767  return;
768  }
769 
770  if (entry.state == State::NotRegistered) {
772  }
773 
774  entry.ptr = entry.createFunc();
775  entry.state = State::Living;
776  }
void singletonWarnLeakyInstantiatingNotRegisteredAndAbort(const TypeDescriptor &type)
Definition: Singleton.cpp:99
static Entry & entryInstance()
Definition: Singleton.h:748
template<typename T , typename Tag = detail::DefaultTag>
static Entry& folly::LeakySingleton< T, Tag >::entryInstance ( )
inlinestaticprivate

Definition at line 748 of file Singleton.h.

748  {
749  /* library-local */ static auto entry = detail::createGlobal<Entry, Tag>();
750  return *entry;
751  }
template<typename T , typename Tag = detail::DefaultTag>
static T& folly::LeakySingleton< T, Tag >::get ( )
inlinestatic

Definition at line 710 of file Singleton.h.

710  {
711  return instance();
712  }
static T & instance()
Definition: Singleton.h:753
template<typename T , typename Tag = detail::DefaultTag>
static T& folly::LeakySingleton< T, Tag >::instance ( )
inlinestaticprivate

Definition at line 753 of file Singleton.h.

References UNLIKELY.

753  {
754  auto& entry = entryInstance();
755  if (UNLIKELY(entry.state != State::Living)) {
756  createInstance();
757  }
758 
759  return *entry.ptr;
760  }
static Entry & entryInstance()
Definition: Singleton.h:748
static void createInstance()
Definition: Singleton.h:762
#define UNLIKELY(x)
Definition: Likely.h:48
template<typename T , typename Tag = detail::DefaultTag>
static void folly::LeakySingleton< T, Tag >::make_mock ( std::nullptr_t  = nullptr)
inlinestatic

Definition at line 714 of file Singleton.h.

References folly::T.

714  {
715  make_mock([]() { return new T; });
716  }
folly::std T
static void make_mock(std::nullptr_t=nullptr)
Definition: Singleton.h:714
template<typename T , typename Tag = detail::DefaultTag>
static void folly::LeakySingleton< T, Tag >::make_mock ( CreateFunc  createFunc)
inlinestatic

Definition at line 718 of file Singleton.h.

References folly::exchange(), folly::detail::singletonThrowNullCreator(), and folly::T.

718  {
719  if (createFunc == nullptr) {
721  }
722 
723  auto& entry = entryInstance();
724  if (entry.ptr) {
725  // Make sure existing pointer doesn't get reported as a leak by LSAN.
726  entry.leakedPtrs.push_back(std::exchange(entry.ptr, nullptr));
727  }
728  entry.createFunc = createFunc;
729  entry.state = State::Dead;
730  }
void singletonThrowNullCreator(const std::type_info &type)
Definition: Singleton.cpp:174
folly::std T
static Entry & entryInstance()
Definition: Singleton.h:748
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120

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