refactoring: rename Serializer to SerializerFlatbuffers 80/246280/1
authorAdrian Szyndela <adrian.s@samsung.com>
Tue, 27 Oct 2020 09:08:39 +0000 (10:08 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Tue, 27 Oct 2020 09:08:39 +0000 (10:08 +0100)
Change-Id: I52d6c22f44d7730dea221c4b9d81df9b7b3af291

Makefile.am
src/internal/serializer.hpp
src/internal/serializer_flatbuffers.cpp [moved from src/internal/serializer.cpp with 81% similarity]
src/internal/serializer_flatbuffers.hpp [new file with mode: 0644]

index aad84e6..3388f17 100644 (file)
@@ -62,7 +62,7 @@ COMMON_SRC =\
        src/internal/own_tree.cpp \
        src/internal/xml_parser.cpp \
        src/internal/tslog.cpp \
-       src/internal/serializer.cpp \
+       src/internal/serializer_flatbuffers.cpp \
        src/internal/print_content.cpp \
        src/internal/storage_backend_serialized.cpp \
        src/internal/storage_backend_xml.cpp
index 466ceb6..6f9c2fa 100644 (file)
@@ -1,93 +1,7 @@
-/*
- * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
-*/
-#ifndef _SERIALIZER_HPP
-#define _SERIALIZER_HPP
+#pragma once
 
-#include <ostream>
-#include <memory>
-#include <string>
+#include "serializer_flatbuffers.hpp"
 
-#include "include/flatbuffers/flatbuffers.h"
-#include "include/fb_generated.h"
-
-#include "storage_backend_xml.hpp"
-#include "policy_containers.hpp"
-
-namespace ldp_serializer
-{
-       enum class SetType : uint8_t {
-               OWN,
-               SEND,
-               RECEIVE,
-               ACCESS
-       };
-
-       template <typename T>
-       using FbOff = flatbuffers::Offset<T>;
-
-       class Serializer {
-       private:
-               template <typename T>
-               struct type_helper;
-
-               const ldp_xml::StorageBackendXML *m_db;
-               flatbuffers::FlatBufferBuilder m_builder;
-
-               template <typename T>
-               auto get_create_set() -> decltype(type_helper<T>::create_set);
-               template <typename T>
-               auto get_create_policy() -> decltype(type_helper<T>::create_policy);
-               template <typename T>
-               auto get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair);
-               template <typename T>
-               auto get_create_item() -> decltype(type_helper<T>::create_item);
-
-               FbOff<FB::PolicyOwn> serialize_tree(const ldp_xml_parser::OwnershipTree &tree);
-               FbOff<FB::PolicyOwnNode> serialize_tree(const std::shared_ptr<ldp_xml_parser::TreeNode> &node);
-               FbOff<FB::DecisionItem> serialize_decision(const ldp_xml_parser::DecisionItem &item);
-
-               template <typename T, typename P>
-               auto serialize_item(const P &item) -> FbOff<typename type_helper<T>::item>;
-
-               template <typename T>
-               auto serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy>;
-
-               template <typename T>
-               auto serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
-                                       -> FbOff<typename type_helper<T>::policy>;
-
-               template <typename T, typename P>
-               auto serialize_pair(const long int id, const P policy)
-                                       -> FbOff<typename type_helper<T>::pair>;
-
-               template <typename TP>
-               auto serialize_set() -> FbOff<typename type_helper<TP>::set>;
-
-               template <typename TP, typename TFP>
-               auto serialize_set(FbOff<TFP> context_default,
-                                       FbOff<TFP> context_mandatory)
-                                               -> FbOff<typename type_helper<TP>::set>;
-       public:
-               Serializer() : m_db(nullptr) {}
-               const uint8_t *serialize(const ldp_xml::StorageBackendXML &db, size_t &size);
-               const uint8_t *serialize(const std::string config_path, size_t &size);
-               const uint8_t *serialize(const std::string config_path, std::ostream &output);
-               friend class SerializerTests;
-       };
+namespace ldp_serializer {
+using Serializer = SerializerFlatbuffers;
 }
-
-
-#endif
similarity index 81%
rename from src/internal/serializer.cpp
rename to src/internal/serializer_flatbuffers.cpp
index 5595751..b79558a 100644 (file)
@@ -47,7 +47,7 @@ std::map<MessageType, FB::MessageType> message_type_map {
 };
 
 template <>
-struct Serializer::type_helper<PolicyOwn> {
+struct SerializerFlatbuffers::type_helper<PolicyOwn> {
        typedef struct FB::OwnSet set;
        typedef struct FB::OwnSetBuilder builder;
        typedef struct FB::PolicyOwn policy;
@@ -58,7 +58,7 @@ struct Serializer::type_helper<PolicyOwn> {
 };
 
 template <>
-struct Serializer::type_helper<PolicySend> {
+struct SerializerFlatbuffers::type_helper<PolicySend> {
        typedef struct FB::SendSet set;
        typedef struct FB::SendSetBuilder builder;
        typedef struct FB::PolicySend policy;
@@ -71,7 +71,7 @@ struct Serializer::type_helper<PolicySend> {
 };
 
 template <>
-struct Serializer::type_helper<PolicyReceive> {
+struct SerializerFlatbuffers::type_helper<PolicyReceive> {
        typedef struct FB::ReceiveSet set;
        typedef struct FB::ReceiveSetBuilder builder;
        typedef struct FB::PolicyReceive policy;
@@ -84,7 +84,7 @@ struct Serializer::type_helper<PolicyReceive> {
 };
 
 template <>
-struct Serializer::type_helper<PolicyAccess> {
+struct SerializerFlatbuffers::type_helper<PolicyAccess> {
        typedef struct FB::AccessSet set;
        typedef struct FB::AccessSetBuilder builder;
        typedef struct FB::PolicyAccess policy;
@@ -94,7 +94,7 @@ struct Serializer::type_helper<PolicyAccess> {
        static constexpr auto create_item = &FB::CreateItemAccess;
 };
 
-const uint8_t* Serializer::serialize(const ldp_xml::StorageBackendXML &db, size_t &size) {
+const uint8_t* SerializerFlatbuffers::serialize(const ldp_xml::StorageBackendXML &db, size_t &size) {
        m_db = &db;
 
        auto own_set = serialize_set<PolicyOwn>();
@@ -115,7 +115,7 @@ const uint8_t* Serializer::serialize(const ldp_xml::StorageBackendXML &db, size_
        return buf;
 }
 
-const uint8_t* Serializer::serialize(const std::string config_path, size_t &size) {
+const uint8_t* SerializerFlatbuffers::serialize(const std::string config_path, size_t &size) {
        tslog::init(tslog::ldp_log_level::DEFAULT);
        ldp_xml::StorageBackendXML xmlStorage;
 
@@ -127,7 +127,7 @@ const uint8_t* Serializer::serialize(const std::string config_path, size_t &size
        return serialize(xmlStorage, size);
 }
 
-const uint8_t *Serializer::serialize(const std::string config_path, std::ostream &output) {
+const uint8_t *SerializerFlatbuffers::serialize(const std::string config_path, std::ostream &output) {
        size_t size = 0;
        auto buf = serialize(config_path, size);
 
@@ -136,33 +136,33 @@ const uint8_t *Serializer::serialize(const std::string config_path, std::ostream
 }
 
 template <typename T>
-auto Serializer::get_create_set() -> decltype(type_helper<T>::create_set) {
+auto SerializerFlatbuffers::get_create_set() -> decltype(type_helper<T>::create_set) {
        return type_helper<T>::create_set;
 }
 
 template <typename T>
-auto Serializer::get_create_policy() -> decltype(type_helper<T>::create_policy) {
+auto SerializerFlatbuffers::get_create_policy() -> decltype(type_helper<T>::create_policy) {
        return type_helper<T>::create_policy;
 }
 
 template <typename T>
-auto Serializer::get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair) {
+auto SerializerFlatbuffers::get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair) {
        return type_helper<T>::create_policy_pair;
 }
 
 template <typename T>
-auto Serializer::get_create_item() -> decltype(type_helper<T>::create_item) {
+auto SerializerFlatbuffers::get_create_item() -> decltype(type_helper<T>::create_item) {
        return type_helper<T>::create_item;
 }
 
-FbOff<FB::PolicyOwn> Serializer::serialize_tree(const OwnershipTree &tree) {
+FbOff<FB::PolicyOwn> SerializerFlatbuffers::serialize_tree(const OwnershipTree &tree) {
        auto tree_item = serialize_tree(tree.getRoot());
        auto policy = FB::CreatePolicyOwn(m_builder, tree_item);
 
        return policy;
 }
 
-FbOff<FB::PolicyOwnNode> Serializer::serialize_tree(const std::shared_ptr<TreeNode> &node) {
+FbOff<FB::PolicyOwnNode> SerializerFlatbuffers::serialize_tree(const std::shared_ptr<TreeNode> &node) {
        auto prefix_decision_item = serialize_decision(node->getOwnPrefixDecisionItem());
        auto decision_item = serialize_decision(node->getOwnDecisionItem());
 
@@ -181,14 +181,14 @@ FbOff<FB::PolicyOwnNode> Serializer::serialize_tree(const std::shared_ptr<TreeNo
        return policy_own;
 }
 
-FbOff<FB::DecisionItem> Serializer::serialize_decision(const DecisionItem &item) {
+FbOff<FB::DecisionItem> SerializerFlatbuffers::serialize_decision(const DecisionItem &item) {
        return FB::CreateDecisionItem(m_builder,
                                      decisions_map[item.getDecision()],
                                      m_builder.CreateString(item.getPrivilege()));
 }
 
 template <>
-auto Serializer::serialize_item<PolicyAccess>(const ItemAccess &item) -> FbOff<FB::ItemAccess> {
+auto SerializerFlatbuffers::serialize_item<PolicyAccess>(const ItemAccess &item) -> FbOff<FB::ItemAccess> {
        auto create_item = get_create_item<PolicyAccess>();
 
        return create_item(m_builder,
@@ -199,7 +199,7 @@ auto Serializer::serialize_item<PolicyAccess>(const ItemAccess &item) -> FbOff<F
 }
 
 template <typename T, typename P>
-auto Serializer::serialize_item(const P &item) -> FbOff<typename type_helper<T>::item> {
+auto SerializerFlatbuffers::serialize_item(const P &item) -> FbOff<typename type_helper<T>::item> {
        auto create_item = get_create_item<T>();
        return create_item(m_builder,
                           serialize_decision(item.getDecision()),
@@ -212,20 +212,20 @@ auto Serializer::serialize_item(const P &item) -> FbOff<typename type_helper<T>:
 }
 
 template <typename T>
-auto Serializer::serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
+auto SerializerFlatbuffers::serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
                                    -> FbOff<typename type_helper<T>::policy> {
        auto create_policy = get_create_policy<T>();
        return create_policy(m_builder, m_builder.CreateVector(items));
 }
 
 template <>
-auto Serializer::serialize_policy(const PolicyOwn &policy)
+auto SerializerFlatbuffers::serialize_policy(const PolicyOwn &policy)
                                    -> FbOff<FB::PolicyOwn> {
        return serialize_tree(policy.getTree());
 }
 
 template <>
-auto Serializer::serialize_policy(const PolicySend &policy)
+auto SerializerFlatbuffers::serialize_policy(const PolicySend &policy)
                                    -> FbOff<FB::PolicySend> {
        std::vector<FbOff<FB::ItemSend>> items;
 
@@ -267,7 +267,7 @@ auto Serializer::serialize_policy(const PolicySend &policy)
 }
 
 template <typename T>
-auto Serializer::serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy> {
+auto SerializerFlatbuffers::serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy> {
        std::vector<FbOff<typename type_helper<T>::item>> items;
 
        for (const auto &item : policy.getItems()) {
@@ -278,14 +278,14 @@ auto Serializer::serialize_policy(const T &policy) -> FbOff<typename type_helper
 }
 
 template <typename T, typename P>
-auto Serializer::serialize_pair(const long int id, const P policy)
+auto SerializerFlatbuffers::serialize_pair(const long int id, const P policy)
                                  -> FbOff<typename type_helper<T>::pair> {
        auto create_policy_pair = get_create_policy_pair<T>();
        return create_policy_pair(m_builder, id, serialize_policy(policy));
 }
 
 template <typename TP>
-auto Serializer::serialize_set() -> FbOff<typename type_helper<TP>::set> {
+auto SerializerFlatbuffers::serialize_set() -> FbOff<typename type_helper<TP>::set> {
        auto context_default = serialize_policy<TP>(m_db->getPolicyContextDefault<TP>());
        auto context_mandatory = serialize_policy<TP>(m_db->getPolicyContextMandatory<TP>());
 
@@ -293,7 +293,7 @@ auto Serializer::serialize_set() -> FbOff<typename type_helper<TP>::set> {
 }
 
 template <>
-auto Serializer::serialize_set<PolicyAccess>(FbOff<FB::PolicyAccess> context_default,
+auto SerializerFlatbuffers::serialize_set<PolicyAccess>(FbOff<FB::PolicyAccess> context_default,
                                             FbOff<FB::PolicyAccess> context_mandatory)
                                               -> FbOff<typename type_helper<PolicyAccess>::set>
 {
@@ -301,7 +301,7 @@ auto Serializer::serialize_set<PolicyAccess>(FbOff<FB::PolicyAccess> context_def
 }
 
 template <typename TP, typename TFP>
-auto Serializer::serialize_set(FbOff<TFP> context_default,
+auto SerializerFlatbuffers::serialize_set(FbOff<TFP> context_default,
                               FbOff<TFP> context_mandatory)
                                 -> FbOff<typename type_helper<TP>::set>
 {
diff --git a/src/internal/serializer_flatbuffers.hpp b/src/internal/serializer_flatbuffers.hpp
new file mode 100644 (file)
index 0000000..3dbed7d
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+#ifndef _SERIALIZER_HPP
+#define _SERIALIZER_HPP
+
+#include <ostream>
+#include <memory>
+#include <string>
+
+#include "include/flatbuffers/flatbuffers.h"
+#include "include/fb_generated.h"
+
+#include "storage_backend_xml.hpp"
+#include "policy_containers.hpp"
+
+namespace ldp_serializer
+{
+       enum class SetType : uint8_t {
+               OWN,
+               SEND,
+               RECEIVE,
+               ACCESS
+       };
+
+       template <typename T>
+       using FbOff = flatbuffers::Offset<T>;
+
+       class SerializerFlatbuffers {
+       private:
+               template <typename T>
+               struct type_helper;
+
+               const ldp_xml::StorageBackendXML *m_db;
+               flatbuffers::FlatBufferBuilder m_builder;
+
+               template <typename T>
+               auto get_create_set() -> decltype(type_helper<T>::create_set);
+               template <typename T>
+               auto get_create_policy() -> decltype(type_helper<T>::create_policy);
+               template <typename T>
+               auto get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair);
+               template <typename T>
+               auto get_create_item() -> decltype(type_helper<T>::create_item);
+
+               FbOff<FB::PolicyOwn> serialize_tree(const ldp_xml_parser::OwnershipTree &tree);
+               FbOff<FB::PolicyOwnNode> serialize_tree(const std::shared_ptr<ldp_xml_parser::TreeNode> &node);
+               FbOff<FB::DecisionItem> serialize_decision(const ldp_xml_parser::DecisionItem &item);
+
+               template <typename T, typename P>
+               auto serialize_item(const P &item) -> FbOff<typename type_helper<T>::item>;
+
+               template <typename T>
+               auto serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy>;
+
+               template <typename T>
+               auto serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
+                                       -> FbOff<typename type_helper<T>::policy>;
+
+               template <typename T, typename P>
+               auto serialize_pair(const long int id, const P policy)
+                                       -> FbOff<typename type_helper<T>::pair>;
+
+               template <typename TP>
+               auto serialize_set() -> FbOff<typename type_helper<TP>::set>;
+
+               template <typename TP, typename TFP>
+               auto serialize_set(FbOff<TFP> context_default,
+                                       FbOff<TFP> context_mandatory)
+                                               -> FbOff<typename type_helper<TP>::set>;
+       public:
+               SerializerFlatbuffers() : m_db(nullptr) {}
+               const uint8_t *serialize(const ldp_xml::StorageBackendXML &db, size_t &size);
+               const uint8_t *serialize(const std::string config_path, size_t &size);
+               const uint8_t *serialize(const std::string config_path, std::ostream &output);
+               friend class SerializerTests;
+       };
+}
+
+
+#endif