1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-14 09:45:36 +00:00
LatAnalyze/lib/Io/File.hpp

124 lines
3.5 KiB
C++
Raw Normal View History

2014-02-20 23:52:45 +00:00
/*
* File.hpp, part of LatAnalyze 3
*
2016-04-06 20:11:23 +01:00
* Copyright (C) 2013 - 2016 Antonin Portelli
2014-02-20 23:52:45 +00:00
*
* LatAnalyze 3 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 3 of the License, or
* (at your option) any later version.
*
* LatAnalyze 3 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 LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
*/
2015-10-01 00:13:34 +01:00
#ifndef Latan_File_hpp_
#define Latan_File_hpp_
2014-02-20 23:52:45 +00:00
2014-03-13 18:51:01 +00:00
#include <LatAnalyze/Global.hpp>
#include <LatAnalyze/Io/IoObject.hpp>
#include <LatAnalyze/Core/Mat.hpp>
#include <LatAnalyze/Statistics/MatSample.hpp>
2014-02-20 23:52:45 +00:00
BEGIN_LATAN_NAMESPACE
2014-02-20 23:52:45 +00:00
/******************************************************************************
* Abstract datafile class *
******************************************************************************/
typedef std::unordered_map<std::string, std::unique_ptr<IoObject>> IoDataTable;
class File
{
public:
class Mode
{
public:
enum
{
null = 0,
write = 1 << 0,
read = 1 << 1,
append = 1 << 2
};
};
public:
// constructors
2014-03-03 14:21:37 +00:00
File(void) = default;
2014-02-20 23:52:45 +00:00
File(const std::string &name, const unsigned int mode);
// destructor
virtual ~File(void);
// access
const std::string & getName(void) const;
unsigned int getMode(void) const;
template <typename IoT>
const IoT & read(const std::string &name = "");
virtual void save(const DMat &m, const std::string &name) = 0;
2016-04-05 15:57:19 +01:00
virtual void save(const DSample &ds, const std::string &name) = 0;
virtual void save(const DMatSample &ms, const std::string &name) = 0;
2015-10-01 00:13:34 +01:00
// read first name
virtual std::string getFirstName(void) = 0;
2014-02-20 23:52:45 +00:00
// tests
virtual bool isOpen(void) const = 0;
// IO
virtual void close(void) = 0;
virtual void open(const std::string &name, const unsigned int mode) = 0;
protected:
// access
void setName(const std::string &name);
void setMode(const unsigned int mode);
// data access
void deleteData(void);
// error checking
void checkWritability(void);
private:
// data access
template <typename IoT>
const IoT& getData(const std::string &name = "") const;
// IO
virtual std::string load(const std::string &name = "") = 0;
protected:
2014-03-03 14:21:37 +00:00
std::string name_{""};
unsigned int mode_{Mode::null};
2014-02-20 23:52:45 +00:00
IoDataTable data_;
};
// Template implementations
template <typename IoT>
const IoT& File::read(const std::string &name)
{
std::string dataName;
dataName = load(name);
return getData<IoT>(dataName);
}
template <typename IoT>
const IoT& File::getData(const std::string &name) const
{
try
{
return dynamic_cast<const IoT &>(*(data_.at(name)));
}
catch(std::out_of_range)
{
LATAN_ERROR(Definition, "no data with name '" + name + "' in file "
+ name_);
}
2016-04-15 18:23:17 +01:00
catch(std::bad_cast)
{
LATAN_ERROR(Definition, "data with name '" + name + "' in file "
+ name_ + " does not have type '" + typeid(IoT).name()
+ "'");
}
2014-02-20 23:52:45 +00:00
}
END_LATAN_NAMESPACE
2014-02-20 23:52:45 +00:00
#endif