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

Side by Side Diff: mojo/public/cpp/bindings/struct_ptr.h

Issue 828373008: Allow constructing {Interface,Struct}Ptr from nullptr (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 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 unified diff | Download patch
OLDNEW
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 <new> 8 #include <new>
9 9
10 #include "mojo/public/cpp/bindings/type_converter.h" 10 #include "mojo/public/cpp/bindings/type_converter.h"
11 #include "mojo/public/cpp/environment/logging.h" 11 #include "mojo/public/cpp/environment/logging.h"
12 #include "mojo/public/cpp/system/macros.h" 12 #include "mojo/public/cpp/system/macros.h"
13 13
14 namespace mojo { 14 namespace mojo {
15 namespace internal { 15 namespace internal {
16 16
17 template <typename Struct> 17 template <typename Struct>
18 class StructHelper { 18 class StructHelper {
19 public: 19 public:
20 template <typename Ptr> 20 template <typename Ptr>
21 static void Initialize(Ptr* ptr) { 21 static void Initialize(Ptr* ptr) {
22 ptr->Initialize(); 22 ptr->Initialize();
23 } 23 }
24 }; 24 };
25 25
26 } // namespace internal 26 } // namespace internal
27 27
28 template <typename Struct> 28 template <typename Struct>
29 class StructPtr { 29 class StructPtr {
30 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr, RValue); 30 MOJO_MOVE_ONLY_TYPE(StructPtr)
31 31
32 public: 32 public:
33 33
34 StructPtr() : ptr_(nullptr) {} 34 StructPtr() : ptr_(nullptr) {}
35 StructPtr(decltype(nullptr)) : ptr_(nullptr) {}
36
35 ~StructPtr() { delete ptr_; } 37 ~StructPtr() { delete ptr_; }
36 38
37 StructPtr(RValue other) : ptr_(nullptr) { Take(other.object); } 39 StructPtr& operator=(decltype(nullptr)) {
38 StructPtr& operator=(RValue other) { 40 reset();
39 Take(other.object);
40 return *this; 41 return *this;
41 } 42 }
42 43
44 StructPtr(StructPtr&& other) : ptr_(nullptr) { Take(&other); }
45 StructPtr& operator=(StructPtr&& other) {
46 Take(&other);
47 return *this;
48 }
49
43 template <typename U> 50 template <typename U>
44 U To() const { 51 U To() const {
45 return TypeConverter<U, StructPtr>::Convert(*this); 52 return TypeConverter<U, StructPtr>::Convert(*this);
46 } 53 }
47 54
48 void reset() { 55 void reset() {
49 if (ptr_) { 56 if (ptr_) {
50 delete ptr_; 57 delete ptr_;
51 ptr_ = nullptr; 58 ptr_ = nullptr;
52 } 59 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 reset(); 101 reset();
95 Swap(other); 102 Swap(other);
96 } 103 }
97 104
98 Struct* ptr_; 105 Struct* ptr_;
99 }; 106 };
100 107
101 // Designed to be used when Struct is small and copyable. 108 // Designed to be used when Struct is small and copyable.
102 template <typename Struct> 109 template <typename Struct>
103 class InlinedStructPtr { 110 class InlinedStructPtr {
104 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr, RValue); 111 MOJO_MOVE_ONLY_TYPE(InlinedStructPtr);
105 112
106 public: 113 public:
107 114
108 InlinedStructPtr() : is_null_(true) {} 115 InlinedStructPtr() : is_null_(true) {}
109 ~InlinedStructPtr() {} 116 ~InlinedStructPtr() {}
110 117
darin (slow to review) 2015/01/14 18:38:07 don't you need the nullptr ctor and assignment ope
jamesr 2015/01/14 18:39:30 Hmm yes - I didn't find a user of this but it'd be
111 InlinedStructPtr(RValue other) : is_null_(true) { Take(other.object); } 118 InlinedStructPtr(InlinedStructPtr&& other) : is_null_(true) { Take(&other); }
112 InlinedStructPtr& operator=(RValue other) { 119 InlinedStructPtr& operator=(InlinedStructPtr&& other) {
113 Take(other.object); 120 Take(&other);
114 return *this; 121 return *this;
115 } 122 }
116 123
117 template <typename U> 124 template <typename U>
118 U To() const { 125 U To() const {
119 return TypeConverter<U, InlinedStructPtr>::Convert(*this); 126 return TypeConverter<U, InlinedStructPtr>::Convert(*this);
120 } 127 }
121 128
122 void reset() { 129 void reset() {
123 is_null_ = true; 130 is_null_ = true;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 Swap(other); 173 Swap(other);
167 } 174 }
168 175
169 mutable Struct value_; 176 mutable Struct value_;
170 bool is_null_; 177 bool is_null_;
171 }; 178 };
172 179
173 } // namespace mojo 180 } // namespace mojo
174 181
175 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ 182 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698