CFEL - ASG Software Suite  2.5.0
CASS
cass.h
Go to the documentation of this file.
1 // Copyright (C) 2009, 2010 Lutz Foucar
2 // Copyright (C) 2010 Jochen Küpper
3 
4 /**
5  * @file cass.h file contains global definitions for project cass
6  *
7  * @author Lutz Foucar
8  */
9 
10 #ifndef CASS_GLOBAL_H
11 #define CASS_GLOBAL_H
12 
13 #include <cassert>
14 #include <iterator>
15 #include <list>
16 #include <vector>
17 #include <map>
18 #include <sstream>
19 #include <fstream>
20 #include <stdexcept>
21 #include <tr1/memory>
22 #include <QtCore/qglobal.h>
23 #include <QtCore/QDataStream>
24 #include <stdint.h>
25 
26 
27 #if defined(CASS_LIBRARY)
28 # define CASSSHARED_EXPORT Q_DECL_EXPORT
29 #else
30 # define CASSSHARED_EXPORT Q_DECL_IMPORT
31 #endif
32 
33 // OUT macro definitions
34 #ifdef VERBOSE
35 #include <iostream>
36 #define VERBOSEOUT(a) (a)
37 #else
38 #define VERBOSEOUT(a) {}
39 #endif
40 #ifdef DEBUG
41 #include <iostream>
42 #define DEBUGOUT(a) (a)
43 #else
44 #define DEBUGOUT(a) {}
45 #endif
46 
47 
48 
49 namespace cass
50 {
51 /** convert any type to a string
52  *
53  * should be used for converting numbers to strings. This function was inspired
54  * by a function found at Sep, 24th 2011 here:
55  * http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
56  *
57  * @tparam Type the type of the number
58  * @param t the number to convert to string
59  *
60  * @author Lutz Foucar
61  */
62 template <typename Type>
63 std::string toString (const Type& t)
64 {
65  std::stringstream ss;
66  ss << t;
67  return ss.str();
68 }
69 
70 /** multiply number by itself
71  *
72  * @tparam T type of value to be squared
73  * @param val value to be squared
74  *
75  * @author Jochen Kuepper
76  */
77 template<typename T>
78 inline T square(const T& val) { return val * val; }
79 
80 /** A resource that will point at a specific location within a file
81  *
82  * @author Lutz Foucar
83  */
85 {
86  /** defining a shared pointer to the stream */
87  typedef std::tr1::shared_ptr<std::ifstream> filestream_t;
88 
89  /** the position with the file */
90  std::streampos _pos;
91 
92  /** the stream to the file */
93  filestream_t _filestream;
94 
95  /** @return a stream to the right position within the file */
96  std::ifstream& getStream()
97  {
98  _filestream->seekg(_pos);
99  return *_filestream.get();
100  }
101 };
102 
103 /** tokenize to return all lines of an ascii file in a vector
104  *
105  * will return a list containing all non empty lines of the file. Before
106  * returning the list strip the 'new line' and 'line feed' from the line.
107  * Also skip all lines that contain either a '#' or a ';'.
108  *
109  * @author Lutz Foucar
110  */
111 struct Tokenizer
112 {
113  /** the operator
114  *
115  * @return vector of string containing all non empty lines of the file
116  * @param file the filestream to tokenize
117  */
118  std::vector<std::string> operator()(std::ifstream &file)
119  {
120  using namespace std;
121  vector<string> lines;
122  while (!file.eof())
123  {
124  string line;
125  getline(file,line);
126  /* remove newline */
127  if(line[line.length()-1] == '\n')
128  {
129  line.resize(line.length()-1);
130  }
131  /* remove line feed */
132  if(line[line.length()-1] == '\r')
133  {
134  line.resize(line.length()-1);
135  }
136  /* dont read newlines */
137  if(line.empty() || line[0] == '\n')
138  {
139  continue;
140  }
141  /* don't read lines containing ';' or '#' */
142  if(line.find(';') != string::npos || line.find('#') != string::npos)
143  {
144  continue;
145  }
146  lines.push_back(line);
147  }
148  return lines;
149  }
150 };
151 
152 /** split the line into the values in that line
153  *
154  * @author Lutz Foucar
155  */
156 struct Splitter
157 {
158  /** the operator for splitting a line of values
159  *
160  * @param line string containing the line that should be split
161  * @param elems vector containing the elements of the line
162  * @param delim the delimiter that the line should be splitted by.
163  */
164  void operator()(const std::string &line, std::vector<double> &elems, char delim)
165  {
166  using namespace std;
167  stringstream ss(line);
168  string str;
169  while(getline(ss, str, delim))
170  {
171  if ((str.size() == 1 && !(isalpha(str[0]))) || str.empty())
172  continue;
173  stringstream ssvalue(str);
174  double value;
175  ssvalue >> value;
176  elems.push_back(value);
177  }
178  }
179 
180  /** the operator for splitting into substrings
181  *
182  * @param line string containing the line that should be split
183  * @param elems vector containing the elements of the line
184  * @param delim the delimiter that the line should be splitted by.
185  */
186  void operator()(const std::string &line, std::vector<std::string> &elems, char delim)
187  {
188  using namespace std;
189  stringstream ss(line);
190  string str;
191  while(getline(ss, str, delim))
192  {
193  if ((str.size() == 1 && !(isalpha(str[0]))) || str.empty())
194  continue;
195  elems.push_back(str);
196  }
197  }
198 };
199 
200 namespace Streaming
201 {
202 /** retrieve a variable from a file stream
203  *
204  * @return the variable
205  * @tparam the type of the variable to retrieve
206  * @param file The file stream to retrieve the vairable from
207  */
208 template <typename T>
209 T retrieve(std::ifstream &file)
210 {
211  T var;
212  file.read(reinterpret_cast<char*>(&var),sizeof(T));
213  return var;
214 }
215 
216 /** retrieve a variable from a file stream without extracting it
217  *
218  * leaves the file stream at the same position it was before
219  *
220  * @return the variable
221  * @tparam the type of the variable to retrieve
222  * @param file The file stream to retrieve the vairable from
223  */
224 template <typename T>
225 T peek(std::ifstream &file)
226 {
227  T var;
228  std::streampos currentpos(file.tellg());
229  file.read(reinterpret_cast<char*>(&var),sizeof(T));
230  file.seekg(currentpos);
231  return var;
232 }
233 
234 /** reading a type from the QDataStream
235  *
236  * @tparam T the type that should be read from the stream
237  * @return reference to the stream
238  * @param stream the stream to read from
239  * @param evt the header to read to
240  *
241  * @author Lutz Foucar
242  */
243 template<typename T>
245 {
246  if(stream.readRawData(reinterpret_cast<char*>(&evt),sizeof(T)) != sizeof(T))
247  throw std::runtime_error("operator>>(QDdataStream&,T&): could not retrieve all requested bytes");
248  return stream;
249 }
250 
251 /** reading a type from the filestream
252  *
253  * @tparam T the type that should be read from the stream
254  * @return reference to the stream
255  * @param stream the stream to read from
256  * @param evt the header to read to
257  *
258  * @author Lutz Foucar
259  */
260 template<typename T>
261 std::ifstream &operator>>(std::ifstream& stream, T& evt)
262 {
263  stream.read(reinterpret_cast<char*>(&evt),sizeof(T));
264  if(stream.rdstate() != std::ios_base::goodbit)
265  throw std::runtime_error("operator>>(ifstream&,T&): could not retrieve all requested bytes");
266  return stream;
267 }
268 
269 }//end namespace FileStreaming
270 
271 /** global variable to set the ring buffer size */
272 const size_t RingBufferSize=32;
273 /** global variable to set the number of worker threads */
274 const size_t NbrOfWorkers=16;
275 //forward decalration//
277 /** pair of a file pointer with the associated file reader */
278 typedef std::pair<std::tr1::shared_ptr<FileReader>, FilePointer> filereaderpointerpair_t;
279 /** map file name to the filepointer */
280 typedef std::vector<filereaderpointerpair_t> positionreaders_t;
281 /** the list of events contained in a file with the associated position and reader*/
282 typedef std::map<uint64_t, positionreaders_t> event2positionreaders_t;
283 }
284 
285 #endif
std::vector< std::string > operator()(std::ifstream &file)
the operator
Definition: cass.h:118
filestream_t _filestream
the stream to the file
Definition: cass.h:93
T peek(std::ifstream &file)
retrieve a variable from a file stream without extracting it
Definition: cass.h:225
A resource that will point at a specific location within a file.
Definition: cass.h:84
STL namespace.
base class for all file readers
Definition: file_reader.h:24
readRawData(char *s, int len)
std::ifstream & getStream()
Definition: cass.h:96
T square(const T &val)
multiply number by itself
Definition: cass.h:78
std::vector< filereaderpointerpair_t > positionreaders_t
map file name to the filepointer
Definition: cass.h:280
std::streampos _pos
the position with the file
Definition: cass.h:90
tokenize to return all lines of an ascii file in a vector
Definition: cass.h:111
split the line into the values in that line
Definition: cass.h:156
std::string toString(const Type &t)
convert any type to a string
Definition: cass.h:63
std::pair< std::tr1::shared_ptr< FileReader >, FilePointer > filereaderpointerpair_t
pair of a file pointer with the associated file reader
Definition: cass.h:276
std::tr1::shared_ptr< std::ifstream > filestream_t
defining a shared pointer to the stream
Definition: cass.h:87
SerializerBackend & operator>>(SerializerBackend &serializer, Axis< T > &axis)
read an Axis from a stream
Definition: result.hpp:61
std::map< uint64_t, positionreaders_t > event2positionreaders_t
the list of events contained in a file with the associated position and reader
Definition: cass.h:282
const size_t NbrOfWorkers
global variable to set the number of worker threads
Definition: cass.h:274
void operator()(const std::string &line, std::vector< double > &elems, char delim)
the operator for splitting a line of values
Definition: cass.h:164
check if there is some light in the chamber based upon the GMD value
T retrieve(std::ifstream &file)
retrieve a variable from a file stream
Definition: cass.h:209
void operator()(const std::string &line, std::vector< std::string > &elems, char delim)
the operator for splitting into substrings
Definition: cass.h:186
const size_t RingBufferSize
global variable to set the ring buffer size
Definition: cass.h:272