OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // PLEASE READ: Do you really need a singleton? | 5 // PLEASE READ: Do you really need a singleton? |
6 // | 6 // |
7 // Singletons make it hard to determine the lifetime of an object, which can | 7 // Singletons make it hard to determine the lifetime of an object, which can |
8 // lead to buggy code and spurious crashes. | 8 // lead to buggy code and spurious crashes. |
9 // | 9 // |
10 // Instead of adding another singleton into the mix, try to identify either: | 10 // Instead of adding another singleton into the mix, try to identify either: |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 // | 198 // |
199 template <typename Type, | 199 template <typename Type, |
200 typename Traits = DefaultSingletonTraits<Type>, | 200 typename Traits = DefaultSingletonTraits<Type>, |
201 typename DifferentiatingType = Type> | 201 typename DifferentiatingType = Type> |
202 class Singleton { | 202 class Singleton { |
203 private: | 203 private: |
204 // Classes using the Singleton<T> pattern should declare a GetInstance() | 204 // Classes using the Singleton<T> pattern should declare a GetInstance() |
205 // method and call Singleton::get() from within that. | 205 // method and call Singleton::get() from within that. |
206 friend Type* Type::GetInstance(); | 206 friend Type* Type::GetInstance(); |
207 | 207 |
| 208 // Allow TraceLog tests to test tracing after OnExit. |
| 209 friend class DeleteTraceLogForTesting; |
| 210 |
208 // This class is safe to be constructed and copy-constructed since it has no | 211 // This class is safe to be constructed and copy-constructed since it has no |
209 // member. | 212 // member. |
210 | 213 |
211 // Return a pointer to the one true instance of the class. | 214 // Return a pointer to the one true instance of the class. |
212 static Type* get() { | 215 static Type* get() { |
213 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 216 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
214 base::ThreadRestrictions::AssertSingletonAllowed(); | 217 base::ThreadRestrictions::AssertSingletonAllowed(); |
215 | 218 |
216 // Our AtomicWord doubles as a spinlock, where a value of | 219 // Our AtomicWord doubles as a spinlock, where a value of |
217 // kBeingCreatedMarker means the spinlock is being held for creation. | 220 // kBeingCreatedMarker means the spinlock is being held for creation. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 instance_ = 0; | 279 instance_ = 0; |
277 } | 280 } |
278 static base::subtle::AtomicWord instance_; | 281 static base::subtle::AtomicWord instance_; |
279 }; | 282 }; |
280 | 283 |
281 template <typename Type, typename Traits, typename DifferentiatingType> | 284 template <typename Type, typename Traits, typename DifferentiatingType> |
282 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 285 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
283 instance_ = 0; | 286 instance_ = 0; |
284 | 287 |
285 #endif // BASE_MEMORY_SINGLETON_H_ | 288 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |