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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 // normal process exit). The Trait::Delete function will not be called on | 142 // normal process exit). The Trait::Delete function will not be called on |
143 // abnormal process exit. | 143 // abnormal process exit. |
144 // | 144 // |
145 // DifferentiatingType is used as a key to differentiate two different | 145 // DifferentiatingType is used as a key to differentiate two different |
146 // singletons having the same memory allocation functions but serving a | 146 // singletons having the same memory allocation functions but serving a |
147 // different purpose. This is mainly used for Locks serving different purposes. | 147 // different purpose. This is mainly used for Locks serving different purposes. |
148 // | 148 // |
149 // Example usage: | 149 // Example usage: |
150 // | 150 // |
151 // In your header: | 151 // In your header: |
152 // #include "base/memory/singleton.h" | 152 // template <typename T> struct DefaultSingletonTraits; |
153 // class FooClass { | 153 // class FooClass { |
154 // public: | 154 // public: |
155 // static FooClass* GetInstance(); <-- See comment below on this. | 155 // static FooClass* GetInstance(); <-- See comment below on this. |
156 // void Bar() { ... } | 156 // void Bar() { ... } |
157 // private: | 157 // private: |
158 // FooClass() { ... } | 158 // FooClass() { ... } |
159 // friend struct DefaultSingletonTraits<FooClass>; | 159 // friend struct DefaultSingletonTraits<FooClass>; |
160 // | 160 // |
161 // DISALLOW_COPY_AND_ASSIGN(FooClass); | 161 // DISALLOW_COPY_AND_ASSIGN(FooClass); |
162 // }; | 162 // }; |
163 // | 163 // |
164 // In your source file: | 164 // In your source file: |
| 165 // #include "base/memory/singleton.h" |
165 // FooClass* FooClass::GetInstance() { | 166 // FooClass* FooClass::GetInstance() { |
166 // return Singleton<FooClass>::get(); | 167 // return Singleton<FooClass>::get(); |
167 // } | 168 // } |
168 // | 169 // |
169 // And to call methods on FooClass: | 170 // And to call methods on FooClass: |
170 // FooClass::GetInstance()->Bar(); | 171 // FooClass::GetInstance()->Bar(); |
171 // | 172 // |
172 // NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance | 173 // NOTE: The method accessing Singleton<T>::get() has to be named as GetInstance |
173 // and it is important that FooClass::GetInstance() is not inlined in the | 174 // and it is important that FooClass::GetInstance() is not inlined in the |
174 // header. This makes sure that when source files from multiple targets include | 175 // header. This makes sure that when source files from multiple targets include |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 instance_ = 0; | 276 instance_ = 0; |
276 } | 277 } |
277 static base::subtle::AtomicWord instance_; | 278 static base::subtle::AtomicWord instance_; |
278 }; | 279 }; |
279 | 280 |
280 template <typename Type, typename Traits, typename DifferentiatingType> | 281 template <typename Type, typename Traits, typename DifferentiatingType> |
281 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: | 282 base::subtle::AtomicWord Singleton<Type, Traits, DifferentiatingType>:: |
282 instance_ = 0; | 283 instance_ = 0; |
283 | 284 |
284 #endif // BASE_MEMORY_SINGLETON_H_ | 285 #endif // BASE_MEMORY_SINGLETON_H_ |
OLD | NEW |