GenericTypedArrayView.h [plain text]
#ifndef GenericTypedArrayView_h
#define GenericTypedArrayView_h
#include "ArrayBuffer.h"
#include "ArrayBufferView.h"
namespace JSC {
template<typename Adaptor>
class GenericTypedArrayView : public ArrayBufferView {
protected:
GenericTypedArrayView(PassRefPtr<ArrayBuffer>, unsigned byteOffset, unsigned length);
public:
static PassRefPtr<GenericTypedArrayView> create(unsigned length);
static PassRefPtr<GenericTypedArrayView> create(const typename Adaptor::Type* array, unsigned length);
static PassRefPtr<GenericTypedArrayView> create(PassRefPtr<ArrayBuffer>, unsigned byteOffset, unsigned length);
static PassRefPtr<GenericTypedArrayView> createUninitialized(unsigned length);
typename Adaptor::Type* data() const { return static_cast<typename Adaptor::Type*>(baseAddress()); }
bool set(GenericTypedArrayView<Adaptor>* array, unsigned offset)
{
return setImpl(array, offset * sizeof(typename Adaptor::Type));
}
bool setRange(const typename Adaptor::Type* data, size_t dataLength, unsigned offset)
{
return setRangeImpl(
reinterpret_cast<const char*>(data),
dataLength * sizeof(typename Adaptor::Type),
offset * sizeof(typename Adaptor::Type));
}
bool zeroRange(unsigned offset, size_t length)
{
return zeroRangeImpl(offset * sizeof(typename Adaptor::Type), length * sizeof(typename Adaptor::Type));
}
void zeroFill() { zeroRange(0, length()); }
unsigned length() const
{
if (isNeutered())
return 0;
return m_length;
}
virtual unsigned byteLength() const override
{
return length() * sizeof(typename Adaptor::Type);
}
typename Adaptor::Type item(unsigned index) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < this->length());
return data()[index];
}
void set(unsigned index, double value) const
{
ASSERT_WITH_SECURITY_IMPLICATION(index < this->length());
data()[index] = Adaptor::toNativeFromDouble(value);
}
bool checkInboundData(unsigned offset, unsigned pos) const
{
unsigned length = this->length();
return (offset <= length
&& offset + pos <= length
&& offset + pos >= offset);
}
PassRefPtr<GenericTypedArrayView> subarray(int start) const;
PassRefPtr<GenericTypedArrayView> subarray(int start, int end) const;
virtual TypedArrayType getType() const override
{
return Adaptor::typeValue;
}
virtual JSArrayBufferView* wrap(ExecState*, JSGlobalObject*) override;
private:
unsigned m_length;
};
}
#endif // GenericTypedArrayView_h