summaryrefslogtreecommitdiff
path: root/source/core/slang-rtti-util.cpp
blob: ed4c32e58ca64a3b27b859a432a887982230936a (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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include "slang-rtti-util.h"

namespace Slang {

/* static */SlangResult RttiUtil::setInt(int64_t value, const RttiInfo* rttiInfo, void* dst)
{
    SLANG_ASSERT(rttiInfo->isIntegral());

    // We could check ranges are appropriate, but for now we just write.
    // Passing in rttiInfo allows for other more complex types to be econverted
    switch (rttiInfo->m_kind)
    {
        case RttiInfo::Kind::I32:   *(int32_t*)dst = int32_t(value); break;
        case RttiInfo::Kind::U32:   *(uint32_t*)dst = uint32_t(value); break;
        case RttiInfo::Kind::I64:   *(int64_t*)dst = int64_t(value); break;
        case RttiInfo::Kind::U64:   *(uint64_t*)dst = uint64_t(value); break;
        default: return SLANG_FAIL;
    }
    return SLANG_OK;
}

/* static */int64_t RttiUtil::getInt64(const RttiInfo* rttiInfo, const void* src)
{
    SLANG_ASSERT(rttiInfo->isIntegral());

    switch (rttiInfo->m_kind)
    {
        case RttiInfo::Kind::I32:   return *(const int32_t*)src;
        case RttiInfo::Kind::U32:   return *(const uint32_t*)src;
        case RttiInfo::Kind::I64:   return *(const int64_t*)src;
        case RttiInfo::Kind::U64:   return *(const uint64_t*)src;
        default: break;
    }

    SLANG_ASSERT(!"Not integral!");
    return -1;
}

/* static */double RttiUtil::asDouble(const RttiInfo* rttiInfo, const void* src)
{
    if (rttiInfo->isIntegral())
    {
        return (double)getInt64(rttiInfo, src);
    }
    else if (rttiInfo->isFloat())
    {
        switch (rttiInfo->m_kind)
        {
            case RttiInfo::Kind::F32:   return *(const float*)src;
            case RttiInfo::Kind::F64:   return *(const double*)src;
            default: break;
        }
    }

    SLANG_ASSERT(!"Cannot convert to float");
    return 0.0;
}

/* static */SlangResult RttiUtil::setFromDouble(double v, const RttiInfo* rttiInfo, void* dst)
{
    if (rttiInfo->isIntegral())
    {
        return setInt(int64_t(v), rttiInfo, dst);
    }
    else if (rttiInfo->isFloat())
    {
        switch (rttiInfo->m_kind)
        {
            case RttiInfo::Kind::F32:   *(float*)dst = float(v); return SLANG_OK;
            case RttiInfo::Kind::F64:   *(double*)dst = v; return SLANG_OK;
            default: break;
        }
    }

    return SLANG_FAIL;
}

/* static */bool RttiUtil::asBool(const RttiInfo* rttiInfo, const void* src)
{
    if (rttiInfo->m_kind == RttiInfo::Kind::Bool)
    {
        return *(const bool*)src;
    }

    if (rttiInfo->isIntegral())
    {
        return getInt64(rttiInfo, src) != 0;
    }
    else if (rttiInfo->isFloat())
    {
        return asDouble(rttiInfo, src) != 0.0;
    }

    SLANG_ASSERT(!"Cannot convert to bool");
    return false;
}

static int64_t _getIntDefaultValue(RttiDefaultValue value)
{
    switch (value)
    {
        default:
        case RttiDefaultValue::Normal:      return 0;
        case RttiDefaultValue::One:         return 1;
        case RttiDefaultValue::MinusOne:    return -1;
    }
}

static bool _isStructDefault(const StructRttiInfo* type, const void* src)
{
    if (type->m_super)
    {
        if (!_isStructDefault(type->m_super, src))
        {
            return false;
        }
    }

    const Byte* base = (const Byte*)src;

    const Index count = type->m_fieldCount;
    for (Index i = 0; i < count; ++i)
    {
        const auto& field = type->m_fields[i];

        const RttiDefaultValue defaultValue = RttiDefaultValue(field.m_flags & uint8_t(RttiDefaultValue::Mask));

        if (!RttiUtil::isDefault(defaultValue, field.m_type, base + field.m_offset))
        {
            return false;
        }
    }

    return true;
}

/* static */bool RttiUtil::isDefault(RttiDefaultValue defaultValue, const RttiInfo* rttiInfo, const void* src)
{
    if (rttiInfo->isIntegral())
    {
        const auto value = getInt64(rttiInfo, src);
        return _getIntDefaultValue(defaultValue) == value;
    }
    else if (rttiInfo->isFloat())
    {
        const auto value = asDouble(rttiInfo, src);
        return _getIntDefaultValue(defaultValue) == value;
    }

    switch (rttiInfo->m_kind)
    {
        case RttiInfo::Kind::Invalid:       return true;
        case RttiInfo::Kind::Bool:          return *(const bool*)src == (_getIntDefaultValue(defaultValue) != 0);
        case RttiInfo::Kind::String:
        {
            return ((const String*)src)->getLength() == 0;
        }
        case RttiInfo::Kind::UnownedStringSlice:
        {
            return ((const UnownedStringSlice*)src)->getLength() == 0;
        }
        case RttiInfo::Kind::Struct:
        {
            return _isStructDefault(static_cast<const StructRttiInfo*>(rttiInfo), src);
        }
        case RttiInfo::Kind::Enum:
        {
            SLANG_ASSERT(!"Not implemented yet");
            return false;
        }
        case RttiInfo::Kind::List:
        {
            const auto& v = *(const List<Byte>*)src;
            return v.getCount() == 0;
        }
        case RttiInfo::Kind::Dictionary:
        {
            const auto& v = *(const Dictionary<Byte, Byte>*)src;
            return v.Count() == 0;
        }
        case RttiInfo::Kind::Other:
        {
            const OtherRttiInfo* otherRttiInfo = static_cast<const OtherRttiInfo*>(rttiInfo);
            return otherRttiInfo->m_isDefaultFunc && otherRttiInfo->m_isDefaultFunc(rttiInfo, src);
        }
        default:
        {
            return false;
        }
    }
}

template <typename T>
struct GetRttiTypeFuncsForBuiltIn
{
    static void ctorArray(const RttiInfo* rttiInfo, void* dst, Index count) { SLANG_UNUSED(rttiInfo);  ::memset(dst, 0, sizeof(T) * count); }
    static void dtorArray(const RttiInfo* rttiInfo, void* dst, Index count) { SLANG_UNUSED(rttiInfo); SLANG_UNUSED(dst); SLANG_UNUSED(count); }
    static void copyArray(const RttiInfo* rttiInfo, void* dst, const void* src, Index count) { SLANG_UNUSED(rttiInfo); ::memcpy(dst, src, sizeof(T) * count); }

    static RttiTypeFuncs getFuncs()
    {
        RttiTypeFuncs funcs;
        funcs.copyArray = &copyArray;
        funcs.dtorArray = &dtorArray;
        funcs.ctorArray = &ctorArray;
        return funcs;
    }
};

struct ListFuncs
{
    static void ctorArray(const RttiInfo* rttiInfo, void* inDst, Index count)
    {
        SLANG_UNUSED(rttiInfo);
        SLANG_ASSERT(rttiInfo->m_kind == RttiInfo::Kind::List);

        // We don't care about the element type, as we can just initialize them all as List<Byte>
        //const ListRttiInfo* listRttiInfo = static_cast<const ListRttiInfo*>(rttiInfo);
        typedef List<Byte> Type;

        Type* dst = (Type*)inDst;

        for (Index i = 0; i < count; ++i)
        {
            new (dst + i) Type;
        }
    }
    static void copyArray(const RttiInfo* rttiInfo, void* inDst, const void* inSrc, Index count)
    {
        SLANG_ASSERT(rttiInfo->m_kind == RttiInfo::Kind::List);
        const ListRttiInfo* listRttiInfo = static_cast<const ListRttiInfo*>(rttiInfo);
        const auto elementType = listRttiInfo->m_elementType;

        // We need to get the type funcs
        auto typeFuncs = RttiUtil::getTypeFuncs(elementType);
        SLANG_ASSERT(typeFuncs.isValid());

        // We need a type that we can get information from the list from - List<Byte> gives us the functions we need.
        typedef List<Byte> Type;

        Type* dst = (Type*)inDst;
        const Type* src = (const Type*)inSrc;

        for (Index i = 0; i < count; ++i)
        {
            auto& dstList = dst[i];
            auto& srcList = src[i];

            const Index srcCount = srcList.getCount();

            if (srcCount > dstList.getCount())
            {
                // Allocate new memory
                const Index dstCapacity = dstList.getCapacity();
                void* oldBuffer = dstList.detachBuffer();

                void* newBuffer = ::malloc(count * elementType->m_size);
                // Initialize it all first
                typeFuncs.ctorArray(elementType, newBuffer, count);
                typeFuncs.copyArray(elementType, newBuffer, oldBuffer, count);

                // Attach the new buffer
                dstList.attachBuffer((Byte*)newBuffer, count, count);

                // Free the old buffer
                if (oldBuffer)
                {
                    typeFuncs.dtorArray(elementType, oldBuffer, dstCapacity);

                    ::free(oldBuffer);
                }
            }
            else
            {
                typeFuncs.copyArray(elementType, dstList.getBuffer(), srcList.getBuffer(), srcCount);
                dstList.unsafeShrinkToCount(srcCount);
            }
        }
    }

    static void dtorArray(const RttiInfo* rttiInfo, void* inDst, Index count)
    {
        SLANG_ASSERT(rttiInfo->m_kind == RttiInfo::Kind::List);
        const ListRttiInfo* listRttiInfo = static_cast<const ListRttiInfo*>(rttiInfo);

        const auto elementType = listRttiInfo->m_elementType;

        // We need to get the type funcs
        auto typeFuncs = RttiUtil::getTypeFuncs(elementType);
        SLANG_ASSERT(typeFuncs.isValid());

        typedef List<Byte> Type;
        Type* dst = (Type*)inDst;

        for (Index i = 0; i < count; ++i)
        {
            auto& dstList = dst[i];

            const Index capacity = dstList.getCapacity();
            Byte* buffer = dstList.detachBuffer();

            if (buffer)
            {
                typeFuncs.dtorArray(elementType, buffer, capacity);
                ::free(buffer);
            }
        }
    }

    static RttiTypeFuncs getFuncs()
    {
        RttiTypeFuncs funcs;
        funcs.copyArray = &copyArray;
        funcs.dtorArray = &dtorArray;
        funcs.ctorArray = &ctorArray;
        return funcs;
    }
};

RttiTypeFuncs RttiUtil::getTypeFuncs(const RttiInfo* rttiInfo)
{
    if (rttiInfo->isBuiltIn())
    {
        switch (rttiInfo->m_size)
        {
            case 1: return GetRttiTypeFuncsForBuiltIn<uint8_t>::getFuncs();
            case 2: return GetRttiTypeFuncsForBuiltIn<uint16_t>::getFuncs();
            case 4: return GetRttiTypeFuncsForBuiltIn<uint32_t>::getFuncs();
            case 8: return GetRttiTypeFuncsForBuiltIn<uint64_t>::getFuncs();
        }
        return RttiTypeFuncs::makeEmpty();
    }

    switch (rttiInfo->m_kind)
    {
        case RttiInfo::Kind::String:                return GetRttiTypeFuncs<String>::getFuncs();
        case RttiInfo::Kind::UnownedStringSlice:    return GetRttiTypeFuncs<UnownedStringSlice>::getFuncs();
        case RttiInfo::Kind::List:                  return ListFuncs::getFuncs();
        default: break;
    }

    return RttiTypeFuncs::makeEmpty();
}

/* static */SlangResult RttiUtil::setListCount(const RttiInfo* elementType, void* dst, Index count)
{
    // NOTE! The following only works because List<T> has capacity initialized members, and
    // setting the count if it is <= capacity just sets the count (ie things aren't released(!)).
    
    List<Byte>& dstList = *(List<Byte>*)dst;
    const Index oldCount = dstList.getCount();
    if (oldCount == count)
    {
        return SLANG_OK;
    }
    if (count < oldCount)
    {
        dstList.unsafeShrinkToCount(count);
        return SLANG_OK;
    }

    // Get funcs needed
    const auto typeFuncs = RttiUtil::getTypeFuncs(elementType);

    if (!typeFuncs.isValid())
    {
        return SLANG_FAIL;
    }

    const Index dstCapacity = dstList.getCapacity();
    void* oldBuffer = dstList.detachBuffer();

    void* newBuffer = ::malloc(count * elementType->m_size);
    // Initialize it all first
    typeFuncs.ctorArray(elementType, newBuffer, count);

    typeFuncs.copyArray(elementType, newBuffer, oldBuffer, oldCount);

    // Attach the new buffer
    dstList.attachBuffer((Byte*)newBuffer, count, count);

    // Free the old buffer
    if (oldBuffer)
    {
        typeFuncs.dtorArray(elementType, oldBuffer, dstCapacity);
        ::free(oldBuffer);
    }

    return SLANG_OK;
}

} // namespace Slang