/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ #pragma once #include #include #include #include #include #include #include #include namespace MCore { class ReflectionSerializer { public: template static AZ::Outcome SerializeMember(const TClass* classPtr, const char* memberName) { return SerializeMember(azrtti_typeid(classPtr), classPtr, memberName); } // Returns a serialized unordered_map with all the memberName->memberValue pairs of an object except the passed // excluded members template static AZ::Outcome SerializeMembersExcept(const TClass* classPtr, const AZStd::vector& excludeMembers) { return SerializeMembersExcept(azrtti_typeid(classPtr), classPtr, excludeMembers); } template static bool DeserializeIntoMember(TClass* classPtr, const char* memberName, const AZStd::string& value) { return DeserializeIntoMember(azrtti_typeid(classPtr), classPtr, memberName, value); } template static AZ::Outcome Serialize(const TClass* classPtr) { return Serialize(azrtti_typeid(classPtr), classPtr); } template static bool Deserialize(TClass* classPtr, const AZStd::string& sourceBuffer) { return Deserialize(azrtti_typeid(classPtr), classPtr, sourceBuffer); } template static TClass* Deserialize(const AZStd::string& sourceBuffer) { AZ::IO::ByteContainerStream byteStream(&sourceBuffer); return AZ::Utils::LoadObjectFromStream(byteStream); } template static AZ::Outcome> SerializeIntoMap(const TClass* classPtr) { return SerializeIntoMap(azrtti_typeid(classPtr), classPtr); } template static AZ::Outcome SerializeIntoCommandLine(const TClass* classPtr) { return SerializeIntoCommandLine(azrtti_typeid(classPtr), classPtr); } template static AZ::Outcome SerializeValue(const TClass* classPtr) { return SerializeValue(azrtti_typeid(classPtr), classPtr); } template static bool Deserialize(TClass* classPtr, const MCore::CommandLine& sourceCommandLine) { return Deserialize(azrtti_typeid(classPtr), classPtr, sourceCommandLine); } // Deserializes a serialized unordered_map of values into the classPtr object. memberValuesMap is the returned // string of the SerializeMembersExcept method template static bool DeserializeMembers(TClass* classPtr, const AZStd::string& memberValuesMap) { return DeserializeMembers(azrtti_typeid(classPtr), classPtr, memberValuesMap); } template static TClass* Clone(const TClass* classPtr) { return reinterpret_cast(Clone(azrtti_typeid(classPtr), classPtr)); } template static void CloneInplace(TClass& dest, const TClass* ptr) { CloneInplace(&dest, ptr, azrtti_typeid(dest)); } template static bool SetIntoMember(AZ::SerializeContext* context, TClass* classPtr, const char* memberName, const TValue& value) { void* ptrToMember = GetPointerToMember(context, azrtti_typeid(classPtr), classPtr, memberName); if (ptrToMember) { *reinterpret_cast(ptrToMember) = value; return true; } return false; } static void Reflect(AZ::ReflectContext* context); private: static AZ::Outcome SerializeMember(const AZ::TypeId& classTypeId, const void* classPtr, const char* memberName); static AZ::Outcome SerializeMembersExcept(const AZ::TypeId& classTypeId, const void* classPtr, const AZStd::vector& excludeMembers); static bool DeserializeIntoMember(const AZ::TypeId& classTypeId, void* classPtr, const char* memberName, const AZStd::string& value); static AZ::Outcome Serialize(const AZ::TypeId& classTypeId, const void* classPtr); static bool Deserialize(const AZ::TypeId& classTypeId, void* classPtr, const AZStd::string& sourceBuffer); static AZ::Outcome> SerializeIntoMap(const AZ::TypeId& classTypeId, const void* classPtr); static AZ::Outcome SerializeIntoCommandLine(const AZ::TypeId& classTypeId, const void* classPtr); static AZ::Outcome SerializeValue(const AZ::TypeId& classTypeId, const void* classPtr); static bool Deserialize(const AZ::TypeId& classTypeId, void* classPtr, const MCore::CommandLine& sourceCommandLine); static bool DeserializeMembers(const AZ::TypeId& classTypeId, void* classPtr, const AZStd::string& memberValuesMap); static void* Clone(const AZ::TypeId& classTypeId, const void* classPtr); static void CloneInplace(void* dest, const void* src, const AZ::Uuid& classId); static void* GetPointerToMember(AZ::SerializeContext* context, const AZ::TypeId& classTypeId, void* classPtr, const char* memberName); }; }