Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(757)

Unified Diff: mojo/public/cpp/bindings/struct_ptr.h

Issue 2689513003: Add field-initializing constructors to generated mojo structs. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/struct_ptr.h
diff --git a/mojo/public/cpp/bindings/struct_ptr.h b/mojo/public/cpp/bindings/struct_ptr.h
index dbc3256ea782eed9d1950fee35552c77ceb347d4..238cacf9bd0338c72f967b14343c8b48cb089fd1 100644
--- a/mojo/public/cpp/bindings/struct_ptr.h
+++ b/mojo/public/cpp/bindings/struct_ptr.h
@@ -21,9 +21,9 @@ constexpr size_t kHashSeed = 31;
template <typename Struct>
class StructHelper {
public:
- template <typename Ptr>
- static void Initialize(Ptr* ptr) {
- ptr->Initialize();
+ template <typename Ptr, typename... Args>
+ static void Initialize(Ptr* ptr, Args&&... args) {
+ ptr->Initialize(std::forward<Args>(args)...);
}
};
@@ -109,9 +109,10 @@ class StructPtr {
friend class internal::StructHelper<Struct>;
friend class internal::StructPtrWTFHelper<Struct>;
- void Initialize() {
+ template <typename... Args>
+ void Initialize(Args&&... args) {
DCHECK(!ptr_);
- ptr_ = new Struct();
+ ptr_ = new Struct(std::forward<Args>(args)...);
}
void Take(StructPtr* other) {
@@ -207,7 +208,12 @@ class InlinedStructPtr {
friend class internal::StructHelper<Struct>;
friend class internal::InlinedStructPtrWTFHelper<Struct>;
- void Initialize() { state_ = VALID; }
+ template <typename... Args>
+ void Initialize(Args&&... args) {
+ state_ = VALID;
+ 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.
+ new (&value_) Struct(std::forward<Args>(args)...);
+ }
void Take(InlinedStructPtr* other) {
reset();

Powered by Google App Engine
This is Rietveld 408576698