Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 ArrayBufferViewHelpers_h | |
| 6 #define ArrayBufferViewHelpers_h | |
| 7 | |
| 8 #include <type_traits> | |
| 9 #include "core/dom/DOMArrayBufferView.h" | |
| 10 #include "platform/heap/Handle.h" | |
| 11 #include "platform/wtf/TypeTraits.h" | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 template <typename T> | |
| 16 class NotShared { | |
| 17 // A wrapper template type that is used to ensure that a TypedArray is not | |
|
haraken
2017/04/13 04:51:12
Nit: Move the comment to above the class declarati
binji
2017/04/13 18:41:59
Done.
| |
| 18 // backed by a SharedArrayBuffer. | |
| 19 // | |
| 20 // Typically this is used as an annotation on C++ functions that are called by | |
| 21 // the bindings layer, e.g.: | |
| 22 // | |
| 23 // void Foo(NotShared<DOMUint32Array> param) { | |
| 24 // DOMUint32Array* array = param.View(); | |
| 25 // ... | |
| 26 // } | |
| 27 static_assert(WTF::IsSubclass<typename std::remove_const<T>::type, | |
| 28 DOMArrayBufferView>::value, | |
| 29 "NotShared<T> must have T as subclass of DOMArrayBufferView"); | |
| 30 STACK_ALLOCATED(); | |
| 31 | |
| 32 public: | |
| 33 using TypedArrayType = T; | |
| 34 | |
| 35 NotShared() {} | |
| 36 | |
| 37 explicit NotShared(T* typedArray) : typed_array_(typedArray) { | |
| 38 DCHECK(!(typedArray && typedArray->View()->IsShared())); | |
| 39 } | |
| 40 NotShared(const NotShared& other) = default; | |
| 41 template <typename U> | |
| 42 NotShared(const NotShared<U>& other) : typed_array_(other.View()) {} | |
| 43 template <typename U> | |
| 44 NotShared(const Member<U>& other) { | |
| 45 DCHECK(!other->View()->IsShared()); | |
| 46 typed_array_ = other.Get(); | |
| 47 } | |
| 48 | |
| 49 NotShared& operator=(const NotShared& other) = default; | |
| 50 template <typename U> | |
| 51 NotShared& operator=(const NotShared<U>& other) { | |
| 52 typed_array_ = other.View(); | |
| 53 return *this; | |
| 54 } | |
| 55 | |
| 56 T* View() const { return typed_array_.Get(); } | |
| 57 | |
| 58 bool operator!() const { return !typed_array_; } | |
| 59 explicit operator bool() const { return !!typed_array_; } | |
| 60 | |
| 61 private: | |
| 62 // Must use an untraced member here since this object may be constructed on a | |
| 63 // thread without a ThreadState (e.g. an Audio worklet). It is safe in that | |
| 64 // case because the pointed-to ArrayBuffer is being kept alive another way | |
| 65 // (e.g. CrossThreadPersistent). | |
| 66 // | |
| 67 // TODO(binji): update to using Member, see crbug.com/710295. | |
| 68 UntracedMember<T> typed_array_; | |
| 69 }; | |
| 70 | |
| 71 template <typename T> | |
| 72 class MaybeShared { | |
| 73 // A wrapper template type that specifies that a TypedArray may be backed by a | |
|
haraken
2017/04/13 04:51:12
Ditto.
binji
2017/04/13 18:41:59
Done.
| |
| 74 // SharedArrayBuffer. | |
| 75 // | |
| 76 // Typically this is used as an annotation on C++ functions that are called by | |
| 77 // the bindings layer, e.g.: | |
| 78 // | |
| 79 // void Foo(MaybeShared<DOMUint32Array> param) { | |
| 80 // DOMUint32Array* array = param.View(); | |
| 81 // ... | |
| 82 // } | |
| 83 static_assert(WTF::IsSubclass<typename std::remove_const<T>::type, | |
| 84 DOMArrayBufferView>::value, | |
| 85 "MaybeShared<T> must have T as subclass of DOMArrayBufferView"); | |
| 86 STACK_ALLOCATED(); | |
| 87 | |
| 88 public: | |
| 89 using TypedArrayType = T; | |
| 90 | |
| 91 MaybeShared() {} | |
| 92 | |
| 93 explicit MaybeShared(T* typedArray) : typed_array_(typedArray) {} | |
| 94 MaybeShared(const MaybeShared& other) = default; | |
| 95 template <typename U> | |
| 96 MaybeShared(const MaybeShared<U>& other) : typed_array_(other.View()) {} | |
| 97 template <typename U> | |
| 98 MaybeShared(const Member<U>& other) { | |
| 99 typed_array_ = other.Get(); | |
| 100 } | |
| 101 | |
| 102 MaybeShared& operator=(const MaybeShared& other) = default; | |
| 103 template <typename U> | |
| 104 MaybeShared& operator=(const MaybeShared<U>& other) { | |
| 105 typed_array_ = other.View(); | |
| 106 return *this; | |
| 107 } | |
| 108 | |
| 109 T* View() const { return typed_array_.Get(); } | |
| 110 | |
| 111 bool operator!() const { return !typed_array_; } | |
| 112 explicit operator bool() const { return !!typed_array_; } | |
| 113 | |
| 114 private: | |
| 115 Member<T> typed_array_; | |
| 116 }; | |
| 117 | |
| 118 } // namespace blink | |
| 119 | |
| 120 #endif // ArrayBufferViewHelpers_h | |
| OLD | NEW |