summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/ecmascript/TypedArray.h
blob: 82da62973c72fd6520dc893a28d9ffb9ab63fb7d (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef TYPEDARRAY_H_99815BLY
#define TYPEDARRAY_H_99815BLY

#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>

namespace uscxml {

class ArrayBuffer {
public:
	class Buffer {
	public:
		~Buffer();
		Buffer(size_t size);
		Buffer(void* data, size_t size);
		char* _data;
		size_t _size;
	};

	ArrayBuffer(unsigned long length);
	ArrayBuffer(boost::shared_ptr<ArrayBuffer::Buffer>);
	unsigned long getByteLength();
	ArrayBuffer slice(long begin, long length);
	ArrayBuffer slice(long begin);
	static bool isView(void*);
	unsigned long getLength() {
		return getByteLength();
	}
	operator bool();
	bool operator== (const ArrayBuffer& other) {
		return other._buffer == _buffer;
	}
	unsigned char get(unsigned long index) {
		if (index >= getLength())
			return 0;
		unsigned char retVal;
		memcpy(&retVal, _buffer->_data + index * sizeof(unsigned char), sizeof(unsigned char));
		return retVal;
	}

	void set(unsigned long index, unsigned char value) {
		memcpy(_buffer->_data + index * sizeof(unsigned char), &value, sizeof(unsigned char));
	}

	boost::shared_ptr<Buffer> _buffer;
};

class ArrayBufferView {
public:
	ArrayBuffer getBuffer();
	virtual unsigned long getByteOffset() = 0;
	virtual unsigned long getByteLength() = 0;
	virtual unsigned long getLength() = 0;
protected:
	boost::shared_ptr<ArrayBuffer::Buffer> _buffer;
	unsigned long _start;
	unsigned long _end;
};

class DataView : ArrayBufferView {
public:
	DataView(ArrayBuffer*, unsigned long, unsigned long);
	DataView(ArrayBuffer*, unsigned long);
	DataView(ArrayBuffer*);

	unsigned long getByteOffset();
	unsigned long getByteLength();
	unsigned long getLength();

	char getInt8(unsigned long);
	unsigned char getUint8(unsigned long);
	short getInt16(unsigned long, bool = false);
	unsigned short getUint16(unsigned long, bool = false);
	long getInt32(unsigned long, bool = false);
	unsigned long getUint32(unsigned long, bool = false);
	float getFloat32(unsigned long, bool = false);
	double getFloat64(unsigned long, bool = false);

	void setInt8(long, char);
	void setUint8(long, unsigned char);
	void setInt16(long, short, bool = false);
	void setUint16(long, unsigned short, bool = false);
	void setInt32(long, long, bool = false);
	void setUint32(long, unsigned long, bool = false);
	void setFloat32(long, float, bool = false);
	void setFloat64(long, double, bool = false);

};

template<class T, class S> class TypedArray : public ArrayBufferView {
public:
	virtual ~TypedArray() {}
	TypedArray(uscxml::ArrayBuffer* buffer, unsigned long start, unsigned long length) {
		_start = start;
		_end = start + length;
		_buffer = buffer->_buffer;
	}
	TypedArray(uscxml::ArrayBuffer* buffer, unsigned long start) {
		_start = start / sizeof(S);
		_end = buffer->_buffer->_size / sizeof(S);
		_buffer = buffer->_buffer;
	}
	TypedArray(uscxml::ArrayBuffer* buffer) {
		_start = 0;
		_end = (buffer->_buffer->_size) / sizeof(S);
		_buffer = buffer->_buffer;
	}

	TypedArray(boost::shared_ptr<ArrayBuffer::Buffer> buffer, unsigned long start, unsigned long length) {
		_start = start;
		_end = start + length;
		_buffer = buffer;
	}
	TypedArray(unsigned long length) {
		_start = 0;
		_end = length;
		_buffer = boost::shared_ptr<ArrayBuffer::Buffer>(new ArrayBuffer::Buffer(length * sizeof(S)));
	}
	TypedArray(std::vector<T> data) {
		_start = 0;
		_end = data.size();
		if (sizeof(T) == sizeof(S)) {
			_buffer = boost::shared_ptr<ArrayBuffer::Buffer>(new ArrayBuffer::Buffer(((void*)&data[0]), data.size() * sizeof(T)));
		} else {
			S* buffer = (S*)malloc(data.size() * sizeof(S));
			typename std::vector<T>::const_iterator dataIter = data.begin();
			unsigned long i = 0;
			while(dataIter != data.end()) {
				buffer[i] = *dataIter;
				dataIter++;
				i++;
			}
			_buffer = boost::shared_ptr<ArrayBuffer::Buffer>(new ArrayBuffer::Buffer(buffer, data.size() * sizeof(S)));
		}
	}
	TypedArray(TypedArray* other) {
		_start = other->_start;
		_end = other->_end;
		_buffer = other->_buffer;
	}
	T get(unsigned long index) {
		if (index >= getLength())
			return static_cast<T>(0);
		S retVal;
		memcpy(&retVal, _buffer->_data + (_start + index) * sizeof(S), sizeof(S));
		return retVal;
	}
	void set(unsigned long index, T value) {
		memcpy(_buffer->_data + (_start + index) * sizeof(S), &value, sizeof(S));
	}

	void set(TypedArray<T, S>* value, unsigned long offset) {
		memcpy(_buffer->_data + (_start) * sizeof(S), &value->_buffer->_data[offset], value->_buffer->_size);
	}

	void set(TypedArray<T, S>* value) {
		set(value, 0);
	}

	void set(std::vector<T> data, unsigned long offset) {
	}

	void set(std::vector<T> data) {
		set(data, 0);
	}

	TypedArray* subarray(long start, long end) {
		return new TypedArray<T, S>(_buffer, start, end);
	}

	unsigned long getLength() {
		return _end - _start;
	}

	unsigned long getByteLength() {
		return (_end - _start) * sizeof(S);
	}

	unsigned long getByteOffset() {
		return _start * sizeof(S);
	}


};

typedef TypedArray<unsigned char, uint8_t> Uint8Array;
typedef TypedArray<unsigned char, uint8_t> Uint8ClampedArray;
typedef TypedArray<char, int8_t> Int8Array;
typedef TypedArray<short, int16_t> Int16Array;
typedef TypedArray<unsigned short, uint16_t> Uint16Array;
typedef TypedArray<long, int32_t> Int32Array;
typedef TypedArray<unsigned long, uint32_t> Uint32Array;
typedef TypedArray<float, float> Float32Array;
typedef TypedArray<double, double> Float64Array;

}

#endif /* end of include guard: TYPEDARRAY_H_99815BLY */