| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 | 107 |
| 108 // Signal the object was already deleted, so it is not revived. | 108 // Signal the object was already deleted, so it is not revived. |
| 109 static base::subtle::Atomic32 dead_; | 109 static base::subtle::Atomic32 dead_; |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 template <typename Type> intptr_t | 112 template <typename Type> intptr_t |
| 113 StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; | 113 StaticMemorySingletonTraits<Type>::buffer_[kBufferSize]; |
| 114 template <typename Type> base::subtle::Atomic32 | 114 template <typename Type> base::subtle::Atomic32 |
| 115 StaticMemorySingletonTraits<Type>::dead_ = 0; | 115 StaticMemorySingletonTraits<Type>::dead_ = 0; |
| 116 | 116 |
| 117 | |
| 118 // The Singleton<Type, Traits, DifferentiatingType> class manages a single | 117 // The Singleton<Type, Traits, DifferentiatingType> class manages a single |
| 119 // instance of Type which will be created on first use and will be destroyed at | 118 // instance of Type which will be created on first use and will be destroyed at |
| 120 // normal process exit). The Trait::Delete function will not be called on | 119 // normal process exit). The Trait::Delete function will not be called on |
| 121 // abnormal process exit. | 120 // abnormal process exit. |
| 122 // | 121 // |
| 123 // DifferentiatingType is used as a key to differentiate two different | 122 // DifferentiatingType is used as a key to differentiate two different |
| 124 // singletons having the same memory allocation functions but serving a | 123 // singletons having the same memory allocation functions but serving a |
| 125 // different purpose. This is mainly used for Locks serving different purposes. | 124 // different purpose. This is mainly used for Locks serving different purposes. |
| 126 // | 125 // |
| 127 // Example usages: (none are preferred, they all result in the same code) | 126 // Example usages: (none are preferred, they all result in the same code) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // initialized. You may wish to cache the result of get(); it will not | 171 // initialized. You may wish to cache the result of get(); it will not |
| 173 // change. | 172 // change. |
| 174 // | 173 // |
| 175 // (b) Your factory function must never throw an exception. This class is not | 174 // (b) Your factory function must never throw an exception. This class is not |
| 176 // exception-safe. | 175 // exception-safe. |
| 177 // | 176 // |
| 178 template <typename Type, | 177 template <typename Type, |
| 179 typename Traits = DefaultSingletonTraits<Type>, | 178 typename Traits = DefaultSingletonTraits<Type>, |
| 180 typename DifferentiatingType = Type> | 179 typename DifferentiatingType = Type> |
| 181 class Singleton { | 180 class Singleton { |
| 182 public: | 181 private: |
| 182 // Classes using the Singleton<T> pattern should declare a GetInstance() |
| 183 // method and call Singleton::get() from within that. |
| 184 friend Type* Type::GetInstance(); |
| 185 |
| 183 // This class is safe to be constructed and copy-constructed since it has no | 186 // This class is safe to be constructed and copy-constructed since it has no |
| 184 // member. | 187 // member. |
| 185 | 188 |
| 186 // Return a pointer to the one true instance of the class. | 189 // Return a pointer to the one true instance of the class. |
| 187 static Type* get() { | 190 static Type* get() { |
| 188 if (!Traits::kAllowedToAccessOnNonjoinableThread) | 191 if (!Traits::kAllowedToAccessOnNonjoinableThread) |
| 189 base::ThreadRestrictions::AssertSingletonAllowed(); | 192 base::ThreadRestrictions::AssertSingletonAllowed(); |
| 190 | 193 |
| 191 // Our AtomicWord doubles as a spinlock, where a value of | 194 // Our AtomicWord doubles as a spinlock, where a value of |
| 192 // kBeingCreatedMarker means the spinlock is being held for creation. | 195 // kBeingCreatedMarker means the spinlock is being held for creation. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 instance_ = 0; | 264 instance_ = 0; |
| 262 } | 265 } |
| 263 static base::subtle::AtomicWord instance_; | 266 static base::subtle::AtomicWord instance_; |
| 264 }; | 267 }; |
| 265 | 268 |
| 266 template <typename Type, typename Traits, typename DifferentiatingType> | 269 template <typename Type, typename Traits, typename DifferentiatingType> |
| 267 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 270 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
| 268 instance_ = 0; | 271 instance_ = 0; |
| 269 | 272 |
| 270 #endif // BASE_SINGLETON_H_ | 273 #endif // BASE_SINGLETON_H_ |
| OLD | NEW |