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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
// slang-serialize-type-info.h
#ifndef SLANG_SERIALIZE_TYPE_INFO_H
#define SLANG_SERIALIZE_TYPE_INFO_H
#include "slang-serialize.h"
namespace Slang {
/* For the serialization system to work we need to defined how native types are represented in the serialized format.
This information is defined by specializing SerialTypeInfo with the native type to be converted
This header provides conversion for common Slang types.
*/
// We need to have a way to map between the two.
// If no mapping is needed, (just a copy), then we don't bother with the functions
template <typename T>
struct SerialBasicTypeInfo
{
typedef T NativeType;
typedef T SerialType;
// We want the alignment to be the same as the size of the type for basic types
// NOTE! Might be different from SLANG_ALIGN_OF(SerialType)
enum { SerialAlignment = sizeof(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial) { SLANG_UNUSED(writer); *(T*)serial = *(const T*)native; }
static void toNative(SerialReader* reader, const void* serial, void* native) { SLANG_UNUSED(reader); *(T*)native = *(const T*)serial; }
static const SerialType* getType()
{
static const SerialType type = { sizeof(SerialType), uint8_t(SerialAlignment), &toSerial, &toNative };
return &type;
}
};
template <typename NATIVE_T, typename SERIAL_T>
struct SerialConvertTypeInfo
{
typedef NATIVE_T NativeType;
typedef SERIAL_T SerialType;
enum { SerialAlignment = SerialBasicTypeInfo<SERIAL_T>::SerialAlignment };
static void toSerial(SerialWriter* writer, const void* native, void* serial) { SLANG_UNUSED(writer); *(SERIAL_T*)serial = SERIAL_T(*(const NATIVE_T*)native); }
static void toNative(SerialReader* reader, const void* serial, void* native) { SLANG_UNUSED(reader); *(NATIVE_T*)native = NATIVE_T(*(const SERIAL_T*)serial); }
};
template <typename T>
struct SerialIdentityTypeInfo
{
typedef T NativeType;
typedef T SerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial) { SLANG_UNUSED(writer); *(T*)serial = *(const T*)native; }
static void toNative(SerialReader* reader, const void* serial, void* native) { SLANG_UNUSED(reader); *(T*)native = *(const T*)serial; }
};
// Don't need to convert the index type
template <>
struct SerialTypeInfo<SerialIndex> : public SerialIdentityTypeInfo<SerialIndex> {};
// Implement for Basic Types
template <>
struct SerialTypeInfo<uint8_t> : public SerialBasicTypeInfo<uint8_t> {};
template <>
struct SerialTypeInfo<uint16_t> : public SerialBasicTypeInfo<uint16_t> {};
template <>
struct SerialTypeInfo<uint32_t> : public SerialBasicTypeInfo<uint32_t> {};
template <>
struct SerialTypeInfo<uint64_t> : public SerialBasicTypeInfo<uint64_t> {};
template <>
struct SerialTypeInfo<int8_t> : public SerialBasicTypeInfo<int8_t> {};
template <>
struct SerialTypeInfo<int16_t> : public SerialBasicTypeInfo<int16_t> {};
template <>
struct SerialTypeInfo<int32_t> : public SerialBasicTypeInfo<int32_t> {};
template <>
struct SerialTypeInfo<int64_t> : public SerialBasicTypeInfo<int64_t> {};
template <>
struct SerialTypeInfo<float> : public SerialBasicTypeInfo<float> {};
template <>
struct SerialTypeInfo<double> : public SerialBasicTypeInfo<double> {};
// Fixed arrays
template <typename T, size_t N>
struct SerialTypeInfo<T[N]>
{
typedef SerialTypeInfo<T> ElementASTSerialType;
typedef typename ElementASTSerialType::SerialType SerialElementType;
typedef T NativeType[N];
typedef SerialElementType SerialType[N];
enum { SerialAlignment = SerialTypeInfo<T>::SerialAlignment };
static void toSerial(SerialWriter* writer, const void* inNative, void* outSerial)
{
SerialElementType* serial = (SerialElementType*)outSerial;
const T* native = (const T*)inNative;
for (Index i = 0; i < Index(N); ++i)
{
ElementASTSerialType::toSerial(writer, native + i, serial + i);
}
}
static void toNative(SerialReader* reader, const void* inSerial, void* outNative)
{
const SerialElementType* serial = (const SerialElementType*)inSerial;
T* native = (T*)outNative;
for (Index i = 0; i < Index(N); ++i)
{
ElementASTSerialType::toNative(reader, serial + i, native + i);
}
}
};
// Special case bool - as we can't rely on size alignment
template <>
struct SerialTypeInfo<bool>
{
typedef bool NativeType;
typedef uint8_t SerialType;
enum { SerialAlignment = sizeof(SerialType) };
static void toSerial(SerialWriter* writer, const void* inNative, void* outSerial)
{
SLANG_UNUSED(writer);
*(SerialType*)outSerial = *(const NativeType*)inNative ? 1 : 0;
}
static void toNative(SerialReader* reader, const void* inSerial, void* outNative)
{
SLANG_UNUSED(reader);
*(NativeType*)outNative = (*(const SerialType*)inSerial) != 0;
}
};
// Pointer
// Could handle different pointer base types with some more template magic here, but instead went with Pointer type to keep
// things simpler.
template <typename T>
struct SerialTypeInfo<T*>
{
typedef T* NativeType;
typedef SerialIndex SerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* inNative, void* outSerial)
{
*(SerialType*)outSerial = writer->addPointer(*(T**)inNative);
}
static void toNative(SerialReader* reader, const void* inSerial, void* outNative)
{
*(T**)outNative = reader->getPointer(*(const SerialType*)inSerial).dynamicCast<T>();
}
};
// RefPtr (pretty much the same as T* - except for native rep)
template <typename T>
struct SerialTypeInfo<RefPtr<T>>
{
typedef RefPtr<T> NativeType;
typedef SerialIndex SerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial)
{
auto& src = *(const NativeType*)native;
*(SerialType*)serial = writer->addPointer(src);
}
static void toNative(SerialReader* reader, const void* serial, void* native)
{
*(NativeType*)native = reader->getPointer(*(const SerialType*)serial).dynamicCast<T>();
}
};
// Special case Name
template <>
struct SerialTypeInfo<Name*> : public SerialTypeInfo<RefObject*>
{
// Special case
typedef Name* NativeType;
static void toNative(SerialReader* reader, const void* inSerial, void* outNative)
{
*(Name**)outNative = reader->getName(*(const SerialType*)inSerial);
}
};
template <>
struct SerialTypeInfo<const Name*> : public SerialTypeInfo<Name*>
{
};
// List
template <typename T, typename ALLOCATOR>
struct SerialTypeInfo<List<T, ALLOCATOR>>
{
typedef List<T, ALLOCATOR> NativeType;
typedef SerialIndex SerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial)
{
auto& src = *(const NativeType*)native;
auto& dst = *(SerialType*)serial;
dst = writer->addArray(src.getBuffer(), src.getCount());
}
static void toNative(SerialReader* reader, const void* serial, void* native)
{
auto& dst = *(NativeType*)native;
auto& src = *(const SerialType*)serial;
reader->getArray(src, dst);
}
};
// String
template <>
struct SerialTypeInfo<String>
{
typedef String NativeType;
typedef SerialIndex SerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial)
{
auto& src = *(const NativeType*)native;
*(SerialType*)serial = writer->addString(src);
}
static void toNative(SerialReader* reader, const void* serial, void* native)
{
auto& src = *(const SerialType*)serial;
auto& dst = *(NativeType*)native;
dst = reader->getString(src);
}
};
// Dictionary
template <typename KEY, typename VALUE>
struct SerialTypeInfo<Dictionary<KEY, VALUE>>
{
typedef Dictionary<KEY, VALUE> NativeType;
struct SerialType
{
SerialIndex keys; ///< Index an array
SerialIndex values; ///< Index an array
};
typedef typename SerialTypeInfo<KEY>::SerialType KeySerialType;
typedef typename SerialTypeInfo<VALUE>::SerialType ValueSerialType;
enum { SerialAlignment = SLANG_ALIGN_OF(SerialIndex) };
static void toSerial(SerialWriter* writer, const void* native, void* serial)
{
auto& src = *(const NativeType*)native;
auto& dst = *(SerialType*)serial;
List<KeySerialType> keys;
List<ValueSerialType> values;
Index count = Index(src.Count());
keys.setCount(count);
values.setCount(count);
Index i = 0;
for (const auto& pair : src)
{
SerialTypeInfo<KEY>::toSerial(writer, &pair.Key, &keys[i]);
SerialTypeInfo<VALUE>::toSerial(writer, &pair.Value, &values[i]);
i++;
}
// When we add the array it is already converted to a serializable type, so add as SerialArray
dst.keys = writer->addSerialArray<KEY>(keys.getBuffer(), count);
dst.values = writer->addSerialArray<VALUE>(values.getBuffer(), count);
}
static void toNative(SerialReader* reader, const void* serial, void* native)
{
auto& src = *(const SerialType*)serial;
auto& dst = *(NativeType*)native;
// Clear it
dst = NativeType();
List<KEY> keys;
List<VALUE> values;
reader->getArray(src.keys, keys);
reader->getArray(src.values, values);
SLANG_ASSERT(keys.getCount() == values.getCount());
const Index count = keys.getCount();
for (Index i = 0; i < count; ++i)
{
dst.Add(keys[i], values[i]);
}
}
};
// KeyValuePair
template<typename KEY, typename VALUE>
struct SerialTypeInfo<KeyValuePair<KEY, VALUE>>
{
typedef KeyValuePair<KEY, VALUE> NativeType;
typedef typename SerialTypeInfo<KEY>::SerialType KeySerialType;
typedef typename SerialTypeInfo<VALUE>::SerialType ValueSerialType;
struct SerialType
{
KeySerialType key;
ValueSerialType value;
};
enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };
static void toSerial(SerialWriter* writer, const void* native, void* serial)
{
auto& src = *(const NativeType*)native;
auto& dst = *(SerialType*)serial;
SerialTypeInfo<KEY>::toSerial(writer, &src.Key, &dst.key);
SerialTypeInfo<VALUE>::toSerial(writer, &src.Value, &dst.value);
}
static void toNative(SerialReader* reader, const void* serial, void* native)
{
auto& src = *(const SerialType*)serial;
auto& dst = *(NativeType*)native;
SerialTypeInfo<KEY>::toNative(reader, &src.key, &dst.Key);
SerialTypeInfo<VALUE>::toNative(reader, &src.value, &dst.Value);
}
};
} // namespace Slang
#endif
|