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 <new> | 8 #include <new> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/move.h" | 11 #include "base/macros.h" |
12 #include "mojo/public/cpp/bindings/type_converter.h" | 12 #include "mojo/public/cpp/bindings/type_converter.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 // Smart pointer wrapping a mojom structure with move-only semantics. | 28 // Smart pointer wrapping a mojom structure with move-only semantics. |
29 template <typename Struct> | 29 template <typename Struct> |
30 class StructPtr { | 30 class StructPtr { |
31 MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr); | |
32 | |
33 public: | 31 public: |
34 | 32 |
35 StructPtr() : ptr_(nullptr) {} | 33 StructPtr() : ptr_(nullptr) {} |
36 StructPtr(decltype(nullptr)) : ptr_(nullptr) {} | 34 StructPtr(decltype(nullptr)) : ptr_(nullptr) {} |
37 | 35 |
38 ~StructPtr() { delete ptr_; } | 36 ~StructPtr() { delete ptr_; } |
39 | 37 |
40 StructPtr& operator=(decltype(nullptr)) { | 38 StructPtr& operator=(decltype(nullptr)) { |
41 reset(); | 39 reset(); |
42 return *this; | 40 return *this; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // that it contains Mojo handles). | 77 // that it contains Mojo handles). |
80 StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); } | 78 StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); } |
81 | 79 |
82 bool Equals(const StructPtr& other) const { | 80 bool Equals(const StructPtr& other) const { |
83 if (is_null() || other.is_null()) | 81 if (is_null() || other.is_null()) |
84 return is_null() && other.is_null(); | 82 return is_null() && other.is_null(); |
85 return ptr_->Equals(*other.ptr_); | 83 return ptr_->Equals(*other.ptr_); |
86 } | 84 } |
87 | 85 |
88 private: | 86 private: |
| 87 // TODO(dcheng): Use an explicit conversion operator. |
89 typedef Struct* StructPtr::*Testable; | 88 typedef Struct* StructPtr::*Testable; |
90 | 89 |
91 public: | 90 public: |
92 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } | 91 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } |
93 | 92 |
94 private: | 93 private: |
95 friend class internal::StructHelper<Struct>; | 94 friend class internal::StructHelper<Struct>; |
96 | 95 |
97 // Forbid the == and != operators explicitly, otherwise StructPtr will be | 96 // Forbid the == and != operators explicitly, otherwise StructPtr will be |
98 // converted to Testable to do == or != comparison. | 97 // converted to Testable to do == or != comparison. |
99 template <typename T> | 98 template <typename T> |
100 bool operator==(const StructPtr<T>& other) const = delete; | 99 bool operator==(const StructPtr<T>& other) const = delete; |
101 template <typename T> | 100 template <typename T> |
102 bool operator!=(const StructPtr<T>& other) const = delete; | 101 bool operator!=(const StructPtr<T>& other) const = delete; |
103 | 102 |
104 void Initialize() { | 103 void Initialize() { |
105 DCHECK(!ptr_); | 104 DCHECK(!ptr_); |
106 ptr_ = new Struct(); | 105 ptr_ = new Struct(); |
107 } | 106 } |
108 | 107 |
109 void Take(StructPtr* other) { | 108 void Take(StructPtr* other) { |
110 reset(); | 109 reset(); |
111 Swap(other); | 110 Swap(other); |
112 } | 111 } |
113 | 112 |
114 Struct* ptr_; | 113 Struct* ptr_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(StructPtr); |
115 }; | 116 }; |
116 | 117 |
117 // Designed to be used when Struct is small and copyable. | 118 // Designed to be used when Struct is small and copyable. |
118 template <typename Struct> | 119 template <typename Struct> |
119 class InlinedStructPtr { | 120 class InlinedStructPtr { |
120 MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr); | |
121 | |
122 public: | 121 public: |
123 | 122 |
124 InlinedStructPtr() : is_null_(true) {} | 123 InlinedStructPtr() : is_null_(true) {} |
125 InlinedStructPtr(decltype(nullptr)) : is_null_(true) {} | 124 InlinedStructPtr(decltype(nullptr)) : is_null_(true) {} |
126 | 125 |
127 ~InlinedStructPtr() {} | 126 ~InlinedStructPtr() {} |
128 | 127 |
129 InlinedStructPtr& operator=(decltype(nullptr)) { | 128 InlinedStructPtr& operator=(decltype(nullptr)) { |
130 reset(); | 129 reset(); |
131 return *this; | 130 return *this; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 InlinedStructPtr Clone() const { | 167 InlinedStructPtr Clone() const { |
169 return is_null() ? InlinedStructPtr() : value_.Clone(); | 168 return is_null() ? InlinedStructPtr() : value_.Clone(); |
170 } | 169 } |
171 bool Equals(const InlinedStructPtr& other) const { | 170 bool Equals(const InlinedStructPtr& other) const { |
172 if (is_null() || other.is_null()) | 171 if (is_null() || other.is_null()) |
173 return is_null() && other.is_null(); | 172 return is_null() && other.is_null(); |
174 return value_.Equals(other.value_); | 173 return value_.Equals(other.value_); |
175 } | 174 } |
176 | 175 |
177 private: | 176 private: |
| 177 // TODO(dcheng): Use an explicit conversion operator. |
178 typedef Struct InlinedStructPtr::*Testable; | 178 typedef Struct InlinedStructPtr::*Testable; |
179 | 179 |
180 public: | 180 public: |
181 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } | 181 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } |
182 | 182 |
183 private: | 183 private: |
184 friend class internal::StructHelper<Struct>; | 184 friend class internal::StructHelper<Struct>; |
185 | 185 |
186 // Forbid the == and != operators explicitly, otherwise InlinedStructPtr will | 186 // Forbid the == and != operators explicitly, otherwise InlinedStructPtr will |
187 // be converted to Testable to do == or != comparison. | 187 // be converted to Testable to do == or != comparison. |
188 template <typename T> | 188 template <typename T> |
189 bool operator==(const InlinedStructPtr<T>& other) const = delete; | 189 bool operator==(const InlinedStructPtr<T>& other) const = delete; |
190 template <typename T> | 190 template <typename T> |
191 bool operator!=(const InlinedStructPtr<T>& other) const = delete; | 191 bool operator!=(const InlinedStructPtr<T>& other) const = delete; |
192 | 192 |
193 void Initialize() { is_null_ = false; } | 193 void Initialize() { is_null_ = false; } |
194 | 194 |
195 void Take(InlinedStructPtr* other) { | 195 void Take(InlinedStructPtr* other) { |
196 reset(); | 196 reset(); |
197 Swap(other); | 197 Swap(other); |
198 } | 198 } |
199 | 199 |
200 mutable Struct value_; | 200 mutable Struct value_; |
201 bool is_null_; | 201 bool is_null_; |
| 202 |
| 203 DISALLOW_COPY_AND_ASSIGN(InlinedStructPtr); |
202 }; | 204 }; |
203 | 205 |
204 } // namespace mojo | 206 } // namespace mojo |
205 | 207 |
206 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 208 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
OLD | NEW |