OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
| 7 |
| 8 #include <assert.h> |
| 9 #include <stdlib.h> |
| 10 |
| 11 #include <new> |
| 12 |
| 13 #include "mojo/public/cpp/system/macros.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace internal { |
| 17 |
| 18 template <typename Struct> |
| 19 class StructHelper { |
| 20 public: |
| 21 template <typename Ptr> |
| 22 static void Initialize(Ptr* ptr) { ptr->Initialize(); } |
| 23 }; |
| 24 |
| 25 } // namespace internal |
| 26 |
| 27 template <typename Struct> |
| 28 class StructPtr { |
| 29 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(StructPtr, RValue); |
| 30 public: |
| 31 StructPtr() : ptr_(NULL) {} |
| 32 ~StructPtr() { |
| 33 delete ptr_; |
| 34 } |
| 35 |
| 36 StructPtr(RValue other) : ptr_(NULL) { Take(other.object); } |
| 37 StructPtr& operator=(RValue other) { |
| 38 Take(other.object); |
| 39 return *this; |
| 40 } |
| 41 |
| 42 template <typename U> |
| 43 U To() const { |
| 44 return TypeConverter<StructPtr, U>::ConvertTo(*this); |
| 45 } |
| 46 |
| 47 void reset() { |
| 48 if (ptr_) { |
| 49 delete ptr_; |
| 50 ptr_ = NULL; |
| 51 } |
| 52 } |
| 53 |
| 54 bool is_null() const { return ptr_ == NULL; } |
| 55 |
| 56 Struct& operator*() const { |
| 57 assert(ptr_); |
| 58 return *ptr_; |
| 59 } |
| 60 Struct* operator->() const { |
| 61 assert(ptr_); |
| 62 return ptr_; |
| 63 } |
| 64 Struct* get() const { return ptr_; } |
| 65 |
| 66 void Swap(StructPtr* other) { |
| 67 std::swap(ptr_, other->ptr_); |
| 68 } |
| 69 |
| 70 private: |
| 71 typedef Struct* StructPtr::*Testable; |
| 72 |
| 73 public: |
| 74 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } |
| 75 |
| 76 private: |
| 77 friend class internal::StructHelper<Struct>; |
| 78 void Initialize() { assert(!ptr_); ptr_ = new Struct(); } |
| 79 |
| 80 void Take(StructPtr* other) { |
| 81 reset(); |
| 82 Swap(other); |
| 83 } |
| 84 |
| 85 Struct* ptr_; |
| 86 }; |
| 87 |
| 88 template <typename Struct> |
| 89 class InlinedStructPtr { |
| 90 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(InlinedStructPtr, RValue); |
| 91 public: |
| 92 InlinedStructPtr() : is_null_(true) {} |
| 93 ~InlinedStructPtr() {} |
| 94 |
| 95 InlinedStructPtr(RValue other) : is_null_(true) { Take(other.object); } |
| 96 InlinedStructPtr& operator=(RValue other) { |
| 97 Take(other.object); |
| 98 return *this; |
| 99 } |
| 100 |
| 101 template <typename U> |
| 102 U To() const { |
| 103 return TypeConverter<InlinedStructPtr, U>::ConvertTo(*this); |
| 104 } |
| 105 |
| 106 void reset() { |
| 107 is_null_ = true; |
| 108 value_.~Struct(); |
| 109 new (&value_) Struct(); |
| 110 } |
| 111 |
| 112 bool is_null() const { return is_null_; } |
| 113 |
| 114 Struct& operator*() const { |
| 115 assert(!is_null_); |
| 116 return value_; |
| 117 } |
| 118 Struct* operator->() const { |
| 119 assert(!is_null_); |
| 120 return &value_; |
| 121 } |
| 122 Struct* get() const { return &value_; } |
| 123 |
| 124 void Swap(InlinedStructPtr* other) { |
| 125 char buf[sizeof(Struct)]; |
| 126 memcpy(buf, &value_, sizeof(value_)); |
| 127 memcpy(&value_, &other->value_, sizeof(value_)); |
| 128 memcpy(&other->value_, buf, sizeof(value_)); |
| 129 std::swap(is_null_, other->is_null_); |
| 130 } |
| 131 |
| 132 private: |
| 133 typedef Struct InlinedStructPtr::*Testable; |
| 134 |
| 135 public: |
| 136 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } |
| 137 |
| 138 private: |
| 139 friend class internal::StructHelper<Struct>; |
| 140 void Initialize() { is_null_ = false; } |
| 141 |
| 142 void Take(InlinedStructPtr* other) { |
| 143 reset(); |
| 144 Swap(other); |
| 145 } |
| 146 |
| 147 mutable Struct value_; |
| 148 bool is_null_; |
| 149 }; |
| 150 |
| 151 #if 0 |
| 152 template <typename Struct, bool use_heap = (sizeof(Struct) > 28)> |
| 153 struct SelectStructPtr; |
| 154 |
| 155 template <typename Struct> |
| 156 struct SelectStructPtr<Struct, true> { |
| 157 typedef StructPtr<Struct> type; |
| 158 }; |
| 159 |
| 160 template <typename Struct> |
| 161 struct SelectStructPtr<Struct, false> { |
| 162 typedef InlinedStructPtr<Struct> type; |
| 163 }; |
| 164 #endif |
| 165 |
| 166 } // namespace mojo |
| 167 |
| 168 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
OLD | NEW |