Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
serialization.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*-
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2011-04-26
7 
8  Copyright (C) 2011 Université Joseph Fourier (Grenoble I)
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #ifndef __Serialization_H
30 #define __Serialization_H 1
31 
32 #include <boost/multi_array.hpp>
33 #include <boost/serialization/split_free.hpp>
34 #include <boost/serialization/shared_ptr.hpp>
35 
36 #include <Eigen/Core>
37 
38 namespace boost
39 {
40 namespace serialization
41 {
42 template<class Archive>
43 void load( Archive & ar,
44  boost::multi_array<double,2> & t,
45  const unsigned int file_version )
46 {
47  typedef boost::multi_array<double,2> multi_array_;
48  typedef typename multi_array_::size_type size_;
49  size_ n0;
50  ar >> BOOST_SERIALIZATION_NVP( n0 );
51  size_ n1;
52  ar >> BOOST_SERIALIZATION_NVP( n1 );
53  t.resize( boost::extents[n0][n1] );
54  ar >> make_array( t.data(), t.num_elements() );
55 }
56 template<typename Archive>
57 void save( Archive & ar,
58  const boost::multi_array<double,2> & t,
59  const unsigned int file_version )
60 {
61  typedef boost::multi_array<double,2> multi_array_;
62  typedef typename multi_array_::size_type size_;
63  size_ n0 = ( t.shape()[0] );
64  ar << BOOST_SERIALIZATION_NVP( n0 );
65  size_ n1 = ( t.shape()[1] );
66  ar << BOOST_SERIALIZATION_NVP( n1 );
67  ar << boost::serialization::make_array( t.data(),
68  t.num_elements() );
69 }
70 template<class Archive>
71 void serialize( Archive & ar,
72  boost::multi_array<double,2>& t,
73  const unsigned int file_version )
74 {
75  split_free( ar, t, file_version );
76 }
77 
78 
79 // 3
80 template<class Archive>
81 void load( Archive & ar,
82  boost::multi_array<double,3> & t,
83  const unsigned int file_version )
84 {
85  typedef boost::multi_array<double,3> multi_array_;
86  typedef typename multi_array_::size_type size_;
87  size_ n0;
88  ar >> BOOST_SERIALIZATION_NVP( n0 );
89  size_ n1;
90  ar >> BOOST_SERIALIZATION_NVP( n1 );
91  size_ n2;
92  ar >> BOOST_SERIALIZATION_NVP( n2 );
93  t.resize( boost::extents[n0][n1][n2] );
94  ar >> make_array( t.data(), t.num_elements() );
95 }
96 template<typename Archive>
97 void save( Archive & ar,
98  const boost::multi_array<double,3> & t,
99  const unsigned int file_version )
100 {
101  typedef boost::multi_array<double,3> multi_array_;
102  typedef typename multi_array_::size_type size_;
103  size_ n0 = ( t.shape()[0] );
104  ar << BOOST_SERIALIZATION_NVP( n0 );
105  size_ n1 = ( t.shape()[1] );
106  ar << BOOST_SERIALIZATION_NVP( n1 );
107  size_ n2 = ( t.shape()[2] );
108  ar << BOOST_SERIALIZATION_NVP( n2 );
109  ar << boost::serialization::make_array( t.data(),
110  t.num_elements() );
111 }
112 template<class Archive>
113 void serialize( Archive & ar,
114  boost::multi_array<double,3>& t,
115  const unsigned int file_version )
116 {
117  split_free( ar, t, file_version );
118 }
119 
120 // 4
121 template<class Archive>
122 void load( Archive & ar,
123  boost::multi_array<double,4> & t,
124  const unsigned int file_version )
125 {
126  typedef boost::multi_array<double,4> multi_array_;
127  typedef typename multi_array_::size_type size_;
128  size_ n0;
129  ar >> BOOST_SERIALIZATION_NVP( n0 );
130  size_ n1;
131  ar >> BOOST_SERIALIZATION_NVP( n1 );
132  size_ n2;
133  ar >> BOOST_SERIALIZATION_NVP( n2 );
134  size_ n3;
135  ar >> BOOST_SERIALIZATION_NVP( n3 );
136  t.resize( boost::extents[n0][n1][n2][n3] );
137  ar >> make_array( t.data(), t.num_elements() );
138 }
139 template<typename Archive>
140 void save( Archive & ar,
141  const boost::multi_array<double,4> & t,
142  const unsigned int file_version )
143 {
144  typedef boost::multi_array<double,4> multi_array_;
145  typedef typename multi_array_::size_type size_;
146  size_ n0 = ( t.shape()[0] );
147  ar << BOOST_SERIALIZATION_NVP( n0 );
148  size_ n1 = ( t.shape()[1] );
149  ar << BOOST_SERIALIZATION_NVP( n1 );
150  size_ n2 = ( t.shape()[2] );
151  ar << BOOST_SERIALIZATION_NVP( n2 );
152  size_ n3 = ( t.shape()[3] );
153  ar << BOOST_SERIALIZATION_NVP( n3 );
154  ar << boost::serialization::make_array( t.data(),
155  t.num_elements() );
156 }
157 template<class Archive>
158 void serialize( Archive & ar,
159  boost::multi_array<double,4>& t,
160  const unsigned int file_version )
161 {
162  split_free( ar, t, file_version );
163 }
164 
165 
166 //
167 // MatrixXd
168 //
169 template<class Archive>
170 void load( Archive & ar,
171  Eigen::MatrixXd & t,
172  const unsigned int file_version )
173 {
174  int n0;
175  ar >> BOOST_SERIALIZATION_NVP( n0 );
176  int n1;
177  ar >> BOOST_SERIALIZATION_NVP( n1 );
178  t.resize( n0, n1 );
179  ar >> make_array( t.data(), t.rows()*t.cols() );
180 }
181 template<typename Archive>
182 void save( Archive & ar,
183  const Eigen::MatrixXd & t,
184  const unsigned int file_version )
185 {
186  int n0 = t.rows();
187  ar << BOOST_SERIALIZATION_NVP( n0 );
188  int n1 = t.cols();
189  ar << BOOST_SERIALIZATION_NVP( n1 );
190  ar << boost::serialization::make_array( t.data(),
191  t.rows()*t.cols() );
192 }
193 template<class Archive>
194 void serialize( Archive & ar,
195  Eigen::MatrixXd& t,
196  const unsigned int file_version )
197 {
198  split_free( ar, t, file_version );
199 }
200 
201 //
202 // VectorXd
203 //
204 template<class Archive>
205 void load( Archive & ar,
206  Eigen::VectorXd & t,
207  const unsigned int file_version )
208 {
209  int n0;
210  ar >> BOOST_SERIALIZATION_NVP( n0 );
211  t.resize( n0 );
212  ar >> make_array( t.data(), t.size() );
213 }
214 template<typename Archive>
215 void save( Archive & ar,
216  const Eigen::VectorXd & t,
217  const unsigned int file_version )
218 {
219  int n0 = t.size();
220  ar << BOOST_SERIALIZATION_NVP( n0 );
221  ar << boost::serialization::make_array( t.data(),
222  t.size() );
223 }
224 template<class Archive>
225 void serialize( Archive & ar,
226  Eigen::VectorXd& t,
227  const unsigned int file_version )
228 {
229  split_free( ar, t, file_version );
230 }
231 
232 
233 //
234 // Matrix<N,M>
235 //
236 template<int N, int M, class Archive>
237 void load( Archive & ar,
238  Eigen::Matrix<double,N,M> & t,
239  const unsigned int file_version )
240 {
241  int n0;
242  ar >> BOOST_SERIALIZATION_NVP( n0 );
243  int n1;
244  ar >> BOOST_SERIALIZATION_NVP( n1 );
245  ar >> make_array( t.data(), n0*n1 );
246 }
247 template<int N, int M, typename Archive>
248 void save( Archive & ar,
249  const Eigen::Matrix<double,N,M> & t,
250  const unsigned int file_version )
251 {
252  int n0 = t.rows();
253  int n1 = t.cols();
254  ar << BOOST_SERIALIZATION_NVP( n0 );
255  ar << BOOST_SERIALIZATION_NVP( n1 );
256  ar << boost::serialization::make_array( t.data(), n0*n1 );
257 }
258 template<int N, int M, class Archive>
259 void serialize( Archive & ar,
260  Eigen::Matrix<double,N,M>& t,
261  const unsigned int file_version )
262 {
263  split_free( ar, t, file_version );
264 }
265 
266 
267 template<class Archive>
268 void load( Archive & ar,
269  boost::multi_array<Eigen::MatrixXd,2> & t,
270  const unsigned int file_version )
271 {
272  typedef boost::multi_array<Eigen::MatrixXd,2> multi_array_;
273  typedef typename multi_array_::size_type size_;
274  size_ n0;
275  ar >> BOOST_SERIALIZATION_NVP( n0 );
276  size_ n1;
277  ar >> BOOST_SERIALIZATION_NVP( n1 );
278  t.resize( boost::extents[n0][n1] );
279  ar >> make_array( t.data(), t.num_elements() );
280 }
281 template<typename Archive>
282 void save( Archive & ar,
283  const boost::multi_array<Eigen::MatrixXd,2> & t,
284  const unsigned int file_version )
285 {
286  typedef boost::multi_array<Eigen::MatrixXd,2> multi_array_;
287  typedef typename multi_array_::size_type size_;
288  size_ n0 = ( t.shape()[0] );
289  ar << BOOST_SERIALIZATION_NVP( n0 );
290  size_ n1 = ( t.shape()[1] );
291  ar << BOOST_SERIALIZATION_NVP( n1 );
292  ar << boost::serialization::make_array( t.data(),
293  t.num_elements() );
294 }
295 template<class Archive>
296 void serialize( Archive & ar,
297  boost::multi_array<Eigen::MatrixXd,2>& t,
298  const unsigned int file_version )
299 {
300  split_free( ar, t, file_version );
301 }
302 
303 template<class Archive>
304 void load( Archive & ar,
305  boost::multi_array<Eigen::VectorXd,2> & t,
306  const unsigned int file_version )
307 {
308  typedef boost::multi_array<Eigen::VectorXd,2> multi_array_;
309  typedef typename multi_array_::size_type size_;
310  size_ n0;
311  ar >> BOOST_SERIALIZATION_NVP( n0 );
312  size_ n1;
313  ar >> BOOST_SERIALIZATION_NVP( n1 );
314  t.resize( boost::extents[n0][n1] );
315  ar >> make_array( t.data(), t.num_elements() );
316 }
317 template<typename Archive>
318 void save( Archive & ar,
319  const boost::multi_array<Eigen::VectorXd,2> & t,
320  const unsigned int file_version )
321 {
322  typedef boost::multi_array<Eigen::VectorXd,2> multi_array_;
323  typedef typename multi_array_::size_type size_;
324  size_ n0 = ( t.shape()[0] );
325  ar << BOOST_SERIALIZATION_NVP( n0 );
326  size_ n1 = ( t.shape()[1] );
327  ar << BOOST_SERIALIZATION_NVP( n1 );
328  ar << boost::serialization::make_array( t.data(),
329  t.num_elements() );
330 }
331 template<class Archive>
332 void serialize( Archive & ar,
333  boost::multi_array<Eigen::VectorXd,2>& t,
334  const unsigned int file_version )
335 {
336  split_free( ar, t, file_version );
337 }
338 
339 
340 //
341 // boost::tuple<T1,T2>
342 //
343 
344 template<typename T1, typename T2, class Archive>
345 void load( Archive & ar,
346  boost::tuple<T1,T2> & t,
347  const unsigned int file_version )
348 {
349  T1 x;
350  ar >> BOOST_SERIALIZATION_NVP( x );
351  T2 y;
352  ar >> BOOST_SERIALIZATION_NVP( y );
353  t = boost::make_tuple( x,y );
354 }
355 template<typename T1, typename T2, typename Archive>
356 void save( Archive & ar,
357  boost::tuple<T1,T2> const& t,
358  const unsigned int file_version )
359 {
360  T1 x;
361  T2 y;
362  boost::tie( x,y ) = t;
363  ar << BOOST_SERIALIZATION_NVP( x );
364  ar << BOOST_SERIALIZATION_NVP( y );
365 }
366 template<typename T1, typename T2, class Archive>
367 void serialize( Archive & ar,
368  boost::tuple<T1,T2> & t,
369  const unsigned int file_version )
370 {
371  split_free( ar, t, file_version );
372 }
373 
374 
375 
376 //
377 // boost::tuple<T1,T2,T3>
378 //
379 
380 template<typename T1, typename T2, typename T3, class Archive>
381 void load( Archive & ar,
382  boost::tuple<T1,T2,T3> & t,
383  const unsigned int file_version )
384 {
385  T1 x;
386  ar >> BOOST_SERIALIZATION_NVP( x );
387  T2 y;
388  ar >> BOOST_SERIALIZATION_NVP( y );
389  T3 z;
390  ar >> BOOST_SERIALIZATION_NVP( z );
391  t = boost::make_tuple( x,y,z );
392 }
393 template<typename T1, typename T2, typename T3, typename Archive>
394 void save( Archive & ar,
395  boost::tuple<T1,T2,T3> const& t,
396  const unsigned int file_version )
397 {
398  T1 x;
399  T2 y;
400  T3 z;
401  boost::tie( x,y,z ) = t;
402  ar << BOOST_SERIALIZATION_NVP( x );
403  ar << BOOST_SERIALIZATION_NVP( y );
404  ar << BOOST_SERIALIZATION_NVP( z );
405 
406 }
407 template<typename T1, typename T2, typename T3, class Archive>
408 void serialize( Archive & ar,
409  boost::tuple<T1,T2,T3> & t,
410  const unsigned int file_version )
411 {
412  split_free( ar, t, file_version );
413 }
414 
415 //
416 // boost::tuple<T1,T2,T3,T4>
417 //
418 
419 template<typename T1, typename T2, typename T3, typename T4, class Archive>
420 void load( Archive & ar,
421  boost::tuple<T1,T2,T3,T4> & t,
422  const unsigned int file_version )
423 {
424  T1 x1;
425  ar >> BOOST_SERIALIZATION_NVP( x1 );
426  T2 x2;
427  ar >> BOOST_SERIALIZATION_NVP( x2 );
428  T3 x3;
429  ar >> BOOST_SERIALIZATION_NVP( x3 );
430  T4 x4;
431  ar >> BOOST_SERIALIZATION_NVP( x4 );
432  t = boost::make_tuple( x1,x2,x3,x4 );
433 }
434 template<typename T1, typename T2, typename T3, typename T4, typename Archive>
435 void save( Archive & ar,
436  boost::tuple<T1,T2,T3,T4> const& t,
437  const unsigned int file_version )
438 {
439  T1 x1;
440  T2 x2;
441  T3 x3;
442  T4 x4;
443  boost::tie( x1,x2,x3,x4 ) = t;
444  ar << BOOST_SERIALIZATION_NVP( x1 );
445  ar << BOOST_SERIALIZATION_NVP( x2 );
446  ar << BOOST_SERIALIZATION_NVP( x3 );
447  ar << BOOST_SERIALIZATION_NVP( x4 );
448 
449 }
450 template<typename T1, typename T2, typename T3, typename T4, class Archive>
451 void serialize( Archive & ar,
452  boost::tuple<T1,T2,T3,T4> & t,
453  const unsigned int file_version )
454 {
455  split_free( ar, t, file_version );
456 }
457 
458 } // serialization
459 } //boost
460 
461 #endif /* __Serialization_H */

Generated on Sun Oct 20 2013 08:25:04 for Feel++ by doxygen 1.8.4