4#ifndef ADAPTYST_OUTPUT_HPP_
5#define ADAPTYST_OUTPUT_HPP_
8#include <nlohmann/json.hpp>
11#include <boost/hana.hpp>
12#include <boost/hana/ext/std/tuple.hpp>
15 namespace fs = std::filesystem;
16 namespace hana = boost::hana;
28 this->metadata = nlohmann::json::object();
42 if (this->metadata[key] != value) {
43 this->metadata[key] = value;
63 return this->metadata.value(key, default_value);
76 return this->metadata[key].get<T>();
96 void setup(fs::path path) {
97 this->path = fs::absolute(path);
99 fs::create_directories(this->path);
100 }
catch (std::exception &e) {
101 throw std::runtime_error(
"Could not create directory " +
102 this->path.string() +
": " +
103 std::string(e.what()));
106 fs::path metadata_path = this->path /
"dirmeta.json";
108 if (fs::exists(metadata_path)) {
109 std::ifstream metadata_stream(metadata_path);
111 if (!metadata_stream) {
112 throw std::runtime_error(
"Could not open " +
113 (this->path /
"dirmeta.json").
string() +
117 std::string json_str((std::istreambuf_iterator<char>(metadata_stream)),
118 std::istreambuf_iterator<char>());
119 this->
metadata = nlohmann::json::parse(json_str);
139 return this->path.c_str();
148 return Path(this->path / second);
158 return Path(this->path / second);
165 std::ofstream metadata_stream(this->path /
"dirmeta.json");
167 if (!metadata_stream) {
168 throw std::runtime_error(
"Could not open " +
169 (this->path /
"dirmeta.json").
string() +
173 metadata_stream << this->
metadata.dump() << std::endl;
200 std::string extension =
"",
201 bool truncate =
true) :
path(
path) {
204 fs::path new_path =
path.path / (
name + extension);
206 if (fs::exists(new_path)) {
207 if (fs::is_directory(new_path)) {
208 throw std::runtime_error(new_path.string() +
" is "
211 this->istream = std::ifstream(new_path);
215 this->ostream = std::ofstream(new_path, std::ios_base::out |
217 std::ios_base::trunc : std::ios_base::app));
219 if (!this->ostream) {
220 throw std::runtime_error(
"Could not open " +
225 fs::path metadata_path =
path.path / (
"meta_" + this->name +
".json");
227 if (fs::exists(metadata_path)) {
228 std::ifstream metadata_stream(metadata_path);
230 if (!metadata_stream) {
231 throw std::runtime_error(
"Could not open " +
232 (
path.path / (
"meta_" + this->name +
".json")).string() +
236 std::string json_str((std::istreambuf_iterator<char>(metadata_stream)),
237 std::istreambuf_iterator<char>());
238 this->
metadata = nlohmann::json::parse(json_str);
243 this->path = source.path;
244 this->name = source.name;
245 this->istream = std::move(source.istream);
246 this->ostream = std::move(source.ostream);
247 this->
metadata.swap(source.metadata);
257 return this->istream;
266 return this->ostream;
273 fs::path metadata_path =
path.path / (
"meta_" + this->name +
".json");
275 std::ofstream metadata_stream(metadata_path);
277 if (!metadata_stream) {
278 throw std::runtime_error(
"Could not open " + metadata_path.string() +
282 metadata_stream << this->
metadata.dump() << std::endl;
288 typename T::first_type;
289 typename T::second_type;
290 } && std::is_same_v<T, std::pair<
typename T::first_type,
291 typename T::second_type> >;
295 std::tuple_size<T>::value;
319 if constexpr (is_pair<T>) {
320 this->istream >> val.first;
321 this->istream >> val.second;
323 hana::for_each(val, [&](
auto &item) {
344 return this->vec[index];
353 return this->vec.size();
363 this->vec.push_back(val);
366 this->
ostream << val.first <<
" " << val.second << std::endl;
368 std::stringstream str_stream;
369 hana::for_each(val, [&](
auto &item) {
370 str_stream << item <<
" ";
373 std::string to_write = str_stream.str();
374 to_write = to_write.substr(0, to_write.length() - 1);
376 this->
ostream << to_write << std::endl;
378 this->
ostream << val << std::endl;
T operator[](int index)
Definition output.hpp:343
void push_back(T val)
Definition output.hpp:362
int size()
Definition output.hpp:352
Array(Path &path, std::string name)
Definition output.hpp:315
File & operator=(File &&source)
Definition output.hpp:242
File(Path &path, std::string name, std::string extension="", bool truncate=true)
Definition output.hpp:199
std::ofstream ostream
Definition output.hpp:187
std::string name
Definition output.hpp:185
std::ifstream & get_istream()
Definition output.hpp:256
Path & path
Definition output.hpp:184
std::ofstream & get_ostream()
Definition output.hpp:265
void save_metadata()
Definition output.hpp:272
std::ifstream istream
Definition output.hpp:186
void save_metadata()
Definition output.hpp:164
const char * get_path_name()
Definition output.hpp:138
Path operator/(const char *second)
Definition output.hpp:157
friend class File
Definition output.hpp:91
Path(fs::path path)
Definition output.hpp:129
Path operator/(std::string second)
Definition output.hpp:147
Definition output.hpp:287
Definition output.hpp:294