62847fe68d46fd7ec6cf23730e91e5e8b340f5c4
[platform/core/system/libdbuspolicy.git] / src / internal / serializer_flatbuffers.hpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16 #ifndef _SERIALIZER_HPP
17 #define _SERIALIZER_HPP
18
19 #include <ostream>
20 #include <memory>
21 #include <string>
22
23 #include "include/flatbuffers/flatbuffers.h"
24 #include "include/fb_generated.h"
25
26 #include "storage_backend_xml.hpp"
27 #include "policy_containers.hpp"
28
29 namespace ldp_serializer
30 {
31         template <typename T>
32         using FbOff = flatbuffers::Offset<T>;
33
34         class SerializerFlatbuffers {
35         private:
36                 template <typename T>
37                 struct type_helper;
38
39                 const ldp_xml::StorageBackendXML *m_db;
40                 flatbuffers::FlatBufferBuilder m_builder;
41
42                 template <typename T>
43                 auto get_create_set() -> decltype(type_helper<T>::create_set);
44                 template <typename T>
45                 auto get_create_policy() -> decltype(type_helper<T>::create_policy);
46                 template <typename T>
47                 auto get_create_policy_pair() -> decltype(type_helper<T>::create_policy_pair);
48                 template <typename T>
49                 auto get_create_item() -> decltype(type_helper<T>::create_item);
50
51                 FbOff<FB::PolicyOwn> serialize_tree(const ldp_xml_parser::OwnershipTree &tree);
52                 FbOff<FB::PolicyOwnNode> serialize_tree(const std::shared_ptr<ldp_xml_parser::TreeNode> &node);
53                 FbOff<FB::DecisionItem> serialize_decision(const ldp_xml_parser::DecisionItem &item);
54
55                 template <typename T, typename P>
56                 auto serialize_item(const P &item) -> FbOff<typename type_helper<T>::item>;
57
58                 template <typename T>
59                 auto serialize_policy(const T &policy) -> FbOff<typename type_helper<T>::policy>;
60
61                 template <typename T>
62                 auto serialize_policy(const std::vector<FbOff<typename type_helper<T>::item>> items)
63                                         -> FbOff<typename type_helper<T>::policy>;
64
65                 template <typename T, typename P>
66                 auto serialize_pair(const long int id, const P policy)
67                                         -> FbOff<typename type_helper<T>::pair>;
68
69                 template <typename TP>
70                 auto serialize_set() -> FbOff<typename type_helper<TP>::set>;
71
72                 template <typename TP, typename TFP>
73                 auto serialize_set(FbOff<TFP> context_default,
74                                         FbOff<TFP> context_mandatory)
75                                                 -> FbOff<typename type_helper<TP>::set>;
76         public:
77                 SerializerFlatbuffers() : m_db(nullptr) {}
78                 const uint8_t *serialize(const ldp_xml::StorageBackendXML &db, size_t &size);
79                 friend class SerializerTests;
80         };
81 }
82
83
84 #endif