yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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{ 58static SlangResult setInt (int64_t value ,const RttiInfo * rttiInfo ,void * dst ); 59static int64_t getInt64 (const RttiInfo * rttiInfo ,const void * src ); 60 61static double asDouble (const RttiInfo * rttiInfo ,const void * src ); 62 63static SlangResult setFromDouble (double v ,const RttiInfo * rttiInfo ,void * dst ); 64 65static bool asBool (const RttiInfo * rttiInfo ,const void * src ); 66 67static bool isDefault (RttiDefaultValue defaultValue ,const RttiInfo * rttiInfo ,const void * src ); 68 69/// Gets funcs for default scenarios 70static RttiTypeFuncs getDefaultTypeFuncs (const RttiInfo * rttiInfo ); 71 72/// Set a list count 73static SlangResult setListCount ( 74RttiTypeFuncsMap * typeMap , 75const RttiInfo * elementType , 76void * dst , 77Index count ); 78 79/// Returns if the type can be zero initialized 80static bool canZeroInit (const RttiInfo * type ); 81/// Returns true if the type needs dtor 82static bool hasDtor (const RttiInfo * type ); 83/// Returns true if we can memcpy to copy 84static bool canMemCpy (const RttiInfo * type ); 85 86static void ctorArray ( 87RttiTypeFuncsMap * typeMap , 88const RttiInfo * rttiInfo , 89void * inDst , 90ptrdiff_t stride , 91Index count ); 92static void copyArray ( 93RttiTypeFuncsMap * typeMap , 94const RttiInfo * rttiInfo , 95void * inDst , 96const void * inSrc , 97ptrdiff_t stride , 98Index count ); 99static void dtorArray ( 100RttiTypeFuncsMap * typeMap , 101const RttiInfo * rttiInfo , 102void * inDst , 103ptrdiff_t stride , 104Index count ); 105}; 106 107}// namespace Slang 108 109#endif // SLANG_CORE_RTTI_UTIL_H