summaryrefslogtreecommitdiffstats
path: root/base/singleton.h
diff options
context:
space:
mode:
authorsatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-14 07:48:49 +0000
committersatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-14 07:48:49 +0000
commit625332e06437018bf696dce93a4b2bd2c5e0b118 (patch)
tree56e128ddf94a81b6de1f29a9eabe59f0d79346c9 /base/singleton.h
parentdc7cdcb970254f223262a66c812f240a8269ae87 (diff)
downloadchromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.zip
chromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.tar.gz
chromium_src-625332e06437018bf696dce93a4b2bd2c5e0b118.tar.bz2
Make members of Singleton<T> private and only visible to the singleton type. This enforces that the Singleton<T> pattern can only be used within classes which want singleton-ness.
As part of this CL I have also fixed up files which got missed in my previous CLs to use a GetInstance() method and use Singleton<T> from the source file. There are a small number of places where I have also switched to LazyInstance as that was more appropriate for types used in a single source file. BUG=65298 TEST=all existing tests should continue to pass. Review URL: http://codereview.chromium.org/5682008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69107 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/singleton.h')
-rw-r--r--base/singleton.h67
1 files changed, 34 insertions, 33 deletions
diff --git a/base/singleton.h b/base/singleton.h
index 8435c43..e5713c4 100644
--- a/base/singleton.h
+++ b/base/singleton.h
@@ -114,7 +114,6 @@ template <typename Type> intptr_t
template <typename Type> base::subtle::Atomic32
StaticMemorySingletonTraits<Type>::dead_ = 0;
-
// The Singleton<Type, Traits, DifferentiatingType> class manages a single
// instance of Type which will be created on first use and will be destroyed at
// normal process exit). The Trait::Delete function will not be called on
@@ -124,15 +123,37 @@ template <typename Type> base::subtle::Atomic32
// singletons having the same memory allocation functions but serving a
// different purpose. This is mainly used for Locks serving different purposes.
//
-// Example usages: (none are preferred, they all result in the same code)
-// 1. FooClass* ptr = Singleton<FooClass>::get();
-// ptr->Bar();
-// 2. Singleton<FooClass>()->Bar();
-// 3. Singleton<FooClass>::get()->Bar();
+// Example usage:
+//
+// In your header:
+// #include "base/singleton.h"
+// class FooClass {
+// public:
+// static FooClass* GetInstance(); <-- See comment below on this.
+// void Bar() { ... }
+// private:
+// FooClass() { ... }
+// friend struct DefaultSingletonTraits<FooClass>;
+//
+// DISALLOW_COPY_AND_ASSIGN(FooClass);
+// };
+//
+// In your source file:
+// FooClass* FooClass::GetInstance() {
+// return Singleton<FooClass>::get();
+// }
+//
+// And to call methods on FooClass:
+// FooClass::GetInstance()->Bar();
+//
+// NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance
+// and it is important that FooClass::GetInstance() is not inlined in the
+// header. This makes sure that when source files from multiple targets include
+// this header they don't end up with different copies of the inlined code
+// creating multiple copies of the singleton.
//
// Singleton<> has no non-static members and doesn't need to actually be
-// instantiated. It does no harm to instantiate it and use it as a class member
-// or at global level since it is acting as a POD type.
+// instantiated.
//
// This class is itself thread-safe. The underlying Type must of course be
// thread-safe if you want to use it concurrently. Two parameters may be tuned
@@ -152,20 +173,6 @@ template <typename Type> base::subtle::Atomic32
// shouldn't be false unless absolutely necessary. Remember that the heap where
// the object is allocated may be destroyed by the CRT anyway.
//
-// If you want to ensure that your class can only exist as a singleton, make
-// its constructors private, and make DefaultSingletonTraits<> a friend:
-//
-// #include "base/singleton.h"
-// class FooClass {
-// public:
-// void Bar() { ... }
-// private:
-// FooClass() { ... }
-// friend struct DefaultSingletonTraits<FooClass>;
-//
-// DISALLOW_COPY_AND_ASSIGN(FooClass);
-// };
-//
// Caveats:
// (a) Every call to get(), operator->() and operator*() incurs some overhead
// (16ns on my P4/2.8GHz) to check whether the object has already been
@@ -179,7 +186,11 @@ template <typename Type,
typename Traits = DefaultSingletonTraits<Type>,
typename DifferentiatingType = Type>
class Singleton {
- public:
+ private:
+ // Classes using the Singleton<T> pattern should declare a GetInstance()
+ // method and call Singleton::get() from within that.
+ friend Type* Type::GetInstance();
+
// This class is safe to be constructed and copy-constructed since it has no
// member.
@@ -240,16 +251,6 @@ class Singleton {
return reinterpret_cast<Type*>(value);
}
- // Shortcuts.
- Type& operator*() {
- return *get();
- }
-
- Type* operator->() {
- return get();
- }
-
- private:
// Adapter function for use with AtExit(). This should be called single
// threaded, so don't use atomic operations.
// Calling OnExit while singleton is in use by other threads is a mistake.