yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
3.9 KiB109 linesraw
1#ifndef SLANG_CORE_RTTI_UTIL_H
2#define SLANG_CORE_RTTI_UTIL_H
3
4#include "slang-rtti-info.h"
5
6// Some macros to help generate StructRttiInfo for structs without too much
7// boilerplate, use as so:
8//
9// struct MyStruct
10// {
11//     int an_optional_field;
12//     List<UnownedStringSlice> a_list_of_strings_field;
13// }
14// SLANG_MAKE_STRUCT_RTTI_INFO(
15//     MyStruct,
16//     SLANG_OPTIONAL_RTTI_FIELD(an_optional_field),
17//     SLANG_RTTI_FIELD(a_list_of_strings_field)
18//  );
19//
20// This allows parsing JSON objects like
21// {
22//     "an_optional_field": 10,
23//     "a_list_of_strings_field": ["hello", "world"]
24// }
25//
26// Convert from such JSON objects using JSONToNativeConverter::convert
27
28#define SLANG_MAKE_STRUCT_RTTI_INFO(S, ...)                            \
29    template<>                                                         \
30    struct GetRttiInfo<S>                                              \
31    {                                                                  \
32        static const RttiInfo* get()                                   \
33        {                                                              \
34            using S_ = S;                                              \
35            const static StructRttiInfo::Field fs[] = {__VA_ARGS__};   \
36            const auto ignoreUnknownFields = true;                     \
37            const static auto ret = StructRttiInfo{                    \
38                {{RttiInfo::Kind::Struct, alignof(S), sizeof(S)}, #S}, \
39                nullptr,                                               \
40                SLANG_COUNT_OF(fs),                                    \
41                fs,                                                    \
42                ignoreUnknownFields};                                  \
43            return &ret;                                               \
44        }                                                              \
45    };
46#define SLANG_RTTI_FIELD_IMPL(m, name, flags)                             \
47    {                                                                     \
48        name, GetRttiInfo<decltype(S_::m)>::get(), offsetof(S_, m), flags \
49    }
50#define SLANG_RTTI_FIELD(m) SLANG_RTTI_FIELD_IMPL(m, #m, 0)
51#define SLANG_OPTIONAL_RTTI_FIELD(m) SLANG_RTTI_FIELD_IMPL(m, #m, StructRttiInfo::Flag::Optional)
52
53namespace Slang
54{
55
56struct RttiUtil
57{
58    static SlangResult setInt(int64_t value, const RttiInfo* rttiInfo, void* dst);
59    static int64_t getInt64(const RttiInfo* rttiInfo, const void* src);
60
61    static double asDouble(const RttiInfo* rttiInfo, const void* src);
62
63    static SlangResult setFromDouble(double v, const RttiInfo* rttiInfo, void* dst);
64
65    static bool asBool(const RttiInfo* rttiInfo, const void* src);
66
67    static bool isDefault(RttiDefaultValue defaultValue, const RttiInfo* rttiInfo, const void* src);
68
69    /// Gets funcs for default scenarios
70    static RttiTypeFuncs getDefaultTypeFuncs(const RttiInfo* rttiInfo);
71
72    /// Set a list count
73    static SlangResult setListCount(
74        RttiTypeFuncsMap* typeMap,
75        const RttiInfo* elementType,
76        void* dst,
77        Index count);
78
79    /// Returns if the type can be zero initialized
80    static bool canZeroInit(const RttiInfo* type);
81    /// Returns true if the type needs dtor
82    static bool hasDtor(const RttiInfo* type);
83    /// Returns true if we can memcpy to copy
84    static bool canMemCpy(const RttiInfo* type);
85
86    static void ctorArray(
87        RttiTypeFuncsMap* typeMap,
88        const RttiInfo* rttiInfo,
89        void* inDst,
90        ptrdiff_t stride,
91        Index count);
92    static void copyArray(
93        RttiTypeFuncsMap* typeMap,
94        const RttiInfo* rttiInfo,
95        void* inDst,
96        const void* inSrc,
97        ptrdiff_t stride,
98        Index count);
99    static void dtorArray(
100        RttiTypeFuncsMap* typeMap,
101        const RttiInfo* rttiInfo,
102        void* inDst,
103        ptrdiff_t stride,
104        Index count);
105};
106
107} // namespace Slang
108
109#endif // SLANG_CORE_RTTI_UTIL_H