#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) { if (!type) { throw std::invalid_argument("ContentTypeReaderActivador: type is null."); } const auto hash = type->GetHashCode(); if (!activators.contains(hash)) return nullptr; auto activador = activators[hash]; if (!activador) return nullptr; return activador(); } static void SetActivador(sptr const& type, Activador activador) { if (!type) { throw std::invalid_argument("ContentTypeReaderActivador: type is null."); } const auto hash = type->GetHashCode(); if (!activators.contains(hash)) activators.insert({ hash, activador }); } private: inline static auto activators = std::map(); ContentTypeReaderActivador(); ContentTypeReaderActivador(ContentTypeReaderActivador&&); ContentTypeReaderActivador(ContentTypeReaderActivador&); }; using PContentTypeReader = sptr; using PType = sptr; //-------------------------------------------------------// // ContentTypeReaderManager // //-------------------------------------------------------// class ContentTypeReaderManager { public: static std::vector ReadTypeManifest(Int typeCount, sptr& contentReader, xna_error_nullarg); static sptr GetTypeReader(sptr const& targetType, sptr& contentReader, xna_error_nullarg); inline sptr GetTypeReader(sptr const& targetType, xna_error_nullarg) { return ContentTypeReaderManager::GetTypeReader(targetType, this->contentReader, err); } 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, xna_error_nullarg); static bool InstantiateTypeReader(String const& readerTypeName, sptr& contentReader, sptr& reader, xna_error_nullarg); static void AddTypeReader(String const& readerTypeName, sptr& contentReader, sptr& reader, xna_error_nullarg); static void RollbackAddReaders(std::vector>& newTypeReaders); static void RollbackAddReader(std::map& dictionary, sptr& reader) { std::map>::iterator it; for (it = dictionary.begin(); it != dictionary.end(); it++) { if (it->second == reader) { dictionary.erase(it->first); it = dictionary.begin(); } } } static void RollbackAddReader(std::map& dictionary, sptr& reader) { std::map>::iterator it; for (it = dictionary.begin(); it != dictionary.end(); it++) { if (it->second == reader) { dictionary.erase(it->first); it = dictionary.begin(); } } } 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