C++ implementation of the ComponentInstance

still untested
This commit is contained in:
Olivier Goffart 2021-03-18 16:29:49 +01:00
parent 0a92dab6c1
commit 9cc69d73a1

View file

@ -246,6 +246,9 @@ private:
using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque; using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque;
ValueOpaque inner; ValueOpaque inner;
friend struct Struct; friend struct Struct;
friend class ComponentInstance;
// Internal constructor that takes ownership of the value
explicit Value(ValueOpaque &inner) : inner(inner) {}
}; };
inline Value::Value(const sixtyfps::SharedVector<Value> &array) inline Value::Value(const sixtyfps::SharedVector<Value> &array)
@ -346,4 +349,75 @@ inline void Struct::set_field(std::string_view name, const Value &value)
cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner); cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner);
} }
class ComponentInstance {
cbindgen_private::ComponentInstance inner;
ComponentInstance() = delete;
ComponentInstance(ComponentInstance &) = delete;
ComponentInstance &operator=(ComponentInstance &) = delete;
public:
void show() const;
void hide() const;
void run() 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);
}
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)) {
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 {
cbindgen_private::Slice<cbindgen_private::ValueOpaque> args_view {
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())),
name.size()
};
cbindgen_private::ValueOpaque out;
if (cbindgen_private::sixtyfps_interpreter_component_instance_invoke_callback(
&inner, name_view, args_view, &out)) {
return Value(out);
} else {
return {};
}
}
template<typename F>
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);
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); });
}
};
} }