forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.h
More file actions
226 lines (173 loc) · 7.69 KB
/
Array.h
File metadata and controls
226 lines (173 loc) · 7.69 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/core/Data.h>
#include <vsg/maths/mat4.h>
#include <vsg/maths/vec2.h>
#include <vsg/maths/vec3.h>
#include <vsg/maths/vec4.h>
#include <vsg/io/Input.h>
#include <vsg/io/Output.h>
#define VSG_array(N, T) \
using N = Array<T>; \
template<> \
constexpr const char* type_name<N>() noexcept { return "vsg::" #N; }
namespace vsg
{
template<typename T>
class Array : public Data
{
public:
using value_type = T;
using iterator = value_type*;
using const_iterator = const value_type*;
Array() :
_size(0),
_data(nullptr) {}
Array(std::uint32_t numElements, value_type* data) :
_size(numElements),
_data(data) {}
Array(std::uint32_t numElements, value_type* data, Layout layout) :
Data(layout),
_size(numElements),
_data(data) {}
explicit Array(std::initializer_list<value_type> l) :
_size(static_cast<std::uint32_t>(l.size())),
_data(new value_type[l.size()])
{
value_type* ptr = _data;
for (value_type const& v : l) { (*ptr++) = v; }
}
explicit Array(std::uint32_t numElements) :
_size(numElements),
_data(new value_type[numElements]) {}
template<typename... Args>
static ref_ptr<Array> create(Args... args)
{
return ref_ptr<Array>(new Array(args...));
}
static ref_ptr<Array> create(std::initializer_list<value_type> l)
{
return ref_ptr<Array>(new Array(l));
}
std::size_t sizeofObject() const noexcept override { return sizeof(Array); }
// implementation provided by Visitor.h
void accept(Visitor& visitor) override;
void accept(ConstVisitor& visitor) const override;
const char* className() const noexcept override { return type_name<Array>(); }
void read(Input& input) override
{
std::size_t original_total_size = size();
Data::read(input);
std::uint32_t width_size = input.readValue<std::uint32_t>("Size");
std::size_t new_total_size = computeValueCountIncludingMipmaps(width_size, 1, 1, _layout.maxNumMipmaps);
if (input.matchPropertyName("Data"))
{
if (_data) // if data already may be able to reuse it
{
if (original_total_size != new_total_size) // if existing data is a different size delete old, and create new
{
delete[] _data;
_data = new value_type[new_total_size];
}
}
else // allocate space for data
{
_data = new value_type[new_total_size];
}
_size = width_size;
input.read(new_total_size, _data);
}
}
void write(Output& output) const override
{
Data::write(output);
output.writeValue<std::uint32_t>("Size", _size);
output.writePropertyName("Data");
output.write(size(), _data);
output.writeEndOfLine();
}
std::size_t size() const { return (_layout.maxNumMipmaps <= 1) ? _size : computeValueCountIncludingMipmaps(_size, 1, 1, _layout.maxNumMipmaps); }
bool empty() const { return _size == 0; }
// should Array be fixed size?
void clear()
{
_size = 0;
if (_data) { delete[] _data; }
_data = nullptr;
}
void assign(std::uint32_t numElements, value_type* data, Layout layout = Layout())
{
if (_data != nullptr) delete[] _data;
_layout = layout;
_size = numElements;
_data = data;
}
// release the data so that ownership can be passed on, the local data pointer and size is set to 0 and destruction of Array will no result in the data being deleted.
void* dataRelease() override
{
void* tmp = _data;
_data = nullptr;
_size = 0;
return tmp;
}
std::size_t valueSize() const override { return sizeof(value_type); }
std::size_t valueCount() const override { return size(); }
std::size_t dataSize() const override { return size() * sizeof(value_type); }
void* dataPointer() override { return _data; }
const void* dataPointer() const override { return _data; }
void* dataPointer(std::size_t i) override { return _data + i; }
const void* dataPointer(std::size_t i) const override { return _data + i; }
std::uint32_t dimensions() const override { return 1; }
std::uint32_t width() const override { return _size; }
std::uint32_t height() const override { return 1; }
std::uint32_t depth() const override { return 1; }
value_type* data() { return _data; }
const value_type* data() const { return _data; }
value_type& operator[](std::size_t i) { return _data[i]; }
const value_type& operator[](std::size_t i) const { return _data[i]; }
value_type& at(std::size_t i) { return _data[i]; }
const value_type& at(std::size_t i) const { return _data[i]; }
void set(std::size_t i, const value_type& v) { _data[i] = v; }
iterator begin() { return _data; }
const_iterator begin() const { return _data; }
iterator end() { return _data + _size; }
const_iterator end() const { return _data + _size; }
protected:
virtual ~Array()
{
if (_data) delete[] _data;
}
private:
std::uint32_t _size;
value_type* _data;
};
VSG_array(ubyteArray, std::uint8_t);
VSG_array(ushortArray, std::uint16_t);
VSG_array(uintArray, std::uint32_t);
VSG_array(floatArray, float);
VSG_array(doubleArray, double);
VSG_array(vec2Array, vec2);
VSG_array(vec3Array, vec3);
VSG_array(vec4Array, vec4);
VSG_array(dvec2Array, dvec2);
VSG_array(dvec3Array, dvec3);
VSG_array(dvec4Array, dvec4);
VSG_array(ubvec2Array, ubvec2);
VSG_array(ubvec3Array, ubvec3);
VSG_array(ubvec4Array, ubvec4);
VSG_array(usvec2Array, usvec2);
VSG_array(usvec3Array, usvec3);
VSG_array(usvec4Array, usvec4);
VSG_array(uivec2Array, uivec2);
VSG_array(uivec3Array, uivec3);
VSG_array(uivec4Array, uivec4);
VSG_array(mat4Array, mat4);
VSG_array(dmat4Array, dmat4);
VSG_array(block64Array, block64);
VSG_array(block128Array, block128);
} // namespace vsg