1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
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 AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef DataView_h
27#define DataView_h
28
29#include <wtf/ArrayBufferView.h>
30#include <wtf/PassRefPtr.h>
31
32namespace WebCore {
33
34typedef int ExceptionCode;
35
36class DataView : public ArrayBufferView {
37public:
38    static PassRefPtr<DataView> create(unsigned length);
39    static PassRefPtr<DataView> create(PassRefPtr<ArrayBuffer>, unsigned byteOffset, unsigned byteLength);
40
41    virtual unsigned length() const { return m_byteLength; }
42    virtual unsigned byteLength() const { return m_byteLength; }
43    virtual PassRefPtr<ArrayBufferView> slice(int, int) const { return 0; }
44
45    int8_t getInt8(unsigned byteOffset, ExceptionCode&);
46    uint8_t getUint8(unsigned byteOffset, ExceptionCode&);
47    int16_t getInt16(unsigned byteOffset, ExceptionCode& ec) { return getInt16(byteOffset, false, ec); }
48    int16_t getInt16(unsigned byteOffset, bool littleEndian, ExceptionCode&);
49    uint16_t getUint16(unsigned byteOffset, ExceptionCode& ec) { return getUint16(byteOffset, false, ec); }
50    uint16_t getUint16(unsigned byteOffset, bool littleEndian, ExceptionCode&);
51    int32_t getInt32(unsigned byteOffset, ExceptionCode& ec) { return getInt32(byteOffset, false, ec); }
52    int32_t getInt32(unsigned byteOffset, bool littleEndian, ExceptionCode&);
53    uint32_t getUint32(unsigned byteOffset, ExceptionCode& ec) { return getUint32(byteOffset, false, ec); }
54    uint32_t getUint32(unsigned byteOffset, bool littleEndian, ExceptionCode&);
55    float getFloat32(unsigned byteOffset, ExceptionCode& ec) { return getFloat32(byteOffset, false, ec); }
56    float getFloat32(unsigned byteOffset, bool littleEndian, ExceptionCode&);
57    double getFloat64(unsigned byteOffset, ExceptionCode& ec) { return getFloat64(byteOffset, false, ec); }
58    double getFloat64(unsigned byteOffset, bool littleEndian, ExceptionCode&);
59
60    void setInt8(unsigned byteOffset, int8_t value, ExceptionCode&);
61    void setUint8(unsigned byteOffset, uint8_t value, ExceptionCode&);
62    void setInt16(unsigned byteOffset, int16_t value, ExceptionCode& ec) { setInt16(byteOffset, value, false, ec); }
63    void setInt16(unsigned byteOffset, int16_t value, bool littleEndian, ExceptionCode&);
64    void setUint16(unsigned byteOffset, uint16_t value, ExceptionCode& ec) { setUint16(byteOffset, value, false, ec); }
65    void setUint16(unsigned byteOffset, uint16_t value, bool littleEndian, ExceptionCode&);
66    void setInt32(unsigned byteOffset, int32_t value, ExceptionCode& ec) { setInt32(byteOffset, value, false, ec); }
67    void setInt32(unsigned byteOffset, int32_t value, bool littleEndian, ExceptionCode&);
68    void setUint32(unsigned byteOffset, uint32_t value, ExceptionCode& ec) { setUint32(byteOffset, value, false, ec); }
69    void setUint32(unsigned byteOffset, uint32_t value, bool littleEndian, ExceptionCode&);
70    void setFloat32(unsigned byteOffset, float value, ExceptionCode& ec) { setFloat32(byteOffset, value, false, ec); }
71    void setFloat32(unsigned byteOffset, float value, bool littleEndian, ExceptionCode&);
72    void setFloat64(unsigned byteOffset, double value, ExceptionCode& ec) { setFloat64(byteOffset, value, false, ec); }
73    void setFloat64(unsigned byteOffset, double value, bool littleEndian, ExceptionCode&);
74
75    virtual ViewType getType() const
76    {
77        return TypeDataView;
78    }
79
80protected:
81    virtual void neuter();
82
83private:
84    DataView(PassRefPtr<ArrayBuffer>, unsigned byteOffset, unsigned byteLength);
85
86    template<typename T>
87    inline bool beyondRange(unsigned byteOffset) const { return byteOffset >= m_byteLength || byteOffset + sizeof(T) > m_byteLength; }
88
89    template<typename T>
90    T getData(unsigned byteOffset, bool littleEndian, ExceptionCode&) const;
91
92    template<typename T>
93    void setData(unsigned byteOffset, T value, bool littleEndian, ExceptionCode&);
94
95    unsigned m_byteLength;
96};
97
98
99} // namespace WebCore
100
101#endif // DataView_h
102