OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
Unpacker.hpp
1 /*
2  * Unpacker.hpp
3  *
4  * Created on: Jul 17, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef SRC_UNPACKER_HPP_
9 #define SRC_UNPACKER_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 #include "Pack_selector.hpp"
17 #include "util/Pack_stat.hpp"
18 #include "memory/PtrMemory.hpp"
19 #include "Packer_util.hpp"
20 
28 template<typename T, typename Mem, int pack_type >
29 class Unpacker
30 {
31 public:
32 
36  static void unpack(ExtPreAlloc<Mem> , T & obj)
37  {
38  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
39  }
40 };
41 
48 template<typename T, typename Mem>
49 class Unpacker<T,Mem,PACKER_PRIMITIVE>
50 {
51 public:
52 
53 
60  static void unpack(ExtPreAlloc<Mem> & ext, T & obj,Unpack_stat & ps)
61  {
62  T * ptr = static_cast<T *>(ext.getPointerOffset(ps.getOffset()));
63  obj = *ptr;
64 
65  ps.addOffset(sizeof(T));
66  }
67 };
68 
69 template<typename T, typename Mem>
70 class Unpacker<T,Mem,PACKER_ARRAY_PRIMITIVE>
71 {
72 public:
73 
74 
81  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
82  {
83 
84  //Unpacking a size of a source vector
85  size_t u2 = 0;
87 
88  //Resize a destination vector
89  obj.resize(u2);
90 
91  memcpy(obj.getPointer(),ext.getPointerOffset(ps.getOffset()),sizeof(typename T::value_type)*obj.size());
92 
93  ps.addOffset(sizeof(typename T::value_type)*obj.size());
94  }
95 };
96 
97 
104 template<typename T, typename Mem>
105 class Unpacker<T,Mem,PACKER_OBJECTS_WITH_WARNING_POINTERS>
106 {
107 public:
108 
115  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
116  {
117  memcpy(&obj,(T *)ext.getPointerOffset(ps.getOffset()),sizeof(T));
118 
119  ps.addOffset(sizeof(T));
120  }
121 };
122 
129 template<typename T, typename Mem>
130 class Unpacker<T,Mem,PACKER_OBJECTS_WITH_POINTER_CHECK>
131 {
132 public:
133 
140  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
141  {
142  memcpy(&obj,(T *)ext.getPointerOffset(ps.getOffset()),sizeof(T));
143 
144  ps.addOffset(sizeof(T));
145  }
146 };
147 
154 template<typename T, typename Mem>
155 class Unpacker<T,Mem,PACKER_GENERAL>
156 {
157 public:
158 
159  template<unsigned int ... prp> void static unpack(ExtPreAlloc<Mem> & mem, T & obj, Unpack_stat & ps)
160  {
161  obj.template unpack<prp...>(mem, ps);
162  };
163 
164  template<unsigned int ... prp> void static unpack(ExtPreAlloc<Mem> & mem, T & obj, Unpack_stat & ps, size_t n)
165  {
166  if (mem.size() == 0)
167  return;
168 
169  obj.template unpack<prp...>(mem, ps);
170  };
171 };
172 
179 template<typename T, typename Mem>
180 class Unpacker<T,Mem,PACKER_GRID>
181 {
182 public:
183 
184  template<unsigned int ... prp> static void unpack(ExtPreAlloc<Mem> & mem, T & obj, Unpack_stat & ps)
185  {
186  obj.template unpack<prp...>(mem, ps);
187  };
188 
189  template<unsigned int ... prp> static void unpack(ExtPreAlloc<Mem> & mem, grid_key_dx_iterator_sub<T::dims> & sub_it, T & obj, Unpack_stat & ps)
190  {
191  obj.template unpack<prp...>(mem, sub_it, ps);
192  };
193 };
194 
201 template<typename T, typename Mem>
202 class Unpacker<T,Mem,PACKER_ENCAP_OBJECTS>
203 {
204 public:
205 
206  // TODO
207 
213 /* void pack(ExtPreAlloc<Mem> & mem, T & eobj)
214  {
215  // Create an object out of the encapsulated object and copy
216  typename T::type obj = eobj;
217 
218  memcpy(mem.getPointer(),&obj,sizeof(T::type));
219  }*/
220 
225 /* void packRequest(std::vector<size_t> & v)
226  {
227  v.push_back(sizeof(T::type));
228  }*/
229 };
230 
231 
232 #endif /* SRC_UNPACKER_HPP_ */
size_t getOffset()
Return the actual counter.
Definition: Pack_stat.hpp:41
Unpacker class.
Definition: Packer_util.hpp:20
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
unpack object
Definition: Unpacker.hpp:115
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
It unpack C++ primitives.
Definition: Unpacker.hpp:60
void * getPointerOffset(size_t offset)
Get the base memory pointer increased with an offset.
void addOffset(size_t off)
Increment the offset pointer by off.
Definition: Pack_stat.hpp:31
Unpacking status object.
Definition: Pack_stat.hpp:15
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
It unpack C++ primitives.
Definition: Unpacker.hpp:81
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:36
Declaration grid_key_dx_iterator_sub.
Definition: grid_sm.hpp:77
virtual size_t size() const
Get the size of the LAST allocated memory.
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
It unpack any object checking that the object does not have pointers inside.
Definition: Unpacker.hpp:140