summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h
blob: 5fa03cb35504a685b89159ff92a32e68ec2f8829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_VIEW_HELPERS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_VIEW_HELPERS_H_

#include <type_traits>

#include "third_party/blink/renderer/core/typed_arrays/dom_array_buffer_view.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/type_traits.h"

namespace blink {

// A wrapper template type that is used to ensure that a TypedArray is not
// backed by a SharedArrayBuffer.  It is usable like a smart pointer.
//
//   void Foo(NotShared<DOMUint32Array> param) {
//     size_t length = param->lengthAsSizeT();
//     ...
//   }
template <typename T>
class NotShared {
  DISALLOW_NEW();
  static_assert(WTF::IsSubclass<typename std::remove_const<T>::type,
                                DOMArrayBufferView>::value,
                "NotShared<T> must have T as subclass of DOMArrayBufferView");

 public:
  using TypedArrayType = T;

  NotShared() = default;
  NotShared(const NotShared<T>& other) = default;
  // Allow implicit upcasts if U inherits from T.
  template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
  NotShared(const NotShared<U>& other) : typed_array_(other.Get()) {}

  explicit NotShared(std::nullptr_t) {}
  explicit NotShared(T* typed_array) : typed_array_(typed_array) {
    DCHECK(!typed_array || !typed_array->IsShared());
  }
  template <typename U>
  explicit NotShared(const Member<U>& other) : typed_array_(other.Get()) {
    DCHECK(!other || !other->IsShared());
  }

  NotShared& operator=(const NotShared& other) = default;
  template <typename U>
  NotShared& operator=(const NotShared<U>& other) {
    typed_array_ = static_cast<T*>(other.Get());
    return *this;
  }

  // |View()| is a legacy API and deprecated.  Use Get() instead.
  T* View() const { return GetRaw(); }
  T* Get() const { return GetRaw(); }
  void Clear() { typed_array_ = nullptr; }

  // Returns true if this object represents IDL null.
  bool IsNull() const { return !GetRaw(); }

  explicit operator bool() const { return GetRaw(); }
  T* operator->() const { return GetRaw(); }
  T& operator*() const { return *GetRaw(); }

  void Trace(Visitor* visitor) { visitor->Trace(typed_array_); }

 private:
  T* GetRaw() const { return typed_array_; }

  Member<T> typed_array_;
};

// A wrapper template type that specifies that a TypedArray *may* be backed by
// a SharedArrayBuffer.  It is usable like a smart pointer.
//
//   void Foo(MaybeShared<DOMUint32Array> param) {
//     DOMArrayBuffer* buffer = param->buffer();
//     ...
//   }
template <typename T>
class MaybeShared {
  DISALLOW_NEW();
  static_assert(WTF::IsSubclass<typename std::remove_const<T>::type,
                                DOMArrayBufferView>::value,
                "MaybeShared<T> must have T as subclass of DOMArrayBufferView");

 public:
  using TypedArrayType = T;

  MaybeShared() = default;
  MaybeShared(const MaybeShared& other) = default;
  // Allow implicit upcasts if U inherits from T.
  template <typename U, std::enable_if_t<std::is_base_of<T, U>::value, int> = 0>
  MaybeShared(const MaybeShared<U>& other) : typed_array_(other.Get()) {}

  explicit MaybeShared(std::nullptr_t) {}
  // [AllowShared] array buffer view may be a view of non-shared array buffer,
  // so we don't check if the buffer is SharedArrayBuffer or not.
  // https://heycam.github.io/webidl/#AllowShared
  explicit MaybeShared(T* typed_array) : typed_array_(typed_array) {}
  template <typename U>
  explicit MaybeShared(const Member<U>& other) : typed_array_(other.Get()) {}

  MaybeShared& operator=(const MaybeShared& other) = default;
  template <typename U>
  MaybeShared& operator=(const MaybeShared<U>& other) {
    typed_array_ = static_cast<T*>(other.Get());
    return *this;
  }

  // |View()| is a legacy API and deprecated.  Use Get() instead.
  T* View() const { return GetRaw(); }
  T* Get() const { return GetRaw(); }
  void Clear() { typed_array_ = nullptr; }

  // Returns true if this object represents IDL null.
  bool IsNull() const { return !GetRaw(); }

  explicit operator bool() const { return GetRaw(); }
  T* operator->() const { return GetRaw(); }
  T& operator*() const { return *GetRaw(); }

  void Trace(Visitor* visitor) { visitor->Trace(typed_array_); }

 private:
  T* GetRaw() const { return typed_array_; }

  Member<T> typed_array_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_TYPED_ARRAYS_ARRAY_BUFFER_VIEW_HELPERS_H_