CEBL  2.1
Session.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 
25 #ifndef SESSION_H
26 #define SESSION_H
27 
28 #include <string>
29 #include <sstream>
30 #include <map>
31 #include <iostream>
32 #include "Serialization.hpp"
33 
34 using namespace std;
35 //forward declarations
36 class TabSet;
37 
38 
39 class Session
40 {
41 private:
42  //internal storage of variables to save and load
43  std::map<std::string, std::map<std::string, std::string> > params;
44  //has this session already been saved
45  bool saved_once;
46  std::string saved_filename;
47  std::string current_section;
48 
49 
50  void printToStream(std::ostream &os);
51  string decodeString(string str);
52  string encodeString(string str);
53 
54 public:
55  Session();
56  ~Session();
57  Session(const Session &);
58 
60  void load(const char *filename);
62  void save(const char *filename);
63 
65  void save();
66 
68  bool shouldSaveAs();
69 
71  template <typename T>
72  void add(std::string param, const T& value, std::string section="")
73  {
74  if(section=="")
75  section = current_section;
76  std::stringstream os;
77  boost::archive::text_oarchive oa(os);
78  oa << value;
79 
80  params[section][param] = os.str();
81  }
83  template <typename T>
84  void add(const char *param, const T& value, std::string section="")
85  {
86  add(string(param),value,section);
87  }
88 
90  template <typename T>
91  Session & operator()(std::string param, const T& value)
92  {
93  this->add(param, value);
94  return *this;
95  }
96 
98  template <typename T>
99  Session & operator()(const char *param, const T& value)
100  {
101  this->add(param, value);
102  return *this;
103  }
104 
106  void clear()
107  {
108  params.clear();
109  }
110 
111  //Accessors
112 
114  void setCurrentSection(std::string sc) { current_section = sc; }
115 
117  bool exists(std::string param, std::string section = "");
118 
120  template <typename T>
121  T get(std::string param, std::string section = "")
122  {
123  if(section=="")
124  section = current_section;
125 
126  std::stringstream is;
127  is << params[section][param];
128  boost::archive::text_iarchive ia(is);
129  T temp;
130  ia >> temp;
131  return temp;
132  }
133 
135  template <typename T>
136  void get(std::string param, T *v, std::string section = "")
137  {
138  if(section=="")
139  section = current_section;
140 
141  std::stringstream is;
142  is << params[section][param];
143  boost::archive::text_iarchive ia(is);
144  ia >> (*v);
145  }
146 
147 
148 
149  friend std::ostream & operator<<(std::ostream &os, Session &s1);
150 };
151 
152 #endif