diff options
Diffstat (limited to 'base/memory/singleton.h')
-rw-r--r-- | base/memory/singleton.h | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/base/memory/singleton.h b/base/memory/singleton.h index 79e4441a8e..5c58d5fe29 100644 --- a/base/memory/singleton.h +++ b/base/memory/singleton.h @@ -22,6 +22,7 @@ #include "base/at_exit.h" #include "base/atomicops.h" #include "base/base_export.h" +#include "base/logging.h" #include "base/macros.h" #include "base/memory/aligned_memory.h" #include "base/threading/thread_restrictions.h" @@ -63,7 +64,7 @@ struct DefaultSingletonTraits { // exit. See below for the required call that makes this happen. static const bool kRegisterAtExit = true; -#ifndef NDEBUG +#if DCHECK_IS_ON() // Set to false to disallow access on a non-joinable thread. This is // different from kRegisterAtExit because StaticMemorySingletonTraits allows // access on non-joinable threads, and gracefully handles this. @@ -78,7 +79,7 @@ struct DefaultSingletonTraits { template<typename Type> struct LeakySingletonTraits : public DefaultSingletonTraits<Type> { static const bool kRegisterAtExit = false; -#ifndef NDEBUG +#if DCHECK_IS_ON() static const bool kAllowedToAccessOnNonjoinableThread = true; #endif }; @@ -152,14 +153,17 @@ subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0; // Example usage: // // In your header: -// template <typename T> struct DefaultSingletonTraits; +// namespace base { +// template <typename T> +// struct DefaultSingletonTraits; +// } // class FooClass { // public: // static FooClass* GetInstance(); <-- See comment below on this. // void Bar() { ... } // private: // FooClass() { ... } -// friend struct DefaultSingletonTraits<FooClass>; +// friend struct base::DefaultSingletonTraits<FooClass>; // // DISALLOW_COPY_AND_ASSIGN(FooClass); // }; @@ -167,7 +171,14 @@ subtle::Atomic32 StaticMemorySingletonTraits<Type>::dead_ = 0; // In your source file: // #include "base/memory/singleton.h" // FooClass* FooClass::GetInstance() { -// return Singleton<FooClass>::get(); +// return base::Singleton<FooClass>::get(); +// } +// +// Or for leaky singletons: +// #include "base/memory/singleton.h" +// FooClass* FooClass::GetInstance() { +// return base::Singleton< +// FooClass, base::LeakySingletonTraits<FooClass>>::get(); // } // // And to call methods on FooClass: @@ -227,7 +238,7 @@ class Singleton { // Return a pointer to the one true instance of the class. static Type* get() { -#ifndef NDEBUG +#if DCHECK_IS_ON() // Avoid making TLS lookup on release builds. if (!Traits::kAllowedToAccessOnNonjoinableThread) ThreadRestrictions::AssertSingletonAllowed(); |