| 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 <assert.h> | |
| 9 | |
| 10 #include <new> | 8 #include <new> |
| 11 | 9 |
| 10 #include "mojo/public/cpp/environment/logging.h" |
| 12 #include "mojo/public/cpp/system/macros.h" | 11 #include "mojo/public/cpp/system/macros.h" |
| 13 | 12 |
| 14 namespace mojo { | 13 namespace mojo { |
| 15 namespace internal { | 14 namespace internal { |
| 16 | 15 |
| 17 template <typename Struct> | 16 template <typename Struct> |
| 18 class StructHelper { | 17 class StructHelper { |
| 19 public: | 18 public: |
| 20 template <typename Ptr> | 19 template <typename Ptr> |
| 21 static void Initialize(Ptr* ptr) { ptr->Initialize(); } | 20 static void Initialize(Ptr* ptr) { ptr->Initialize(); } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 48 void reset() { | 47 void reset() { |
| 49 if (ptr_) { | 48 if (ptr_) { |
| 50 delete ptr_; | 49 delete ptr_; |
| 51 ptr_ = NULL; | 50 ptr_ = NULL; |
| 52 } | 51 } |
| 53 } | 52 } |
| 54 | 53 |
| 55 bool is_null() const { return ptr_ == NULL; } | 54 bool is_null() const { return ptr_ == NULL; } |
| 56 | 55 |
| 57 Struct& operator*() const { | 56 Struct& operator*() const { |
| 58 assert(ptr_); | 57 MOJO_DCHECK(ptr_); |
| 59 return *ptr_; | 58 return *ptr_; |
| 60 } | 59 } |
| 61 Struct* operator->() const { | 60 Struct* operator->() const { |
| 62 assert(ptr_); | 61 MOJO_DCHECK(ptr_); |
| 63 return ptr_; | 62 return ptr_; |
| 64 } | 63 } |
| 65 Struct* get() const { return ptr_; } | 64 Struct* get() const { return ptr_; } |
| 66 | 65 |
| 67 void Swap(StructPtr* other) { | 66 void Swap(StructPtr* other) { |
| 68 std::swap(ptr_, other->ptr_); | 67 std::swap(ptr_, other->ptr_); |
| 69 } | 68 } |
| 70 | 69 |
| 71 private: | 70 private: |
| 72 typedef Struct* StructPtr::*Testable; | 71 typedef Struct* StructPtr::*Testable; |
| 73 | 72 |
| 74 public: | 73 public: |
| 75 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } | 74 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } |
| 76 | 75 |
| 77 private: | 76 private: |
| 78 friend class internal::StructHelper<Struct>; | 77 friend class internal::StructHelper<Struct>; |
| 79 void Initialize() { assert(!ptr_); ptr_ = new Struct(); } | 78 void Initialize() { |
| 79 MOJO_DCHECK(!ptr_); |
| 80 ptr_ = new Struct(); |
| 81 } |
| 80 | 82 |
| 81 void Take(StructPtr* other) { | 83 void Take(StructPtr* other) { |
| 82 reset(); | 84 reset(); |
| 83 Swap(other); | 85 Swap(other); |
| 84 } | 86 } |
| 85 | 87 |
| 86 Struct* ptr_; | 88 Struct* ptr_; |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 // Designed to be used when Struct is small and copyable. | 91 // Designed to be used when Struct is small and copyable. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 109 | 111 |
| 110 void reset() { | 112 void reset() { |
| 111 is_null_ = true; | 113 is_null_ = true; |
| 112 value_.~Struct(); | 114 value_.~Struct(); |
| 113 new (&value_) Struct(); | 115 new (&value_) Struct(); |
| 114 } | 116 } |
| 115 | 117 |
| 116 bool is_null() const { return is_null_; } | 118 bool is_null() const { return is_null_; } |
| 117 | 119 |
| 118 Struct& operator*() const { | 120 Struct& operator*() const { |
| 119 assert(!is_null_); | 121 MOJO_DCHECK(!is_null_); |
| 120 return value_; | 122 return value_; |
| 121 } | 123 } |
| 122 Struct* operator->() const { | 124 Struct* operator->() const { |
| 123 assert(!is_null_); | 125 MOJO_DCHECK(!is_null_); |
| 124 return &value_; | 126 return &value_; |
| 125 } | 127 } |
| 126 Struct* get() const { return &value_; } | 128 Struct* get() const { return &value_; } |
| 127 | 129 |
| 128 void Swap(InlinedStructPtr* other) { | 130 void Swap(InlinedStructPtr* other) { |
| 129 std::swap(value_, other->value_); | 131 std::swap(value_, other->value_); |
| 130 std::swap(is_null_, other->is_null_); | 132 std::swap(is_null_, other->is_null_); |
| 131 } | 133 } |
| 132 | 134 |
| 133 private: | 135 private: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 145 Swap(other); | 147 Swap(other); |
| 146 } | 148 } |
| 147 | 149 |
| 148 mutable Struct value_; | 150 mutable Struct value_; |
| 149 bool is_null_; | 151 bool is_null_; |
| 150 }; | 152 }; |
| 151 | 153 |
| 152 } // namespace mojo | 154 } // namespace mojo |
| 153 | 155 |
| 154 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 156 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
| OLD | NEW |