Move max_align_v and max_align_t to folly/lang/Align.h
[folly.git] / folly / Singleton.h
index 104054afaa42b30a6e8c7d9876e49a97e75b883f..921c443ab3eeca0ff200894cedb82a8720aba1aa 100644 (file)
 // should call reenableInstances.
 
 #pragma once
-#include <folly/Baton.h>
 #include <folly/Demangle.h>
 #include <folly/Exception.h>
 #include <folly/Executor.h>
-#include <folly/Hash.h>
 #include <folly/Memory.h>
 #include <folly/RWSpinLock.h>
 #include <folly/Synchronized.h>
 #include <folly/detail/StaticSingletonManager.h>
 #include <folly/experimental/ReadMostlySharedPtr.h>
+#include <folly/hash/Hash.h>
+#include <folly/synchronization/Baton.h>
 
 #include <algorithm>
 #include <atomic>
@@ -328,7 +328,7 @@ struct SingletonHolder : public SingletonHolderBase {
   SingletonHolder(SingletonHolder&&) = delete;
 };
 
-}
+} // namespace detail
 
 class SingletonVault {
  public:
@@ -405,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);
@@ -532,9 +532,10 @@ class SingletonVault {
 // singletons.  Create instances of this class in the global scope of
 // type Singleton<T> to register your singleton for later access via
 // Singleton<T>::try_get().
-template <typename T,
-          typename Tag = detail::DefaultTag,
-          typename VaultTag = detail::DefaultTag /* for testing */>
+template <
+    typename T,
+    typename Tag = detail::DefaultTag,
+    typename VaultTag = detail::DefaultTag /* for testing */>
 class Singleton {
  public:
   typedef std::function<T*(void)> CreateFunc;
@@ -669,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 };
 
@@ -718,6 +734,6 @@ class LeakySingleton {
     entry.state = State::Living;
   }
 };
-}
+} // namespace folly
 
 #include <folly/Singleton-inl.h>