mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 06:11:16 +00:00
Add internal convenience API to construct a C++ slice from a string view
This commit is contained in:
parent
591ae8557b
commit
0c21008861
2 changed files with 24 additions and 28 deletions
|
@ -374,22 +374,16 @@ public:
|
||||||
|
|
||||||
bool set_property(std::string_view name, const Value &value) const
|
bool set_property(std::string_view name, const Value &value) const
|
||||||
{
|
{
|
||||||
cbindgen_private::Slice<uint8_t> name_view {
|
using namespace cbindgen_private;
|
||||||
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
|
return sixtyfps_interpreter_component_instance_set_property(
|
||||||
name.size()
|
&inner, Slice<uint8_t>::from_string(name), &value.inner);
|
||||||
};
|
|
||||||
return cbindgen_private::sixtyfps_interpreter_component_instance_set_property(
|
|
||||||
&inner, name_view, &value.inner);
|
|
||||||
}
|
}
|
||||||
std::optional<Value> get_property(std::string_view name) const
|
std::optional<Value> get_property(std::string_view name) const
|
||||||
{
|
{
|
||||||
cbindgen_private::Slice<uint8_t> name_view {
|
using namespace cbindgen_private;
|
||||||
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
|
ValueOpaque out;
|
||||||
name.size()
|
if (sixtyfps_interpreter_component_instance_get_property(
|
||||||
};
|
&inner, Slice<uint8_t>::from_string(name), &out)) {
|
||||||
cbindgen_private::ValueOpaque out;
|
|
||||||
if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property(
|
|
||||||
&inner, name_view, &out)) {
|
|
||||||
return Value(out);
|
return Value(out);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
|
@ -398,16 +392,11 @@ public:
|
||||||
// FIXME! Slice in public API? Should be std::span (c++20) or we need to improve the Slice API
|
// FIXME! Slice in public API? Should be std::span (c++20) or we need to improve the Slice API
|
||||||
std::optional<Value> invoke_callback(std::string_view name, Slice<Value> args) const
|
std::optional<Value> invoke_callback(std::string_view name, Slice<Value> args) const
|
||||||
{
|
{
|
||||||
cbindgen_private::Slice<cbindgen_private::ValueOpaque> args_view {
|
using namespace cbindgen_private;
|
||||||
reinterpret_cast<cbindgen_private::ValueOpaque *>(args.ptr), args.len
|
Slice<ValueOpaque> args_view { reinterpret_cast<ValueOpaque *>(args.ptr), args.len };
|
||||||
};
|
ValueOpaque out;
|
||||||
cbindgen_private::Slice<uint8_t> name_view {
|
if (sixtyfps_interpreter_component_instance_invoke_callback(
|
||||||
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
|
&inner, Slice<uint8_t>::from_string(name), args_view, &out)) {
|
||||||
name.size()
|
|
||||||
};
|
|
||||||
cbindgen_private::ValueOpaque out;
|
|
||||||
if (cbindgen_private::sixtyfps_interpreter_component_instance_invoke_callback(
|
|
||||||
&inner, name_view, args_view, &out)) {
|
|
||||||
return Value(out);
|
return Value(out);
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return {};
|
||||||
|
@ -418,17 +407,13 @@ public:
|
||||||
bool set_callback(std::string_view name, F callback) const
|
bool set_callback(std::string_view name, F callback) const
|
||||||
{
|
{
|
||||||
using cbindgen_private::ValueOpaque;
|
using cbindgen_private::ValueOpaque;
|
||||||
cbindgen_private::Slice<uint8_t> name_view {
|
|
||||||
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
|
|
||||||
name.size()
|
|
||||||
};
|
|
||||||
auto actual_cb = [](void *data, Slice<ValueOpaque> arg, ValueOpaque *ret) {
|
auto actual_cb = [](void *data, Slice<ValueOpaque> arg, ValueOpaque *ret) {
|
||||||
Slice<Value> args_view { reinterpret_cast<Value *>(arg.ptr), arg.len };
|
Slice<Value> args_view { reinterpret_cast<Value *>(arg.ptr), arg.len };
|
||||||
Value r = (*reinterpret_cast<F *>(data))(arg);
|
Value r = (*reinterpret_cast<F *>(data))(arg);
|
||||||
new (ret) Value(std::move(r));
|
new (ret) Value(std::move(r));
|
||||||
};
|
};
|
||||||
return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback(
|
return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback(
|
||||||
&inner, name_view, actual_cb, new F(std::move(callback)),
|
&inner, Slice<uint8_t>::from_string(name), actual_cb, new F(std::move(callback)),
|
||||||
[](void *data) { delete reinterpret_cast<F *>(data); });
|
[](void *data) { delete reinterpret_cast<F *>(data); });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -103,6 +103,17 @@ fn gen_corelib(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let mut string_config = config.clone();
|
let mut string_config = config.clone();
|
||||||
string_config.export.exclude = vec!["SharedString".into()];
|
string_config.export.exclude = vec!["SharedString".into()];
|
||||||
|
string_config.export.body.insert(
|
||||||
|
"Slice".to_owned(),
|
||||||
|
" static Slice<T> from_string(std::string_view str) {
|
||||||
|
return {
|
||||||
|
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(str.data())),
|
||||||
|
str.size()
|
||||||
|
};
|
||||||
|
}"
|
||||||
|
.to_owned(),
|
||||||
|
);
|
||||||
|
|
||||||
cbindgen::Builder::new()
|
cbindgen::Builder::new()
|
||||||
.with_config(string_config)
|
.with_config(string_config)
|
||||||
.with_src(crate_dir.join("string.rs"))
|
.with_src(crate_dir.join("string.rs"))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue