Skip to content

Commit

Permalink
Refactor code to unify Buf structure and use FfiBuf
Browse files Browse the repository at this point in the history
The changes unify the `RBuf` and `SBuf` structures into one, `Buf`, which takes ownership of its data. The `FfiId`, `FfiStr`, `FfiStrVec` types have been replaced across the code with `FfiBuf`,
  • Loading branch information
xorza committed Jun 9, 2024
1 parent 2ede8eb commit 1c291f8
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 84 deletions.
2 changes: 1 addition & 1 deletion ScenariumEditor.QML/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) {
{
auto ctx = Ctx{};
auto funcs = ctx.get_funcs();
ctx.new_node(funcs[0].id);
auto new_node = ctx.new_node(funcs[0].id);
auto nodes = ctx.get_nodes();
}

Expand Down
130 changes: 61 additions & 69 deletions ScenariumEditor.QML/src/CoreContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ struct FfiBuf {
}


struct RBuf {
struct Buf {
FfiBuf ffi_buf;
bool owns_data = true;

explicit RBuf(FfiBuf ffi_buf) : ffi_buf(ffi_buf) {}
explicit Buf(FfiBuf ffi_buf) : ffi_buf(ffi_buf), owns_data(false) {}

~RBuf();
explicit Buf(const std::string &str) {
ffi_buf.len = str.size();
ffi_buf.cap = str.size();
ffi_buf.data = malloc(ffi_buf.len);
memcpy(ffi_buf.data, str.data(), ffi_buf.len);
owns_data = true;
}


~Buf();

[[nodiscard]] uint32_t len() const {
return ffi_buf.len;
Expand Down Expand Up @@ -58,28 +68,9 @@ struct RBuf {
}
};

struct SBuf {
FfiBuf ffi_buf{};

explicit SBuf(const std::string &str) {
ffi_buf.len = str.size();
ffi_buf.cap = str.size();
ffi_buf.data = malloc(ffi_buf.len);
memcpy(ffi_buf.data, str.data(), ffi_buf.len);
}

~SBuf() {
if (ffi_buf.data != nullptr) {
free(ffi_buf.data);
}
ffi_buf.data = nullptr;
ffi_buf.len = 0;
ffi_buf.cap = 0;
}
};

template<>
std::vector<std::string> RBuf::read_vec<std::string>() const {
std::vector<std::string> Buf::read_vec<std::string>() const {
if (len() == 0) {
return {};
}
Expand All @@ -97,28 +88,29 @@ std::vector<std::string> RBuf::read_vec<std::string>() const {
}

struct FfiFunc {
RBuf id;
RBuf name;
RBuf category;
FfiBuf id;
FfiBuf name;
FfiBuf category;
uint32_t behaviour;
bool output;
RBuf inputs;
RBuf outputs;
RBuf events;
FfiBuf inputs;
FfiBuf outputs;
FfiBuf events;
};



extern "C" {
struct FfiNode {
RBuf id;
RBuf func_id;
RBuf name;
FfiBuf id;
FfiBuf func_id;
FfiBuf name;
bool output;
bool cache_outputs;
RBuf inputs;
RBuf outputs;
FfiBuf inputs;
FfiBuf outputs;
};

extern "C" {

__declspec(dllimport) void *create_context();
__declspec(dllimport) void destroy_context(void *ctx);
__declspec(dllimport) void destroy_ffi_buf(FfiBuf buf);
Expand All @@ -129,8 +121,17 @@ __declspec(dllimport) FfiNode new_node(void *ctx, FfiBuf func_id);

}

RBuf::~RBuf() {
destroy_ffi_buf(ffi_buf);
Buf::~Buf() {
if (owns_data) {
if (ffi_buf.data != nullptr) {
free(ffi_buf.data);
}
ffi_buf.data = nullptr;
ffi_buf.len = 0;
ffi_buf.cap = 0;
} else {
destroy_ffi_buf(ffi_buf);
}
}


Expand All @@ -144,67 +145,58 @@ Ctx::~Ctx() {
}

std::vector<Func> Ctx::get_funcs() const {
RBuf buf = RBuf{::get_funcs(this->ctx)};
Buf buf = Buf{::get_funcs(this->ctx)};

auto funcs = buf.read_vec<FfiFunc>();
std::vector<Func> result;
result.reserve(funcs.size());

for (uint32_t i = 0; i < funcs.size(); i++) {
auto ffi_func = &funcs[i];

auto events = ffi_func->events.read_vec<std::string>();

Func func{
ffi_func->id.to_string(),
ffi_func->name.to_string(),
ffi_func->category.to_string(),
ffi_func->behaviour,
ffi_func->output,
{},
{},
events,
};
Func func{funcs[i]};
result.push_back(func);
}

return result;
}

std::vector<Node> Ctx::get_nodes() const {
RBuf buf = RBuf{::get_nodes(this->ctx)};
Buf buf = Buf{::get_nodes(this->ctx)};

auto nodes = buf.read_vec<FfiNode>();
std::vector<Node> result;
result.reserve(nodes.size());

for (uint32_t i = 0; i < nodes.size(); i++) {
auto ffi_node = &nodes[i];
Node node{
ffi_node->id.to_string(),
ffi_node->func_id.to_string(),
ffi_node->name.to_string(),
ffi_node->output,
ffi_node->cache_outputs,
{},
{},
};
Node node{nodes[i]};
result.push_back(node);
}

return result;
}

void Ctx::new_node(const std::string &func_id) const {
auto buf = SBuf{func_id};

Node Ctx::new_node(const std::string &func_id) const {
auto buf = Buf{func_id};
auto ffi_node = ::new_node(this->ctx, buf.ffi_buf);
return Node{ffi_node};
}

Func::Func(const FfiFunc &ffi_func) {

this->id = Buf(ffi_func.id).to_string();
this->name = Buf(ffi_func.name).to_string();
this->category = Buf(ffi_func.category).to_string();
this->behaviour = ffi_func.behaviour;
this->output = ffi_func.output;
this->inputs = {};
this->outputs = {};
this->events = Buf(ffi_func.events).read_vec<std::string>();
}

Node::Node(const FfiNode &ffi_node) {

this->id = Buf(ffi_node.id).to_string();
this->func_id = Buf(ffi_node.func_id).to_string();
this->name = Buf(ffi_node.name).to_string();
this->output = ffi_node.output;
this->cache_outputs = ffi_node.cache_outputs;
this->inputs = {};
this->outputs = {};
}
2 changes: 1 addition & 1 deletion ScenariumEditor.QML/src/CoreContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ struct Ctx {

[[nodiscard]] std::vector<Node> get_nodes() const;

void new_node(const std::string &func_id) const;
Node new_node(const std::string &func_id) const;
};

16 changes: 8 additions & 8 deletions cs_interop/src/func_lib_api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::ffi::c_void;
use crate::{get_context, FfiBuf, FfiId, FfiStr, FfiStrVec};
use crate::{get_context, FfiBuf};

#[repr(C)]
#[derive(Debug)]
Expand All @@ -11,14 +11,14 @@ pub enum FuncBehavior {
#[repr(C)]
#[derive(Debug)]
struct FfiFunc {
id: FfiId,
name: FfiStr,
category: FfiStr,
id: FfiBuf, // string
name: FfiBuf, // string
category: FfiBuf, // string
behaviour: FuncBehavior,
is_output: bool,
inputs: FfiBuf,
outputs: FfiBuf,
events: FfiStrVec,
inputs: FfiBuf, // vector of
outputs: FfiBuf, // vector of
events: FfiBuf, // vector of strings
}

#[no_mangle]
Expand All @@ -36,7 +36,7 @@ extern "C" fn get_funcs(ctx: *mut c_void) -> FfiBuf {

impl From<&graph::function::Func> for FfiFunc {
fn from(func: &graph::function::Func) -> Self {
let events: FfiStrVec = FfiStrVec::from_iter(
let events: FfiBuf = FfiBuf::from_iter(
func.events
.iter()
.map(|event| event.name.clone())
Expand Down
10 changes: 5 additions & 5 deletions cs_interop/src/graph_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ struct FfiInput {

#[repr(C)]
struct FfiNode {
id: FfiBuf,
func_id: FfiBuf,
name: FfiBuf,
id: FfiBuf, // string
func_id: FfiBuf, // string
name: FfiBuf, // string
is_output: bool,
cache_outputs: bool,
inputs: FfiBuf,
events: FfiBuf,
inputs: FfiBuf, // vector of
events: FfiBuf, // vector of ids of subscriber nodes
}

impl From<&graph::graph::Node> for FfiNode {
Expand Down

0 comments on commit 1c291f8

Please sign in to comment.