| 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. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // Use placement new to initialize our instance in our preallocated space. | 48 // Use placement new to initialize our instance in our preallocated space. |
| 49 // The parenthesis is very important here to force POD type initialization. | 49 // The parenthesis is very important here to force POD type initialization. |
| 50 return new (instance) Type(); | 50 return new (instance) Type(); |
| 51 } | 51 } |
| 52 static void Delete(void* instance) { | 52 static void Delete(void* instance) { |
| 53 // Explicitly call the destructor. | 53 // Explicitly call the destructor. |
| 54 reinterpret_cast<Type*>(instance)->~Type(); | 54 reinterpret_cast<Type*>(instance)->~Type(); |
| 55 } | 55 } |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 template <typename Type> |
| 59 struct LeakyLazyInstanceTraits { |
| 60 static Type* New(void* instance) { |
| 61 return DefaultLazyInstanceTraits<Type>::New(instance); |
| 62 } |
| 63 // Rather than define an empty Delete function, we make Delete itself |
| 64 // a null pointer. This allows us to completely sidestep registering |
| 65 // this object with an AtExitManager, which allows you to use |
| 66 // LeakyLazyInstanceTraits in contexts where you don't have an |
| 67 // AtExitManager. |
| 68 static void (*Delete)(void* instance); |
| 69 }; |
| 70 |
| 71 template <typename Type> |
| 72 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; |
| 73 |
| 58 // We pull out some of the functionality into a non-templated base, so that we | 74 // We pull out some of the functionality into a non-templated base, so that we |
| 59 // can implement the more complicated pieces out of line in the .cc file. | 75 // can implement the more complicated pieces out of line in the .cc file. |
| 60 class LazyInstanceHelper { | 76 class LazyInstanceHelper { |
| 61 protected: | 77 protected: |
| 62 enum { | 78 enum { |
| 63 STATE_EMPTY = 0, | 79 STATE_EMPTY = 0, |
| 64 STATE_CREATING = 1, | 80 STATE_CREATING = 1, |
| 65 STATE_CREATED = 2 | 81 STATE_CREATED = 2 |
| 66 }; | 82 }; |
| 67 | 83 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 private: | 133 private: |
| 118 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 134 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
| 119 Type *instance_; | 135 Type *instance_; |
| 120 | 136 |
| 121 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 137 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 122 }; | 138 }; |
| 123 | 139 |
| 124 } // namespace base | 140 } // namespace base |
| 125 | 141 |
| 126 #endif // BASE_LAZY_INSTANCE_H_ | 142 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |