CEBL  2.1
Serialization.hpp
Go to the documentation of this file.
1 /*
2 * CEBL : CSU EEG Brain-Computer Interface Lab
3 *
4 * Author: Jeshua Bratman - jeshuabratman@gmail.com
5 *
6 * This file is part of CEBL.
7 *
8 * CEBL is free software; you can redistribute it and/or modify it.
9 * We only ask that if you use our code that you cite the source in
10 * your project or publication.
11 *
12 * EEG Group (www.cs.colostate.edu/eeg)
13 * Department of Computer Science
14 * Colorado State University
15 *
16 */
17 
18 //Serialization used in CEBL. The plugins need a separate file.
19 #ifndef CEBLBOOSTSERIALIZATION_H
20 #define CEBLBOOSTSERIALIZATION_H
21 
22 //seems to be needed for some reason,
23 //otherwise with get a problem at vector.hpp:126
24 #define BOOST_NO_INTRINSIC_INT64_T
25 
26 #include <boost/archive/text_iarchive.hpp>
27 #include <boost/archive/text_oarchive.hpp>
28 #include <boost/serialization/base_object.hpp>
29 #include <boost/serialization/utility.hpp>
30 
31 #include <boost/serialization/vector.hpp>
32 #include <boost/serialization/map.hpp>
33 #include <boost/serialization/string.hpp>
34 
35 //ublas includes
36 #include <boost/numeric/ublas/vector.hpp>
37 #include <boost/numeric/ublas/matrix.hpp>
38 #include <boost/static_assert.hpp>
39 #include "cppR/cppR.hpp"
40 //----------------------------------------------------------------------
41 
42 namespace ublas = boost::numeric::ublas;
43 
44 namespace boost{
45  template <> struct STATIC_ASSERTION_FAILURE<false> { enum { value = 0 }; };
46 }
47 
48 namespace boost {
49  namespace serialization {
51  template<class Archive, class T>
52  void save(Archive & ar, const ublas::matrix<T> & mat, const unsigned int version)
53  {
54  std::vector<std::vector<T> > newmat;
55  newmat.resize(0);
56  if(mat.size1() > 0)
57  {
58  for(unsigned row=0;row < mat.size1(); row++)
59  {
60  std::vector<T> row_vec;
61  newmat.push_back(row_vec);
62  for(unsigned col=0;col < mat.size2(); col++)
63  {
64  newmat.at(row).push_back(mat(row,col));
65  }
66  }
67  }
68  ar & newmat;
69  }
70 
72  template<class Archive, class T>
73  void load(Archive & ar, ublas::matrix<T> & mat, const unsigned int version)
74  {
75 
76  std::vector<std::vector<T> > loadedmat;
77  ar & loadedmat;
78  if(loadedmat.size() > 0)
79  {
80  int nrows = loadedmat.size();
81  int ncols = loadedmat.at(0).size();
82  mat.resize(nrows,ncols);
83  for(unsigned row=0; row<mat.size1(); row++)
84  {
85  for(unsigned col=0;col < mat.size2(); col++)
86  {
87  mat(row,col) = loadedmat.at(row).at(col);
88  }
89  }
90  }
91  }
92 
93 
95  template<class Archive, class T>
96  void serialize(Archive & ar, ublas::matrix<T> & mat,const unsigned int file_version)
97  {
98  boost::serialization::split_free(ar, mat, file_version);
99  }
100 
102  template<class Archive, class T>
103  void save(Archive & ar, const ublas::vector<T> & vec, const unsigned int version)
104  {
105  std::vector<T> newvec = cppR::asStdVector(vec);
106  ar & newvec;
107  }
108 
110  template<class Archive, class T>
111  void load(Archive & ar, ublas::vector<T> & vec, const unsigned int version)
112  {
113  std::vector<T> newvec;
114  ar & newvec;
115  vec = cppR::asUblasVector(newvec);
116  }
117 
119  template<class Archive, class T>
120  void serialize(Archive & ar, ublas::vector<T> & vec,const unsigned int file_version)
121  {
122  boost::serialization::split_free(ar, vec, file_version);
123  }
124  } // namespace serialization
125 } // namespace boost
126 
127 #endif