CEBL  2.1
CEBLSerialization.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 
27 #ifndef CEBLSERIALIZATION_H
28 #define CEBLSERIALIZATION_H
29 
30 #include <vector>
31 #include "cppR/cppR.hpp"
32 #include <string>
33 #include <sstream>
34 
35 namespace CEBL
36 {
37 
38  typedef string SerializedObject;
39 
40  using std::string;
41  using std::stringstream;
42 
43  //----------------------------------------------------------------------
44  //PRIMITIVES
45 
46  template<typename T>
47  string serialize(const T& val)
48  {
49  stringstream ss;
50  ss << val;
51  return ss.str();
52  }
53 
54  template<typename T>
55  T& deserialize(stringstream& ss, T& output)
56  {
57  T temp;
58  ss >> temp;
59  output = temp;
60  return output;
61  }
62 
63 
64  //----------------------------------------------------------------------
65  //STL CLASSES
66 
67  template<typename T>
68  string serialize(const std::vector<T>& val)
69  {
70  stringstream ss;
71  ss << val.size() << " ";
72  for(unsigned i=0; i<val.size(); i++)
73  {
74  ss << serialize(val[i]) << " ";
75  }
76  return ss.str();
77  }
78 
79  template<typename T>
80  std::vector<T>& deserialize(stringstream& ss, std::vector<T> & output)
81  {
82  std::vector<T> ret;
83  int size;
84  ss >> size;
85  ret.resize(size);
86 
87  for(unsigned i=0; i<ret.size(); i++)
88  {
89  T temp;
90  deserialize(ss,temp);
91  ret[i] = temp;
92  }
93  output = ret;
94 
95  return output;
96  }
97 
98  //----------------------------------------------------------------------
99 
106  template<typename T>
107  string deserialize(const string& str, T& output)
108  {
109  stringstream ss;
110  ss << str;
111  deserialize(ss,output);
112  return ss.str();
113  }
114 }
115 
116 #endif