diff options
author | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-27 16:23:56 +0100 |
---|---|---|
committer | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-30 12:18:58 +0100 |
commit | 6fbd21c76421008477a1c489d962a1130e951c5f (patch) | |
tree | d5c9842aff4742ce2bd0d0c94af842747884f607 | |
parent | 40dc543f66e96ec549d76ed5954d69afb1c5763f (diff) | |
download | libdbuspolicy-6fbd21c76421008477a1c489d962a1130e951c5f.tar.gz libdbuspolicy-6fbd21c76421008477a1c489d962a1130e951c5f.tar.bz2 libdbuspolicy-6fbd21c76421008477a1c489d962a1130e951c5f.zip |
refactoring: move local functions to the class
Change-Id: I98e3f0ab900be2b487df47f06d131299507cd7a3
-rw-r--r-- | src/internal/storage_backend_flatbuffers.cpp | 79 | ||||
-rw-r--r-- | src/internal/storage_backend_flatbuffers.hpp | 26 |
2 files changed, 60 insertions, 45 deletions
diff --git a/src/internal/storage_backend_flatbuffers.cpp b/src/internal/storage_backend_flatbuffers.cpp index 1cac9ce..b4dd030 100644 --- a/src/internal/storage_backend_flatbuffers.cpp +++ b/src/internal/storage_backend_flatbuffers.cpp @@ -26,34 +26,30 @@ using ldp_xml_parser::MatchItemSend; namespace ldp_serialized { -typedef boost::tokenizer<boost::char_separator<char>> tokenizer; - template <typename T> struct type_helper; -namespace { - -inline boost::string_ref s(const flatbuffers::String *str) { +boost::string_ref StorageBackendFlatbuffers::toStringRef(const flatbuffers::String *str) const { return boost::string_ref(str->c_str(), str->size()); } template <typename T, typename I> -bool match(const T &match, const I *i) { +bool StorageBackendFlatbuffers::match(const T &match, const I *i) const { return match.match(makeMessageType(i->type()), - s(i->interface()), - s(i->path()), - s(i->member()), - s(i->name()), + toStringRef(i->interface()), + toStringRef(i->path()), + toStringRef(i->member()), + toStringRef(i->name()), i->is_name_prefix(), makeDecision(i->decision()->decision())); } -template <> bool match(const ldp_xml_parser::MatchItemAccess &match, const FB::ItemAccess *item) { +bool StorageBackendFlatbuffers::match(const ldp_xml_parser::MatchItemAccess &match, const FB::ItemAccess *item) const { return match.match(makeBusAccessType(item->type()), item->uid(), item->gid()); } template <typename T, typename P = typename type_helper<T>::policy_type> -ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P *policy) { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItem(const T &item, const P *policy) const { const auto *v = policy->items(); for (auto it = v->rbegin(); it != v->rend(); ++it) { if (match(item, *it)) @@ -62,10 +58,10 @@ ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P *policy) { return ldp_xml_parser::Decision::ANY; } -const DecisionItem *getDecisionItemFromTree(const FB::PolicyOwnNode *node, - tokenizer &tokens, - tokenizer::iterator &iterator) { - if (iterator == tokens.end()) { +auto StorageBackendFlatbuffers::getDecisionItemFromTree(const FB::PolicyOwnNode *node, + const tokenizer::iterator &tokens_end, + tokenizer::iterator &iterator) const { + if (iterator == tokens_end) { if (node->decision_item()->decision() != Decision_ANY) return node->decision_item(); else @@ -77,15 +73,16 @@ const DecisionItem *getDecisionItemFromTree(const FB::PolicyOwnNode *node, return node->prefix_decision_item(); ++iterator; - const DecisionItem *child_decision = getDecisionItemFromTree(child, tokens, iterator); + const DecisionItem *child_decision = getDecisionItemFromTree(child, tokens_end, iterator); if (child_decision->decision() == Decision_ANY) return node->prefix_decision_item(); return child_decision; } -template <> ldp_xml_parser::DecisionItem getDecisionItem(const ldp_xml_parser::MatchItemOwn &item, - const PolicyOwn *policy) { +template <> +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItem(const ldp_xml_parser::MatchItemOwn &item, + const PolicyOwn *policy) const { if (item.getName().length() == 0) return ldp_xml_parser::Decision::DENY; @@ -98,22 +95,17 @@ template <> ldp_xml_parser::DecisionItem getDecisionItem(const ldp_xml_parser::M auto iterator = tokens.begin(); - return makeDecisionItem(getDecisionItemFromTree(node, tokens, iterator)); + return makeDecisionItem(getDecisionItemFromTree(node, tokens.end(), iterator)); } template <typename T, typename P = typename type_helper<T>::policy_type> -ldp_xml_parser::DecisionItem getDecisionItemMaybeNull(const T &item, const P *policyPair) { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemMaybeNull(const T &item, const P *policyPair) const { if (nullptr == policyPair) return ldp_xml_parser::Decision::ANY; return getDecisionItem(item, policyPair->policy()); } -} // anonymous namespace - -template <typename T, typename M = typename type_helper<T>::policy_set_type> -const M *getPolicySet(const FB::File *file); - -ldp_xml_parser::DecisionItem getDecisionFromSendIndex(const MatchItemSend &item, const FB::PolicySend *policy) { +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemFromSendIndex(const MatchItemSend &item, const FB::PolicySend *policy) const { const auto *index = policy->index(); @@ -122,7 +114,7 @@ ldp_xml_parser::DecisionItem getDecisionFromSendIndex(const MatchItemSend &item, std::pair<const ItemSend*, uint32_t> currentBest(nullptr, 0); - auto updateCurrentBest = [¤tBest, &item, &policy](const flatbuffers::Vector<uint32_t> *vec) { + auto updateCurrentBest = [&](const flatbuffers::Vector<uint32_t> *vec) { // look from the back, the rule is the same as for the full database // we now only check among less elements, because the database is indexed to small lists // item_scores are in increasing order in the index, and they serve also as ids of the policy rules @@ -139,7 +131,7 @@ ldp_xml_parser::DecisionItem getDecisionFromSendIndex(const MatchItemSend &item, } }; - auto searchAndUpdateCurrentBest = [¤tBest, &index, &updateCurrentBest](boost::string_ref name_ref) { + auto searchAndUpdateCurrentBest = [&](boost::string_ref name_ref) { // we need to create C-string for flatbuffers lookups // boost::string_ref gives us correct start, but possibly NUL-terminated in a wrong place, as it does not modify // input string and keeps only the length @@ -222,8 +214,7 @@ void StorageBackendFlatbuffers::printContent(const bool xml_format) const { typedef FB::Policy##T policy_type; \ }; \ template <> \ - auto getPolicySet<ldp_xml_parser::MatchItem##T>(const FB::File *file) \ - -> const typename type_helper<ldp_xml_parser::MatchItem##T>::policy_set_type * { \ + auto StorageBackendFlatbuffers::getPolicySet<ldp_xml_parser::MatchItem##T>() const { \ assert(file); \ return file->m_##t##_set(); \ } @@ -233,47 +224,47 @@ TYPE_HELPER(Send, send) TYPE_HELPER(Receive, receive) TYPE_HELPER(Access, access) -template <typename T> -ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const T &item) const { - return getDecisionItem(item, getPolicySet<T>(file)->context_mandatory()); -} - template <> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const MatchItemSend &item) const { - return getDecisionFromSendIndex(item, getPolicySet<MatchItemSend>(file)->context_mandatory()); + return getDecisionItemFromSendIndex(item, getPolicySet<MatchItemSend>()->context_mandatory()); } template <> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextDefault(const MatchItemSend &item) const { - return getDecisionFromSendIndex(item, getPolicySet<MatchItemSend>(file)->context_default()); + return getDecisionItemFromSendIndex(item, getPolicySet<MatchItemSend>()->context_default()); } template <> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemUser(uid_t uid, const MatchItemSend &item) const { - auto *policyPair = getPolicySet<MatchItemSend>(file)->user()->LookupByKey(uid); + auto *policyPair = getPolicySet<MatchItemSend>()->user()->LookupByKey(uid); if (nullptr == policyPair) return ldp_xml_parser::Decision::ANY; - return getDecisionFromSendIndex(item, policyPair->policy()); + return getDecisionItemFromSendIndex(item, policyPair->policy()); +} + +template <typename T> +ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextMandatory(const T &item) const { + return getDecisionItem(item, getPolicySet<T>()->context_mandatory()); } template <typename T> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemContextDefault(const T &item) const { - return getDecisionItem(item, getPolicySet<T>(file)->context_default()); + return getDecisionItem(item, getPolicySet<T>()->context_default()); } template <typename T> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemUser(uid_t uid, const T &item) const { - return getDecisionItemMaybeNull(item, getPolicySet<T>(file)->user()->LookupByKey(uid)); + return getDecisionItemMaybeNull(item, getPolicySet<T>()->user()->LookupByKey(uid)); } template <typename T> ldp_xml_parser::DecisionItem StorageBackendFlatbuffers::getDecisionItemGroup(gid_t gid, const T &item) const { - return getDecisionItemMaybeNull(item, getPolicySet<T>(file)->group()->LookupByKey(gid)); + return getDecisionItemMaybeNull(item, getPolicySet<T>()->group()->LookupByKey(gid)); } template <typename T> bool StorageBackendFlatbuffers::existsPolicyForGroup(gid_t gid) const { - return getPolicySet<T>(file)->group()->LookupByKey(gid) != nullptr; + return getPolicySet<T>()->group()->LookupByKey(gid) != nullptr; } #define T_INSTANTIATION(T) \ diff --git a/src/internal/storage_backend_flatbuffers.hpp b/src/internal/storage_backend_flatbuffers.hpp index ac86258..107c55b 100644 --- a/src/internal/storage_backend_flatbuffers.hpp +++ b/src/internal/storage_backend_flatbuffers.hpp @@ -18,7 +18,7 @@ #include "include/fb_generated.h" #include "policy.hpp" #include "serialization_traits.hpp" - +#include <boost/tokenizer.hpp> #include <memory> namespace ldp_serialized { @@ -207,8 +207,32 @@ public: size_t stringGetSize(const flatbuffers::String *str) const { return str->size(); } + typedef boost::tokenizer<boost::char_separator<char>> tokenizer; + private: const FB::File *file{nullptr}; + + template <typename MatchItem> + auto getPolicySet() const; + + template <typename T, typename P> + ldp_xml_parser::DecisionItem getDecisionItem(const T &item, const P *policy) const; + + ldp_xml_parser::DecisionItem getDecisionItemFromSendIndex(const ldp_xml_parser::MatchItemSend &item, const FB::PolicySend *policy) const; + + template <typename T, typename P> + ldp_xml_parser::DecisionItem getDecisionItemMaybeNull(const T &item, const P *policyPair) const; + + auto getDecisionItemFromTree(const FB::PolicyOwnNode *node, + const tokenizer::iterator &tokens_end, + tokenizer::iterator &iterator) const; + + template <typename T, typename I> + bool match(const T &match, const I *i) const; + + bool match(const ldp_xml_parser::MatchItemAccess &match, const FB::ItemAccess *item) const; + + boost::string_ref toStringRef(const flatbuffers::String *str) const; }; } |