diff options
author | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-27 15:08:27 +0100 |
---|---|---|
committer | Adrian Szyndela <adrian.s@samsung.com> | 2020-10-30 12:18:53 +0100 |
commit | 40dc543f66e96ec549d76ed5954d69afb1c5763f (patch) | |
tree | 9b12134c0cfa45bfe2effd67d0ce50e96c6ba457 | |
parent | 8f572a48df3854f257a9ed2560eb48a39cfadb97 (diff) | |
download | libdbuspolicy-40dc543f66e96ec549d76ed5954d69afb1c5763f.tar.gz libdbuspolicy-40dc543f66e96ec549d76ed5954d69afb1c5763f.tar.bz2 libdbuspolicy-40dc543f66e96ec549d76ed5954d69afb1c5763f.zip |
serialization: add new Storage extracting interface
Change-Id: I947a1c73e0396034ebd0038fb483f5208a43bba3
-rw-r--r-- | src/internal/serialization_traits.hpp | 57 | ||||
-rw-r--r-- | src/internal/storage_backend_flatbuffers.hpp | 188 |
2 files changed, 245 insertions, 0 deletions
diff --git a/src/internal/serialization_traits.hpp b/src/internal/serialization_traits.hpp new file mode 100644 index 0000000..c52c599 --- /dev/null +++ b/src/internal/serialization_traits.hpp @@ -0,0 +1,57 @@ +#pragma once + +/* MIT License + * + * Copyright (c) 2020 Samsung Electronics Co., Ltd + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. */ + +namespace ldp_serialization { + +/* Specialize HasUserGroup for backend's sets to indicate if they have user and group maps. + * In specialization put std::true_type or std::false_type as the result. + */ +template <typename Set> +struct HasUserGroup; + +/* Specialize ContentType for backend's policies to indicate if their contents type + * In specialization put ldp_serialization::TreeType or ldp_serialization::ItemsType as the result. + */ +struct ItemsType {}; +struct TreeType {}; + +template <typename Policy> +struct PolicyContentType; + +/* Specialize PolicyHasIndex for backend's PolicySend to indicate it is indexed. + * In specialization put std::true_type as the result. + */ +template <typename Policy> +struct PolicyHasIndex { typedef std::false_type result; }; + +/* Specialize ItemType for backend's item types using SendType, ReceiveType and AccessType as the result field type. + */ +struct SendType {}; +struct ReceiveType {}; +struct AccessType {}; + +template <typename Item> +struct ItemType; + +} diff --git a/src/internal/storage_backend_flatbuffers.hpp b/src/internal/storage_backend_flatbuffers.hpp index d0946fd..ac86258 100644 --- a/src/internal/storage_backend_flatbuffers.hpp +++ b/src/internal/storage_backend_flatbuffers.hpp @@ -17,6 +17,7 @@ #include "include/fb_generated.h" #include "policy.hpp" +#include "serialization_traits.hpp" #include <memory> @@ -49,8 +50,195 @@ public: template <typename T> bool existsPolicyForGroup(gid_t gid) const; + const FB::File *getFile() const + { return file; } + + auto fileGetOwnSet(const FB::File *file) const + { return file->m_own_set(); } + auto fileGetSendSet(const FB::File *file) const + { return file->m_send_set(); } + auto fileGetReceiveSet(const FB::File *file) const + { return file->m_receive_set(); } + auto fileGetAccessSet(const FB::File *file) const + { return file->m_access_set(); } + + template <typename Set> + auto setGetContextDefault(const Set *set) const + { return set->context_default(); } + + template <typename Set> + auto setGetContextMandatory(const Set *set) const + { return set->context_mandatory(); } + + template <typename Set> + auto setGetUser(const Set *set) const + { return set->user(); } + + template <typename Set> + auto setGetGroup(const Set *set) const + { return set->group(); } + + template <typename SetUserGroup> + auto setUserGroupGetId(const SetUserGroup *set_user_group) const + { return set_user_group->id(); } + + template <typename SetUserGroup> + auto setUserGroupGetPolicy(const SetUserGroup *set_user_group) const + { return set_user_group->policy(); } + + template <typename Policy> + auto policyGetTree(const Policy *policy) const + { return policy->tree(); } + + template <typename Policy> + auto policyGetItems(const Policy *policy) const + { return policy->items(); } + + template <typename Policy> + auto policyHasIndex(const Policy *policy) const + { return policy->index() != nullptr && policy->index()->size() > 0; } + + template <typename Policy> + auto policyGetIndex(const Policy *policy) const + { return policy->index(); } + + template <typename Policy> + auto policyGetPrefixIndex(const Policy *policy) const + { return policy->prefix_index(); } + + auto policyIndexGetBestScore(const FB::NameScoresPair *p) const + { return p->best_score(); } + + auto policyIndexGetItemRefs(const FB::NameScoresPair *p) const + { return p->item_refs(); } + + template <typename Container> + auto containerGetReverseIterator(const Container *container) const + { return container->rbegin(); } + + template <typename Container> + auto containerGetReverseIteratorEnd(const Container *container) const + { return container->rend(); } + + template <typename Container> + auto containerGetIterator(const Container *container) const + { return container->begin(); } + + template <typename Container> + auto containerGetIteratorEnd(const Container *container) const + { return container->end(); } + + template <typename Container> + auto containerGetSize(const Container *container) const + { return container->size(); } + + template <typename Container> + auto containerLookupByIndex(const Container *container, size_t index) const + { return container->Get(index); } + + template <typename Container> + auto containerEmpty(const Container *container) const + { return container->size() == 0; } + + template <typename Container, typename Key> + auto containerLookupByKey(const Container *container, Key key) const { + auto elem = container->LookupByKey(key); + return std::make_pair(elem != nullptr, elem); + } + + auto decisionItemGetDecision(const FB::DecisionItem *item) const + { return item->decision(); } + + auto decisionItemGetPrivilege(const FB::DecisionItem *item) const + { return item->privilege(); } + + auto ownNodeGetToken(const FB::PolicyOwnNode *node) const + { return node->token(); } + + auto ownNodeGetDecisionItem(const FB::PolicyOwnNode *node) const + { return node->decision_item(); } + + auto ownNodeGetPrefixDecisionItem(const FB::PolicyOwnNode *node) const + { return node->prefix_decision_item(); } + + auto ownNodeGetChildren(const FB::PolicyOwnNode *node) const + { return node->children(); } + + auto itemAccessGetType(const FB::ItemAccess *item) const + { return item->type(); } + + auto itemAccessGetUid(const FB::ItemAccess *item) const + { return item->uid(); } + + auto itemAccessGetGid(const FB::ItemAccess *item) const + { return item->gid(); } + + template <typename ItemSR> + auto itemSrGetName(const ItemSR *item) const + { return item->name(); } + + template <typename ItemSR> + auto itemSrGetIsNamePrefix(const ItemSR *item) const + { return item->is_name_prefix(); } + + template <typename ItemSR> + auto itemSrGetInterface(const ItemSR *item) const + { return item->interface(); } + + template <typename ItemSR> + auto itemSrGetMember(const ItemSR *item) const + { return item->member(); } + + template <typename ItemSR> + auto itemSrGetPath(const ItemSR *item) const + { return item->path(); } + + template <typename ItemSR> + auto itemSrGetMessageType(const ItemSR *item) const + { return item->type(); } + + template <typename Item> + auto itemGetDecisionItem(const Item *item) const + { return item->decision(); } + + const char *stringGetCStr(const flatbuffers::String *str) const + { return str->c_str(); } + + size_t stringGetSize(const flatbuffers::String *str) const + { return str->size(); } + private: const FB::File *file{nullptr}; }; } + +namespace ldp_serialization { +template <> struct HasUserGroup<const FB::OwnSet *> +{ typedef std::true_type result; }; +template <> struct HasUserGroup<const FB::SendSet *> +{ typedef std::true_type result; }; +template <> struct HasUserGroup<const FB::ReceiveSet *> +{ typedef std::true_type result; }; +template <> struct HasUserGroup<const FB::AccessSet *> +{ typedef std::false_type result; }; + +template <> struct PolicyContentType<const FB::PolicyOwn *> +{ typedef TreeType result; }; +template <> struct PolicyContentType<const FB::PolicySend *> +{ typedef ItemsType result; }; +template <> struct PolicyContentType<const FB::PolicyReceive *> +{ typedef ItemsType result; }; +template <> struct PolicyContentType<const FB::PolicyAccess *> +{ typedef ItemsType result; }; + +template <> struct PolicyHasIndex<const FB::PolicySend *> +{ typedef std::true_type result; }; + +template <> struct ItemType<const FB::ItemSend *> +{ typedef SendType result; }; +template <> struct ItemType<const FB::ItemReceive *> +{ typedef ReceiveType result; }; +template <> struct ItemType<const FB::ItemAccess *> +{ typedef AccessType result; }; +} |