feat: add generic reader class

This commit is contained in:
august-alt 2021-07-30 13:43:27 +04:00
parent d4b6e1f887
commit 5c7d7ef1b6
3 changed files with 113 additions and 0 deletions

View File

@ -3,6 +3,8 @@ find_package(Qt5 COMPONENTS Core REQUIRED)
set(HEADERS
genericfile.h
genericfile.inl
genericreader.h
genericreader.inl
io.h
policydefinitionsfile.h
policyresourcesfile.h

42
src/io/genericreader.h Normal file
View File

@ -0,0 +1,42 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/
#ifndef GPUI_GENERIC_READER_H
#define GPUI_GENERIC_READER_H
#include "io.h"
#include <memory>
#include <string>
namespace io {
class GPUI_IO_EXPORT GenericReader
{
public:
virtual ~GenericReader() = default;
template<typename TData, typename TFormat>
std::unique_ptr<TData> load(const std::string& fileName);
};
}
#include "genericreader.inl"
#endif // GPUI_GENERIC_READER_H

69
src/io/genericreader.inl Normal file
View File

@ -0,0 +1,69 @@
/***********************************************************************************************************************
**
** Copyright (C) 2021 BaseALT Ltd. <org@basealt.ru>
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
***********************************************************************************************************************/
#include "genericreader.h"
#include "../model/pluginstorage.h"
#include <fstream>
#include <QString>
#include <QDebug>
namespace io {
template<typename TData, typename TFormat>
std::unique_ptr<TData> GenericReader::load(const std::string &fileName)
{
std::unique_ptr<TData> fileData;
QString pluginName = QString::fromStdString(fileName);
pluginName = pluginName.mid(pluginName.lastIndexOf('.') + 1);
TFormat* format = gpui::PluginStorage::instance()->createPluginClass<TFormat>(pluginName);
if (!format)
{
qWarning() << "Format supporting: " << pluginName << " not found.";
return fileData;
}
std::ifstream file;
file.open(fileName, std::ifstream::in);
if (file.good()) {
fileData = std::make_unique<TData>();
if (!format->read(file, fileData.get()))
{
qWarning() << fileName.c_str() << " " << format->getErrorString().c_str();
}
}
file.close();
delete format;
return fileData;
}
}