summaryrefslogtreecommitdiffstats
path: root/base/singleton_unittest.cc
diff options
context:
space:
mode:
authorsatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-11 10:23:37 +0000
committersatish@chromium.org <satish@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-11 10:23:37 +0000
commit5820d2f0458c851b18df616ef3aff80cb4f8dba4 (patch)
treedda803c05296f1bd8ee622c6d708a494373dcd1a /base/singleton_unittest.cc
parent9acd869ec5621373757a6959310f39e1f5ec3f3d (diff)
downloadchromium_src-5820d2f0458c851b18df616ef3aff80cb4f8dba4.zip
chromium_src-5820d2f0458c851b18df616ef3aff80cb4f8dba4.tar.gz
chromium_src-5820d2f0458c851b18df616ef3aff80cb4f8dba4.tar.bz2
Revert 68932 - 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 TBR=satish@chromium.org Review URL: http://codereview.chromium.org/5721005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68936 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/singleton_unittest.cc')
-rw-r--r--base/singleton_unittest.cc126
1 files changed, 41 insertions, 85 deletions
diff --git a/base/singleton_unittest.cc b/base/singleton_unittest.cc
index 288b89a..acb1247 100644
--- a/base/singleton_unittest.cc
+++ b/base/singleton_unittest.cc
@@ -16,127 +16,83 @@ template<typename Type>
struct LockTrait : public DefaultSingletonTraits<Type> {
};
-typedef void (*CallbackFunc)();
+struct Init5Trait : public DefaultSingletonTraits<int> {
+ static int* New() {
+ return new int(5);
+ }
+};
-class IntSingleton {
- public:
- class DummyDifferentiatingClass {
- };
-
- struct Init5Trait : public DefaultSingletonTraits<IntSingleton> {
- static IntSingleton* New() {
- IntSingleton* instance = new IntSingleton();
- instance->value_ = 5;
- return instance;
- }
- };
+typedef void (*CallbackFunc)();
- static IntSingleton* GetInstance() {
- return Singleton<IntSingleton>::get();
- }
- static IntSingleton* GetInstanceWithDefaultTraits() {
- return Singleton<IntSingleton,
- DefaultSingletonTraits<IntSingleton> >::get();
- }
- static IntSingleton* GetInstanceWithDifferentiatingClass() {
- return Singleton<IntSingleton,
- DefaultSingletonTraits<IntSingleton>,
- DummyDifferentiatingClass>::get();
- }
- static IntSingleton* GetInstanceWithLockTrait() {
- return Singleton<IntSingleton, LockTrait<IntSingleton> >::get();
+struct CallbackTrait : public DefaultSingletonTraits<CallbackFunc> {
+ static void Delete(CallbackFunc* p) {
+ if (*p)
+ (*p)();
+ DefaultSingletonTraits<CallbackFunc>::Delete(p);
}
- static IntSingleton* GetInstanceWithInit5Trait() {
- return Singleton<IntSingleton, Init5Trait>::get();
+};
+
+struct StaticCallbackTrait : public StaticMemorySingletonTraits<CallbackFunc> {
+ static void Delete(CallbackFunc* p) {
+ if (*p)
+ (*p)();
+ StaticMemorySingletonTraits<CallbackFunc>::Delete(p);
}
+};
+
- int value_;
+struct NoLeakTrait : public CallbackTrait {
+};
+
+struct LeakTrait : public CallbackTrait {
+ static const bool kRegisterAtExit = false;
};
int* SingletonInt1() {
- return &IntSingleton::GetInstance()->value_;
+ return Singleton<int>::get();
}
int* SingletonInt2() {
// Force to use a different singleton than SingletonInt1.
- return &IntSingleton::GetInstanceWithDefaultTraits()->value_;
+ return Singleton<int, DefaultSingletonTraits<int> >::get();
}
+class DummyDifferentiatingClass {
+};
+
int* SingletonInt3() {
// Force to use a different singleton than SingletonInt1 and SingletonInt2.
- return &IntSingleton::GetInstanceWithDifferentiatingClass()->value_;
+ // Note that any type can be used; int, float, std::wstring...
+ return Singleton<int, DefaultSingletonTraits<int>,
+ DummyDifferentiatingClass>::get();
}
int* SingletonInt4() {
- return &IntSingleton::GetInstanceWithLockTrait()->value_;
+ return Singleton<int, LockTrait<int> >::get();
}
int* SingletonInt5() {
- return &IntSingleton::GetInstanceWithInit5Trait()->value_;
-}
-
-class CallbackSingleton {
- public:
- struct CallbackTrait : public DefaultSingletonTraits<CallbackSingleton> {
- static void Delete(CallbackSingleton* instance) {
- if (instance->callback_)
- (instance->callback_)();
- DefaultSingletonTraits<CallbackSingleton>::Delete(instance);
- }
- };
-
- struct NoLeakTrait : public CallbackTrait {
- };
-
- struct LeakTrait : public CallbackTrait {
- static const bool kRegisterAtExit = false;
- };
-
- CallbackSingleton() : callback_(NULL) {
- }
-
- static CallbackSingleton* GetInstanceWithNoLeakTrait() {
- return Singleton<CallbackSingleton, NoLeakTrait>::get();
- }
- static CallbackSingleton* GetInstanceWithLeakTrait() {
- return Singleton<CallbackSingleton, LeakTrait>::get();
- }
- static CallbackSingleton* GetInstanceWithStaticTrait();
-
- CallbackFunc callback_;
-};
-
-struct StaticCallbackTrait
- : public StaticMemorySingletonTraits<CallbackSingleton> {
- static void Delete(CallbackSingleton* instance) {
- if (instance->callback_)
- (instance->callback_)();
- StaticMemorySingletonTraits<CallbackSingleton>::Delete(instance);
- }
-};
-
-CallbackSingleton* CallbackSingleton::GetInstanceWithStaticTrait() {
- return Singleton<CallbackSingleton, StaticCallbackTrait>::get();
+ return Singleton<int, Init5Trait>::get();
}
void SingletonNoLeak(CallbackFunc CallOnQuit) {
- CallbackSingleton::GetInstanceWithNoLeakTrait()->callback_ = CallOnQuit;
+ *Singleton<CallbackFunc, NoLeakTrait>::get() = CallOnQuit;
}
void SingletonLeak(CallbackFunc CallOnQuit) {
- CallbackSingleton::GetInstanceWithLeakTrait()->callback_ = CallOnQuit;
+ *Singleton<CallbackFunc, LeakTrait>::get() = CallOnQuit;
}
CallbackFunc* GetLeakySingleton() {
- return &CallbackSingleton::GetInstanceWithLeakTrait()->callback_;
+ return Singleton<CallbackFunc, LeakTrait>::get();
}
void SingletonStatic(CallbackFunc CallOnQuit) {
- CallbackSingleton::GetInstanceWithStaticTrait()->callback_ = CallOnQuit;
+ *Singleton<CallbackFunc, StaticCallbackTrait>::get() = CallOnQuit;
}
CallbackFunc* GetStaticSingleton() {
- return &CallbackSingleton::GetInstanceWithStaticTrait()->callback_;
+ return Singleton<CallbackFunc, StaticCallbackTrait>::get();
}
} // namespace
@@ -279,7 +235,7 @@ TEST_F(SingletonTest, Basic) {
{
// Resurrect the static singleton, and assert that it
// still points to the same (static) memory.
- StaticMemorySingletonTraits<CallbackSingleton>::Resurrect();
+ StaticMemorySingletonTraits<CallbackFunc>::Resurrect();
EXPECT_EQ(GetStaticSingleton(), static_singleton);
}
}