From 0dee313366afaf0370f3c8f3dcceb2bc5ca9483b Mon Sep 17 00:00:00 2001 From: Ralph Wessel Date: Wed, 23 Oct 2024 09:32:05 +0100 Subject: [PATCH] Fixed conversion of enum property values --- .../Speckle/Record/Property/Setting.cpp | 10 ++++++ SpeckleLib/Speckle/Record/Property/Setting.h | 11 ++++++ .../Speckle/Record/Property/Template.cpp | 36 +++++++++++++++++-- SpeckleLib/Speckle/Record/Property/Template.h | 6 ++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/SpeckleLib/Speckle/Record/Property/Setting.cpp b/SpeckleLib/Speckle/Record/Property/Setting.cpp index 35c3d5a..ee615f6 100644 --- a/SpeckleLib/Speckle/Record/Property/Setting.cpp +++ b/SpeckleLib/Speckle/Record/Property/Setting.cpp @@ -67,6 +67,16 @@ String Setting::getDisplayValue() const { } //Setting::getDisplayValue +/*-------------------------------------------------------------------- + Append a value to the setting + + value: The value to append + --------------------------------------------------------------------*/ +void Setting::append(Value&& value) { + m_values.emplace_back(value); +} //Setting::append + + #ifdef ARCHICAD /*-------------------------------------------------------------------- Receive a value from an Archicad property diff --git a/SpeckleLib/Speckle/Record/Property/Setting.h b/SpeckleLib/Speckle/Record/Property/Setting.h index b7f75ed..89ffa6d 100644 --- a/SpeckleLib/Speckle/Record/Property/Setting.h +++ b/SpeckleLib/Speckle/Record/Property/Setting.h @@ -76,6 +76,11 @@ namespace speckle::record::property { // MARK: - Functions (const) + /*! + Determine if the setting is empty, i.e. has no values + @return True if the setting is empty + */ + bool empty() const { return m_values.empty(); } /*! Determine if the setting has a defined value @return True if a defined value is found @@ -94,6 +99,12 @@ namespace speckle::record::property { // MARK: - Functions (mutating) + /*! + Append a value to the setting + @param value The value to append + */ + void append(Value&& value); + #ifdef ARCHICAD /*! Receive a value from an Archicad property diff --git a/SpeckleLib/Speckle/Record/Property/Template.cpp b/SpeckleLib/Speckle/Record/Property/Template.cpp index cbf3058..1af4635 100644 --- a/SpeckleLib/Speckle/Record/Property/Template.cpp +++ b/SpeckleLib/Speckle/Record/Property/Template.cpp @@ -183,10 +183,22 @@ std::unique_ptr Template::getGroup() const { bool Template::convert(const API_PropertyValue& value, Setting& setting) const { auto measure = Template::convert(m_measure); switch (m_type) { - case Type::single: case Type::singleEnum: + case Type::single: return setting.receive(value.singleVariant, API_VariantStatusNormal, measure); - case Type::array: case Type::arrayEnum: + case Type::singleEnum: + if (auto enumValue = findEnumValue(Value{value.singleVariant.variant, API_VariantStatusNormal, measure}); enumValue) { + setting.append(std::move(*enumValue)); + return true; + } + return false; + case Type::array: return setting.receive(value.listVariant, API_VariantStatusNormal, measure); + case Type::arrayEnum: + for (const auto& val : value.listVariant.variants) { + if (auto enumValue = findEnumValue(Value{val, API_VariantStatusNormal, measure}); enumValue) + setting.append(std::move(*enumValue)); + } + return !setting.empty(); default: break; } @@ -207,6 +219,26 @@ bool Template::convert(const Setting& setting, API_PropertyValue& value) const { } //Template::convert #endif + +/*-------------------------------------------------------------------- + Find an enumerated value by key + + key: The value key + + return: The enum value paired with the specified key (nullopt on failure) + --------------------------------------------------------------------*/ +Value::Option Template::findEnumValue(const Value& key) const { + if (key.getValue() != nullptr) { + if (auto iter = std::find_if(m_enumValues.begin(), m_enumValues.end(), [&key](const auto& value){ + if (value.getKey() == nullptr) + return false; + return *key.getValue() == *value.getKey(); + }); iter != m_enumValues.end()) + return *iter; + } + return std::nullopt; +} //Template::findEnumValue + /*-------------------------------------------------------------------- Fill an inventory with the package items diff --git a/SpeckleLib/Speckle/Record/Property/Template.h b/SpeckleLib/Speckle/Record/Property/Template.h index be5262d..f23bff3 100644 --- a/SpeckleLib/Speckle/Record/Property/Template.h +++ b/SpeckleLib/Speckle/Record/Property/Template.h @@ -156,6 +156,12 @@ namespace speckle::record::property { @return True if the conversion was successful */ bool convert(const Setting& setting, API_PropertyValue& value) const; + /*! + Find an enumerated value by key + @param key The value key + @return The enum value paired with the specified key (nullopt on failure) + */ + Value::Option findEnumValue(const Value& key) const; // MARK: - Functions (mutating)