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
This commit is contained in:
John Ralls 2023-04-28 16:31:07 -07:00
parent e8c80f2f5c
commit 0d175dce12
4 changed files with 15 additions and 10 deletions

View File

@ -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());
}

View File

@ -36,9 +36,15 @@
#ifndef GNC_FEATURES_H
#define GNC_FEATURES_H
#include "qof.h"
#ifdef __cplusplus
#include <string_view>
#include <unordered_map>
#include <vector>
using Feature = std::pair<std::string_view, std::string_view>;
using FeaturesTable = std::unordered_map<std::string_view, std::string_view>;
using FeatureSet = std::vector<Feature>;
extern "C" {
#endif

View File

@ -1267,14 +1267,14 @@ qof_book_set_feature (QofBook *book, const gchar *key, const gchar *descr)
}
}
std::vector<std::string>
FeatureSet
qof_book_get_unknown_features (QofBook *book, const FeaturesTable& features)
{
std::vector<std::string> 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<const char*>());
rv.emplace_back (feature.first, feature.second->get<const char*>());
};
auto frame = qof_instance_get_slots (QOF_INSTANCE (book));
auto slot = frame->get_slot({GNC_FEATURES});

View File

@ -26,10 +26,9 @@
#include <string>
#include "qof.h"
#include "gnc-features.h"
using FeaturesTable = std::unordered_map<std::string,std::string>;
std::vector<std::string>
FeatureSet
qof_book_get_unknown_features (QofBook *book, const FeaturesTable& features);
bool qof_book_test_feature (QofBook*, const char*);