1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementações em pipeline

This commit is contained in:
Danilo Borges Santos 2024-12-02 11:51:19 -03:00
parent a2b6351de5
commit a034d08168
8 changed files with 166 additions and 19 deletions

View File

@ -3,6 +3,7 @@
#include <string>
#include <any>
#include <memory>
#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<IntermediateSerializer> SingletonInstance();
template <typename T>
static void Serialize(XmlWriter& output, T& value, std::string const& referenceRelocationPath);
template <typename T>
static T Deserialize(XmlReader& input, std::string referenceRelocationPath) {
}
private:
IntermediateSerializer() {}
private:
static inline std::shared_ptr<IntermediateSerializer> 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<IntermediateSerializer> const& serializer,
std::shared_ptr<XmlReader> const& xmlReader)
: serializer(serializer), xmlReader(xmlReader){}
//Gets the parent serializer.
std::shared_ptr<IntermediateSerializer> Serializer() const { return serializer; }
//Gets the XML input stream.
std::shared_ptr<XmlReader> Xml() const { return xmlReader; }
//Reads a single object from the input XML stream.
template <class T>
T ReadObject(ContentSerializerAttribute& format) {
}
//Reads a single object from the input XML stream.
template <class T>
T ReadObject(ContentSerializerAttribute& format, T& existingInstance);
//Reads a single object from the input XML stream.
template <class T>
T ReadObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer);
//Reads a single object from the input XML stream.
template <class T>
T ReadObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance);
//Reads a single object from the input XML stream.
template <class T>
T ReadRawObject(ContentSerializerAttribute& format);
//Reads a single object from the input XML stream.
template <class T>
T ReadRawObject(ContentSerializerAttribute& format, T& existingInstance);
//Reads a single object from the input XML stream.
template <class T>
T ReadRawObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer);
//Reads a single object from the input XML stream.
template <class T>
T ReadRawObject(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance);
private:
//Reads a single object from the input XML stream.
template <class T>
T ReadObjectInternal(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance);
//Reads a single object from the input XML stream.
template <class T>
T ReadRawObjectInternal(ContentSerializerAttribute& format, ContentTypeSerializer& typeSerializer, T& existingInstance);
private:
std::shared_ptr<IntermediateSerializer> serializer;
std::shared_ptr<XmlReader> 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 <typename T>
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<T>(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<T>() && !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 {

View File

@ -0,0 +1,48 @@
#ifndef XNA_PIPELINE_SERIALIZERS_HPP
#define XNA_PIPELINE_SERIALIZERS_HPP
#include "serialization.hpp"
#include "xna/exception.hpp"
#include <any>
#include <memory>
#include <string>
#include <vector>
namespace xna {
class XmlListReader {
public:
XmlListReader(std::shared_ptr<IntermediateReader> const& reader) : reader(reader) {
if (this->reader == nullptr)
Exception::Throw(Exception::INVALID_OPERATION);
//this.enumerator = ((IEnumerable<string>) reader.Xml.ReadContentAsString().Split(XmlListReader.listSeparators, StringSplitOptions.RemoveEmptyEntries)).GetEnumerator();
}
constexpr bool AtEnd() const { return atEnd; }
private:
std::shared_ptr<IntermediateReader> reader;
std::vector<std::string> enumerator;
bool atEnd{ false };
};
struct IXmlListItemSerializer {
virtual std::any Deserialize(XmlListReader const& list) = 0;
};
template <typename T>
class XmlListItemSerializer : public ContentTypeSerializer_T<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

View File

@ -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).

View File

@ -7,6 +7,10 @@ namespace xna {
class XmlReader {
};
class XmlWriter {
};
}
#endif

View File

@ -1,3 +1,3 @@
add_subdirectory ("framework")
add_subdirectory ("framework-dx")
add_subdirectory ("pipeline")
#add_subdirectory ("pipeline")

View File

@ -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)

View File

@ -0,0 +1,9 @@
#include "pipeline/serialization.hpp"
namespace xna {
std::shared_ptr<IntermediateSerializer> IntermediateSerializer::SingletonInstance() {
if (singletonInstance == nullptr)
singletonInstance = std::make_shared<IntermediateSerializer>();
return singletonInstance;
}

View File

@ -0,0 +1,5 @@
#include "pipeline/serializers.hpp"
namespace xna {
}