From 0d175dce12239eda4db54e3d93c3cb6ebbdfe0f9 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Fri, 28 Apr 2023 16:31:07 -0700 Subject: [PATCH] Refactor gnc-features. * type aliases to gnc-features.h * string_views to avoid copying (all of the strings are static) * qof_book_get_unknown_features returns a FeatureSet --- libgnucash/engine/gnc-features.cpp | 4 ++-- libgnucash/engine/gnc-features.h | 10 ++++++++-- libgnucash/engine/qofbook.cpp | 6 +++--- libgnucash/engine/qofbook.hpp | 5 ++--- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/libgnucash/engine/gnc-features.cpp b/libgnucash/engine/gnc-features.cpp index 8af413dd89..109b2dd3c8 100644 --- a/libgnucash/engine/gnc-features.cpp +++ b/libgnucash/engine/gnc-features.cpp @@ -69,7 +69,7 @@ gchar *gnc_features_test_unknown (QofBook *book) if (unknowns.empty()) return nullptr; - auto accum = [](const auto& a, const auto& b){ return a + "\n* " + b; }; + auto accum = [](const auto& a, const auto& b){ return a + "\n* " + b.second.data(); }; auto msg {std::accumulate (unknowns.begin(), unknowns.end(), std::string (_(header)), accum)}; return g_strdup (msg.c_str()); @@ -88,7 +88,7 @@ void gnc_features_set_used (QofBook *book, const gchar *feature) return; } - qof_book_set_feature (book, feature, iter->second.c_str()); + qof_book_set_feature (book, feature, iter->second.data()); } diff --git a/libgnucash/engine/gnc-features.h b/libgnucash/engine/gnc-features.h index b5960ac7ad..b82aad762b 100644 --- a/libgnucash/engine/gnc-features.h +++ b/libgnucash/engine/gnc-features.h @@ -36,9 +36,15 @@ #ifndef GNC_FEATURES_H #define GNC_FEATURES_H -#include "qof.h" - #ifdef __cplusplus +#include +#include +#include + +using Feature = std::pair; +using FeaturesTable = std::unordered_map; +using FeatureSet = std::vector; + extern "C" { #endif diff --git a/libgnucash/engine/qofbook.cpp b/libgnucash/engine/qofbook.cpp index be5cc46916..aae4cc6cf8 100644 --- a/libgnucash/engine/qofbook.cpp +++ b/libgnucash/engine/qofbook.cpp @@ -1267,14 +1267,14 @@ qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr) } } -std::vector +FeatureSet qof_book_get_unknown_features (QofBook *book, const FeaturesTable& features) { - std::vector rv; + FeatureSet rv; auto test_feature = [&](const KvpFrameImpl::map_type::value_type& feature) { if (features.find (feature.first) == features.end ()) - rv.push_back (feature.second->get()); + rv.emplace_back (feature.first, feature.second->get()); }; auto frame = qof_instance_get_slots (QOF_INSTANCE (book)); auto slot = frame->get_slot({GNC_FEATURES}); diff --git a/libgnucash/engine/qofbook.hpp b/libgnucash/engine/qofbook.hpp index 402de17119..3727f96ad4 100644 --- a/libgnucash/engine/qofbook.hpp +++ b/libgnucash/engine/qofbook.hpp @@ -26,10 +26,9 @@ #include #include "qof.h" +#include "gnc-features.h" -using FeaturesTable = std::unordered_map; - -std::vector +FeatureSet qof_book_get_unknown_features (QofBook *book, const FeaturesTable& features); bool qof_book_test_feature (QofBook*, const char*);