forked from andreacasalino/csvcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Write.h
32 lines (27 loc) · 800 Bytes
/
Write.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include <csvpp/String.h>
#include <fstream>
#include <sstream>
#include <type_traits>
namespace csvpp {
template <typename... Ts> class Writer {
public:
template <typename... Headers>
Writer(const std::string &fileName, Headers &&...fields) : stream_(fileName) {
static_assert(sizeof...(Headers) == sizeof...(Ts),
"number of fields should be equal to number of types");
if (!stream_.is_open()) {
std::stringstream err;
err << fileName << " is an invalid path" << std::endl;
throw std::runtime_error{err.str()};
}
join(stream_, ',', std::forward<Headers>(fields)...);
}
void add(const Ts &...values) {
stream_ << '\n';
join(stream_, ',', values...);
}
protected:
std::ofstream stream_;
};
} // namespace csvpp