// 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 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 struct SerialConvertTypeInfo { typedef NATIVE_T NativeType; typedef SERIAL_T SerialType; enum { SerialAlignment = SerialBasicTypeInfo::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 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 : public SerialIdentityTypeInfo {}; // Implement for Basic Types template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; template <> struct SerialTypeInfo : public SerialBasicTypeInfo {}; // Fixed arrays template struct SerialTypeInfo { typedef SerialTypeInfo ElementASTSerialType; typedef typename ElementASTSerialType::SerialType SerialElementType; typedef T NativeType[N]; typedef SerialElementType SerialType[N]; enum { SerialAlignment = SerialTypeInfo::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 { 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 struct SerialTypeInfo { 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(); } }; // RefPtr (pretty much the same as T* - except for native rep) template struct SerialTypeInfo> { typedef RefPtr 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(); } }; // Special case Name template <> struct SerialTypeInfo : public SerialTypeInfo { // 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 : public SerialTypeInfo { }; // List template struct SerialTypeInfo> { typedef List 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 { 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 struct SerialTypeInfo> { typedef Dictionary NativeType; struct SerialType { SerialIndex keys; ///< Index an array SerialIndex values; ///< Index an array }; typedef typename SerialTypeInfo::SerialType KeySerialType; typedef typename SerialTypeInfo::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 keys; List values; Index count = Index(src.Count()); keys.setCount(count); values.setCount(count); Index i = 0; for (const auto& pair : src) { SerialTypeInfo::toSerial(writer, &pair.Key, &keys[i]); SerialTypeInfo::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(keys.getBuffer(), count); dst.values = writer->addSerialArray(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 keys; List 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 struct SerialTypeInfo> { typedef KeyValuePair NativeType; typedef typename SerialTypeInfo::SerialType KeySerialType; typedef typename SerialTypeInfo::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::toSerial(writer, &src.Key, &dst.key); SerialTypeInfo::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::toNative(reader, &src.key, &dst.Key); SerialTypeInfo::toNative(reader, &src.value, &dst.Value); } }; } // namespace Slang #endif