Expose Value::to_string() conversion and construction in C++

This commit is contained in:
Simon Hausmann 2021-03-17 13:28:53 +01:00
parent 6e9d4f9e8d
commit 8e72d29669
3 changed files with 64 additions and 8 deletions

View file

@ -22,10 +22,26 @@ class Value
public: public:
Value() { cbindgen_private::sixtyfps_interpreter_value_new(&inner); } Value() { cbindgen_private::sixtyfps_interpreter_value_new(&inner); }
Value(const Value &); Value(const Value &other) { sixtyfps_interpreter_value_clone(&other.inner, &inner); }
Value(Value &&); Value(Value &&other)
Value &operator=(Value &&); {
Value &operator=(const Value &); inner = other.inner;
cbindgen_private::sixtyfps_interpreter_value_new(&other.inner);
}
Value &operator=(Value &&other)
{
inner = other.inner;
cbindgen_private::sixtyfps_interpreter_value_new(&other.inner);
return *this;
}
Value &operator=(const Value &other)
{
if (this == &other)
return *this;
cbindgen_private::sixtyfps_interpreter_value_destructor(&inner);
sixtyfps_interpreter_value_clone(&other.inner, &inner);
return *this;
}
~Value() { cbindgen_private::sixtyfps_interpreter_value_destructor(&inner); } ~Value() { cbindgen_private::sixtyfps_interpreter_value_destructor(&inner); }
using Type = cbindgen_private::ValueType; using Type = cbindgen_private::ValueType;
@ -38,7 +54,14 @@ public:
// optional<int> to_int() const; // optional<int> to_int() const;
// optional<float> to_float() const; // optional<float> to_float() const;
std::optional<double> to_number() const; std::optional<double> to_number() const;
std::optional<sixtyfps::SharedString> to_string() const; std::optional<sixtyfps::SharedString> to_string() const
{
if (auto *str = cbindgen_private::sixtyfps_interpreter_value_to_string(&inner)) {
return *str;
} else {
return {};
}
}
std::optional<bool> to_bool() const; std::optional<bool> to_bool() const;
std::optional<sixtyfps::SharedVector<Value>> to_array() const; std::optional<sixtyfps::SharedVector<Value>> to_array() const;
std::optional<std::shared_ptr<sixtyfps::Model<Value>>> to_model() const; std::optional<std::shared_ptr<sixtyfps::Model<Value>>> to_model() const;
@ -47,7 +70,10 @@ public:
// template<typename T> std::optional<T> get() const; // template<typename T> std::optional<T> get() const;
Value(double); Value(double);
Value(const SharedString &); Value(const SharedString &str)
{
cbindgen_private::sixtyfps_interpreter_value_new_string(&str, &inner);
}
Value(bool); Value(bool);
Value(const SharedVector<Value> &); Value(const SharedVector<Value> &);
Value(const std::shared_ptr<sixtyfps::Model<Value>> &); Value(const std::shared_ptr<sixtyfps::Model<Value>> &);

View file

@ -32,7 +32,19 @@ SCENARIO("SharedString API")
SCENARIO("Value API") SCENARIO("Value API")
{ {
using namespace sixtyfps::interpreter; using namespace sixtyfps::interpreter;
Value str; Value value;
REQUIRE(str.type() == Value::Type::Void); REQUIRE(value.type() == Value::Type::Void);
SECTION("Construct a string")
{
REQUIRE(!value.to_string().has_value());
sixtyfps::SharedString cpp_str("Hello World");
value = Value(cpp_str);
REQUIRE(value.type() == Value::Type::String);
auto string_opt = value.to_string();
REQUIRE(string_opt.has_value());
REQUIRE(string_opt.value() == "Hello World");
}
} }

View file

@ -761,12 +761,30 @@ pub(crate) mod ffi {
std::ptr::write(val as *mut Value, Value::default()) std::ptr::write(val as *mut Value, Value::default())
} }
/// Construct a new Value in the given memory location
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_interpreter_value_clone(
other: &ValueOpaque,
val: *mut ValueOpaque,
) {
std::ptr::write(val as *mut Value, other.as_value().clone())
}
/// Destruct the value in that memory location /// Destruct the value in that memory location
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sixtyfps_interpreter_value_destructor(val: *mut ValueOpaque) { pub unsafe extern "C" fn sixtyfps_interpreter_value_destructor(val: *mut ValueOpaque) {
drop(std::ptr::read(val as *mut Value)) drop(std::ptr::read(val as *mut Value))
} }
/// Construct a new Value in the given memory location as string
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_interpreter_value_new_string(
str: &SharedString,
val: *mut ValueOpaque,
) {
std::ptr::write(val as *mut Value, Value::String(str.clone()))
}
#[repr(i8)] #[repr(i8)]
pub enum ValueType { pub enum ValueType {
Void, Void,