forked from parihaaraka/cpp2tnt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmp_writer.h
More file actions
276 lines (248 loc) · 7.48 KB
/
mp_writer.h
File metadata and controls
276 lines (248 loc) · 7.48 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#ifndef MP_WRITER_H
#define MP_WRITER_H
#include <string_view>
#include <array>
#include <map>
#include <tuple>
#include <optional>
#include "msgpuck/msgpuck.h"
#include "wtf_buffer.h"
namespace tnt {
class connection;
enum class request_type : uint8_t;
}
/** msgpuck wrapper.
*
* A caller must ensure there is enough free space in the buffer.
*/
class mp_writer
{
public:
/// Wrap writer object around specified buffer.
mp_writer(wtf_buffer &buf);
/// Put array header with <max_size> cardinality and move current position over it.
/// A caller must call finalize() to close the array and actualize its initial size.
void begin_array(uint32_t max_cardinality);
/// Put map header with <max_size> cardinality and move current position over it.
/// A caller must call finalize() to close the map and actualize its initial size.
void begin_map(uint32_t max_cardinality);
/// Replace initial cardinality settled with begin_array(), begin_map() with actual
/// value (counted untill now).
void finalize();
mp_writer& operator<< (const std::string_view &val);
mp_writer& operator<< (const mp_writer &data);
template <typename T>
mp_writer& operator<< (const std::optional<T> &val) noexcept
{
if (!val.has_value())
{
_buf.end = mp_encode_nil(_buf.end);
if (!_opened_containers.empty())
++_opened_containers.top().items_count;
return *this;
}
*this << val.value();
return *this;
}
/*
template <typename T>
mp_writer& operator<< (const std::vector<T> &val) noexcept
{
begin_array(val.size());
for( auto& it : val )
*this<<it;
finalize();
return *this;
}
template <typename K,typename V>
mp_writer& operator<< (const std::map<K,V>& val) noexcept
{
begin_map(val.size());
for( auto& it : val )
*this << it.first << it.second;
finalize();
return *this;
}
*/
template <typename T, typename = std::enable_if_t<std::is_integral_v<T> && sizeof(T) < 16>>
mp_writer& operator<< (const T &val) noexcept
{
if constexpr (std::is_same_v<T, bool>)
{
_buf.end = mp_encode_bool(_buf.end, val);
}
else
{
_buf.end = (val >= 0 ?
mp_encode_uint(_buf.end, static_cast<uint64_t>(val)) :
mp_encode_int(_buf.end, val));
}
if (!_opened_containers.empty())
++_opened_containers.top().items_count;
return *this;
}
/*
template<class Tuple, size_t N>
class TupleWriter
{
public:
static void out_tuple(mp_writer *stream, const Tuple &tuple)
{
TupleWriter<Tuple, N - 1>::out_tuple(stream, tuple);
*stream << std::get<N - 1>(tuple);
}
};
template<class Tuple>
class TupleWriter<Tuple, 1>
{
public:
static void out_tuple(mp_writer *stream, const Tuple &tuple)
{
*stream << std::get<0>(tuple);
}
};
template<class Tuple>
class TupleWriter<Tuple, 0>
{
public:
static void out_tuple(mp_writer *stream, const Tuple &tuple)
{}
};
template<typename... Args>
mp_writer& operator<<(const std::tuple<Args...> &value)
{
begin_array(std::tuple_size<std::tuple<Args...>>::value);
TupleWriter<std::tuple<Args...>, sizeof...(Args)>::out_tuple(this, value);
finalize();
return *this;
}
*/
template<typename... Args>
mp_writer& operator<< (const std::tuple<Args...> &val)
{
begin_array(std::tuple_size_v<std::tuple<Args...>>);
std::apply(
[this](const auto&... item)
{
((*this << item), ...);
},
val
);
finalize();
return *this;
}
template <typename T>
mp_writer& operator<< (const std::vector<T> &val)
{
begin_array(val.size());
for (const auto& elem: val)
*this << elem;
finalize();
return *this;
}
template <typename KeyT, typename ValueT>
mp_writer& operator<< (const std::map<KeyT, ValueT> &val)
{
begin_map(val.size());
for (const auto& elem: val)
{
*this << elem.first;
*this << elem.second;
}
finalize();
return *this;
}
protected:
template <typename T, std::size_t N = 16>
class wtf_stack
{
private:
std::array<T, N> _items;
size_t _size = 0;
public:
wtf_stack() = default;
void push(T &&value)
{
_items[_size++] = std::move(value);
}
T& pop() noexcept // undefined if empty
{
if (!_size)
return _items[_size];
return _items[--_size];
}
T& top() noexcept // undefined if empty
{
return _items[_size ? _size - 1 : _size];
}
size_t size() const noexcept
{
return _size;
}
bool empty() const noexcept
{
return _size == 0;
}
};
struct container_meta
{
size_t head_offset; // we'll detect container type by the first byte
uint32_t max_cardinality;
uint32_t items_count = 0; // map will have 2x items
};
wtf_stack<container_meta> _opened_containers;
wtf_buffer &_buf;
};
/** Helper to compose requests.
*
* A caller must ensure there is enough free space in the underlying buffer.
*/
class iproto_writer : public mp_writer
{
public:
/// Wrap writer object around cn.output_buffer().
iproto_writer(tnt::connection &cn);
/// Wrap writer object around specified buffer.
iproto_writer(tnt::connection &cn, wtf_buffer &buf);
/// Initiate request of specified type. A caller must compose a body afterwards and call finalize()
/// to finalize request.
void encode_header(tnt::request_type req_type);
/// Put authentication request into the underlying buffer. User and password
/// from connection string are used.
void encode_auth_request();
/// Put authentication request into the underlying buffer.
void encode_auth_request(std::string_view user, std::string_view password);
/// Put ping request into the underlying buffer.
void encode_ping_request();
/// Initiate call request. A caller must pass an array of arguments afterwards
/// and call finalize() to finalize request.
void begin_call(std::string_view fn_name);
void begin_eval(std::string_view script);
/// Call request all-in-one wrapper.
template <typename ...Ts>
void call(std::string_view fn_name, Ts const&... args)
{
begin_call(fn_name);
begin_array(sizeof...(args));
((*this << args), ...);
finalize_all();
}
/// Call request all-in-one wrapper.
template <typename ...Ts>
void eval(std::string_view script, Ts const&... args)
{
begin_eval(script);
begin_array(sizeof...(args));
((*this << args), ...);
finalize_all();
}
/// Replace initial request size settled with begin_call() or encode_header()
/// with actual value (counted untill now).
void finalize();
/// Finalize all non-finalized containers and call (if exists).
void finalize_all();
using mp_writer::operator<<;
private:
tnt::connection &_cn; ///< request id, initial user name and password source
};
#endif // MP_WRITER_H