| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SINGLETON_H_ | 5 #ifndef BASE_SINGLETON_H_ |
| 6 #define BASE_SINGLETON_H_ | 6 #define BASE_SINGLETON_H_ |
| 7 | 7 |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
| 10 #include "base/dynamic_annotations.h" | 10 #include "base/dynamic_annotations.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // its constructors private, and make DefaultSingletonTraits<> a friend: | 83 // its constructors private, and make DefaultSingletonTraits<> a friend: |
| 84 // | 84 // |
| 85 // #include "base/singleton.h" | 85 // #include "base/singleton.h" |
| 86 // class FooClass { | 86 // class FooClass { |
| 87 // public: | 87 // public: |
| 88 // void Bar() { ... } | 88 // void Bar() { ... } |
| 89 // private: | 89 // private: |
| 90 // FooClass() { ... } | 90 // FooClass() { ... } |
| 91 // friend struct DefaultSingletonTraits<FooClass>; | 91 // friend struct DefaultSingletonTraits<FooClass>; |
| 92 // | 92 // |
| 93 // DISALLOW_EVIL_CONSTRUCTORS(FooClass); | 93 // DISALLOW_COPY_AND_ASSIGN(FooClass); |
| 94 // }; | 94 // }; |
| 95 // | 95 // |
| 96 // Caveats: | 96 // Caveats: |
| 97 // (a) Every call to get(), operator->() and operator*() incurs some overhead | 97 // (a) Every call to get(), operator->() and operator*() incurs some overhead |
| 98 // (16ns on my P4/2.8GHz) to check whether the object has already been | 98 // (16ns on my P4/2.8GHz) to check whether the object has already been |
| 99 // initialized. You may wish to cache the result of get(); it will not | 99 // initialized. You may wish to cache the result of get(); it will not |
| 100 // change. | 100 // change. |
| 101 // | 101 // |
| 102 // (b) Your factory function must never throw an exception. This class is not | 102 // (b) Your factory function must never throw an exception. This class is not |
| 103 // exception-safe. | 103 // exception-safe. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 // threaded, but we might as well take the precautions anyway. | 178 // threaded, but we might as well take the precautions anyway. |
| 179 static void OnExit(void* unused) { | 179 static void OnExit(void* unused) { |
| 180 // AtExit should only ever be register after the singleton instance was | 180 // AtExit should only ever be register after the singleton instance was |
| 181 // created. We should only ever get here with a valid instance_ pointer. | 181 // created. We should only ever get here with a valid instance_ pointer. |
| 182 Traits::Delete(reinterpret_cast<Type*>( | 182 Traits::Delete(reinterpret_cast<Type*>( |
| 183 base::subtle::NoBarrier_AtomicExchange(&instance_, 0))); | 183 base::subtle::NoBarrier_AtomicExchange(&instance_, 0))); |
| 184 } | 184 } |
| 185 static base::subtle::AtomicWord instance_; | 185 static base::subtle::AtomicWord instance_; |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 template <typename Type, typename DifferentiatingType = Type> |
| 189 class LeakySingleton |
| 190 : public Singleton<Type, |
| 191 LeakySingletonTraits<Type>, |
| 192 DifferentiatingType> { |
| 193 }; |
| 194 |
| 188 template <typename Type, typename Traits, typename DifferentiatingType> | 195 template <typename Type, typename Traits, typename DifferentiatingType> |
| 189 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 196 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
| 190 instance_ = 0; | 197 instance_ = 0; |
| 191 | 198 |
| 192 #endif // BASE_SINGLETON_H_ | 199 #endif // BASE_SINGLETON_H_ |
| OLD | NEW |