OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
7 | 7 |
8 #include <functional> | 8 #include <functional> |
9 #include <new> | 9 #include <new> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "mojo/public/cpp/bindings/lib/hash_util.h" | 13 #include "mojo/public/cpp/bindings/lib/hash_util.h" |
14 #include "mojo/public/cpp/bindings/type_converter.h" | 14 #include "mojo/public/cpp/bindings/type_converter.h" |
15 | 15 |
16 namespace mojo { | 16 namespace mojo { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 constexpr size_t kHashSeed = 31; | 19 constexpr size_t kHashSeed = 31; |
20 | 20 |
21 template <typename Struct> | 21 template <typename Struct> |
22 class StructHelper { | 22 class StructHelper { |
23 public: | 23 public: |
24 template <typename Ptr> | 24 template <typename Ptr, typename... Args> |
25 static void Initialize(Ptr* ptr) { | 25 static void Initialize(Ptr* ptr, Args&&... args) { |
26 ptr->Initialize(); | 26 ptr->Initialize(std::forward<Args>(args)...); |
27 } | 27 } |
28 }; | 28 }; |
29 | 29 |
30 template <typename Struct> | 30 template <typename Struct> |
31 class StructPtrWTFHelper; | 31 class StructPtrWTFHelper; |
32 | 32 |
33 template <typename Struct> | 33 template <typename Struct> |
34 class InlinedStructPtrWTFHelper; | 34 class InlinedStructPtrWTFHelper; |
35 | 35 |
36 } // namespace internal | 36 } // namespace internal |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 return internal::HashCombine(seed, 0); | 102 return internal::HashCombine(seed, 0); |
103 return ptr_->Hash(seed); | 103 return ptr_->Hash(seed); |
104 } | 104 } |
105 | 105 |
106 explicit operator bool() const { return !is_null(); } | 106 explicit operator bool() const { return !is_null(); } |
107 | 107 |
108 private: | 108 private: |
109 friend class internal::StructHelper<Struct>; | 109 friend class internal::StructHelper<Struct>; |
110 friend class internal::StructPtrWTFHelper<Struct>; | 110 friend class internal::StructPtrWTFHelper<Struct>; |
111 | 111 |
112 void Initialize() { | 112 template <typename... Args> |
113 void Initialize(Args&&... args) { | |
113 DCHECK(!ptr_); | 114 DCHECK(!ptr_); |
114 ptr_ = new Struct(); | 115 ptr_ = new Struct(std::forward<Args>(args)...); |
115 } | 116 } |
116 | 117 |
117 void Take(StructPtr* other) { | 118 void Take(StructPtr* other) { |
118 reset(); | 119 reset(); |
119 Swap(other); | 120 Swap(other); |
120 } | 121 } |
121 | 122 |
122 Struct* ptr_; | 123 Struct* ptr_; |
123 | 124 |
124 DISALLOW_COPY_AND_ASSIGN(StructPtr); | 125 DISALLOW_COPY_AND_ASSIGN(StructPtr); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 return internal::HashCombine(seed, 0); | 201 return internal::HashCombine(seed, 0); |
201 return value_.Hash(seed); | 202 return value_.Hash(seed); |
202 } | 203 } |
203 | 204 |
204 explicit operator bool() const { return !is_null(); } | 205 explicit operator bool() const { return !is_null(); } |
205 | 206 |
206 private: | 207 private: |
207 friend class internal::StructHelper<Struct>; | 208 friend class internal::StructHelper<Struct>; |
208 friend class internal::InlinedStructPtrWTFHelper<Struct>; | 209 friend class internal::InlinedStructPtrWTFHelper<Struct>; |
209 | 210 |
210 void Initialize() { state_ = VALID; } | 211 template <typename... Args> |
212 void Initialize(Args&&... args) { | |
213 state_ = VALID; | |
214 value_.~Struct(); | |
yzshen1
2017/02/13 17:45:53
It seems we always default construct value_, destr
Sam McNally
2017/02/14 02:32:24
Done.
| |
215 new (&value_) Struct(std::forward<Args>(args)...); | |
216 } | |
211 | 217 |
212 void Take(InlinedStructPtr* other) { | 218 void Take(InlinedStructPtr* other) { |
213 reset(); | 219 reset(); |
214 Swap(other); | 220 Swap(other); |
215 } | 221 } |
216 | 222 |
217 enum State { | 223 enum State { |
218 VALID, | 224 VALID, |
219 NIL, | 225 NIL, |
220 DELETED, // For use in WTF::HashMap only | 226 DELETED, // For use in WTF::HashMap only |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 template <typename T> | 295 template <typename T> |
290 struct hash<mojo::InlinedStructPtr<T>> { | 296 struct hash<mojo::InlinedStructPtr<T>> { |
291 size_t operator()(const mojo::InlinedStructPtr<T>& value) const { | 297 size_t operator()(const mojo::InlinedStructPtr<T>& value) const { |
292 return value.Hash(mojo::internal::kHashSeed); | 298 return value.Hash(mojo::internal::kHashSeed); |
293 } | 299 } |
294 }; | 300 }; |
295 | 301 |
296 } // namespace std | 302 } // namespace std |
297 | 303 |
298 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 304 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
OLD | NEW |