This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.hpp
100 lines (88 loc) · 3.35 KB
/
utils.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
#ifndef LIB_RUBY_PARSER_UTILS_HPP
#define LIB_RUBY_PARSER_UTILS_HPP
#include <cassert>
#include <cstring>
#include <cstdint>
#include <utility>
#define BLOB(T) \
extern "C" \
{ \
struct T##Blob \
{ \
uint8_t bytes[sizeof(T)]; \
}; \
}
template <typename T, typename B>
B into_blob(T t)
{
assert(sizeof(T) == sizeof(B));
union U
{
T t;
B b;
U() { std::memset(this, 0, sizeof(U)); }
~U() {}
};
U u;
u.t = std::move(t);
return u.b;
}
template <typename B, typename T>
T from_blob(B b)
{
assert(sizeof(T) == sizeof(B));
union U
{
T t;
B b;
U() { std::memset(this, 0, sizeof(U)); }
~U() {}
};
U u;
u.b = b;
return std::move(u.t);
}
#define LIST_IMPL(LIST, ITEM, DROP) \
LIST::LIST(ITEM *ptr_, \
size_t len_, \
size_t capacity_) : ptr(ptr_), \
capacity(capacity_), \
len(len_) {} \
\
extern "C" \
{ \
void DROP(LIST *list); \
} \
LIST::~LIST() \
{ \
DROP(this); \
this->ptr = nullptr; \
this->len = 0; \
this->capacity = 0; \
} \
LIST::LIST(LIST &&other) \
{ \
this->ptr = other.ptr; \
this->len = other.len; \
this->capacity = other.capacity; \
\
other.ptr = nullptr; \
other.len = 0; \
other.capacity = 0; \
} \
LIST &LIST::operator=(LIST &&other) \
{ \
this->ptr = other.ptr; \
this->len = other.len; \
this->capacity = other.capacity; \
\
other.ptr = nullptr; \
other.len = 0; \
other.capacity = 0; \
\
return *this; \
} \
class Ignore##LIST \
{ \
}
#endif // LIB_RUBY_PARSER_UTILS_HPP