/* LICENSE BEGIN This file is part of the SixtyFPS Project -- https://sixtyfps.io Copyright (c) 2020 Olivier Goffart Copyright (c) 2020 Simon Hausmann SPDX-License-Identifier: GPL-3.0-only This file is also available under commercial licensing terms. Please contact info@sixtyfps.io for more information. LICENSE END */ #pragma once #include "sixtyfps.h" #include "sixtyfps_interpreter_internal.h" #include namespace sixtyfps::interpreter { class Value; struct Struct { public: Struct() { cbindgen_private::sixtyfps_interpreter_struct_new(&inner); } Struct(const Struct &other) { cbindgen_private::sixtyfps_interpreter_struct_clone(&other.inner, &inner); } Struct(Struct &&other) { inner = other.inner; cbindgen_private::sixtyfps_interpreter_struct_new(&other.inner); } Struct &operator=(const Struct &other) { if (this == &other) return *this; cbindgen_private::sixtyfps_interpreter_struct_destructor(&inner); sixtyfps_interpreter_struct_clone(&other.inner, &inner); return *this; } Struct &operator=(Struct &&other) { if (this == &other) return *this; cbindgen_private::sixtyfps_interpreter_struct_destructor(&inner); inner = other.inner; cbindgen_private::sixtyfps_interpreter_struct_new(&other.inner); return *this; } ~Struct() { cbindgen_private::sixtyfps_interpreter_struct_destructor(&inner); } #if 0 Struct(std::initializer_list>) template< typename InputIterator, typename = std::enable_if< std::is_same(decltype(*std::declval())), std::pair>> // InputIterator produces // std::pair Struct(InputIterator begin, InputIterator end); // Creates // Value::Struct struct iterator { //... iterator API to key/value pairs } iterator begin() const; iterator end() const; #endif inline std::optional get_field(std::string_view name) const; inline void set_field(std::string_view name, const Value &value); // internal Struct(const sixtyfps::cbindgen_private::StructOpaque &other) { cbindgen_private::sixtyfps_interpreter_struct_clone(&other, &inner); } private: using StructOpaque = sixtyfps::cbindgen_private::StructOpaque; StructOpaque inner; friend class Value; }; class Value { public: Value() { cbindgen_private::sixtyfps_interpreter_value_new(&inner); } 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=(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 &operator=(Value &&other) { if (this == &other) return *this; cbindgen_private::sixtyfps_interpreter_value_destructor(&inner); inner = other.inner; cbindgen_private::sixtyfps_interpreter_value_new(&other.inner); return *this; } ~Value() { cbindgen_private::sixtyfps_interpreter_value_destructor(&inner); } using Type = cbindgen_private::ValueType; // optional to_int() const; // optional to_float() const; std::optional to_number() const { if (auto *number = cbindgen_private::sixtyfps_interpreter_value_to_number(&inner)) { return *number; } else { return {}; } } std::optional to_string() const { if (auto *str = cbindgen_private::sixtyfps_interpreter_value_to_string(&inner)) { return *str; } else { return {}; } } std::optional to_bool() const { if (auto *b = cbindgen_private::sixtyfps_interpreter_value_to_bool(&inner)) { return *b; } else { return {}; } } inline std::optional> to_array() const; std::optional>> to_model() const; std::optional to_brush() const { if (auto *brush = cbindgen_private::sixtyfps_interpreter_value_to_brush(&inner)) { return *brush; } else { return {}; } } std::optional to_struct() const { if (auto *opaque_struct = cbindgen_private::sixtyfps_interpreter_value_to_struct(&inner)) { return Struct(*opaque_struct); } else { return {}; } } // template std::optional get() const; Value(double value) { cbindgen_private::sixtyfps_interpreter_value_new_double(value, &inner); } Value(const SharedString &str) { cbindgen_private::sixtyfps_interpreter_value_new_string(&str, &inner); } Value(bool b) { cbindgen_private::sixtyfps_interpreter_value_new_bool(b, &inner); } inline Value(const SharedVector &); Value(const std::shared_ptr> &); Value(const sixtyfps::Brush &brush) { cbindgen_private::sixtyfps_interpreter_value_new_brush(&brush, &inner); } Value(const Struct &struc) { cbindgen_private::sixtyfps_interpreter_value_new_struct(&struc.inner, &inner); } Type type() const { return cbindgen_private::sixtyfps_interpreter_value_type(&inner); } // internal Value(const sixtyfps::cbindgen_private::ValueOpaque &inner) : inner(inner) { } private: using ValueOpaque = sixtyfps::cbindgen_private::ValueOpaque; ValueOpaque inner; friend class Struct; }; inline Value::Value(const sixtyfps::SharedVector &array) { cbindgen_private::sixtyfps_interpreter_value_new_array( &reinterpret_cast &>(array), &inner); } inline std::optional> Value::to_array() const { if (auto *array = cbindgen_private::sixtyfps_interpreter_value_to_array(&inner)) { return *reinterpret_cast *>(array); } else { return {}; } } inline Value::Value(const std::shared_ptr> &model) { using cbindgen_private::ModelAdaptorVTable; using vtable::VRef; struct ModelWrapper : AbstractRepeaterView { std::shared_ptr> model; cbindgen_private::ModelNotifyOpaque notify; // This kind of mean that the rust code has ownership of "this" until the drop funciton is // called std::shared_ptr self; ~ModelWrapper() { cbindgen_private::sixtyfps_interpreter_model_notify_destructor(¬ify); } void row_added(int index, int count) override { cbindgen_private::sixtyfps_interpreter_model_notify_row_added(¬ify, index, count); } void row_changed(int index) override { cbindgen_private::sixtyfps_interpreter_model_notify_row_changed(¬ify, index); } void row_removed(int index, int count) override { cbindgen_private::sixtyfps_interpreter_model_notify_row_removed(¬ify, index, count); } }; auto wrapper = std::make_shared(); wrapper->model = model; wrapper->self = wrapper; cbindgen_private::sixtyfps_interpreter_model_notify_new(&wrapper->notify); model->attach_peer(wrapper); auto row_count = [](VRef self) -> uintptr_t { return reinterpret_cast(self.instance)->model->row_count(); }; auto row_data = [](VRef self, uintptr_t row, ValueOpaque *out) { Value v = reinterpret_cast(self.instance)->model->row_data(row); *out = v.inner; cbindgen_private::sixtyfps_interpreter_value_new(&v.inner); }; auto set_row_data = [](VRef self, uintptr_t row, const ValueOpaque *value) { Value v = *reinterpret_cast(value); reinterpret_cast(self.instance)->model->set_row_data(row, v); }; auto get_notify = [](VRef self) -> const cbindgen_private::ModelNotifyOpaque * { return &reinterpret_cast(self.instance)->notify; }; auto drop = [](vtable::VRefMut self) { reinterpret_cast(self.instance)->self = nullptr; }; static const ModelAdaptorVTable vt { row_count, row_data, set_row_data, get_notify, drop }; vtable::VBox wrap { &vt, wrapper.get() }; cbindgen_private::sixtyfps_interpreter_value_new_model(wrap, &inner); } inline std::optional Struct::get_field(std::string_view name) const { cbindgen_private::Slice name_view { const_cast(reinterpret_cast(name.data())), name.size() }; if (auto *value = cbindgen_private::sixtyfps_interpreter_struct_get_field(&inner, name_view)) { return *value; } else { return {}; } } inline void Struct::set_field(std::string_view name, const Value &value) { cbindgen_private::Slice name_view { const_cast(reinterpret_cast(name.data())), name.size() }; cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner); } }