Skip to content

Commit 70819d1

Browse files
committed
Add name to nameless struct and union members
1 parent 100f343 commit 70819d1

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

include/fine.hpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,15 @@ template <typename T> struct ResourceWrapper {
167167
union {
168168
struct {
169169
bool initialized;
170-
};
170+
} data;
171171
std::max_align_t _unused;
172-
};
172+
} payload;
173+
174+
bool initialized() const { return payload.data.initialized; }
175+
176+
void set_initialized(bool initialized) {
177+
payload.data.initialized = initialized;
178+
}
173179

174180
const T *resource() const { return reinterpret_cast<const T *>(this + 1); }
175181

@@ -183,7 +189,7 @@ template <typename T> struct ResourceWrapper {
183189
static void dtor(ErlNifEnv *env, void *ptr) {
184190
auto resource_wrapper = reinterpret_cast<ResourceWrapper<T> *>(ptr);
185191

186-
if (resource_wrapper->initialized) {
192+
if (resource_wrapper->initialized()) {
187193
if constexpr (has_destructor<T>::value) {
188194
resource_wrapper->resource()->destructor(env);
189195
}
@@ -297,14 +303,14 @@ ResourcePtr<T> make_resource(Args &&...args) {
297303
// We use a wrapper struct with an extra field to track if the
298304
// resource has actually been initialized. This way if the constructor
299305
// below throws, we can skip the destructor calls in the Erlang dtor
300-
resource_wrapper->initialized = false;
306+
resource_wrapper->set_initialized(false);
301307

302308
// Invoke the constructor with prefect forwarding to initialize the
303309
// object at the VM-allocated memory
304310
new (reinterpret_cast<U *>(resource_wrapper->resource()))
305311
U(std::forward<Args>(args)...);
306312

307-
resource_wrapper->initialized = true;
313+
resource_wrapper->set_initialized(true);
308314

309315
return resource;
310316
}

0 commit comments

Comments
 (0)