Chromium Code Reviews| 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 <functional> | |
| 8 #include <new> | 9 #include <new> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "mojo/public/cpp/bindings/lib/hash_util.h" | |
| 12 #include "mojo/public/cpp/bindings/type_converter.h" | 14 #include "mojo/public/cpp/bindings/type_converter.h" |
| 13 | 15 |
| 14 namespace mojo { | 16 namespace mojo { |
| 15 namespace internal { | 17 namespace internal { |
| 16 | 18 |
| 19 constexpr size_t kHashSeed = 31; | |
| 20 | |
| 17 template <typename Struct> | 21 template <typename Struct> |
| 18 class StructHelper { | 22 class StructHelper { |
| 19 public: | 23 public: |
| 20 template <typename Ptr> | 24 template <typename Ptr> |
| 21 static void Initialize(Ptr* ptr) { | 25 static void Initialize(Ptr* ptr) { |
| 22 ptr->Initialize(); | 26 ptr->Initialize(); |
| 23 } | 27 } |
| 24 }; | 28 }; |
| 25 | 29 |
| 26 } // namespace internal | 30 } // namespace internal |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 } | 75 } |
| 72 Struct* get() const { return ptr_; } | 76 Struct* get() const { return ptr_; } |
| 73 | 77 |
| 74 void Swap(StructPtr* other) { std::swap(ptr_, other->ptr_); } | 78 void Swap(StructPtr* other) { std::swap(ptr_, other->ptr_); } |
| 75 | 79 |
| 76 // Please note that calling this method will fail compilation if the value | 80 // Please note that calling this method will fail compilation if the value |
| 77 // type |Struct| doesn't have a Clone() method defined (which usually means | 81 // type |Struct| doesn't have a Clone() method defined (which usually means |
| 78 // that it contains Mojo handles). | 82 // that it contains Mojo handles). |
| 79 StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); } | 83 StructPtr Clone() const { return is_null() ? StructPtr() : ptr_->Clone(); } |
| 80 | 84 |
| 85 // Compares the pointees (which might both be null). | |
| 81 bool Equals(const StructPtr& other) const { | 86 bool Equals(const StructPtr& other) const { |
| 82 if (is_null() || other.is_null()) | 87 if (is_null() || other.is_null()) |
| 83 return is_null() && other.is_null(); | 88 return is_null() && other.is_null(); |
| 84 return ptr_->Equals(*other.ptr_); | 89 return ptr_->Equals(*other.ptr_); |
| 85 } | 90 } |
| 86 | 91 |
| 87 private: | 92 // Hashes based on the pointee (which might be null). |
| 88 // TODO(dcheng): Use an explicit conversion operator. | 93 size_t Hash(size_t seed) const { |
| 89 typedef Struct* StructPtr::*Testable; | 94 if (is_null()) |
| 95 return internal::HashCombine(seed, 0); | |
| 96 return ptr_->Hash(seed); | |
| 97 } | |
| 90 | 98 |
| 91 public: | 99 // For use in WTF::HashMap only: |
| 92 operator Testable() const { return ptr_ ? &StructPtr::ptr_ : 0; } | 100 bool isHashTableDeletedValue() const { |
| 101 return ptr_ == reinterpret_cast<Struct*>(1u); | |
| 102 } | |
| 103 | |
| 104 // For use in WTF::HashMap only: | |
| 105 static void constructDeletedValue(mojo::StructPtr<S>& slot) { | |
| 106 // Dirty trick: implant an invalid pointer in |ptr_|. Destructor isn't | |
| 107 // called for deleted buckets, so this is okay. | |
| 108 new (&slot) StructPtr(); | |
| 109 slot.ptr_ = reinterpret_cast<Struct*>(1u); | |
| 110 } | |
| 111 | |
| 112 template <typename T> | |
| 113 bool operator==(const StructPtr<T>& other) const { | |
|
yzshen1
2016/09/21 23:17:53
Something to think about: If we decide to have "==
tibell
2016/09/22 05:17:23
I added a TODO. Both Sam and I want to get rid of
| |
| 114 return this->Equals(other); | |
| 115 } | |
| 116 template <typename T> | |
| 117 bool operator!=(const StructPtr<T>& other) const { | |
| 118 return !(this->Equals(other)); | |
| 119 } | |
| 120 | |
| 121 explicit operator bool() const { return !is_null(); } | |
| 93 | 122 |
| 94 private: | 123 private: |
| 95 friend class internal::StructHelper<Struct>; | 124 friend class internal::StructHelper<Struct>; |
| 96 | 125 |
| 97 // Forbid the == and != operators explicitly, otherwise StructPtr will be | |
| 98 // converted to Testable to do == or != comparison. | |
| 99 template <typename T> | |
| 100 bool operator==(const StructPtr<T>& other) const = delete; | |
| 101 template <typename T> | |
| 102 bool operator!=(const StructPtr<T>& other) const = delete; | |
| 103 | |
| 104 void Initialize() { | 126 void Initialize() { |
| 105 DCHECK(!ptr_); | 127 DCHECK(!ptr_); |
| 106 ptr_ = new Struct(); | 128 ptr_ = new Struct(); |
| 107 } | 129 } |
| 108 | 130 |
| 109 void Take(StructPtr* other) { | 131 void Take(StructPtr* other) { |
| 110 reset(); | 132 reset(); |
| 111 Swap(other); | 133 Swap(other); |
| 112 } | 134 } |
| 113 | 135 |
| 114 Struct* ptr_; | 136 Struct* ptr_; |
| 115 | 137 |
| 116 DISALLOW_COPY_AND_ASSIGN(StructPtr); | 138 DISALLOW_COPY_AND_ASSIGN(StructPtr); |
| 117 }; | 139 }; |
| 118 | 140 |
| 119 // Designed to be used when Struct is small and copyable. | 141 // Designed to be used when Struct is small and copyable. |
| 120 template <typename S> | 142 template <typename S> |
| 121 class InlinedStructPtr { | 143 class InlinedStructPtr { |
| 122 public: | 144 public: |
| 123 using Struct = S; | 145 using Struct = S; |
| 124 | 146 |
| 125 InlinedStructPtr() : is_null_(true) {} | 147 InlinedStructPtr() : state_(NIL) {} |
| 126 InlinedStructPtr(decltype(nullptr)) : is_null_(true) {} | 148 InlinedStructPtr(decltype(nullptr)) : state_(NIL) {} |
| 127 | 149 |
| 128 ~InlinedStructPtr() {} | 150 ~InlinedStructPtr() {} |
| 129 | 151 |
| 130 InlinedStructPtr& operator=(decltype(nullptr)) { | 152 InlinedStructPtr& operator=(decltype(nullptr)) { |
| 131 reset(); | 153 reset(); |
| 132 return *this; | 154 return *this; |
| 133 } | 155 } |
| 134 | 156 |
| 135 InlinedStructPtr(InlinedStructPtr&& other) : is_null_(true) { Take(&other); } | 157 InlinedStructPtr(InlinedStructPtr&& other) : state_(NIL) { Take(&other); } |
| 136 InlinedStructPtr& operator=(InlinedStructPtr&& other) { | 158 InlinedStructPtr& operator=(InlinedStructPtr&& other) { |
| 137 Take(&other); | 159 Take(&other); |
| 138 return *this; | 160 return *this; |
| 139 } | 161 } |
| 140 | 162 |
| 141 template <typename U> | 163 template <typename U> |
| 142 U To() const { | 164 U To() const { |
| 143 return TypeConverter<U, InlinedStructPtr>::Convert(*this); | 165 return TypeConverter<U, InlinedStructPtr>::Convert(*this); |
| 144 } | 166 } |
| 145 | 167 |
| 146 void reset() { | 168 void reset() { |
| 147 is_null_ = true; | 169 state_ = NIL; |
| 148 value_. ~Struct(); | 170 value_. ~Struct(); |
| 149 new (&value_) Struct(); | 171 new (&value_) Struct(); |
| 150 } | 172 } |
| 151 | 173 |
| 152 bool is_null() const { return is_null_; } | 174 bool is_null() const { return state_ == NIL; } |
| 153 | 175 |
| 154 Struct& operator*() const { | 176 Struct& operator*() const { |
| 155 DCHECK(!is_null_); | 177 DCHECK(state_ == VALID); |
| 156 return value_; | 178 return value_; |
| 157 } | 179 } |
| 158 Struct* operator->() const { | 180 Struct* operator->() const { |
| 159 DCHECK(!is_null_); | 181 DCHECK(state_ == VALID); |
| 160 return &value_; | 182 return &value_; |
| 161 } | 183 } |
| 162 Struct* get() const { return &value_; } | 184 Struct* get() const { return &value_; } |
| 163 | 185 |
| 164 void Swap(InlinedStructPtr* other) { | 186 void Swap(InlinedStructPtr* other) { |
| 165 std::swap(value_, other->value_); | 187 std::swap(value_, other->value_); |
| 166 std::swap(is_null_, other->is_null_); | 188 std::swap(state_, other->state_); |
| 167 } | 189 } |
| 168 | 190 |
| 169 InlinedStructPtr Clone() const { | 191 InlinedStructPtr Clone() const { |
| 170 return is_null() ? InlinedStructPtr() : value_.Clone(); | 192 return is_null() ? InlinedStructPtr() : value_.Clone(); |
| 171 } | 193 } |
| 194 | |
| 195 // Compares the pointees (which might both be null). | |
| 172 bool Equals(const InlinedStructPtr& other) const { | 196 bool Equals(const InlinedStructPtr& other) const { |
| 173 if (is_null() || other.is_null()) | 197 if (is_null() || other.is_null()) |
| 174 return is_null() && other.is_null(); | 198 return is_null() && other.is_null(); |
| 175 return value_.Equals(other.value_); | 199 return value_.Equals(other.value_); |
| 176 } | 200 } |
| 177 | 201 |
| 178 private: | 202 // Hashes based on the pointee (which might be null). |
| 179 // TODO(dcheng): Use an explicit conversion operator. | 203 size_t Hash(size_t seed) const { |
| 180 typedef Struct InlinedStructPtr::*Testable; | 204 if (is_null()) |
| 205 return internal::HashCombine(seed, 0); | |
| 206 return value_.Hash(seed); | |
| 207 } | |
| 181 | 208 |
| 182 public: | 209 // For use in WTF::HashMap only: |
| 183 operator Testable() const { return is_null_ ? 0 : &InlinedStructPtr::value_; } | 210 bool isHashTableDeletedValue() const { return state_ == DELETED; } |
| 211 | |
| 212 // For use in WTF::HashMap only: | |
| 213 static void constructDeletedValue(mojo::InlinedStructPtr<S>& slot) { | |
| 214 new (&slot) InlinedStructPtr(); | |
|
yzshen1
2016/09/21 23:17:52
Is |slot|'s constructor already called before pass
tibell
2016/09/22 05:17:23
|slot| referred to a previous, real value that got
yzshen1
2016/09/22 23:48:56
Please add a comment about this to the correspondi
| |
| 215 slot.state_ = DELETED; | |
| 216 } | |
| 217 | |
| 218 template <typename T> | |
| 219 bool operator==(const InlinedStructPtr<T>& other) const { | |
| 220 return this->Equals(other); | |
| 221 } | |
| 222 template <typename T> | |
| 223 bool operator!=(const InlinedStructPtr<T>& other) const { | |
| 224 return !(this->Equals(other)); | |
| 225 } | |
| 226 | |
| 227 explicit operator bool() const { return !is_null(); } | |
| 184 | 228 |
| 185 private: | 229 private: |
| 186 friend class internal::StructHelper<Struct>; | 230 friend class internal::StructHelper<Struct>; |
| 187 | 231 |
| 188 // Forbid the == and != operators explicitly, otherwise InlinedStructPtr will | 232 void Initialize() { state_ = VALID; } |
| 189 // be converted to Testable to do == or != comparison. | |
| 190 template <typename T> | |
| 191 bool operator==(const InlinedStructPtr<T>& other) const = delete; | |
| 192 template <typename T> | |
| 193 bool operator!=(const InlinedStructPtr<T>& other) const = delete; | |
| 194 | |
| 195 void Initialize() { is_null_ = false; } | |
| 196 | 233 |
| 197 void Take(InlinedStructPtr* other) { | 234 void Take(InlinedStructPtr* other) { |
| 198 reset(); | 235 reset(); |
| 199 Swap(other); | 236 Swap(other); |
| 200 } | 237 } |
| 201 | 238 |
| 239 enum State { | |
| 240 VALID, | |
| 241 NIL, | |
| 242 DELETED, // For use in WTF::HashMap only | |
| 243 }; | |
| 244 | |
| 202 mutable Struct value_; | 245 mutable Struct value_; |
| 203 bool is_null_; | 246 State state_; |
| 204 | 247 |
| 205 DISALLOW_COPY_AND_ASSIGN(InlinedStructPtr); | 248 DISALLOW_COPY_AND_ASSIGN(InlinedStructPtr); |
| 206 }; | 249 }; |
| 207 | 250 |
| 208 } // namespace mojo | 251 } // namespace mojo |
| 209 | 252 |
| 253 namespace std { | |
| 254 | |
| 255 template <typename T> | |
| 256 struct hash<mojo::StructPtr<T>> { | |
| 257 size_t operator()(const mojo::StructPtr<T>& value) const { | |
| 258 return value.Hash(mojo::internal::kHashSeed); | |
| 259 } | |
| 260 }; | |
| 261 | |
| 262 template <typename T> | |
| 263 struct hash<mojo::InlinedStructPtr<T>> { | |
| 264 size_t operator()(const mojo::InlinedStructPtr<T>& value) const { | |
| 265 return value.Hash(mojo::internal::kHashSeed); | |
| 266 } | |
| 267 }; | |
| 268 | |
| 269 } // namespace std | |
| 270 | |
| 210 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ | 271 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_PTR_H_ |
| OLD | NEW |