-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstream_op_helper.hpp
155 lines (130 loc) · 3.81 KB
/
stream_op_helper.hpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <array>
#include <cstdint>
#include <cstring> // strlen
#include <iomanip> // quoted
#include <ios> // boolalpha
#include <ostream> // ostream
#include <sstream> // stringstream
#include <string>
#include <string_view>
#include <utility> // move
#include "databento/datetime.hpp" // TimeDeltaNanos, UnixNanos
namespace databento {
template <typename T>
// Helper for types that implement a stream operator
std::string MakeString(const T& val) {
std::ostringstream ss;
ss << val;
return ss.str();
}
class StreamOpHelper {
private:
template <typename T>
void FmtToStream(const T& val) {
stream_ << val;
}
void FmtToStream(const std::string& val) { stream_ << std::quoted(val); }
void FmtToStream(std::string_view val) { stream_ << std::quoted(val); }
void FmtToStream(const bool& val) {
// otherwise bool is formatted (1|0)
stream_ << std::boolalpha << val;
}
void FmtToStream(const char& val) { stream_ << '\'' << val << '\''; }
void FmtToStream(const std::uint8_t& val) {
// otherwise is formatted as a char
stream_ << static_cast<std::uint16_t>(val);
}
void FmtToStream(const std::int8_t& val) {
// otherwise is formatted as a char
stream_ << static_cast<std::int16_t>(val);
}
void FmtToStream(const UnixNanos& val) { stream_ << ToIso8601(val); }
void FmtToStream(const TimeDeltaNanos& val) { stream_ << ToString(val); }
void FmtToStream(const std::ostringstream& val) { stream_ << val.str(); }
template <std::size_t N>
void FmtToStream(const std::array<char, N>& val) {
stream_ << '"';
stream_.write(val.data(),
static_cast<std::streamsize>(::strlen(val.data())));
stream_ << '"';
}
public:
StreamOpHelper(std::ostream& stream, std::string_view type_name,
std::string spacer, std::string indent)
: stream_{stream},
spacer_{std::move(spacer)},
indent_{std::move(indent)} {
if (type_name.empty()) {
stream_ << '{';
} else {
stream_ << type_name << " {";
}
}
template <typename T>
StreamOpHelper& AddField(std::string_view field_name, const T& field_val) {
if (!is_first_) {
stream_ << ',';
}
stream_ << spacer_ << indent_ << field_name << " = ";
FmtToStream(field_val);
is_first_ = false;
return *this;
}
template <typename T>
StreamOpHelper& AddItem(const T& item) {
if (!is_first_) {
stream_ << ',';
}
stream_ << spacer_ << indent_;
FmtToStream(item);
is_first_ = false;
return *this;
}
std::ostream& Finish() {
if (spacer_.find('\n') == std::string::npos) {
// no spacing required if empty
if (!is_first_) {
stream_ << spacer_;
}
} else {
stream_ << '\n' << indent_;
}
stream_ << '}';
return stream_;
}
private:
std::ostream& stream_;
std::string spacer_;
std::string indent_;
bool is_first_{true};
};
class StreamOpBuilder {
public:
explicit StreamOpBuilder(std::ostream& stream) : stream_{stream} {}
StreamOpBuilder& SetTypeName(std::string type_name) {
type_name_ = std::move(type_name);
return *this;
}
// Sets what's inserted between the comma and the next element
StreamOpBuilder& SetSpacer(std::string spacer) {
spacer_ = std::move(spacer);
return *this;
}
// Sets any indentation that should be applied to all elements including the
// closing '}'. Primarily used for nested structures.
StreamOpBuilder& SetIndent(std::string indent) {
indent_ = std::move(indent);
return *this;
}
// Instantiate a `StreamOpHelper` with the current settings.
StreamOpHelper Build() {
return StreamOpHelper{stream_, type_name_, spacer_, indent_};
}
private:
std::ostream& stream_;
std::string indent_;
std::string type_name_;
std::string spacer_;
};
} // namespace databento