summaryrefslogtreecommitdiff
path: root/include/CommonAPI/Struct.hpp
blob: b8d748fa55ce64a7ee23eaeeb4917b29a33f02cc (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (C) 2014-2015 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#if !defined (COMMONAPI_INTERNAL_COMPILATION)
#error "Only <CommonAPI/CommonAPI.hpp> can be included directly, this file may disappear or change contents."
#endif

#ifndef COMMONAPI_STRUCT_HPP_
#define COMMONAPI_STRUCT_HPP_

#include <iostream>
#include <tuple>

namespace CommonAPI {

typedef uint32_t Serial;

template<class _Derived>
class InputStream;

template<class _Derived>
class OutputStream;

template<class _Derived>
class TypeOutputStream;

template<int, class, class, class>
struct StructReader;

template<
	int _Index, class _Input,
    template<class...> class _V, class... _Values,
    template<class...> class _D, class... _Depls>
struct StructReader<_Index, _Input, _V<_Values...>, _D<_Depls...>> {
	void operator()(InputStream<_Input> &_input,
					_V<_Values...> &_values,
					const _D<_Depls...> *_depls) {
		StructReader<_Index-1, _Input, _V<_Values...>, _D<_Depls...>>{}(_input, _values, _depls);
		_input.template readValue<>(std::get<_Index>(_values.values_),
									(_depls ? std::get<_Index>(_depls->values_) : nullptr));
	}
};

template<
	int _Index, class _Input,
	template<class...> class _V, class... _Values,
	class _D>
struct StructReader<_Index, _Input, _V<_Values...>, _D> {
	void operator()(InputStream<_Input> &_input,
					_V<_Values...> &_values,
					const _D *_depls) {
		StructReader<_Index-1, _Input, _V<_Values...>, _D>{}(_input, _values, _depls);
		_input.template readValue<_D>(std::get<_Index>(_values.values_));
	}
};

template<class _Input,
	template<class...> class _V, class... _Values,
	template<class...> class _D, class... _Depls>
struct StructReader<0, _Input, _V<_Values...>, _D<_Depls...>> {
	void operator()(InputStream<_Input> &_input,
					_V<_Values...> &_values,
					const _D<_Depls...> *_depls) {
		_input.template readValue<>(std::get<0>(_values.values_),
									(_depls ? std::get<0>(_depls->values_) : nullptr));
	}
};

template<class _Input,
	template<class...> class _V, class... _Values,
	class _D>
struct StructReader<0, _Input, _V<_Values...>, _D> {
	void operator()(InputStream<_Input> &_input,
					_V<_Values...> &_values,
					const _D *_depls) {
		_input.template readValue<_D>(std::get<0>(_values.values_));
	}
};


template< int, class, class, class >
struct StructWriter;

template<
	int _Index, class _Output,
    template<class ...> class _V, class... _Values,
    template <class...> class _D, class... _Depls>
struct StructWriter<_Index, _Output, _V<_Values...>, _D<_Depls...>> {
	void operator()(OutputStream<_Output> &_output,
					const _V<_Values...> &_values,
					const _D<_Depls...> *_depls) {
		StructWriter<_Index-1, _Output, _V<_Values...>, _D<_Depls...>>{}(_output, _values, _depls);
		_output.template writeValue<>(std::get<_Index>(_values.values_),
									  (_depls ? std::get<_Index>(_depls->values_) : nullptr));
	}
};

template<
	int _Index, class _Output,
	template<class...> class _V, class... _Values,
	class _D>
struct StructWriter<_Index, _Output, _V<_Values...>, _D> {
	void operator()(OutputStream<_Output> &_output,
					const _V<_Values...> &_values,
					const _D *_depls) {
		StructWriter<_Index-1, _Output, _V<_Values...>, _D>{}(_output, _values, _depls);
		_output.template writeValue<_D>(std::get<_Index>(_values.values_));
	}
};

template<class _Output,
	template<class...> class _V, class... _Values,
	template<class...> class _D, class... _Depls>
struct StructWriter<0, _Output, _V<_Values...>, _D<_Depls...>> {
	void operator()(OutputStream<_Output> &_output,
					const _V<_Values...> &_values,
					const _D<_Depls...> *_depls) {
		_output.template writeValue<>(std::get<0>(_values.values_),
									  (_depls ? std::get<0>(_depls->values_) : nullptr));
	}
};

template<class _Output,
	template<class...> class _V, class... _Values,
	class _D>
struct StructWriter<0, _Output, _V<_Values...>, _D> {
	void operator()(OutputStream<_Output> &_output,
					const _V<_Values...> &_values,
					const _D *_depls) {
		_output.template writeValue<_D>(std::get<0>(_values.values_));
	}
};

template<int, class, class>
struct StructTypeWriter;

template<int _Index, class _TypeOutput,
	template<class...> class _V, class... _Values>
struct StructTypeWriter<_Index, _TypeOutput, _V<_Values...>> {
	void operator()(TypeOutputStream<_TypeOutput> &_output,
					const _V<_Values...> &_values) {
		StructTypeWriter<_Index-1, _TypeOutput, _V<_Values...>>{}(_output, _values);
#ifdef WIN32
		_output.writeType(std::get<_Index>(_values.values_));
#else
		_output.template writeType(std::get<_Index>(_values.values_));
#endif
	}
};

template<class _TypeOutput,
	template<class...> class _V, class... _Values>
struct StructTypeWriter<0, _TypeOutput, _V<_Values...>> {
	void operator()(TypeOutputStream<_TypeOutput> &_output,
					const _V<_Values...> &_values) {
#ifdef WIN32
		_output.writeType(std::get<0>(_values.values_));
#else
		_output.template writeType(std::get<0>(_values.values_));
#endif
	}
};

// Structures are mapped to a (generated) struct which inherits from CommonAPI::Struct.
// CommonAPI::Struct holds the structured data in a tuple. The generated class provides
// getter- and setter-methods for the structure members.
template<typename... _Types>
struct Struct {
	std::tuple<_Types...> values_;
};

// Polymorphic structs are mapped to an interface that is derived from the base class
// PolymorphicStruct and contain their parameter in a Struct.
struct PolymorphicStruct {
	virtual const Serial getSerial() const = 0;
};

} // namespace CommonAPI

#endif // COMMONAPI_STRUCT_HPP_