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