-
Notifications
You must be signed in to change notification settings - Fork 60
/
types.h
292 lines (242 loc) · 7.49 KB
/
types.h
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef __types_h__
#define __types_h__
#include <cassert>
#include <iterator>
#include "traits.h"
template <typename T>
class RawArray
{
public:
RawArray(T *data, size_t size) : myData(data), mySize(size) {}
T *data() const { return myData; }
size_t size() const { return mySize; }
private:
T *myData;
size_t mySize;
};
template <typename T>
struct ArrayTrait<RawArray<T>>
{
static const bool isArray = true;
typedef RawArray<T> ArrayType;
typedef T ElementType;
static const bool canGetData = true;
static const T *data(const ArrayType &array) { return array.data(); }
static T *data(ArrayType &array) { return array.data(); }
static size_t size(const ArrayType &array) { return array.size(); }
static void resize(ArrayType &array, size_t size)
{
// Don't assert. This worksaround the issue where the plugin assumes a
// Maya-specific detail attribute has tuple size == 1, but the detail
// attribute actually has tuple size > 1.
// assert(array.size() == size );
}
static const ElementType &getElement(const ArrayType &array, size_t i)
{
return array.data()[i];
}
static ElementType &getElement(ArrayType &array, size_t i)
{
return array.data()[i];
}
};
template <typename T>
RawArray<T>
rawArray(T *data, size_t size)
{
return RawArray<T>(data, size);
}
template <typename ArrayType>
class ArrayIterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ArrayType;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
typedef ARRAYTRAIT(ArrayType) Trait;
typedef ELEMENTTYPE(ArrayType) ElementType;
ArrayIterator(ArrayType &array, size_t index = 0)
: myArray(&array), myIndex(index)
{
}
ArrayIterator(const ArrayIterator<ArrayType> &o)
: myArray(o.myArray), myIndex(o.myIndex)
{
}
size_t operator-(const ArrayIterator<ArrayType> &o) const
{
return myIndex - o.myIndex;
}
ArrayIterator<ArrayType> &operator++()
{
++myIndex;
return *this;
}
bool operator==(const ArrayIterator<ArrayType> &o) const
{
return myIndex == o.myIndex;
}
bool operator!=(const ArrayIterator<ArrayType> &o) const
{
return !(*this == o);
}
const ElementType &operator*() const
{
return Trait::getElement(*myArray, myIndex);
}
ElementType &operator*() { return Trait::getElement(*myArray, myIndex); }
const ElementType *operator->() const
{
return &Trait::getElement(*myArray, myIndex);
}
ElementType *operator->() { return &Trait::getElement(*myArray, myIndex); }
private:
ArrayType *myArray;
size_t myIndex;
};
template <typename ArrayType>
ArrayIterator<ArrayType>
arrayBegin(ArrayType &array)
{
return ArrayIterator<ArrayType>(array);
}
template <typename ArrayType>
ArrayIterator<ArrayType>
arrayEnd(ArrayType &array)
{
return ArrayIterator<ArrayType>(array, ARRAYTRAIT(ArrayType)::size(array));
}
template <typename ArrayType, size_t StartComponent, size_t NumComponents>
class ComponentWrapper
{
public:
typedef ComponentWrapper<ArrayType, StartComponent, NumComponents> Type;
typedef ARRAYTRAIT(ArrayType) Trait;
typedef ELEMENTTRAIT(ArrayType) ElementTrait;
public:
ComponentWrapper(ArrayType &array, size_t index, size_t component)
: myArray(&array), myIndex(index), myComponent(component)
{
assert(myComponent == StartComponent);
}
Type &operator=(const Type &o)
{
// Assignment should always mean copying components
assert(myArray != o.myArray);
return this->operator=<Type>(o);
}
template <typename O>
Type &operator=(const O &o)
{
for (size_t i = 0; i < NumComponents; i++)
{
(*this)[i] = o[i];
}
return *this;
}
const typename ElementTrait::ComponentType &operator[](size_t i) const
{
size_t index = myIndex +
(StartComponent + i) / ElementTrait::numComponents;
size_t component = (StartComponent + i) % ElementTrait::numComponents;
return ElementTrait::getComponent(
Trait::getElement(*myArray, index), component);
}
typename ElementTrait::ComponentType &operator[](size_t i)
{
size_t index = myIndex +
(StartComponent + i) / ElementTrait::numComponents;
size_t component = (StartComponent + i) % ElementTrait::numComponents;
return ElementTrait::getComponent(
Trait::getElement(*myArray, index), component);
}
private:
ArrayType *myArray;
size_t myIndex;
size_t myComponent;
};
template <typename ArrayType,
size_t StartComponent,
size_t NumComponents,
size_t Stride>
class ComponentIterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ArrayType;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
typedef ComponentIterator<ArrayType, StartComponent, NumComponents, Stride>
Type;
typedef ARRAYTRAIT(ArrayType) Trait;
typedef ELEMENTTRAIT(ArrayType) ElementTrait;
typedef ComponentWrapper<ArrayType,
StartComponent % ElementTrait::numComponents,
NumComponents>
ComponentWrapperType;
public:
ComponentIterator(ArrayType &array, size_t index) : myArray(&array)
{
myIndex = (StartComponent + (Stride * index)) /
ElementTrait::numComponents;
myComponent = (StartComponent + (Stride * index)) %
ElementTrait::numComponents;
assert(Stride % ElementTrait::numComponents == 0);
}
Type &operator++()
{
myIndex += (myComponent + Stride) / ElementTrait::numComponents;
myComponent = (myComponent + Stride) % ElementTrait::numComponents;
return *this;
}
bool operator==(const Type &o) const
{
return myIndex == o.myIndex && myComponent == o.myComponent;
}
bool operator!=(const Type &o) const { return !(*this == o); }
const ComponentWrapperType operator*() const
{
return ComponentWrapperType(*myArray, myIndex, myComponent);
}
ComponentWrapperType operator*()
{
return ComponentWrapperType(*myArray, myIndex, myComponent);
}
const ComponentWrapperType *operator->() const
{
return &ComponentWrapperType(*myArray, myIndex, myComponent);
}
ComponentWrapperType *operator->()
{
return &ComponentWrapperType(*myArray, myIndex, myComponent);
}
private:
ArrayType *myArray;
size_t myIndex;
size_t myComponent;
};
template <size_t startComponent,
size_t numComponents,
size_t stride,
typename ArrayType>
ComponentIterator<ArrayType, startComponent, numComponents, stride>
componentBegin(ArrayType &array)
{
return ComponentIterator<ArrayType, startComponent, numComponents, stride>(
array, 0);
}
template <size_t startComponent,
size_t numComponents,
size_t stride,
typename ArrayType>
ComponentIterator<ArrayType, startComponent, numComponents, stride>
componentEnd(ArrayType &array)
{
return ComponentIterator<ArrayType, startComponent, numComponents, stride>(
array, ARRAYTRAIT(ArrayType)::size(array) *
ELEMENTTRAIT(ArrayType)::numComponents / stride);
}
#endif