OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 // The LazyInstance<Type, Traits> class manages a single instance of Type, | 5 // The LazyInstance<Type, Traits> class manages a single instance of Type, |
6 // which will be lazily created on the first time it's accessed. This class is | 6 // which will be lazily created on the first time it's accessed. This class is |
7 // useful for places you would normally use a function-level static, but you | 7 // useful for places you would normally use a function-level static, but you |
8 // need to have guaranteed thread-safety. The Type constructor will only ever | 8 // need to have guaranteed thread-safety. The Type constructor will only ever |
9 // be called once, even if two threads are racing to create the object. Get() | 9 // be called once, even if two threads are racing to create the object. Get() |
10 // and Pointer() will always return the same, completely initialized instance. | 10 // and Pointer() will always return the same, completely initialized instance. |
11 // When the instance is constructed it is registered with AtExitManager. The | 11 // When the instance is constructed it is registered with AtExitManager. The |
12 // destructor will be called on program exit. | 12 // destructor will be called on program exit. |
13 // | 13 // |
14 // LazyInstance is completely thread safe, assuming that you create it safely. | 14 // LazyInstance is completely thread safe, assuming that you create it safely. |
15 // The class was designed to be POD initialized, so it shouldn't require a | 15 // The class was designed to be POD initialized, so it shouldn't require a |
16 // static constructor. It really only makes sense to declare a LazyInstance as | 16 // static constructor. It really only makes sense to declare a LazyInstance as |
17 // a global variable using the base::LinkerInitialized constructor. | 17 // a global variable using the base::LinkerInitialized constructor. |
18 // | 18 // |
19 // LazyInstance is similar to Singleton, except it does not have the singleton | 19 // LazyInstance is similar to Singleton, except it does not have the singleton |
20 // property. You can have multiple LazyInstance's of the same type, and each | 20 // property. You can have multiple LazyInstance's of the same type, and each |
21 // will manage a unique instance. It also preallocates the space for Type, as | 21 // will manage a unique instance. It also preallocates the space for Type, as |
22 // to avoid allocating the Type instance on the heap. This may help with the | 22 // to avoid allocating the Type instance on the heap. This may help with the |
23 // performance of creating the instance, and reducing heap fragmentation. This | 23 // performance of creating the instance, and reducing heap fragmentation. This |
24 // requires that Type be a complete type so we can determine the size. | 24 // requires that Type be a complete type so we can determine the size. |
25 // | 25 // |
26 // Example usage: | 26 // Example usage: |
27 // static LazyInstance<MyClass> my_instance(base::LINKER_INITALIZED); | 27 // static LazyInstance<MyClass> my_instance(base::LINKER_INITIALIZED); |
28 // void SomeMethod() { | 28 // void SomeMethod() { |
29 // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() | 29 // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() |
30 // | 30 // |
31 // MyClass* ptr = my_instance.Pointer(); | 31 // MyClass* ptr = my_instance.Pointer(); |
32 // ptr->DoDoDo(); // MyClass::DoDoDo | 32 // ptr->DoDoDo(); // MyClass::DoDoDo |
33 // } | 33 // } |
34 | 34 |
35 #ifndef BASE_LAZY_INSTANCE_H_ | 35 #ifndef BASE_LAZY_INSTANCE_H_ |
36 #define BASE_LAZY_INSTANCE_H_ | 36 #define BASE_LAZY_INSTANCE_H_ |
37 | 37 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 private: | 102 private: |
103 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 103 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
104 | 104 |
105 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 105 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
106 }; | 106 }; |
107 | 107 |
108 } // namespace base | 108 } // namespace base |
109 | 109 |
110 #endif // BASE_LAZY_INSTANCE_H_ | 110 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |