Packer_util.hpp 7.89 KB
Newer Older
Yaroslav's avatar
Yaroslav committed
1
2
3
4
5
6
7
8
9
10
/*
 * Packer_util.hpp
 *
 *  Created on: Jan 25, 2016
 *      Author: Yaroslav Zaluzhnyi
 */

#ifndef PACKER_UTIL_HPP_
#define PACKER_UTIL_HPP_

Pietro Incardona's avatar
Pietro Incardona committed
11
12
13
14
15
16
#include "prp_all_zero.hpp"
#include "Packer_Unpacker/Pack_selector.hpp"
#include "memory/HeapMemory.hpp"
#include "Vector/map_vector_grow_p.hpp"
#include "Vector/vect_isel.hpp"
#include "memory_ly/memory_conf.hpp"
Yaroslav's avatar
Yaroslav committed
17

Pietro Incardona's avatar
Pietro Incardona committed
18
19
20
template<typename T, typename Mem, int pack_type=Pack_selector<T>::value > class Packer;
template<typename T, typename Mem, int pack_type=Pack_selector<T>::value > class Unpacker;

incardon's avatar
incardon committed
21
#include "Vector/vector_def.hpp"
Pietro Incardona's avatar
Pietro Incardona committed
22
23
24
25
26
27
28
29
30
31

///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// FUNCTORS FOR ENCAP ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////


//A functor for call_aggregatePackRequest
template<typename encap, typename Mem>
struct call_packRequest_enc_functor
{
incardon's avatar
incardon committed
32
	//! encap object to pack (serialize)
Pietro Incardona's avatar
Pietro Incardona committed
33
	encap & obj;
incardon's avatar
incardon committed
34
35

	//! byte counter
36
	size_t & req;
Pietro Incardona's avatar
Pietro Incardona committed
37

incardon's avatar
incardon committed
38
39
40
41
42
43
	/* \brief Constructor
	 *
	 * \param obj object to pack/serialize
	 * \param req byte to pack/serialize
	 *
	 */
44
45
	call_packRequest_enc_functor(encap & obj, size_t & req)
	:obj(obj),req(req)
Pietro Incardona's avatar
Pietro Incardona committed
46
47
48
49
50
51
52
53
	{}

	//! It calls the pack request for each property
	template<typename T>
	inline void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename encap::T_type,T>::type obj_type;

54
		Packer<obj_type,Mem>::packRequest(obj.template get<T::value>(),req);
Pietro Incardona's avatar
Pietro Incardona committed
55
56
57
58
59
60
61
62
	}
};


//Calls a pack request in nested way
template<typename encap, typename Mem, int ... prp>
struct call_encapPackRequest
{
incardon's avatar
incardon committed
63
64
65
66
67
68
	/*! \brief pack/serialize
	 *
	 * \param obj object to serialize
	 * \param req byte counter needed to pack/serialize the object
	 *
	 */
69
	static inline void call_packRequest(encap & obj, size_t & req)
Pietro Incardona's avatar
Pietro Incardona committed
70
71
72
73
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
		typedef typename prp_all_zero<encap,sizeof...(prp) == 0,prp...>::type b_prp;

74
		call_packRequest_enc_functor<encap,Mem> functor(obj,req);
Pietro Incardona's avatar
Pietro Incardona committed
75
76
77
78
79
80
81
82
83
84

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
};

//A functor for call_aggregatePack
template<typename encap, typename Mem>
struct call_pack_enc_functor
{
incardon's avatar
incardon committed
85
	//! Memory that pack/serialize the object
Pietro Incardona's avatar
Pietro Incardona committed
86
	ExtPreAlloc<Mem> & mem;
incardon's avatar
incardon committed
87
88

	//! Object to serialize
Pietro Incardona's avatar
Pietro Incardona committed
89
	const encap & obj;
incardon's avatar
incardon committed
90
91

	//! serialization status
Pietro Incardona's avatar
Pietro Incardona committed
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
	Pack_stat & sts;

	call_pack_enc_functor(ExtPreAlloc<Mem> & mem,const encap & obj, Pack_stat & sts)
	:mem(mem), obj(obj), sts(sts)
	{
	}

	//! It calls the packer for each property
	template<typename T>
	inline void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename encap::type,T>::type obj_type;

		Packer<obj_type,Mem>::pack(mem,obj.template get<T::value>(),sts);
	}
};

//Calls a packer in nested way
template<typename encap, typename Mem, int ... prp>
struct call_encapPack
{
	static inline void call_pack(const encap & obj, ExtPreAlloc<Mem> & mem, Pack_stat & sts)
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
		typedef typename prp_all_zero<typename encap::T_type,sizeof...(prp) == 0,prp...>::type b_prp;

		call_pack_enc_functor<encap,Mem> functor(mem,obj,sts);

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
};


//A functor for call_aggregateUnpack
template<typename encap, typename Mem, int ... prp>
struct call_unpack_encap_functor
Yaroslav's avatar
Yaroslav committed
129
{
Pietro Incardona's avatar
Pietro Incardona committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	ExtPreAlloc<Mem> & mem;
	const encap & obj;
	Unpack_stat & ps;

	call_unpack_encap_functor(ExtPreAlloc<Mem> & mem, const encap & obj, Unpack_stat & ps)
	:mem(mem), obj(obj), ps(ps)
	{
	}

	//! It calls the unpacker for each property
	template<typename T>
	inline void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename encap::type,T>::type obj_type;

		Unpacker<obj_type,Mem>::unpack(mem,obj.template get<T::value>(),ps);
	}
Yaroslav's avatar
Yaroslav committed
147
};
Pietro Incardona's avatar
Pietro Incardona committed
148
149
150
151

//Calls an unpacker in nested way
template<typename encap, typename Mem, int ... prp>
struct call_encapUnpack
Yaroslav's avatar
Yaroslav committed
152
{
Pietro Incardona's avatar
Pietro Incardona committed
153
154
155
156
157
158
159
160
161
162
	static inline void call_unpack(encap & obj, ExtPreAlloc<Mem> & mem, Unpack_stat & ps)
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
		typedef typename prp_all_zero<encap,sizeof...(prp) == 0,prp...>::type b_prp;

		call_unpack_encap_functor<encap,Mem,prp ... > functor(mem,obj,ps);

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
Yaroslav's avatar
Yaroslav committed
163
164
165
};


Pietro Incardona's avatar
Pietro Incardona committed
166
167
168
169
////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// FUNCTORS FOR AGGREGATE ////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

Yaroslav's avatar
Yaroslav committed
170
//A functor for call_aggregatePackRequest
Yaroslav's avatar
Yaroslav committed
171

Yaroslav's avatar
Yaroslav committed
172
template<typename obj_type, typename Mem>
Yaroslav's avatar
Yaroslav committed
173
174
175
struct call_packRequest_agg_functor
{

Yaroslav's avatar
Yaroslav committed
176
	const obj_type & obj;
177
	size_t & req;
Yaroslav's avatar
Yaroslav committed
178

179
180
	call_packRequest_agg_functor(const obj_type & obj, size_t & req)
	:obj(obj),req(req)
Yaroslav's avatar
Yaroslav committed
181
182
183
184
185
186
	{}

	//! It calls the pack request for each property
	template<typename T>
	inline void operator()(T& t)
	{
Yaroslav's avatar
Yaroslav committed
187
		typedef typename boost::mpl::at<typename obj_type::type,T>::type obj_t;
Yaroslav's avatar
Yaroslav committed
188

Yaroslav's avatar
Yaroslav committed
189
		//for (size_t i = 0; i < obj_type::max_prop ; i++)
190
		Packer<obj_t,Mem>::packRequest(obj.template get<T::value>(),req);
Yaroslav's avatar
Yaroslav committed
191
192
193
	}
};

Pietro Incardona's avatar
Pietro Incardona committed
194

Yaroslav's avatar
Yaroslav committed
195
//Calls a pack request in nested way
Yaroslav's avatar
Yaroslav committed
196
template<typename obj_type, typename Mem, int ... prp>
Yaroslav's avatar
Yaroslav committed
197
198
struct call_aggregatePackRequest
{
199
	static inline void call_packRequest(const obj_type & obj, size_t & req)
Yaroslav's avatar
Yaroslav committed
200
201
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
Yaroslav's avatar
Yaroslav committed
202
		typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
Yaroslav's avatar
Yaroslav committed
203

204
		call_packRequest_agg_functor<obj_type,Mem> functor(obj,req);
Yaroslav's avatar
Yaroslav committed
205
206
207
208
209
210
211

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
};

//A functor for call_aggregatePack
Yaroslav's avatar
Yaroslav committed
212
template<typename obj_type, typename Mem>
Yaroslav's avatar
Yaroslav committed
213
214
215
struct call_pack_agg_functor
{
	ExtPreAlloc<Mem> & mem;
Yaroslav's avatar
Yaroslav committed
216
	const obj_type & obj;
Yaroslav's avatar
Yaroslav committed
217
218
	Pack_stat & sts;

Yaroslav's avatar
Yaroslav committed
219
	call_pack_agg_functor(ExtPreAlloc<Mem> & mem, const obj_type & obj, Pack_stat & sts)
Yaroslav's avatar
Yaroslav committed
220
221
222
223
224
225
226
227
	:mem(mem), obj(obj), sts(sts)
	{
	}

	//! It calls the packer for each property
	template<typename T>
	inline void operator()(T& t)
	{
Yaroslav's avatar
Yaroslav committed
228
		typedef typename boost::mpl::at<typename obj_type::type,T>::type obj_t;
Yaroslav's avatar
Yaroslav committed
229

Yaroslav's avatar
Yaroslav committed
230
		//for (size_t i = 0; i < obj.size(); i++)
Yaroslav's avatar
Yaroslav committed
231
			Packer<obj_t,Mem>::pack(mem,obj.template get<T::value>(),sts);
Yaroslav's avatar
Yaroslav committed
232
233
234
235
	}
};

//Calls a packer in nested way
Yaroslav's avatar
Yaroslav committed
236
template<typename obj_type, typename Mem, int ... prp>
Yaroslav's avatar
Yaroslav committed
237
238
struct call_aggregatePack
{
Yaroslav's avatar
Yaroslav committed
239
	static inline void call_pack(const obj_type & obj, ExtPreAlloc<Mem> & mem, Pack_stat & sts)
Yaroslav's avatar
Yaroslav committed
240
241
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
Yaroslav's avatar
Yaroslav committed
242
		typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
Yaroslav's avatar
Yaroslav committed
243

Yaroslav's avatar
Yaroslav committed
244
		call_pack_agg_functor<obj_type,Mem> functor(mem,obj,sts);
Yaroslav's avatar
Yaroslav committed
245
246
247
248
249
250
251
252

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
};


//A functor for call_aggregateUnpack
Yaroslav's avatar
Yaroslav committed
253
template<typename obj_type, typename Mem>
Yaroslav's avatar
Yaroslav committed
254
255
256
struct call_unpack_agg_functor
{
	ExtPreAlloc<Mem> & mem;
Yaroslav's avatar
Yaroslav committed
257
	const obj_type & obj;
Yaroslav's avatar
Yaroslav committed
258
259
	Unpack_stat & ps;

Yaroslav's avatar
Yaroslav committed
260
	call_unpack_agg_functor(ExtPreAlloc<Mem> & mem, const obj_type & obj, Unpack_stat & ps)
Yaroslav's avatar
Yaroslav committed
261
262
263
264
265
266
267
268
	:mem(mem), obj(obj), ps(ps)
	{
	}

	//! It calls the unpacker for each property
	template<typename T>
	inline void operator()(T& t)
	{
Yaroslav's avatar
Yaroslav committed
269
		typedef typename boost::mpl::at<typename obj_type::type,T>::type obj_t;
Yaroslav's avatar
Yaroslav committed
270

Yaroslav's avatar
Yaroslav committed
271
		//for (size_t i = 0; i < obj.size(); i++)
Yaroslav's avatar
Yaroslav committed
272
			Unpacker<obj_t,Mem>::unpack(mem,obj.template get<T::value>(),ps);
Yaroslav's avatar
Yaroslav committed
273
274
275
276
	}
};

//Calls an unpacker in nested way
Yaroslav's avatar
Yaroslav committed
277
template<typename obj_type, typename Mem, int ... prp>
Yaroslav's avatar
Yaroslav committed
278
279
struct call_aggregateUnpack
{
Yaroslav's avatar
Yaroslav committed
280
	static inline void call_unpack(const obj_type & obj, ExtPreAlloc<Mem> & mem, Unpack_stat & ps)
Yaroslav's avatar
Yaroslav committed
281
282
	{
		//Property sequence into boost::mpl::range_c or boost::mpl::vector, depending on sizeof...(prp)
Yaroslav's avatar
Yaroslav committed
283
		typedef typename prp_all_zero<obj_type,sizeof...(prp) == 0,prp...>::type b_prp;
Yaroslav's avatar
Yaroslav committed
284

Yaroslav's avatar
Yaroslav committed
285
		call_unpack_agg_functor<obj_type,Mem> functor(mem,obj,ps);
Yaroslav's avatar
Yaroslav committed
286
287
288
289
290
291
292

		//Apply functor for each property
		boost::mpl::for_each_ref<b_prp>(functor);
	}
};

#endif /* PACKER_UTIL_HPP_ */