OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Packer.hpp
1 /*
2  * Packer_cls.hpp
3  *
4  * Created on: Jul 15, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef SRC_PACKER_HPP_
9 #define SRC_PACKER_HPP_
10 
11 #include "util/object_util.hpp"
12 //#include "Grid/util.hpp"
13 #include "Vector/util.hpp"
14 #include "memory/ExtPreAlloc.hpp"
15 #include "util/util_debug.hpp"
16 
17 
18 #include "Grid/grid_sm.hpp"
19 #include "util/Pack_stat.hpp"
20 #include "Pack_selector.hpp"
21 #include "has_pack_encap.hpp"
22 #include "Packer_util.hpp"
23 
43 template<typename T, typename Mem, int pack_type >
44 class Packer
45 {
46 public:
47 
51  static void pack(ExtPreAlloc<Mem> , const T & obj)
52  {
53  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
54  }
55 
59  static size_t packRequest(const T & obj, size_t & req)
60  {
61  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
62  return 0;
63  }
64 };
65 
72 template<typename T, typename Mem>
73 class Packer<T,Mem,PACKER_PRIMITIVE>
74 {
75 public:
76 
84  inline static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts)
85  {
86  ext.allocate(sizeof(T));
87  *(T *)ext.getPointer() = obj;
88 
89  // update statistic
90  sts.incReq();
91  }
92 
98  static void packRequest(const T & obj, size_t & req)
99  {
100  req += sizeof(T);
101  }
102 
108  static void packRequest(size_t & req)
109  {
110  req += sizeof(T);
111  }
112 };
113 
122 template<typename T, typename Mem>
123 class Packer<T,Mem,PACKER_ARRAY_PRIMITIVE>
124 {
125 public:
126 
134  inline static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts, size_t n)
135  {
136  //Pack the size of a vector
137  Packer<size_t, Mem>::pack(ext,obj.size(),sts);
138 
139  //Pack a vector
140  ext.allocate(sizeof(typename T::value_type)*n);
141  memcpy(ext.getPointer(),obj.getPointer(),sizeof(typename T::value_type)*n);
142 
143  // update statistic
144  sts.incReq();
145  }
146 
152  static void packRequest(T & obj,size_t & req)
153  {
154  req += sizeof(typename T::value_type)*obj.size();
155  }
156 };
157 
158 
165 template<typename T, typename Mem>
166 class Packer<T,Mem,PACKER_OBJECTS_WITH_WARNING_POINTERS>
167 {
168 public:
169 
177  static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts)
178  {
179 #ifdef SE_CLASS1
180  if (ext.ref() == 0)
181  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
182 
183  if (!(std::is_array<T>::value == true && std::is_fundamental<typename std::remove_all_extents<T>::type>::value == true))
184  std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " impossible to check the type " << demangle(typeid(T).name()) << " please consider to add a static method like \"static bool noPointers() {return true;}\" \n" ;
185 #endif
186  ext.allocate(sizeof(T));
187  memcpy((T *)ext.getPointer(),&obj,sizeof(T));
188 
189  // update statistic
190  sts.incReq();
191  }
192 
198  static void packRequest(const T & obj,size_t & req)
199  {
200  req += sizeof(T);
201  }
202 
208  static void packRequest(size_t & req)
209  {
210  req += sizeof(T);
211  }
212 };
213 
220 template<typename T, typename Mem>
221 class Packer<T,Mem,PACKER_OBJECTS_WITH_POINTER_CHECK>
222 {
223 public:
224 
232  static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts)
233  {
234 #ifdef DEBUG
235  if (ext.ref() == 0)
236  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
237 
238  if (obj.noPointers() == false)
239  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " has pointers inside, sending pointers values has no sense\n";
240 #endif
241  ext.allocate(sizeof(T));
242  memcpy((T *)ext.getPointer(),&obj,sizeof(T));
243 
244  // Update statistic
245  sts.incReq();
246  }
247 
253  static void packRequest(const T & obj,size_t & req)
254  {
255  req += sizeof(T);
256  }
257 
263  static void packRequest(size_t & req)
264  {
265  req += sizeof(T);
266  }
267 };
268 
275 template<typename T, typename Mem>
276 class Packer<T,Mem,PACKER_GENERAL>
277 {
278 public:
279 
280  template<int ... prp> static void packRequest(const T & obj, size_t & req)
281  {
282  obj.template packRequest<prp...>(req);
283  };
284 
285  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, const T & obj, Pack_stat & sts)
286  {
287  obj.template pack<prp...>(mem, sts);
288  };
289 };
290 
297 template<typename T, typename Mem>
298 class Packer<T,Mem,PACKER_GRID>
299 {
300 public:
301 
302  template<int ... prp> static void packRequest(const T & obj, size_t & req)
303  {
304  obj.template packRequest<prp...>(req);
305  };
306 
307  template<int ... prp> static void packRequest(T & obj, grid_key_dx_iterator_sub<T::dims> & sub, size_t & req)
308  {
309  obj.template packRequest<prp...>(sub, req);
310  };
311 
312  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, const T & obj, Pack_stat & sts)
313  {
314  obj.template pack<prp...>(mem, sts);
315  }
316 
317  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, T & obj, grid_key_dx_iterator_sub<T::dims> & sub_it, Pack_stat & sts)
318  {
319  obj.template pack<prp...>(mem, sub_it, sts);
320  }
321 };
322 
323 template<typename T, typename Mem>
324 class Packer<T,Mem,PACKER_ENCAP_OBJECTS>
325 {
326 public:
327 
332  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, const T & eobj, Pack_stat & sts)
333  {
334 #ifdef DEBUG
335  if (mem.ref() == 0)
336  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
337 #endif
338 
341  else
342  {
343  if (sizeof...(prp) == 0)
344  {
345  mem.allocate(sizeof(typename T::T_type));
346  encapc<1,typename T::T_type,typename memory_traits_lin< typename T::T_type >::type> enc(*static_cast<typename T::T_type::type *>(mem.getPointer()));
347  enc = eobj;
348  }
349  else
350  {
351  typedef object<typename object_creator<typename T::type,prp...>::type> prp_object;
352  mem.allocate(sizeof(prp_object));
353  encapc<1,prp_object,typename memory_traits_lin< typename T::T_type >::type> enc(*static_cast<typename prp_object::type *>(mem.getPointer()));
354  object_si_d<T,decltype(enc),OBJ_ENCAP,prp ... >(eobj,enc);
355  }
356  }
357 
358  // update statistic
359  sts.incReq();
360  }
361 
366  template<int ... prp> void packRequest(T & eobj,size_t & req)
367  {
368  if (has_pack_encap<T>::value == true)
370  else
371  {
372  if (sizeof...(prp) == 0)
373  return;
374 
375  typedef object<typename object_creator<typename T::type,prp...>::type> prp_object;
376 
377  req += sizeof(prp_object);
378  }
379  }
380 };
381 
382 #endif /* SRC_PACKER_HPP_ */
static void packRequest(const T &obj, size_t &req)
it add a request to pack an object
Definition: Packer.hpp:253
static void packRequest(size_t &req)
It add a request to pack a C++ primitive.
Definition: Packer.hpp:108
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts)
It pack any C++ primitives.
Definition: Packer.hpp:84
static void packRequest(const T &obj, size_t &req)
It add a request to pack a C++ primitive.
Definition: Packer.hpp:98
virtual void * getPointer()
Return the pointer of the last allocation.
static void packRequest(T &obj, size_t &req)
It add a request to pack a C++ primitive.
Definition: Packer.hpp:152
static void packRequest(size_t &req)
it add a request to pack an object
Definition: Packer.hpp:263
virtual bool allocate(size_t sz)
Allocate a chunk of memory.
Definition: ExtPreAlloc.hpp:92
virtual long int ref()
Return the reference counter.
Definition: ExtPreAlloc.hpp:77
static size_t packRequest(const T &obj, size_t &req)
Error, no implementation.
Definition: Packer.hpp:59
void incReq()
Increment the request pointer.
Definition: Pack_stat.hpp:70
Packing class.
Definition: Packer.hpp:44
static void packRequest(const T &obj, size_t &req)
it add a request to pack an object
Definition: Packer.hpp:198
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts)
It pack any object checking that the object does not have pointers inside.
Definition: Packer.hpp:232
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:77
It create a boost::fusion vector with the selected properties.
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts, size_t n)
It packs arrays of C++ primitives.
Definition: Packer.hpp:134
static void packRequest(size_t &req)
it add a request to pack an object
Definition: Packer.hpp:208
Packing status object.
Definition: Pack_stat.hpp:51
virtual size_t size() const
Get the size of the LAST allocated memory.
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:51
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts)
It pack an object.
Definition: Packer.hpp:177
It copy the properties from one object to another.
static void call_packRequest(encap &obj, size_t &req)
pack/serialize
Definition: Packer_util.hpp:70