bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
csv_file_writer.h
Go to the documentation of this file.
1#ifndef CSV_FILE_WRITER_H
2#define CSV_FILE_WRITER_H
3
4#include "bitrl/bitrl_types.h"
6
7#include <initializer_list>
8#include <tuple>
9#include <vector>
10
11namespace bitrl
12{
13namespace utils
14{
15namespace io
16{
17
22{
23
24 public:
28 static char default_delimiter() { return ','; }
29
33 CSVWriter(const std::string &filename, char delim = CSVWriter::default_delimiter());
34
38 void write_column_names(const std::vector<std::string> &col_names, bool write_header = true);
39
43 void write_column_names(const std::vector<std::string_view> &col_names,
44 bool write_header = true);
45
49 void write_column_names(const std::initializer_list<std::string_view> &col_names,
50 bool write_header = true);
51
55 template <typename T> void write_row(const std::vector<T> &vals);
56
60 template <typename T> void write_row(const DynVec<T> &vals);
61
65 template <typename T> void write_column_vector(const std::vector<T> &vals);
66
70 template <typename... T> void write_row(const std::tuple<T...> &row);
71
75 void set_delimiter(char delim) noexcept { delim_ = delim; }
76
80 char get_delimiter() const noexcept { return delim_; }
81
82 private:
86 char delim_;
87};
88
89template <typename T> void CSVWriter::write_row(const std::vector<T> &vals)
90{
91
92 if (!this->is_open())
93 {
94 throw std::logic_error("File " + this->file_name_ + " is not open");
95 }
96
97 auto &f = this->get_file_stream();
98 for (uint_t c = 0; c < vals.size(); ++c)
99 {
100
101 f << vals[c];
102
103 if (c == vals.size() - 1)
104 {
105 f << std::endl;
106 }
107 else
108 {
109 f << ",";
110 }
111 }
112}
113
114template <typename T> void CSVWriter::write_row(const DynVec<T> &vals)
115{
116
117 if (!this->is_open())
118 {
119 throw std::logic_error("File " + this->file_name_ + " is not open");
120 }
121
122 auto &f = this->get_file_stream();
123 for (uint_t c = 0; c < static_cast<uint_t>(vals.size()); ++c)
124 {
125
126 f << vals[c];
127
128 if (c == static_cast<uint_t>(vals.size()) - 1)
129 {
130 f << std::endl;
131 }
132 else
133 {
134 f << ",";
135 }
136 }
137}
138
139template <typename T> void CSVWriter::write_column_vector(const std::vector<T> &vals)
140{
141
142 if (!this->is_open())
143 {
144 throw std::logic_error("File " + this->file_name_ + " is not open");
145 }
146
147 auto &f = this->get_file_stream();
148 for (uint_t c = 0; c < vals.size(); ++c)
149 {
150 f << vals[c] << std::endl;
151 }
152}
153
154template <typename... T> void CSVWriter::write_row(const std::tuple<T...> &row)
155{
156
157 auto &f = this->get_file_stream();
158 std::apply([&](auto &&...args) { ((f << args << ","), ...); }, row);
159 f << std::endl;
160}
161
162} // namespace io
163} // namespace utils
164} // namespace bitrl
165
166#endif // CSV_FILE_WRITER_H
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
void write_column_names(const std::vector< std::string > &col_names, bool write_header=true)
Write the column names.
Definition csv_file_writer.cpp:16
char get_delimiter() const noexcept
Returns the column delimiter.
Definition csv_file_writer.h:80
void write_row(const std::vector< T > &vals)
Write a row of the file.
Definition csv_file_writer.h:89
void write_column_vector(const std::vector< T > &vals)
Write the given vector as a column.
Definition csv_file_writer.h:139
static char default_delimiter()
The default column delimiter.
Definition csv_file_writer.h:28
void set_delimiter(char delim) noexcept
Set the delimiter.
Definition csv_file_writer.h:75
handler_type & get_file_stream() noexcept
Returns the underlying file stream.
Definition file_handler_base.h:39
std::string file_name_
The name of the file to write.
Definition file_handler_base.h:81
bool is_open() const noexcept
Return true if and only if the file is open.
Definition file_handler_base.h:55
The FileWriterBase class.
Definition file_writer_base.h:20
virtual void write_header()
Write the header of the file. By default some information such as date and time the file was created ...
Definition file_writer_base.cpp:59
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
Definition bitrl_consts.h:14
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
std::size_t uint_t
uint_t
Definition bitrl_types.h:43