OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
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 #ifdef SRC_PACK_SELECTOR_HPP_
18 #error blabla
19 #endif
20 
21 #include "Grid/grid_sm.hpp"
22 #include "util/Pack_stat.hpp"
23 #include "Pack_selector.hpp"
24 //#include "Vector/map_vector.hpp"
25 
45 template<typename T, typename Mem, int pack_type=Pack_selector<T>::value >
46 class Packer
47 {
48 public:
49 
53  static void pack(ExtPreAlloc<Mem> , const T & obj)
54  {
55  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
56  }
57 
61  static size_t packRequest(std::vector<size_t> & req)
62  {
63  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
64  return 0;
65  }
66 };
67 
74 template<typename T, typename Mem>
75 class Packer<T,Mem,PACKER_PRIMITIVE>
76 {
77 public:
78 
86  inline static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts)
87  {
88  ext.allocate(sizeof(T));
89  *(T *)ext.getPointer() = obj;
90 
91  // update statistic
92  sts.incReq();
93  }
94 
100  static void packRequest(std::vector<size_t> & req)
101  {
102  req.push_back(sizeof(T));
103  }
104 };
105 
112 template<typename T, typename Mem>
113 class Packer<T,Mem,PACKER_ARRAY_PRIMITIVE>
114 {
115 public:
116 
124  inline static void pack(ExtPreAlloc<Mem> & ext, const T & obj, Pack_stat & sts, size_t n)
125  {
126  //Pack the size of a vector
127  Packer<size_t, Mem>::pack(ext,obj.size(),sts);
128 
129  //Pack a vector
130  ext.allocate(sizeof(typename T::value_type)*n);
131  memcpy(ext.getPointer(),obj.getPointer(),sizeof(typename T::value_type)*n);
132 
133  // update statistic
134  sts.incReq();
135  }
136 
142  static void packRequestDummy(std::vector<size_t> & req)
143  {
144  req.push_back(sizeof(T));
145  }
146 };
147 
148 
155 template<typename T, typename Mem>
156 class Packer<T,Mem,PACKER_OBJECTS_WITH_WARNING_POINTERS>
157 {
158 public:
159 
167  static void pack(ExtPreAlloc<Mem> & ext, T & obj, Pack_stat & sts)
168  {
169 #ifdef DEBUG
170  if (ext.ref() == 0)
171  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
172 
173  std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " impossible to check the type " << demangle(typeid(T).name()) << " please consider to add a static method \"void noPointers()\" \n" ;
174 #endif
175  ext.allocate(sizeof(T));
176  memcpy((T *)ext.getPointer(),&obj,sizeof(T));
177 
178  // update statistic
179  sts.incReq();
180  }
181 
187  static void packRequest(std::vector<size_t> & req)
188  {
189  req.push_back(sizeof(T));
190  }
191 };
192 
199 template<typename T, typename Mem>
200 class Packer<T,Mem,PACKER_OBJECTS_WITH_POINTER_CHECK>
201 {
202 public:
203 
211  static void pack(ExtPreAlloc<Mem> & ext, T & obj, Pack_stat & sts)
212  {
213 #ifdef DEBUG
214  if (ext.ref() == 0)
215  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
216 
217  if (obj.noPointers() == false)
218  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " has pointers inside, sending pointers values has no sense\n";
219 #endif
220  ext.allocate(sizeof(T));
221  memcpy((T *)ext.getPointer(),&obj,sizeof(T));
222 
223  // Update statistic
224  sts.incReq();
225  }
226 
232  static void packRequest(std::vector<size_t> & req)
233  {
234  req.push_back(sizeof(T));
235  }
236 };
237 
244 template<typename T, typename Mem>
245 class Packer<T,Mem,PACKER_VECTOR>
246 {
247 public:
248 
249  template<int ... prp> static void packRequest(T & obj, std::vector<size_t> & v)
250  {
251  obj.template packRequest<prp...>(v);
252  };
253 
254  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, T & obj, Pack_stat & sts)
255  {
256 #ifdef DEBUG
257  std::cout << "Inside vector pack() function! (packer.hpp)" << std::endl;
258 #endif
259  obj.template pack<prp...>(mem, sts);
260  };
261 };
262 
269 template<typename T, typename Mem>
270 class Packer<T,Mem,PACKER_GRID>
271 {
272 public:
273 
274  template<int ... prp> static void packRequest(T & obj, std::vector<size_t> & v)
275  {
276  obj.template packRequest<prp...>(v);
277  };
278 
279  template<int ... prp> static void packRequest(T & obj, grid_key_dx_iterator_sub<T::dims> & sub, std::vector<size_t> & v)
280  {
281  obj.template packRequest<prp...>(sub, v);
282  };
283 
284  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, T & obj, Pack_stat & sts)
285  {
286  obj.template pack<prp...>(mem, sts);
287  }
288 
289  template<int ... prp> static void pack(ExtPreAlloc<Mem> & mem, T & obj, grid_key_dx_iterator_sub<T::dims> & sub_it, Pack_stat & sts)
290  {
291  obj.template pack<prp...>(mem, sub_it, sts);
292  }
293 
294 };
295 
296 template<typename T, typename Mem>
297 class Packer<T,Mem,PACKER_ENCAP_OBJECTS>
298 {
299 public:
300 
305  void pack(ExtPreAlloc<Mem> & mem, T & eobj)
306  {
307 #ifdef DEBUG
308  if (mem.ref() == 0)
309  std::cerr << "Error : " << __FILE__ << ":" << __LINE__ << " the reference counter of mem should never be zero when packing \n";
310 #endif
311 
312  // Create an object out of the encapsulated object and copy
313  typename T::type obj = eobj;
314 
315  memcpy(mem.getPointer(),&obj,sizeof(T::type));
316  }
317 
322  void packRequest(std::vector<size_t> & v)
323  {
324  v.push_back(sizeof(T::type));
325  }
326 };
327 
328 #endif /* SRC_PACKER_HPP_ */
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts)
It pack any C++ primitives.
Definition: Packer.hpp:86
static void pack(ExtPreAlloc< Mem > &ext, T &obj, Pack_stat &sts)
It pack any object checking that the object does not have pointers inside.
Definition: Packer.hpp:211
static void packRequest(std::vector< size_t > &req)
It add a request to pack a C++ primitive.
Definition: Packer.hpp:100
static size_t packRequest(std::vector< size_t > &req)
Error, no implementation.
Definition: Packer.hpp:61
static void packRequest(std::vector< size_t > &req)
it add a request to pack an object
Definition: Packer.hpp:187
void incReq()
Increment the request pointer.
Definition: Pack_stat.hpp:64
Packing class.
Definition: Packer.hpp:46
static void pack(ExtPreAlloc< Mem > &ext, T &obj, Pack_stat &sts)
It pack an object.
Definition: Packer.hpp:167
static void packRequest(std::vector< size_t > &req)
it add a request to pack an object
Definition: Packer.hpp:232
static void pack(ExtPreAlloc< Mem > &ext, const T &obj, Pack_stat &sts, size_t n)
It packs arrays of C++ primitives.
Definition: Packer.hpp:124
Packing status object.
Definition: Pack_stat.hpp:49
static void packRequestDummy(std::vector< size_t > &req)
It add a request to pack a C++ primitive.
Definition: Packer.hpp:142
static void pack(ExtPreAlloc< Mem >, const T &obj)
Error, no implementation.
Definition: Packer.hpp:53