Remove the null state in ComponentDefinition

This isn't needed as it turns out and a cleaner API this way
This commit is contained in:
Simon Hausmann 2021-03-19 17:08:04 +01:00
parent 4705c455b9
commit 59d6866b13

View file

@ -422,17 +422,17 @@ class ComponentDefinition
{ {
friend class ComponentCompiler; friend class ComponentCompiler;
union { using ComponentDefinitionOpaque = sixtyfps::cbindgen_private::ComponentDefinitionOpaque;
sixtyfps::cbindgen_private::ComponentDefinitionOpaque inner; ComponentDefinitionOpaque inner;
void *ptr;
}; ComponentDefinition() = delete;
// Internal constructor that takes ownership of the component definition
explicit ComponentDefinition(ComponentDefinitionOpaque &inner) : inner(inner) { }
public: public:
ComponentDefinition() { ptr = nullptr; }
ComponentDefinition(const ComponentDefinition &other) ComponentDefinition(const ComponentDefinition &other)
{ {
ptr = nullptr; sixtyfps_interpreter_component_definition_clone(&other.inner, &inner);
operator=(other);
} }
ComponentDefinition &operator=(const ComponentDefinition &other) ComponentDefinition &operator=(const ComponentDefinition &other)
{ {
@ -441,21 +441,12 @@ public:
if (this == &other) if (this == &other)
return *this; return *this;
if (ptr != nullptr) sixtyfps_interpreter_component_definition_destructor(&inner);
sixtyfps_interpreter_component_definition_destructor(&inner); sixtyfps_interpreter_component_definition_clone(&other.inner, &inner);
if (other.ptr != nullptr)
sixtyfps_interpreter_component_definition_clone(&other.inner, &inner);
else
ptr = nullptr;
return *this; return *this;
} }
~ComponentDefinition() ~ComponentDefinition() { sixtyfps_interpreter_component_definition_destructor(&inner); }
{
if (ptr != nullptr)
sixtyfps_interpreter_component_definition_destructor(&inner);
}
}; };
class ComponentCompiler class ComponentCompiler
@ -488,12 +479,12 @@ public:
std::optional<ComponentDefinition> build_from_source(std::string_view source_code, std::optional<ComponentDefinition> build_from_source(std::string_view source_code,
std::string_view path) std::string_view path)
{ {
ComponentDefinition result; cbindgen_private::ComponentDefinitionOpaque result;
if (cbindgen_private::sixtyfps_interpreter_component_compiler_build_from_source( if (cbindgen_private::sixtyfps_interpreter_component_compiler_build_from_source(
&inner, sixtyfps::private_api::string_to_slice(source_code), &inner, sixtyfps::private_api::string_to_slice(source_code),
sixtyfps::private_api::string_to_slice(path), &result.inner)) { sixtyfps::private_api::string_to_slice(path), &result)) {
return result; return ComponentDefinition(result);
} else { } else {
return {}; return {};
} }