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
#ifndef WASMTIME_HELPERS_HH
#define WASMTIME_HELPERS_HH
#include <memory>
#define WASMTIME_OWN_WRAPPER(name, capi_type) \
public: \
/** \
* \brief Non-owning reference to an instance of this type. \
*/ \
using Raw = capi_type##_t; \
\
private: \
struct deleter { \
void operator()(Raw *p) const { capi_type##_delete(p); } \
}; \
\
std::unique_ptr<Raw, deleter> ptr; \
\
public: \
/** \
* \brief Takes ownership of `raw` and wraps it with this class. \
*/ \
explicit name(Raw *raw) : ptr(raw) {} \
\
~name() = default; \
\
/** \
* \brief Moves type information from another type into this one. \
*/ \
name(name &&other) = default; \
\
/** \
* \brief Moves type information from another type into this one. \
*/ \
name &operator=(name &&other) = default; \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
const Raw *capi() const { return ptr.get(); } \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
Raw *capi() { return ptr.get(); } \
\
/** \
* \brief Releases the underlying C API pointer. \
*/ \
Raw *capi_release() { return ptr.release(); } \
\
/** \
* Converts the raw C API representation to this class without taking \
* ownership. \
*/ \
static const name *from_capi(Raw *const *capi) { \
static_assert(sizeof(name) == sizeof(void *)); \
return reinterpret_cast<const name *>(capi); \
}
#define WASMTIME_CLONE_WRAPPER(name, capi_type) \
WASMTIME_OWN_WRAPPER(name, capi_type) \
\
public: \
/** \
* \brief Copies another type into this one. \
*/ \
name(const name &other) : ptr(capi_type##_clone(other.ptr.get())) {} \
\
/** \
* \brief Copies another type into this one. \
*/ \
name &operator=(const name &other) { \
ptr.reset(capi_type##_clone(other.ptr.get())); \
return *this; \
}
#define WASMTIME_CLONE_EQUAL_WRAPPER(name, capi_type) \
WASMTIME_CLONE_WRAPPER(name, capi_type) \
\
/** \
* \brief Compares two types for equality. \
*/ \
bool operator==(const name &other) const { \
return capi_type##_equal(ptr.get(), other.ptr.get()); \
} \
\
/** \
* \brief Compares two types for inequality. \
*/ \
bool operator!=(const name &other) const { return !(*this == other); }
#endif // WASMTIME_HELPERS_HH