forked from jmzkChain/jmzk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevt_impl.hpp
56 lines (48 loc) · 1.23 KB
/
evt_impl.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
/**
* @file
* @copyright defined in evt/LICENSE.txt
*/
#pragma once
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <libevt/evt.h>
#include <fc/io/raw.hpp>
#define CATCH_AND_RETURN(err) \
catch(fc::exception& e) { \
evt_set_last_error(e.code()); \
return err; \
} \
catch(...) { \
evt_set_last_error(-1); \
return err; \
}
template <typename T>
evt_data_t*
get_evt_data(const T& val) {
auto rsz = fc::raw::pack_size<T>(val);
auto sz = sizeof(evt_data_t) + rsz;
auto data = (evt_data_t*)malloc(sz);
data->sz = rsz;
auto ds = fc::datastream<char*>(data->buf, rsz);
fc::raw::pack(ds, val);
return data;
}
template <typename T>
int
extract_data(evt_data_t* data, T& val) {
auto ds = fc::datastream<char*>(data->buf, data->sz);
try {
fc::raw::unpack(ds, val);
}
CATCH_AND_RETURN(EVT_INVALID_BINARY)
return EVT_OK;
}
inline char*
strdup(const std::string& str) {
auto s = (char*)malloc(str.size() + 1); // add '\0'
memcpy(s, str.data(), str.size());
s[str.size()] = '\0';
return s;
}