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:
Value() { cbindgen_private::sixtyfps_interpreter_value_new(&inner); }
Value(const Value &);
Value(Value &&);
Value &operator=(Value &&);
Value &operator=(const Value &);
Value(const Value &other) { sixtyfps_interpreter_value_clone(&other.inner, &inner); }
Value(Value &&other)
{
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); }
using Type = cbindgen_private::ValueType;
@ -38,7 +54,14 @@ public:
// optional<int> to_int() const;
// optional<float> to_float() 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<sixtyfps::SharedVector<Value>> to_array() 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;
Value(double);
Value(const SharedString &);
Value(const SharedString &str)
{
cbindgen_private::sixtyfps_interpreter_value_new_string(&str, &inner);
}
Value(bool);
Value(const SharedVector<Value> &);
Value(const std::shared_ptr<sixtyfps::Model<Value>> &);

View file

@ -32,7 +32,19 @@ SCENARIO("SharedString API")
SCENARIO("Value API")
{
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");
}
}