summaryrefslogtreecommitdiff
path: root/src/internal/serializer.hpp
blob: 466ceb6254ec305bfbf782faa50d1ce96b1ac960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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 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;
	};
}


#endif