From: Jiahao Li Date: Fri, 7 Jul 2017 19:22:51 +0000 (-0700) Subject: Adds `make_mock` to `folly::LeakySingleton`. X-Git-Tag: v2017.07.10.00~3 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=7f15a179e20b0d61bfb5e872956993b0c6afaa30;p=folly.git Adds `make_mock` to `folly::LeakySingleton`. Summary: Provides a `make_mock` interface to `folly::LeakySingleton` for unit test mocking. Reviewed By: andriigrynenko Differential Revision: D5382003 fbshipit-source-id: 9ab81dd64703f4f02772bfb8c85e020680abcc85 --- diff --git a/folly/Singleton.h b/folly/Singleton.h index 104054af..64b6f375 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -669,6 +669,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 };