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 "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 // Smart pointer wrapping a mojom structure with move-only semantics. | 28 // Smart pointer wrapping a mojom structure or union, with move-only semantics. |
29 template <typename Struct> | 29 template <typename Struct> |
30 class StructPtr { | 30 class StructPtr { |
31 public: | 31 public: |
32 | |
33 StructPtr() : ptr_(nullptr) {} | 32 StructPtr() : ptr_(nullptr) {} |
34 StructPtr(decltype(nullptr)) : ptr_(nullptr) {} | 33 StructPtr(decltype(nullptr)) : ptr_(nullptr) {} |
35 | 34 |
36 ~StructPtr() { delete ptr_; } | 35 ~StructPtr() { delete ptr_; } |
37 | 36 |
38 StructPtr& operator=(decltype(nullptr)) { | 37 StructPtr& operator=(decltype(nullptr)) { |
39 reset(); | 38 reset(); |
40 return *this; | 39 return *this; |
41 } | 40 } |
42 | 41 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 void Take(StructPtr* other) { | 95 void Take(StructPtr* other) { |
97 reset(); | 96 reset(); |
98 Swap(other); | 97 Swap(other); |
99 } | 98 } |
100 | 99 |
101 Struct* ptr_; | 100 Struct* ptr_; |
102 | 101 |
103 MOJO_MOVE_ONLY_TYPE(StructPtr); | 102 MOJO_MOVE_ONLY_TYPE(StructPtr); |
104 }; | 103 }; |
105 | 104 |
106 // Designed to be used when Struct is small and copyable. | 105 // Designed to be used when Struct is small and copyable. Unions are always |
| 106 // InlinedStructPtr in practice. |
107 template <typename Struct> | 107 template <typename Struct> |
108 class InlinedStructPtr { | 108 class InlinedStructPtr { |
109 public: | 109 public: |
110 | |
111 InlinedStructPtr() : is_null_(true) {} | 110 InlinedStructPtr() : is_null_(true) {} |
112 InlinedStructPtr(decltype(nullptr)) : is_null_(true) {} | 111 InlinedStructPtr(decltype(nullptr)) : is_null_(true) {} |
113 | 112 |
114 ~InlinedStructPtr() {} | 113 ~InlinedStructPtr() {} |
115 | 114 |
116 InlinedStructPtr& operator=(decltype(nullptr)) { | 115 InlinedStructPtr& operator=(decltype(nullptr)) { |
117 reset(); | 116 reset(); |
118 return *this; | 117 return *this; |
119 } | 118 } |
120 | 119 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 174 |
176 mutable Struct value_; | 175 mutable Struct value_; |
177 bool is_null_; | 176 bool is_null_; |
178 | 177 |
179 MOJO_MOVE_ONLY_TYPE(InlinedStructPtr); | 178 MOJO_MOVE_ONLY_TYPE(InlinedStructPtr); |
180 }; | 179 }; |
181 | 180 |
182 } // namespace mojo | 181 } // namespace mojo |
183 | 182 |
184 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 183 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
OLD | NEW |