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
3 // found in the LICENSE file.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_
10 #include "mojo/public/cpp/bindings/type_converter.h"
11 #include "mojo/public/cpp/environment/logging.h"
12 #include "mojo/public/cpp/system/macros.h"
17 template <typename Struct>
20 template <typename Ptr>
21 static void Initialize(Ptr* ptr) {
26 } // namespace internal
28 template <typename Struct>
30 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr, RValue);
34 StructPtr() : ptr_(nullptr) {}
35 ~StructPtr() { delete ptr_; }
37 StructPtr(RValue other) : ptr_(nullptr) { Take(other.object); }
38 StructPtr& operator=(RValue other) {
45 return TypeConverter<U, StructPtr>::Convert(*this);
55 bool is_null() const { return ptr_ == nullptr; }
57 Struct& operator*() const {
61 Struct* operator->() const {
65 Struct* get() const { return ptr_; }
67 void Swap(StructPtr* other) { std::swap(ptr_, other->ptr_); }
69 // Please note that calling this method will fail compilation if the value
70 // type |Struct| doesn't have a Clone() method defined (which usually means
71 // that it contains Mojo handles).
72 StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); }
74 bool Equals(const StructPtr& other) const {
75 if (is_null() || other.is_null())
76 return is_null() && other.is_null();
77 return ptr_->Equals(*other.ptr_);
81 typedef Struct* StructPtr::*Testable;
84 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; }
87 friend class internal::StructHelper<Struct>;
93 void Take(StructPtr* other) {
101 // Designed to be used when Struct is small and copyable.
102 template <typename Struct>
103 class InlinedStructPtr {
104 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr, RValue);
108 InlinedStructPtr() : is_null_(true) {}
109 ~InlinedStructPtr() {}
111 InlinedStructPtr(RValue other) : is_null_(true) { Take(other.object); }
112 InlinedStructPtr& operator=(RValue other) {
117 template <typename U>
119 return TypeConverter<U, InlinedStructPtr>::Convert(*this);
125 new (&value_) Struct();
128 bool is_null() const { return is_null_; }
130 Struct& operator*() const {
131 MOJO_DCHECK(!is_null_);
134 Struct* operator->() const {
135 MOJO_DCHECK(!is_null_);
138 Struct* get() const { return &value_; }
140 void Swap(InlinedStructPtr* other) {
141 std::swap(value_, other->value_);
142 std::swap(is_null_, other->is_null_);
145 InlinedStructPtr Clone() const {
146 return is_null() ? InlinedStructPtr() : value_.Clone();
148 bool Equals(const InlinedStructPtr& other) const {
149 if (is_null() || other.is_null())
150 return is_null() && other.is_null();
151 return value_.Equals(other.value_);
155 typedef Struct InlinedStructPtr::*Testable;
158 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; }
161 friend class internal::StructHelper<Struct>;
162 void Initialize() { is_null_ = false; }
164 void Take(InlinedStructPtr* other) {
169 mutable Struct value_;
175 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_