From a034d081684e68b31261b580794d5ea5a4bbf199 Mon Sep 17 00:00:00 2001 From: Danilo Borges Santos Date: Mon, 2 Dec 2024 11:51:19 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20pipeline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/pipeline/serialization.hpp | 110 ++++++++++++++++++++++++---- includes/pipeline/serializers.hpp | 48 ++++++++++++ includes/xna/content/serializer.hpp | 2 +- includes/xna/csharp/xml.hpp | 4 + sources/CMakeLists.txt | 2 +- sources/pipeline/CMakeLists.txt | 5 +- sources/pipeline/serialization.cpp | 9 +++ sources/pipeline/serializers.cpp | 5 ++ 8 files changed, 166 insertions(+), 19 deletions(-) create mode 100644 includes/pipeline/serializers.hpp create mode 100644 sources/pipeline/serialization.cpp create mode 100644 sources/pipeline/serializers.cpp diff --git a/includes/pipeline/serialization.hpp b/includes/pipeline/serialization.hpp index 493ffc1..8078b55 100644 --- a/includes/pipeline/serialization.hpp +++ b/includes/pipeline/serialization.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "xna/csharp/xml.hpp" #include "xna/content/serializer.hpp" #include "xna/helpers.hpp" @@ -13,21 +14,35 @@ namespace xna { class IntermediateSerializer { public: + static std::shared_ptr SingletonInstance(); + + template + static void Serialize(XmlWriter& output, T& value, std::string const& referenceRelocationPath); + template static T Deserialize(XmlReader& input, std::string referenceRelocationPath) { - } + } + + private: + IntermediateSerializer() {} + + private: + static inline std::shared_ptr singletonInstance = nullptr; }; //Provides methods for serializing and deserializing a specific managed type. class ContentTypeSerializer { public: + ContentTypeSerializer() {} + ContentTypeSerializer(std::string const& xmlTypeName) : xmlTypeName(xmlTypeName) {} + //Retrieves and caches any nested type serializers and allows reflection over the target data type. - virtual void Initialize(IntermediateSerializer serializer) {}; + virtual void Initialize(IntermediateSerializer& serializer) {}; //Serializes an object to intermediate XML format. - virtual void Serialize(IntermediateWriter output, std::any const& value, ContentSerializerAttribute& format); + virtual void Serialize(IntermediateWriter& output, std::any const& value, ContentSerializerAttribute& format); //Serializes an object to intermediate XML format. - virtual std::any Deserialize(IntermediateWriter input, ContentSerializerAttribute& format, std::any const& existingInstance); + virtual std::any Deserialize(IntermediateWriter& input, ContentSerializerAttribute& format, std::any const& existingInstance); //Gets a value indicating whether this component may load data into an existing object or if it must it construct a new instance of the object before loading the data. virtual bool CanDeserializeIntoExistingObject() const { return false; } @@ -36,30 +51,97 @@ namespace xna { //Gets a short-form XML name for the target type, or null if there is none. constexpr std::string XmlTypeName() const { return xmlTypeName; } - protected: - ContentTypeSerializer() {} - ContentTypeSerializer(std::string const& xmlTypeName) : xmlTypeName(xmlTypeName) {} - private: std::string xmlTypeName; }; + //Provides an implementation of many of the methods of IntermediateSerializer. + //Deserializes and tracks state for shared resources and external references. + class IntermediateReader { + public: + IntermediateReader( + std::shared_ptr const& serializer, + std::shared_ptr const& xmlReader) + : serializer(serializer), xmlReader(xmlReader){} + + //Gets the parent serializer. + std::shared_ptr Serializer() const { return serializer; } + + //Gets the XML input stream. + std::shared_ptr Xml() const { return xmlReader; } + + //Reads a single object from the input XML stream. + template + T ReadObject(ContentSerializerAttribute& format) { + + } + + //Reads a single object from the input XML stream. + template + T ReadObject(ContentSerializerAttribute& format, T& existingInstance); + + //Reads a single object from the input XML stream. + template + T ReadObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer); + + //Reads a single object from the input XML stream. + template + T ReadObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance); + + //Reads a single object from the input XML stream. + template + T ReadRawObject(ContentSerializerAttribute& format); + + //Reads a single object from the input XML stream. + template + T ReadRawObject(ContentSerializerAttribute& format, T& existingInstance); + + //Reads a single object from the input XML stream. + template + T ReadRawObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer); + + //Reads a single object from the input XML stream. + template + T ReadRawObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance); + + private: + //Reads a single object from the input XML stream. + template + T ReadObjectInternal(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance); + + //Reads a single object from the input XML stream. + template + T ReadRawObjectInternal(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance); + + private: + std::shared_ptr serializer; + std::shared_ptr xmlReader; + }; + struct ISerialization { virtual void Serialize(ContentTypeSerializer& serializer, bool serializeOptional = true) = 0; }; + //Provides a generic implementation of ContentTypeSerializer methods and properties for serializing and deserializing a specific managed type. template class ContentTypeSerializer_T : public ContentTypeSerializer { public: - virtual void Serialize(IntermediateWriter output, T const& value, ContentSerializerAttribute& format) = 0; + ContentTypeSerializer_T() {} + ContentTypeSerializer_T(std::string const& xmlTypeName) : ContentTypeSerializer(xmlTypeName) {} - void Serialize(IntermediateWriter output, std::any const& value, ContentSerializerAttribute& format) override { + //Serializes an object to intermediate XML format. + virtual void Serialize(IntermediateWriter& output, T const& value, ContentSerializerAttribute& format) = 0; + + //Serializes an object to intermediate XML format. + void Serialize(IntermediateWriter& output, std::any const& value, ContentSerializerAttribute& format) override { Serializer(output, CastType(value), format); } - virtual T Deserialize(IntermediateWriter input, ContentSerializerAttribute& format, T& existingInstance) = 0; + //Deserializes a strongly typed object from intermediate XML format. + virtual T Deserialize(IntermediateWriter& input, ContentSerializerAttribute& format, T& existingInstance) = 0; - std::any Deserialize(IntermediateWriter input, ContentSerializerAttribute& format, std::any const& existingInstance) override { + //Deserializes a strongly typed object from intermediate XML format. + std::any Deserialize(IntermediateWriter& input, ContentSerializerAttribute& format, std::any const& existingInstance) override { if constexpr (XnaHelper::IsSmartPoint() && !existingInstance.has_value()) return T(); @@ -71,10 +153,6 @@ namespace xna { return obj; } - protected: - ContentTypeSerializer_T() {} - ContentTypeSerializer_T(std::string const& xmlTypeName) : ContentTypeSerializer(xmlTypeName){} - private: static T CastType(std::any const& value) { try { diff --git a/includes/pipeline/serializers.hpp b/includes/pipeline/serializers.hpp new file mode 100644 index 0000000..274ed3c --- /dev/null +++ b/includes/pipeline/serializers.hpp @@ -0,0 +1,48 @@ +#ifndef XNA_PIPELINE_SERIALIZERS_HPP +#define XNA_PIPELINE_SERIALIZERS_HPP + +#include "serialization.hpp" +#include "xna/exception.hpp" +#include +#include +#include +#include + +namespace xna { + class XmlListReader { + public: + XmlListReader(std::shared_ptr const& reader) : reader(reader) { + if (this->reader == nullptr) + Exception::Throw(Exception::INVALID_OPERATION); + + //this.enumerator = ((IEnumerable) reader.Xml.ReadContentAsString().Split(XmlListReader.listSeparators, StringSplitOptions.RemoveEmptyEntries)).GetEnumerator(); + } + + constexpr bool AtEnd() const { return atEnd; } + + private: + std::shared_ptr reader; + std::vector enumerator; + bool atEnd{ false }; + }; + + struct IXmlListItemSerializer { + virtual std::any Deserialize(XmlListReader const& list) = 0; + }; + + template + class XmlListItemSerializer : public ContentTypeSerializer_T, public IXmlListItemSerializer { + public: + XmlListItemSerializer(){} + XmlListItemSerializer(std::string const& xmlTypeName) : ContentTypeSerializer_T(xmlTypeName) {} + + virtual T Deserialize(XmlListReader& input) = 0; + + T Deserialize(IntermediateReader& input, ContentSerializerAttribute& format, T& existingInstance) override { + + + } + }; +} + +#endif \ No newline at end of file diff --git a/includes/xna/content/serializer.hpp b/includes/xna/content/serializer.hpp index 2bfafc2..656616d 100644 --- a/includes/xna/content/serializer.hpp +++ b/includes/xna/content/serializer.hpp @@ -11,7 +11,7 @@ namespace xna { //Gets or sets the XML element name for each item in a collection (default = "Item"). std::string CollectionItemName; //Gets or sets a value idicating whether to write member contents directly into the current XML context rather than wrapping the member in a new XML element (default=false). - bool FlatternContent{ false }; + bool FlattenContent{ false }; //Indicates whether to write this element if the member is null and skip past it if not found when deserializing XML (default=false). bool Optional{ false }; //Get or set a value indicating whether this member can have a null value (default=true). diff --git a/includes/xna/csharp/xml.hpp b/includes/xna/csharp/xml.hpp index 69a505e..5cacfd1 100644 --- a/includes/xna/csharp/xml.hpp +++ b/includes/xna/csharp/xml.hpp @@ -7,6 +7,10 @@ namespace xna { class XmlReader { }; + + class XmlWriter { + + }; } #endif \ No newline at end of file diff --git a/sources/CMakeLists.txt b/sources/CMakeLists.txt index 251e18f..d600457 100644 --- a/sources/CMakeLists.txt +++ b/sources/CMakeLists.txt @@ -1,3 +1,3 @@ add_subdirectory ("framework") add_subdirectory ("framework-dx") -add_subdirectory ("pipeline") +#add_subdirectory ("pipeline") diff --git a/sources/pipeline/CMakeLists.txt b/sources/pipeline/CMakeLists.txt index 380f2da..04729b0 100644 --- a/sources/pipeline/CMakeLists.txt +++ b/sources/pipeline/CMakeLists.txt @@ -6,7 +6,10 @@ add_library (Xn65Pipeline STATIC "writer.cpp" "compiler.cpp" - "importer.cpp" "graphics.cpp" "pipeline.cpp") +"importer.cpp" +"graphics.cpp" +"pipeline.cpp" +"serializers.cpp" "serialization.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET Xn65Pipeline PROPERTY CXX_STANDARD 20) diff --git a/sources/pipeline/serialization.cpp b/sources/pipeline/serialization.cpp new file mode 100644 index 0000000..112bd32 --- /dev/null +++ b/sources/pipeline/serialization.cpp @@ -0,0 +1,9 @@ +#include "pipeline/serialization.hpp" + +namespace xna { + std::shared_ptr IntermediateSerializer::SingletonInstance() { + if (singletonInstance == nullptr) + singletonInstance = std::make_shared(); + + return singletonInstance; + } \ No newline at end of file diff --git a/sources/pipeline/serializers.cpp b/sources/pipeline/serializers.cpp new file mode 100644 index 0000000..f717cb7 --- /dev/null +++ b/sources/pipeline/serializers.cpp @@ -0,0 +1,5 @@ +#include "pipeline/serializers.hpp" + +namespace xna { + +} \ No newline at end of file