#ifndef XNA_CONTENT_TYPEREADER_HPP #define XNA_CONTENT_TYPEREADER_HPP #include "../csharp/type.hpp" #include "../default.hpp" #include #include #include namespace xna { //-------------------------------------------------------// // ContentTypeReader // //-------------------------------------------------------// //Worker for reading a specific managed type from a binary format. class ContentTypeReader { public: virtual Int TypeVersion() { return 0; } virtual bool CanDeserializeIntoExistingObject() { return false; } //Retrieves and caches nested type readers. Called by the framework at creation time. virtual void Initialize(sptr const& manager) {} //Gets the type handled by this reader component. sptr TargetType() { return _targetType; } //Reads a strongly typed object from the current stream. virtual std::any Read(ContentReader& input, std::any& existingInstance) = 0; protected: ContentTypeReader(sptr const& targetType) : _targetType(targetType) { } public: //Let's admit that the target is primarily of type value, if not it must be manually set to false bool TargetIsValueType{ true }; private: sptr _targetType = nullptr; }; //Worker for reading a specific managed type from a binary format. //Derive from this class to add new data types to the content pipeline system. template class ContentTypeReaderT : public ContentTypeReader { public: //For some reason ListReader needs a default constructor ContentTypeReaderT() : ContentTypeReader(typeof()) {} protected: ContentTypeReaderT(sptr const& targetType) : ContentTypeReader(targetType) {} public: //Reads a strongly typed object from the current stream. std::any Read(ContentReader& input, std::any& existingInstance) override { if (existingInstance.has_value() && !(existingInstance.type() == typeid(T))) throw std::runtime_error("ContentTypeReader::Read: bad xbn, wrong type."); auto existingInstance1 = XnaHelper::ReturnDefaultOrNull(); auto obj = Read(input, existingInstance1); return obj; } //Reads a strongly typed object from the current stream. virtual T Read(ContentReader& input, T& existingInstance) = 0; }; //-------------------------------------------------------// // ContentTypeReaderActivador // //-------------------------------------------------------// class ContentTypeReaderActivador { public: using Activador = sptr(*)(); static sptr CreateInstance(sptr const& type); static void SetActivador(sptr const& type, Activador activador); private: inline static auto activators = std::map(); ContentTypeReaderActivador(); ContentTypeReaderActivador(ContentTypeReaderActivador&&); ContentTypeReaderActivador(ContentTypeReaderActivador&); }; using PContentTypeReader = sptr; using PType = sptr; //-------------------------------------------------------// // ContentTypeReaderManager // //-------------------------------------------------------// //A manager that constructs and keeps track of type reader objects. class ContentTypeReaderManager { public: static std::vector ReadTypeManifest(Int typeCount, sptr& contentReader); //Looks up a reader for the specified type. static sptr GetTypeReader(sptr const& targetType); inline static bool ContainsTypeReader(sptr const& targetType) { return ContentTypeReaderManager::targetTypeToReader.contains(targetType); } private: ContentTypeReaderManager(sptr& contentReader); static sptr GetTypeReader(String const& readerTypeName, sptr& contentReader, std::vector& newTypeReaders); static bool InstantiateTypeReader(String const& readerTypeName, sptr& contentReader, sptr& reader); static void AddTypeReader(String const& readerTypeName, sptr& contentReader, sptr& reader); static void RollbackAddReaders(std::vector>& newTypeReaders); static void RollbackAddReader(std::map& dictionary, sptr& reader); static void RollbackAddReader(std::map& dictionary, sptr& reader); private: sptr contentReader = nullptr; inline static auto nameToReader = std::map(); inline static auto targetTypeToReader = std::map(); inline static auto readerTypeToReader = std::map(); static void initMaps(); }; } #endif