From ea4754d70a8265cf9d46599ce94e5386ad122c18 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Mon, 28 May 2018 10:13:26 +0200 Subject: [PATCH] wip input/output file type registry --- .../InputFileTypeRegistration.cpp | 11 ++++ .../InputFileTypeRegistration.hpp | 17 ++++++ .../OutputFileTypeRegistration.cpp | 11 ++++ .../OutputFileTypeRegistration.hpp | 17 ++++++ alib2cli/src/registry/InputFileRegistry.cpp | 25 ++++++++ alib2cli/src/registry/InputFileRegistry.hpp | 57 +++++++++++++++++++ alib2cli/src/registry/OutputFileRegistry.cpp | 25 ++++++++ alib2cli/src/registry/OutputFileRegistry.hpp | 57 +++++++++++++++++++ 8 files changed, 220 insertions(+) create mode 100644 alib2cli/src/registration/InputFileTypeRegistration.cpp create mode 100644 alib2cli/src/registration/InputFileTypeRegistration.hpp create mode 100644 alib2cli/src/registration/OutputFileTypeRegistration.cpp create mode 100644 alib2cli/src/registration/OutputFileTypeRegistration.hpp create mode 100644 alib2cli/src/registry/InputFileRegistry.cpp create mode 100644 alib2cli/src/registry/InputFileRegistry.hpp create mode 100644 alib2cli/src/registry/OutputFileRegistry.cpp create mode 100644 alib2cli/src/registry/OutputFileRegistry.hpp diff --git a/alib2cli/src/registration/InputFileTypeRegistration.cpp b/alib2cli/src/registration/InputFileTypeRegistration.cpp new file mode 100644 index 0000000000..6088d0df02 --- /dev/null +++ b/alib2cli/src/registration/InputFileTypeRegistration.cpp @@ -0,0 +1,11 @@ +#include "InputFileTypeRegistration.hpp" + +namespace { + + std::shared_ptr < abstraction::OperationAbstraction > dummy ( const std::string & /* type */, const ext::vector < std::string > & /* templateParams */ ) { + return nullptr; + } + +auto xmlInputFileHandler = registration::InputFileRegister ( "xml", dummy ); + +} diff --git a/alib2cli/src/registration/InputFileTypeRegistration.hpp b/alib2cli/src/registration/InputFileTypeRegistration.hpp new file mode 100644 index 0000000000..28cb35ef38 --- /dev/null +++ b/alib2cli/src/registration/InputFileTypeRegistration.hpp @@ -0,0 +1,17 @@ +#ifndef _INPUT_FILE_REGISTER_HPP_ +#define _INPUT_FILE_REGISTER_HPP_ + +#include <registry/InputFileRegistry.hpp> + +namespace registration { + +class InputFileRegister { +public: + InputFileRegister ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) { + abstraction::InputFileRegistry::registerInputFileHandler ( fileType, callback ); + } +}; + +} /* namespace registration */ + +#endif // _INPUT_FILE_REGISTER_HPP_ diff --git a/alib2cli/src/registration/OutputFileTypeRegistration.cpp b/alib2cli/src/registration/OutputFileTypeRegistration.cpp new file mode 100644 index 0000000000..2f1cbca702 --- /dev/null +++ b/alib2cli/src/registration/OutputFileTypeRegistration.cpp @@ -0,0 +1,11 @@ +#include "OutputFileTypeRegistration.hpp" + +namespace { + + std::shared_ptr < abstraction::OperationAbstraction > dummy ( ) { + return nullptr; + } + +auto xmlOutputFileHandler = registration::OutputFileRegister ( "xml", dummy ); + +} diff --git a/alib2cli/src/registration/OutputFileTypeRegistration.hpp b/alib2cli/src/registration/OutputFileTypeRegistration.hpp new file mode 100644 index 0000000000..223310fbd9 --- /dev/null +++ b/alib2cli/src/registration/OutputFileTypeRegistration.hpp @@ -0,0 +1,17 @@ +#ifndef _INPUT_FILE_REGISTER_HPP_ +#define _INPUT_FILE_REGISTER_HPP_ + +#include <registry/OutputFileRegistry.hpp> + +namespace registration { + +class OutputFileRegister { +public: + OutputFileRegister ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( ) ) { + abstraction::OutputFileRegistry::registerOutputFileHandler ( fileType, callback ); + } +}; + +} /* namespace registration */ + +#endif // _INPUT_FILE_REGISTER_HPP_ diff --git a/alib2cli/src/registry/InputFileRegistry.cpp b/alib2cli/src/registry/InputFileRegistry.cpp new file mode 100644 index 0000000000..e06f0be975 --- /dev/null +++ b/alib2cli/src/registry/InputFileRegistry.cpp @@ -0,0 +1,25 @@ +/* + * InputFileRegistry.cpp + * + * Created on: 28. 8. 2017 + * Author: Jan Travnicek + */ + +#include <registry/InputFileRegistry.hpp> +#include <abstraction/XmlComposerAbstraction.hpp> + +namespace abstraction { + +std::shared_ptr < abstraction::OperationAbstraction > InputFileRegistry::getAbstraction ( const std::string & fileType, const std::string & type, const ext::vector < std::string > & templateParams ) { + auto res = getEntries ( ).find ( fileType ); + if ( res == getEntries ( ).end ( ) ) + throw exception::CommonException ( "Entry " + fileType + " not available." ); + + return res->second->getAbstraction ( type, templateParams ); +} + +std::shared_ptr < abstraction::OperationAbstraction > InputFileRegistry::EntryImpl::getAbstraction ( const std::string & type, const ext::vector < std::string > & templateParams ) const { + return m_callback ( type, templateParams ); +} + +} /* namespace abstraction */ diff --git a/alib2cli/src/registry/InputFileRegistry.hpp b/alib2cli/src/registry/InputFileRegistry.hpp new file mode 100644 index 0000000000..6ecf007bec --- /dev/null +++ b/alib2cli/src/registry/InputFileRegistry.hpp @@ -0,0 +1,57 @@ +/* + * InputFileRegistry.hpp + * + * Created on: 25. 5. 2018 + * Author: Jan Travnicek + */ + +#ifndef _INPUT_FILE_REGISTRY_HPP_ +#define _INPUT_FILE_REGISTRY_HPP_ + +#include <alib/memory> +#include <alib/string> +#include <alib/map> +#include <alib/functional> + +#include <exception/CommonException.h> +#include <abstraction/OperationAbstraction.hpp> + +namespace abstraction { + +class InputFileRegistry { + class Entry { + public: + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & type, const ext::vector < std::string > & templateParams ) const = 0; + + virtual ~Entry ( ) { + } + }; + + class EntryImpl : public Entry { + std::function < std::shared_ptr < abstraction::OperationAbstraction > ( const std::string & type, const ext::vector < std::string > & templateParams ) > m_callback; + public: + EntryImpl ( std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) : m_callback ( callback ) { + } + + virtual ~EntryImpl ( ) { + } + + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & type, const ext::vector < std::string > & templateParams ) const override; + }; + + static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ) { + static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; + return inputFileHandlers; + } + +public: + static void registerInputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( const std::string & type, const ext::vector < std::string > & templateParams ) ) { + getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); + } + + static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & fileType, const std::string & type, const ext::vector < std::string > & templateParams ); +}; + +} /* namespace abstraction */ + +#endif /* _INPUT_FILE_REGISTRY_HPP_ */ diff --git a/alib2cli/src/registry/OutputFileRegistry.cpp b/alib2cli/src/registry/OutputFileRegistry.cpp new file mode 100644 index 0000000000..3aa32f1ddb --- /dev/null +++ b/alib2cli/src/registry/OutputFileRegistry.cpp @@ -0,0 +1,25 @@ +/* + * OutputFileRegistry.cpp + * + * Created on: 28. 8. 2017 + * Author: Jan Travnicek + */ + +#include <registry/OutputFileRegistry.hpp> +#include <abstraction/XmlComposerAbstraction.hpp> + +namespace abstraction { + +std::shared_ptr < abstraction::OperationAbstraction > OutputFileRegistry::getAbstraction ( const std::string & fileType ) { + auto res = getEntries ( ).find ( fileType ); + if ( res == getEntries ( ).end ( ) ) + throw exception::CommonException ( "Entry " + fileType + " not available." ); + + return res->second->getAbstraction ( ); +} + +std::shared_ptr < abstraction::OperationAbstraction > OutputFileRegistry::EntryImpl::getAbstraction ( ) const { + return m_callback ( ); +} + +} /* namespace abstraction */ diff --git a/alib2cli/src/registry/OutputFileRegistry.hpp b/alib2cli/src/registry/OutputFileRegistry.hpp new file mode 100644 index 0000000000..4d9dd73b52 --- /dev/null +++ b/alib2cli/src/registry/OutputFileRegistry.hpp @@ -0,0 +1,57 @@ +/* + * OutputFileRegistry.hpp + * + * Created on: 25. 5. 2018 + * Author: Jan Travnicek + */ + +#ifndef _OUTPUT_FILE_REGISTRY_HPP_ +#define _OUTPUT_FILE_REGISTRY_HPP_ + +#include <alib/memory> +#include <alib/string> +#include <alib/map> +#include <alib/functional> + +#include <exception/CommonException.h> +#include <abstraction/OperationAbstraction.hpp> + +namespace abstraction { + +class OutputFileRegistry { + class Entry { + public: + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const = 0; + + virtual ~Entry ( ) { + } + }; + + class EntryImpl : public Entry { + std::function < std::shared_ptr < abstraction::OperationAbstraction > ( ) > m_callback; + public: + EntryImpl ( std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( ) ) : m_callback ( callback ) { + } + + virtual ~EntryImpl ( ) { + } + + virtual std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( ) const override; + }; + + static ext::map < std::string, std::unique_ptr < Entry > > & getEntries ( ) { + static ext::map < std::string, std::unique_ptr < Entry > > inputFileHandlers; + return inputFileHandlers; + } + +public: + static void registerOutputFileHandler ( const std::string & fileType, std::shared_ptr < abstraction::OperationAbstraction > ( * callback ) ( ) ) { + getEntries ( ).insert ( std::make_pair ( fileType, std::unique_ptr < Entry > ( new EntryImpl ( callback ) ) ) ); + } + + static std::shared_ptr < abstraction::OperationAbstraction > getAbstraction ( const std::string & fileType ); +}; + +} /* namespace abstraction */ + +#endif /* _OUTPUT_FILE_REGISTRY_HPP_ */ -- GitLab