| 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/environment/logging.h" | 10 #include "mojo/public/cpp/environment/logging.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 Struct* operator->() const { | 60 Struct* operator->() const { |
| 61 MOJO_DCHECK(ptr_); | 61 MOJO_DCHECK(ptr_); |
| 62 return ptr_; | 62 return ptr_; |
| 63 } | 63 } |
| 64 Struct* get() const { return ptr_; } | 64 Struct* get() const { return ptr_; } |
| 65 | 65 |
| 66 void Swap(StructPtr* other) { | 66 void Swap(StructPtr* other) { |
| 67 std::swap(ptr_, other->ptr_); | 67 std::swap(ptr_, other->ptr_); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Please note that calling this method will fail compilation if the value |
| 71 // type |Struct| doesn't have a Clone() method defined (which usually means |
| 72 // that it contains Mojo handles). |
| 73 StructPtr Clone() const { |
| 74 return is_null() ? StructPtr() : ptr_->Clone(); |
| 75 } |
| 76 |
| 70 private: | 77 private: |
| 71 typedef Struct* StructPtr::*Testable; | 78 typedef Struct* StructPtr::*Testable; |
| 72 | 79 |
| 73 public: | 80 public: |
| 74 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } | 81 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } |
| 75 | 82 |
| 76 private: | 83 private: |
| 77 friend class internal::StructHelper<Struct>; | 84 friend class internal::StructHelper<Struct>; |
| 78 void Initialize() { | 85 void Initialize() { |
| 79 MOJO_DCHECK(!ptr_); | 86 MOJO_DCHECK(!ptr_); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 MOJO_DCHECK(!is_null_); | 132 MOJO_DCHECK(!is_null_); |
| 126 return &value_; | 133 return &value_; |
| 127 } | 134 } |
| 128 Struct* get() const { return &value_; } | 135 Struct* get() const { return &value_; } |
| 129 | 136 |
| 130 void Swap(InlinedStructPtr* other) { | 137 void Swap(InlinedStructPtr* other) { |
| 131 std::swap(value_, other->value_); | 138 std::swap(value_, other->value_); |
| 132 std::swap(is_null_, other->is_null_); | 139 std::swap(is_null_, other->is_null_); |
| 133 } | 140 } |
| 134 | 141 |
| 142 InlinedStructPtr Clone() const { |
| 143 return is_null() ? InlinedStructPtr() : value_.Clone(); |
| 144 } |
| 145 |
| 135 private: | 146 private: |
| 136 typedef Struct InlinedStructPtr::*Testable; | 147 typedef Struct InlinedStructPtr::*Testable; |
| 137 | 148 |
| 138 public: | 149 public: |
| 139 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } | 150 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } |
| 140 | 151 |
| 141 private: | 152 private: |
| 142 friend class internal::StructHelper<Struct>; | 153 friend class internal::StructHelper<Struct>; |
| 143 void Initialize() { is_null_ = false; } | 154 void Initialize() { is_null_ = false; } |
| 144 | 155 |
| 145 void Take(InlinedStructPtr* other) { | 156 void Take(InlinedStructPtr* other) { |
| 146 reset(); | 157 reset(); |
| 147 Swap(other); | 158 Swap(other); |
| 148 } | 159 } |
| 149 | 160 |
| 150 mutable Struct value_; | 161 mutable Struct value_; |
| 151 bool is_null_; | 162 bool is_null_; |
| 152 }; | 163 }; |
| 153 | 164 |
| 154 } // namespace mojo | 165 } // namespace mojo |
| 155 | 166 |
| 156 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 167 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
| OLD | NEW |