OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // ManualConstructor statically-allocates space in which to store some | 5 // ManualConstructor statically-allocates space in which to store some |
6 // object, but does not initialize it. You can then call the constructor | 6 // object, but does not initialize it. You can then call the constructor |
7 // and destructor for the object yourself as you see fit. This is useful | 7 // and destructor for the object yourself as you see fit. This is useful |
8 // for memory management optimizations, where you want to initialize and | 8 // for memory management optimizations, where you want to initialize and |
9 // destroy an object multiple times but only allocate it once. | 9 // destroy an object multiple times but only allocate it once. |
10 // | 10 // |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 return space_.template data_as<Type>(); | 47 return space_.template data_as<Type>(); |
48 } | 48 } |
49 | 49 |
50 inline Type* operator->() { return get(); } | 50 inline Type* operator->() { return get(); } |
51 inline const Type* operator->() const { return get(); } | 51 inline const Type* operator->() const { return get(); } |
52 | 52 |
53 inline Type& operator*() { return *get(); } | 53 inline Type& operator*() { return *get(); } |
54 inline const Type& operator*() const { return *get(); } | 54 inline const Type& operator*() const { return *get(); } |
55 | 55 |
56 template <typename... Ts> | 56 template <typename... Ts> |
57 inline void Init(const Ts&... params) { | 57 inline void Init(Ts&&... params) { |
58 new(space_.void_data()) Type(params...); | 58 new(space_.void_data()) Type(std::forward<Ts>(params)...); |
59 } | |
60 | |
61 inline void Move(ManualConstructor<Type>&& o) { | |
danakj
2016/02/27 00:32:02
std::move() means you're moving the object, this i
btolsch
2016/02/27 01:18:37
Done.
| |
62 Init(std::move(*o)); | |
59 } | 63 } |
60 | 64 |
61 inline void Destroy() { | 65 inline void Destroy() { |
62 get()->~Type(); | 66 get()->~Type(); |
63 } | 67 } |
64 | 68 |
65 private: | 69 private: |
66 AlignedMemory<sizeof(Type), ALIGNOF(Type)> space_; | 70 AlignedMemory<sizeof(Type), ALIGNOF(Type)> space_; |
67 }; | 71 }; |
68 | 72 |
69 } // namespace base | 73 } // namespace base |
70 | 74 |
71 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ | 75 #endif // BASE_MEMORY_MANUAL_CONSTRUCTOR_H_ |
OLD | NEW |