Skip to content

Commit

Permalink
centralize Deleter
Browse files Browse the repository at this point in the history
  • Loading branch information
cpetig committed May 4, 2024
1 parent 88dbae6 commit a949b10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions crates/cpp/helper-types/wit-guest.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>
#include <string_view>
#include <string>
#include <memory> // unique_ptr
#include "wit-common.h"

namespace wit {
Expand Down Expand Up @@ -82,6 +83,11 @@ class vector {

template <class R> class ResourceExportBase {
public:
struct Deleter {
void operator()(R* ptr) const { R::Dtor(ptr); }
};
typedef std::unique_ptr<R, Deleter> Owned;

static const int32_t invalid = -1;

int32_t handle;
Expand Down
14 changes: 12 additions & 2 deletions crates/cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct Includes {
needs_tuple: bool,
// needs wit types
needs_wit: bool,
needs_memory: bool,
}

#[derive(Clone)]
Expand Down Expand Up @@ -563,6 +564,9 @@ impl WorldGenerator for Cpp {
self.include("<wit-guest.h>");
}
}
if self.dependencies.needs_memory {
self.include("<memory>");
}

if self.opts.short_cut {
uwriteln!(h_str.src, "#define WIT_HOST_DIRECT");
Expand Down Expand Up @@ -1145,9 +1149,9 @@ impl CppInterfaceGenerator<'_> {
uwrite!(
self.gen.h_src.src,
"{{\
return new {}({});\
return Owned(new {}({}));\
}}",
cpp_sig.namespace.join("::"),
cpp_sig.namespace.last().unwrap(), //join("::"),
cpp_sig
.arguments
.iter()
Expand Down Expand Up @@ -1786,6 +1790,8 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for CppInterfaceGenerator<'a>
self.gen.dependencies.needs_exported_resources = true;
}
self.gen.dependencies.needs_wit = true;
// for unique_ptr
// self.gen.dependencies.needs_memory = true;

let base_type = match (definition, self.gen.opts.host_side()) {
(true, false) => format!("wit::{RESOURCE_EXPORT_BASE_CLASS_NAME}<{pascal}>"),
Expand Down Expand Up @@ -1822,6 +1828,10 @@ impl<'a> wit_bindgen_core::InterfaceGenerator<'a> for CppInterfaceGenerator<'a>
};
self.generate_function(&func, &TypeOwner::Interface(intf), variant);
}
// uwriteln!(self.gen.h_src.src, "struct Deleter {{
// void operator()({pascal}* ptr) const {{ {pascal}::Dtor(ptr); }}
// }};
// typedef std::unique_ptr<{pascal}, {pascal}::Deleter> Owned;");
let funcs = self.resolve.interfaces[intf].functions.values();
for func in funcs {
if match &func.kind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class R : public wit::ResourceExportBase<R> {

public:
static void Dtor(R *self) { delete self; };
struct Deleter {
void operator()(R* ptr) const { R::Dtor(ptr); }
};
typedef std::unique_ptr<R, R::Deleter> Owned;
R(uint32_t a) : value(a) {}
static Owned New(uint32_t a) { return Owned(new R(a)); }
void Add(uint32_t b) { value += b; }
Expand Down

0 comments on commit a949b10

Please sign in to comment.