| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * Copyright (c) 2010, Google Inc. All rights reserved. | 3 // found in the LICENSE file. |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | 4 |
| 27 #ifndef TypedArrayBase_h | 5 #include "platform/wtf/typed_arrays/TypedArrayBase.h" |
| 28 #define TypedArrayBase_h | |
| 29 | 6 |
| 30 #include "wtf/typed_arrays/ArrayBuffer.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 31 #include "wtf/typed_arrays/ArrayBufferView.h" | 8 // WTF migration project. See the following post for details: |
| 32 | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 33 namespace WTF { | |
| 34 | |
| 35 template <typename T> | |
| 36 class TypedArrayBase : public ArrayBufferView { | |
| 37 public: | |
| 38 typedef T ValueType; | |
| 39 | |
| 40 T* data() const { return static_cast<T*>(baseAddress()); } | |
| 41 | |
| 42 bool set(TypedArrayBase<T>* array, unsigned offset) { | |
| 43 return setImpl(array, offset * sizeof(T)); | |
| 44 } | |
| 45 | |
| 46 // Overridden from ArrayBufferView. This must be public because of | |
| 47 // rules about inheritance of members in template classes, and | |
| 48 // because it is accessed via pointers to subclasses. | |
| 49 unsigned length() const { return m_length; } | |
| 50 | |
| 51 unsigned byteLength() const final { return m_length * sizeof(T); } | |
| 52 | |
| 53 unsigned typeSize() const final { return sizeof(T); } | |
| 54 | |
| 55 // Invoked by the indexed getter. Does not perform range checks; caller | |
| 56 // is responsible for doing so and returning undefined as necessary. | |
| 57 T item(unsigned index) const { | |
| 58 SECURITY_DCHECK(index < TypedArrayBase<T>::m_length); | |
| 59 return TypedArrayBase<T>::data()[index]; | |
| 60 } | |
| 61 | |
| 62 protected: | |
| 63 TypedArrayBase(PassRefPtr<ArrayBuffer> buffer, | |
| 64 unsigned byteOffset, | |
| 65 unsigned length) | |
| 66 : ArrayBufferView(std::move(buffer), byteOffset), m_length(length) {} | |
| 67 | |
| 68 template <class Subclass> | |
| 69 static PassRefPtr<Subclass> create(unsigned length) { | |
| 70 RefPtr<ArrayBuffer> buffer = ArrayBuffer::create(length, sizeof(T)); | |
| 71 return create<Subclass>(buffer.release(), 0, length); | |
| 72 } | |
| 73 | |
| 74 template <class Subclass> | |
| 75 static PassRefPtr<Subclass> create(const T* array, unsigned length) { | |
| 76 RefPtr<Subclass> a = create<Subclass>(length); | |
| 77 if (a) | |
| 78 for (unsigned i = 0; i < length; ++i) | |
| 79 a->set(i, array[i]); | |
| 80 return a; | |
| 81 } | |
| 82 | |
| 83 template <class Subclass> | |
| 84 static PassRefPtr<Subclass> create(PassRefPtr<ArrayBuffer> buffer, | |
| 85 unsigned byteOffset, | |
| 86 unsigned length) { | |
| 87 RefPtr<ArrayBuffer> buf(std::move(buffer)); | |
| 88 RELEASE_ASSERT(verifySubRange<T>(buf, byteOffset, length)); | |
| 89 return adoptRef(new Subclass(buf.release(), byteOffset, length)); | |
| 90 } | |
| 91 | |
| 92 template <class Subclass> | |
| 93 static PassRefPtr<Subclass> createOrNull(unsigned length) { | |
| 94 RefPtr<ArrayBuffer> buffer = ArrayBuffer::createOrNull(length, sizeof(T)); | |
| 95 if (!buffer) | |
| 96 return nullptr; | |
| 97 return create<Subclass>(buffer.release(), 0, length); | |
| 98 } | |
| 99 | |
| 100 void neuter() final { | |
| 101 ArrayBufferView::neuter(); | |
| 102 m_length = 0; | |
| 103 } | |
| 104 | |
| 105 // We do not want to have to access this via a virtual function in subclasses, | |
| 106 // which is why it is protected rather than private. | |
| 107 unsigned m_length; | |
| 108 }; | |
| 109 | |
| 110 } // namespace WTF | |
| 111 | |
| 112 using WTF::TypedArrayBase; | |
| 113 | |
| 114 #endif // TypedArrayBase_h | |
| OLD | NEW |