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
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 
27 template<typename T, typename Mem, int pack_type=Pack_selector<T>::value >
28 class Unpacker
29 {
30 public:
31 
35  static void unpack(ExtPreAlloc<Mem> , T & obj)
36  {
37  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " packing for the type " << demangle(typeid(T).name()) << " is not implemented\n";
38  }
39 };
40 
47 template<typename T, typename Mem>
48 class Unpacker<T,Mem,PACKER_PRIMITIVE>
49 {
50 public:
51 
52 
59  static void unpack(ExtPreAlloc<Mem> & ext, T & obj,Unpack_stat & ps)
60  {
61  T * ptr = static_cast<T *>(ext.getPointerOffset(ps.getOffset()));
62  obj = *ptr;
63 
64  ps.addOffset(sizeof(T));
65  }
66 };
67 
68 template<typename T, typename Mem>
69 class Unpacker<T,Mem,PACKER_ARRAY_PRIMITIVE>
70 {
71 public:
72 
73 
80  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
81  {
82 
83  //Unpacking a size of a source vector
84  size_t u2 = 0;
86 
87  //Resize a destination vector
88  obj.resize(u2);
89 
90  memcpy(obj.getPointer(),ext.getPointerOffset(ps.getOffset()),sizeof(typename T::value_type)*obj.size());
91 
92  ps.addOffset(sizeof(typename T::value_type)*obj.size());
93  }
94 };
95 
96 
103 template<typename T, typename Mem>
104 class Unpacker<T,Mem,PACKER_OBJECTS_WITH_WARNING_POINTERS>
105 {
106 public:
107 
114  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
115  {
116 #ifdef DEBUG
117  std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " impossible to check the type " << demangle(typeid(T).name()) << " please consider to add a static method \"void noPointers()\" \n" ;
118 #endif
119  memcpy(&obj,(T *)ext.getPointerOffset(ps.getOffset()),sizeof(T));
120 
121  ps.addOffset(sizeof(T));
122  }
123 };
124 
131 template<typename T, typename Mem>
132 class Unpacker<T,Mem,PACKER_OBJECTS_WITH_POINTER_CHECK>
133 {
134 public:
135 
142  static void unpack(ExtPreAlloc<Mem> & ext, T & obj, Unpack_stat & ps)
143  {
144 #ifdef DEBUG
145  if (obj.noPointers() == false)
146  std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " the type " << demangle(typeid(T).name()) << " has pointers inside, sending pointers values has no sense\n";
147 #endif
148  memcpy(&obj,(T *)ext.getPointerOffset(ps.getOffset()),sizeof(T));
149 
150  ps.addOffset(sizeof(T));
151  }
152 };
153 
160 template<typename T, typename Mem>
161 class Unpacker<T,Mem,PACKER_VECTOR>
162 {
163 public:
164 
165  template<unsigned int ... prp> void static unpack(ExtPreAlloc<Mem> & mem, T & obj, Unpack_stat & ps)
166  {
167  obj.template unpack<prp...>(mem, ps);
168  };
169 };
170 
177 template<typename T, typename Mem>
178 class Unpacker<T,Mem,PACKER_GRID>
179 {
180 public:
181 
182  template<unsigned int ... prp> static void unpack(ExtPreAlloc<Mem> & mem, T & obj, Unpack_stat & ps)
183  {
184  obj.template unpack<prp...>(mem, ps);
185  };
186 
187  template<unsigned int ... prp> static void unpack(ExtPreAlloc<Mem> & mem, grid_key_dx_iterator_sub<T::dims> & sub_it, T & obj, Unpack_stat & ps)
188  {
189  obj.template unpack<prp...>(mem, sub_it, ps);
190  };
191 };
192 
199 template<typename T, typename Mem>
200 class Unpacker<T,Mem,PACKER_ENCAP_OBJECTS>
201 {
202 public:
203 
204  // TODO
205 
210 /* void pack(ExtPreAlloc<Mem> & mem, T & eobj)
211  {
212  // Create an object out of the encapsulated object and copy
213  typename T::type obj = eobj;
214 
215  memcpy(mem.getPointer(),&obj,sizeof(T::type));
216  }*/
217 
222 /* void packRequest(std::vector<size_t> & v)
223  {
224  v.push_back(sizeof(T::type));
225  }*/
226 };
227 
228 
229 #endif /* SRC_UNPACKER_HPP_ */
size_t getOffset()
Return the actual counter.
Definition: Pack_stat.hpp:39
Unpacker class.
Definition: Unpacker.hpp:28
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
unpack object
Definition: Unpacker.hpp:114
static void unpack(ExtPreAlloc< Mem > &ext, T &obj, Unpack_stat &ps)
It unpack C++ primitives.
Definition: Unpacker.hpp:59
void addOffset(size_t off)
Increment the request pointer.
Definition: Pack_stat.hpp:30
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:80
static void unpack(ExtPreAlloc< Mem >, T &obj)
Error, no implementation.
Definition: Unpacker.hpp:35
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:142