From: Andrii Grynenko Date: Wed, 17 Sep 2014 04:12:10 +0000 (-0700) Subject: Fix folly::Singleton to work with objects w/o default constructor X-Git-Tag: v0.22.0~300 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=21487671f626f3800103aaa8e8ee8c1cadf629f1;p=folly.git Fix folly::Singleton to work with objects w/o default constructor Test Plan: unit tests Reviewed By: ostap@fb.com Subscribers: netego-diffs@, trunkagent, alikhtarov, marccelani, mshneer, zhuohuang, lins, njormrod FB internal diff: D1560406 --- diff --git a/folly/experimental/Singleton.h b/folly/experimental/Singleton.h index c2100e0d..524e5217 100644 --- a/folly/experimental/Singleton.h +++ b/folly/experimental/Singleton.h @@ -415,25 +415,38 @@ class Singleton { T& operator*() { return *ptr(); } T* operator->() { return ptr(); } - explicit Singleton(Singleton::CreateFunc c = nullptr, + template + explicit Singleton(CreateFunc c = nullptr, Singleton::TeardownFunc t = nullptr, SingletonVault* vault = nullptr /* for testing */) : Singleton({typeid(T), ""}, c, t, vault) {} + template explicit Singleton(const char* name, - Singleton::CreateFunc c = nullptr, + CreateFunc c = nullptr, Singleton::TeardownFunc t = nullptr, SingletonVault* vault = nullptr /* for testing */) : Singleton({typeid(T), name}, c, t, vault) {} private: explicit Singleton(detail::TypeDescriptor type, - Singleton::CreateFunc c = nullptr, - Singleton::TeardownFunc t = nullptr, - SingletonVault* vault = nullptr /* for testing */) + std::nullptr_t, + Singleton::TeardownFunc t, + SingletonVault* vault) : + Singleton (type, + []() { return new T; }, + std::move(t), + vault) { + } + + explicit Singleton(detail::TypeDescriptor type, + Singleton::CreateFunc c, + Singleton::TeardownFunc t, + SingletonVault* vault) : type_descriptor_(type) { if (c == nullptr) { - c = []() { return new T; }; + throw std::logic_error( + "nullptr_t should be passed if you want T to be default constructed"); } SingletonVault::TeardownFunc teardown; if (t == nullptr) {