Add the skeleton for CompilerCompiler in C++

This commit is contained in:
Simon Hausmann 2021-03-19 09:26:42 +01:00
parent 7303374927
commit 7af77839ad
3 changed files with 77 additions and 28 deletions

View file

@ -248,7 +248,7 @@ private:
friend struct Struct;
friend class ComponentInstance;
// Internal constructor that takes ownership of the value
explicit Value(ValueOpaque &inner) : inner(inner) {}
explicit Value(ValueOpaque &inner) : inner(inner) { }
};
inline Value::Value(const sixtyfps::SharedVector<Value> &array)
@ -349,48 +349,57 @@ inline void Struct::set_field(std::string_view name, const Value &value)
cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner);
}
class ComponentInstance {
class ComponentInstance
{
cbindgen_private::ComponentInstance inner;
ComponentInstance() = delete;
ComponentInstance(ComponentInstance &) = delete;
ComponentInstance &operator=(ComponentInstance &) = delete;
public:
void show() const {
void show() const
{
cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, true);
}
void hide() const {
void hide() const
{
cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, false);
}
void run() const {
void run() const
{
show();
cbindgen_private::sixtyfps_run_event_loop();
hide();
}
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 {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
name.size()
};
return cbindgen_private::sixtyfps_interpreter_component_instance_set_property(&inner, name_view, &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 {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
name.size()
};
cbindgen_private::ValueOpaque out;
if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property(&inner, name_view, &out)) {
if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property(
&inner, name_view, &out)) {
return Value(out);
} else {
return {};
}
}
// 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 {
reinterpret_cast<cbindgen_private::ValueOpaque *>(args.ptr),
args.len
reinterpret_cast<cbindgen_private::ValueOpaque *>(args.ptr), args.len
};
cbindgen_private::Slice<uint8_t> name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
@ -398,7 +407,7 @@ public:
};
cbindgen_private::ValueOpaque out;
if (cbindgen_private::sixtyfps_interpreter_component_instance_invoke_callback(
&inner, name_view, args_view, &out)) {
&inner, name_view, args_view, &out)) {
return Value(out);
} else {
return {};
@ -406,26 +415,39 @@ public:
}
template<typename F>
bool set_callback(std::string_view name, F callback) const {
bool set_callback(std::string_view name, F callback) const
{
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) {
Slice<Value> args_view {
reinterpret_cast<Value*>(arg.ptr),
arg.len
};
Value r = (*reinterpret_cast<F*>(data))(arg);
Slice<Value> args_view { reinterpret_cast<Value *>(arg.ptr), arg.len };
Value r = (*reinterpret_cast<F *>(data))(arg);
new (ret) Value(std::move(r));
};
return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback(
&inner, name_view, actual_cb,
new F(std::move(callback)),
[](void *data) { delete reinterpret_cast<F*>(data); });
&inner, name_view, actual_cb, new F(std::move(callback)),
[](void *data) { delete reinterpret_cast<F *>(data); });
}
};
class ComponentCompiler
{
cbindgen_private::ComponentCompilerOpaque inner;
ComponentCompiler() = delete;
ComponentCompiler(ComponentCompiler &) = delete;
ComponentCompiler &operator=(ComponentCompiler &) = delete;
public:
ComponentCompiler() { cbindgen_private::sixtyfps_interpreter_component_compiler_new(&inner); }
~ComponentCompiler()
{
cbindgen_private::sixtyfps_interpreter_component_compiler_destructor(&inner);
}
};
}