HIPO  4.3.0
High Performance Output data format for experimental physics
utils.h
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 
7 /*
8  * File: utils.h
9  * Author: gavalian
10  *
11  * Created on April 27, 2017, 10:01 AM
12  */
13 
19 
20 #ifndef UTILS_H
21 #define UTILS_H
22 
23 #include <iostream>
24 #include <cstdlib>
25 #include <cstdio>
26 #include <string>
27 #include <vector>
28 #include<chrono>
29 
30 namespace hipo {
31 
36  class utils {
37  private:
38 
39  public:
41  utils();
43  ~utils();
44 
51  static void tokenize(const std::string& str,
52  std::vector<std::string>& tokens,
53  const std::string& delimiters = " ");
54 
63  static std::string substring(const std::string &str,
64  const char *start_delim,
65  const char *end_delim, int order);
66 
74  static int findposition(const std::string &str,
75  const char *delim, int order);
76 
83  static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r "){
84  str.erase(0, str.find_first_not_of(chars));return str;
85  }
86 
93  static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r "){
94  str.erase(str.find_last_not_of(chars) + 1);return str;
95  }
96 
103  static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r "){
104  return ltrim(rtrim(str, chars), chars);
105  }
106 
108  static void printLogo();
109 
111  static std::string getHeader();
113  static std::string getFileHeader();
114 
120  static std::string getFileTrailer(const char *code);
121 
123  static std::string getSConstruct();
124 
131  static void writeInt( char *buffer, int position, int value);
132 
139  static void writeLong( char *buffer, int position, long value);
140 
147  static void writeByte( char *buffer, int position, uint8_t value );
148  };
149 
157  class benchmark {
158  private:
159 
160  std::chrono::high_resolution_clock clock;
161  std::chrono::time_point<std::chrono::high_resolution_clock> first, second;
162  std::string benchmarkName;
163 
164  long running_time;
165  int counter;
166  int printoutFrequency;
167 
168  public:
169 
174  benchmark(const char *name){
175  benchmarkName = name;
176  running_time = 0;counter = 0; printoutFrequency = -1;
177  }
178 
180  benchmark(){ running_time = 0;counter = 0; printoutFrequency = -1;}
181 
186  benchmark(int freq){ running_time = 0;counter = 0; printoutFrequency = freq;}
187 
190 
192  void reset(){ running_time = 0;counter = 0; printoutFrequency = -1;}
193 
198  void setName(const char *name){ benchmarkName = name;}
199 
201  void resume();
203  void pause();
205  long getTime();
207  double getTimeSec();
209  int getCounter();
211  void show();
212  };
213 }
215 
216 #endif /* UTILS_H */
Simple timer for measuring code performance.
Definition: utils.h:157
benchmark(const char *name)
Constructs a benchmark with the given name.
Definition: utils.h:174
int getCounter()
Returns the number of resume/pause cycles recorded.
Definition: utils.cpp:223
void resume()
Starts or resumes the timer.
Definition: utils.cpp:196
void setName(const char *name)
Sets the display name of this benchmark.
Definition: utils.h:198
void pause()
Pauses the timer and accumulates elapsed time.
Definition: utils.cpp:206
benchmark(int freq)
Constructs a benchmark with automatic printout frequency.
Definition: utils.h:186
double getTimeSec()
Returns the accumulated time in seconds.
Definition: utils.cpp:219
void show()
Prints the benchmark name, accumulated time, and counter.
Definition: utils.cpp:202
benchmark()
Default constructor.
Definition: utils.h:180
~benchmark()
Destructor.
Definition: utils.h:189
void reset()
Resets accumulated time and counter to zero.
Definition: utils.h:192
long getTime()
Returns the accumulated time in microseconds.
Definition: utils.cpp:215
Utility functions for string manipulation, serialization, and HIPO file generation.
Definition: utils.h:36
static std::string getFileHeader()
Returns the file header template string.
Definition: utils.cpp:114
static void writeByte(char *buffer, int position, uint8_t value)
Writes a single byte into a byte buffer at the given position.
Definition: utils.cpp:80
static std::string substring(const std::string &str, const char *start_delim, const char *end_delim, int order)
Extracts a substring between two delimiters.
Definition: utils.cpp:60
static std::string getSConstruct()
Returns the SCons build configuration string.
Definition: utils.cpp:148
static void writeLong(char *buffer, int position, long value)
Writes a 64-bit long into a byte buffer at the given position.
Definition: utils.cpp:75
static std::string & ltrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trims leading whitespace characters from a string.
Definition: utils.h:83
static std::string & rtrim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trims trailing whitespace characters from a string.
Definition: utils.h:93
utils()
Default constructor.
Definition: utils.cpp:16
static std::string getFileTrailer(const char *code)
Returns the file trailer template string.
Definition: utils.cpp:130
static std::string getHeader()
Returns the HIPO header string for code generation.
Definition: utils.cpp:95
static void tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
Splits a string into tokens using the given delimiters.
Definition: utils.cpp:19
static void printLogo()
Prints the HIPO library logo to standard output.
Definition: utils.cpp:85
static int findposition(const std::string &str, const char *delim, int order)
Finds the position of the nth occurrence of a delimiter.
Definition: utils.cpp:41
static void writeInt(char *buffer, int position, int value)
Writes a 32-bit integer into a byte buffer at the given position.
Definition: utils.cpp:70
static std::string & trim(std::string &str, const std::string &chars="\t\n\v\f\r ")
Trims both leading and trailing whitespace from a string.
Definition: utils.h:103
~utils()
Destructor.
Definition: utils.cpp:17
Definition: bank.cpp:47