| Index: ppapi/cpp/dev/typed_array_dev.h
|
| diff --git a/ppapi/cpp/dev/typed_array_dev.h b/ppapi/cpp/dev/typed_array_dev.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..078e9dd9651083f5eea9a467f0f4daa991aad2fa
|
| --- /dev/null
|
| +++ b/ppapi/cpp/dev/typed_array_dev.h
|
| @@ -0,0 +1,139 @@
|
| +// Copyright (c) 2011 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 PPAPI_CPP_DEV_TYPED_ARRAY_DEV_H_
|
| +#define PPAPI_CPP_DEV_TYPED_ARRAY_DEV_H_
|
| +
|
| +#include "ppapi/cpp/dev/array_buffer.h"
|
| +
|
| +namespace pp {
|
| +class ArrayBuffer_Dev;
|
| +class Var;
|
| +}
|
| +
|
| +template <class T>
|
| +class TypedArray_Dev : public ArrayBuffer_Dev {
|
| + public:
|
| + typedef T value_type;
|
| + typedef T* iterator;
|
| + typedef const T* const_iterator;
|
| +
|
| + // Default constructor. Creates an empty TypedArray_Dev.
|
| + TypedArray_Dev();
|
| +
|
| + // Create a TypedArray_Dev that refers to the given Var. Effectively a down-
|
| + // cast.
|
| + explicit TypedArray_Dev(const Var& var);
|
| + explicit TypedArray_Dev(const ArrayBuffer_Dev& buffer);
|
| + // Create a TypedArray_Dev containing the given number of elements, zero-
|
| + // initialized.
|
| + explicit TypedArray_Dev(uint32_t num_elements);
|
| + // Create a TypedArray_Dev containing the given number of elements,
|
| + // initialized to the given value.
|
| + TypedArray_Dev(uint32_t num_elements, const T& value);
|
| + // Create a TypedArray_Dev containing a copy of the given sequence.
|
| + template <class Iterator>
|
| + TypedArray_Dev(const Iterator& begin, const Iterator& end);
|
| +
|
| + iterator begin();
|
| + iterator end();
|
| + const_iterator begin() const;
|
| + const_iterator end() const;
|
| +
|
| + T& operator[](uint32_t index);
|
| + const T& operator[](uint32_t index) const;
|
| +
|
| + uint32_t Length();
|
| +
|
| + virtual ~TypedArray_Dev() {}
|
| + private:
|
| + // Convert count to a size in bytes. If that overflows, return 'default_size'
|
| + // instead.
|
| + static uint32_t CountToByteLengthOrDefault(uint32_t count,
|
| + uint32_t default_size) {
|
| + uint64_t size_in_bytes = count * static_cast<uint64_t>(sizeof(T));
|
| + if (size_in_bytes <= std::numeric_limits<uint32_t>::max())
|
| + return static_cast<uint32_t>(size_in_bytes);
|
| + return size_in_bytes;
|
| + }
|
| +};
|
| +
|
| +TypedArray_Dev::TypedArray_Dev(const Var& var)
|
| + : ArrayBuffer_Dev(var) {
|
| + PP_DCHECK(var.type == PP_VARTYPE_ARRAY_BUFFER);
|
| +}
|
| +
|
| +TypedArray_Dev::TypedArray_Dev(const ArrayBuffer_Dev& buffer)
|
| + : ArrayBuffer(buffer) {
|
| +}
|
| +
|
| +TypedArray_Dev(uint32_t num_elements)
|
| + : ArrayBuffer_Dev(CountToByteLengthOrDefault(num_elements, 0)) {
|
| +}
|
| +
|
| +TypedArray_Dev(uint32_t num_elements, const T& value)
|
| + : ArrayBuffer_Dev(CountToByteLengthOrDefault(num_elements, 0)) {
|
| + T* buffer_iter = static_cast<T*>(GetBuffer());
|
| + T* buffer_end = buffer_iter + num_elments;
|
| + for (; buffer_iter != buffer_end; ++buffer_iter)
|
| + *buffer_iter = value;
|
| +}
|
| +
|
| +// This only works with iterators for which 'operator-' is defined (i.e.,
|
| +// random-access iterators). TODO(dmichael): We could make it work with
|
| +// forward iterators, too, with some template metaprogramming (using iterator
|
| +// traits), falling back to vector-like resizing when we can't determine the
|
| +// size up-front.
|
| +template <class Iterator>
|
| +TypedArray_Dev::TypedArray_Dev(const Iterator& begin, const Iterator& end)
|
| + : ArrayBuffer_Dev(CountToByteLengthOrDefault(end - begin, 0)) {
|
| + T* buffer_iter = static_cast<T*>(GetBuffer());
|
| + for (Iterator input_iter = begin;
|
| + input_iter != end;
|
| + ++input_iter, ++buffer_iter)
|
| + *buffer_iter = *input_iter;
|
| +}
|
| +
|
| +iterator TypedArray_Dev::begin() {
|
| + return static_cast<T*>(GetBuffer());
|
| +}
|
| +iterator TypedArray_Dev::end() {
|
| + return static_cast<T*>(GetBuffer()) + Length();
|
| +}
|
| +const_iterator TypedArray_Dev::begin() const {
|
| + return static_cast<const T*>(GetBuffer());
|
| +}
|
| +const_iterator TypedArray_Dev::end() const {
|
| + return static_cast<const T*>(GetBuffer()) + Length();
|
| +}
|
| +
|
| +T& TypedArray_Dev::operator[](uint32_t index) {
|
| + PP_DCHECK(index < Length());
|
| + return *(static_cast<T*>(GetBuffer()) + index);
|
| +}
|
| +
|
| +const T& TypedArray_Dev::operator[](uint32_t index) const {
|
| + PP_DCHECK(index < Length());
|
| + return *(static_cast<const T*>(GetBuffer()) + index);
|
| +}
|
| +
|
| +uint32_t TypedArray_Dev::Length() const {
|
| + return ByteLength() / sizeof(T);
|
| +}
|
| +
|
| +
|
| +typedef TypedArray_Dev<int8_t> Int8Array;
|
| +typedef TypedArray_Dev<uint8_t> Uint8Array;
|
| +typedef TypedArray_Dev<int16_t> Int16Array;
|
| +typedef TypedArray_Dev<uint16_t> Uint16Array;
|
| +typedef TypedArray_Dev<int32_t> Int32Array;
|
| +typedef TypedArray_Dev<uint32_t> Uint32Array;
|
| +typedef TypedArray_Dev<float_t> Float32Array;
|
| +PP_COMPILE_ASSERT_SIZE_IN_BYTES(float_t, 4);
|
| +typedef TypedArray_Dev<double_t> Float64Array;
|
| +PP_COMPILE_ASSERT_SIZE_IN_BYTES(double_t, 8);
|
| +
|
| +} // namespace pp
|
| +
|
| +#endif // PPAPI_CPP_DEV_TYPED_ARRAY_DEV_H_
|
|
|