-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherl++.h
298 lines (240 loc) · 7.1 KB
/
erl++.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* This is free and unencumbered software released into the public domain. */
#ifndef ERLXX_H
#define ERLXX_H
/**
* <erl++.h>
*/
#ifndef __cplusplus
#error "<erl++.h> requires a C++ compiler"
#endif
#if __cplusplus < 201703L
#error "<erl++.h> requires a C++17 compiler (CXXFLAGS='-std=c++17')"
#endif
////////////////////////////////////////////////////////////////////////////////
#include <erl_interface.h>
#include <ei.h>
#include <cassert> /* for assert() */
#include <cstdio> /* for FILE */
#include <cstring> /* for strndup() */
#include <utility> /* for std::pair */
////////////////////////////////////////////////////////////////////////////////
namespace erl {
class term;
}
////////////////////////////////////////////////////////////////////////////////
class erl::term {
public:
using handle_ptr = std::unique_ptr<ETERM, void(*)(ETERM*)>;
protected:
// @see http://erlang.org/doc/man/erl_malloc.html#erl_free_term
handle_ptr _handle{nullptr, erl_free_term};
public:
static term decode(void* const buffer) {
ETERM* const handle = erl_decode(reinterpret_cast<unsigned char*>(buffer));
if (!handle) throw std::invalid_argument{"erl_decode()"};
return term{handle};
}
static term make_list() {
ETERM* const handle = erl_mk_empty_list();
if (!handle) throw std::invalid_argument{"erl_mk_empty_list()"};
return term{handle};
}
static term make_tuple(ETERM* first) {
ETERM* elements[1] = {first};
ETERM* const handle = erl_mk_tuple(elements, 1);
elements[0] = nullptr;
if (!handle) throw std::invalid_argument{"erl_mk_empty_list()"};
return term{handle};
}
static term make_tuple(ETERM* first, ETERM* second) {
ETERM* elements[2] = {first, second};
ETERM* const handle = erl_mk_tuple(elements, 2);
elements[0] = elements[1] = nullptr;
if (!handle) throw std::invalid_argument{"erl_mk_empty_list()"};
return term{handle};
}
static term make_pid() {
ETERM* const handle = erl_mk_pid(erl_thisnodename(), 1, 2, 3); // FIXME
if (!handle) throw std::invalid_argument{"erl_mk_long_ref()"};
return term{handle};
}
static term make_ref() {
ETERM* const handle = erl_mk_long_ref(erl_thisnodename(), 1, 2, 3, 0); // FIXME
if (!handle) throw std::invalid_argument{"erl_mk_long_ref()"};
return term{handle};
}
explicit term() noexcept
: _handle{nullptr, erl_free_term} {}
explicit term(ETERM* const handle) noexcept
: _handle{handle, erl_free_term} {}
const ETERM* handle() const noexcept {
return _handle.get();
}
ETERM* handle() noexcept {
return _handle.get();
}
ETERM* release() noexcept {
return _handle.release();
}
void reset(ETERM* const handle) noexcept {
_handle.reset(handle);
}
void print(FILE* const stream) const {
assert(handle());
assert(ERL_TYPE(handle()) != 0);
if (erl_print_term(stream, handle()) < 0) {
/* ignore errors */
}
}
bool is_int() const noexcept {
assert(handle());
return ERL_IS_INTEGER(handle());
}
bool is_uint() const noexcept {
assert(handle());
return ERL_IS_UNSIGNED_INTEGER(handle());
}
bool is_float() const noexcept {
assert(handle());
return ERL_IS_FLOAT(handle());
}
bool is_atom() const noexcept {
assert(handle());
return ERL_IS_ATOM(handle());
}
bool is_pid() const noexcept {
assert(handle());
return ERL_IS_PID(handle());
}
bool is_port() const noexcept {
assert(handle());
return ERL_IS_PORT(handle());
}
bool is_ref() const noexcept {
assert(handle());
return ERL_IS_REF(handle());
}
bool is_tuple() const noexcept {
assert(handle());
return ERL_IS_TUPLE(handle());
}
bool is_binary() const noexcept {
assert(handle());
return ERL_IS_BINARY(handle());
}
bool is_nil() const noexcept {
assert(handle());
return ERL_IS_NIL(handle());
}
bool is_empty_list() const noexcept {
assert(handle());
return ERL_IS_EMPTY_LIST(handle());
}
bool is_cons() const noexcept {
assert(handle());
return ERL_IS_CONS(handle());
}
bool is_list() const noexcept {
assert(handle());
return ERL_IS_LIST(handle());
}
std::size_t length() const {
assert(handle());
const auto result = erl_length(handle());
if (result == -1) {
throw std::logic_error{"not a proper list"};
}
return result;
}
std::size_t size() const {
assert(handle());
const auto result = erl_size(handle());
if (result == -1) {
throw std::logic_error{"not a tuple or binary object"};
}
return result;
}
std::size_t encoded_size() const {
assert(handle());
const auto result = erl_term_len(const_cast<ETERM*>(handle()));
return static_cast<std::size_t>(result);
}
erl::term operator[](const std::size_t index) {
assert(handle());
return erl::term{erl_element(index + 1, handle())};
}
std::size_t encode(void* buffer) const {
return ::erl_encode(const_cast<ETERM*>(handle()),
reinterpret_cast<unsigned char*>(buffer));
}
void prepend(ETERM* const element) {
_handle.reset(erl_cons(element, release()));
}
void prepend(const int element) {
prepend(erl_mk_int(element));
}
bool as_bool() const {
assert(handle());
return as_int() ? true : false;
}
int as_int() const {
assert(handle());
return ERL_INT_VALUE(handle());
}
long as_long() const {
assert(handle());
return as_int();
}
std::size_t as_size() const {
assert(handle());
return as_long();
}
float as_float() const {
assert(handle());
return ERL_FLOAT_VALUE(handle());
}
double as_double() const {
assert(handle());
return as_float();
}
char* as_c_str() const {
assert(handle());
return strndup(
reinterpret_cast<const char*>(ERL_BIN_PTR(handle())),
static_cast<std::size_t>(ERL_BIN_SIZE(handle())));
}
std::string as_string() const {
assert(handle());
switch (ERL_TYPE(handle())) {
case ERL_ATOM:
return std::string{
reinterpret_cast<const char*>(ERL_ATOM_PTR_UTF8(handle()))};
case ERL_BINARY:
return std::string{
reinterpret_cast<const char*>(ERL_BIN_PTR(handle())),
static_cast<std::size_t>(ERL_BIN_SIZE(handle()))};
default: break;
}
throw std::logic_error("not convertible to a string");
}
std::pair<std::size_t, std::size_t> as_size_pair() const {
assert(handle());
assert(ERL_IS_TUPLE(handle()));
assert(erl_size(handle()) == 2);
const erl::term first{erl_element(1, handle())};
const erl::term second{erl_element(2, handle())};
return {first.as_size(), second.as_size()};
}
};
////////////////////////////////////////////////////////////////////////////////
namespace erl {
template<typename... Args> inline term
format(const char* fmt, Args&&... args) {
// @see http://erlang.org/doc/man/erl_format.html#erl_format
ETERM* const handle = erl_format(const_cast<char*>(fmt), args...);
if (!handle) throw std::invalid_argument{"erl_format()"};
return term{handle};
}
}
////////////////////////////////////////////////////////////////////////////////
#endif /* ERLXX_H */