35 namespace xerus {
namespace misc {
51 template<class T, typename std::enable_if<std::is_arithmetic<T>::value,
bool>::type =
true>
54 _stream << _value <<
'\t';
56 _stream.write(reinterpret_cast<const char*>(&_value), std::streamsize(
sizeof(T)));
62 write_to_stream<size_t>(_stream, _value.size(), _format);
63 for (
size_t i = 0; i < _value.size(); ++i) {
64 write_to_stream<T>(_stream, _value[i], _format);
82 template<class T, typename std::enable_if<std::is_arithmetic<T>::value,
bool>::type =
true>
87 _stream.read(reinterpret_cast<char*>(&_obj),
sizeof(T));
93 _obj.resize(read_from_stream<size_t>(_stream, _format));
94 for (
size_t i = 0; i < _obj.size(); ++i) {
95 read_from_stream<T>(_stream, _obj[i], _format);
106 std::ofstream out(_filename, std::ofstream::out | std::ofstream::binary);
107 std::string header = std::string(
"Xerus ") +
misc::demangle_cxa(
typeid(T).name()) +
" datafile.\nFormat: Binary\n";
108 out.write(header.c_str(), std::streamsize(header.size()));
109 write_to_stream<T>(out, _obj, _format);
111 std::ofstream out(_filename, std::ofstream::out);
112 out << std::string(
"Xerus ") <<
misc::demangle_cxa(
typeid(T).name()) <<
" datafile.\nFormat: TSV\n";
113 write_to_stream<T>(out, _obj, _format);
116 throw e <<
"error occured while writing to file " << _filename <<
'\n';
124 std::ifstream in(_filename, std::ifstream::in);
126 std::string firstLine;
127 std::getline(in, firstLine);
129 XERUS_REQUIRE(in,
"Unexpected end of stream in load_from_file().");
131 XERUS_REQUIRE(firstLine == std::string(
"Xerus ") +
misc::demangle_cxa(
typeid(T).name()) +
" datafile.",
"Invalid binary input file " << _filename <<
". DBG: " << firstLine);
133 std::string formatQual, formatValue;
134 in >> formatQual >> formatValue;
136 XERUS_REQUIRE(formatQual == std::string(
"Format:"),
"Invalid Sytax detected in file " << _filename <<
". DBG: " << formatQual);
139 if(formatValue == std::string(
"TSV")) {
141 }
else if(formatValue == std::string(
"Binary")) {
145 const std::streamoff currPos = in.tellg();
147 in.open(_filename, std::ifstream::in | std::ifstream::binary);
150 XERUS_LOG(fatal,
"Invalid value for format detected. " << formatValue);
153 read_from_stream<T>(in, _obj, format);
155 throw e <<
"error occured in file " << _filename <<
'\n';
162 load_from_file<T>(result, _filename);
Header file for CHECK and REQUIRE macros.
void stream_reader(std::istream &_stream, Tensor &_obj, const FileFormat _format)
tries to restore the tensor from a stream of data.
The main namespace of xerus.
void load_from_file(T &_obj, const std::string &_filename)
XERUS_force_inline void read_from_stream(std::istream &_stream, T &_obj, const FileFormat _format)
FileFormat
possible file formats for tensor storage
The xerus exception class.
Header file for xerus::misc::generic_error exception class.
Header file for all logging macros and log-buffer functionality.
#define XERUS_LOG(lvl,...)
logs the message msg with severity level lvl
void save_to_file(const T &_obj, const std::string &_filename, const FileFormat _format=FileFormat::BINARY)
#define XERUS_REQUIRE(condition, message)
Checks whether condition is true. logs an error otherwise via XERUS_LOG(error, message).
Header file for some elementary string manipulation routines.
XERUS_force_inline void write_to_stream(std::ostream &_stream, const T &_value, FileFormat _format)
void stream_writer(std::ostream &_stream, const Tensor &_obj, const FileFormat _format)
pipes all information necessary to restore the current tensor into _stream.
std::string XERUS_warn_unused demangle_cxa(const std::string &_cxa)
Demangles the function and class names created by gcc into a more readable format.
#define XERUS_force_inline
Collection of attributes to force gcc to inline a specific function.