NeoPZ
run_stats_table.h
Go to the documentation of this file.
1 
5 #ifndef RUN_STATS_TABLE_H
6 
7 #include"stats_recorder.h"
8 #include <fstream>
9 
10 #include"arglib.h"
11 
35 {
36 public:
37 
39  RunStatsTable(const char* arg, const char* desc) : filename(arg,desc,""), stats_recorded(false)
40  {}
41 
44  {
45  if (filename.was_set()) {
46  switch(flush_to_file())
47  {
48  case 1: cerr << "WARNING: number of cells in one or more rows does not "
49  << "match the number of column headers when reading the \""
50  << filename.get_value() << "\" run stats file." << endl; break;
51  case -1: cerr << "WARNING: could not read the table headers when reading the \""
52  << filename.get_value() << "\" run stats file."; break;
53  case -2: cerr << "WARNING: could not open the \"" << filename.get_value()
54  << "\" run stats file for read." << endl; break;
55  case -3: cerr << "WARNING: could not open the \"" << filename.get_value()
56  << "\" run stats file for write." << endl; break;
57  case -4: cerr << "WARNING: error when appending statistics to \"" << filename.get_value()
58  << "\" run stats file for write." << endl; break;
59  default:
60  break;
61  }
62  }
63  }
64 
66  void start() {if (filename.was_set()) { stats_recorded=true; stats.start(); }}
68  void lap() {if (filename.was_set()) { stats_recorded=true; stats.lap(); }}
70  void stop() {if (filename.was_set()) { stats_recorded=true; stats.stop(); }}
71 
85  {
86  CSVStringTable table;
87  int ret;
88 
89  if (!stats_recorded) return 2;
90 
91  /* Read the table. */
92  if ((ret=read_from_file(table, filename.get_value()))) {
93  if (ret != -2) return ret;
94  // Continue if could not open for read (-2).
95  }
96 
97  /* Update the table. */
98  if (stats.append_to(table) < 0) {
99  return -4;
100  }
101 
102  stats.clear();
103  stats_recorded = false;
104 
105  /* Write the table back. */
106  return write_to_file(table, filename.get_value());
107  }
108 
109  bool was_set() const {
110  return filename.was_set();
111  }
112 
113 private:
114 
122  {
123  ifstream ifs(filename.c_str());
124 
125  if (ifs.is_open()) {
126  int ret=table.read(ifs);
127  ifs.close();
128  return ret;
129  }
130  else
131  return -2;
132  }
133 
138  int write_to_file(const CSVStringTable& table, string filename)
139  {
140  ofstream ofs(filename.c_str());
141 
142  if (ofs.is_open()) {
143  table.write(ofs);
144  ofs.close();
145  return 0;
146  }
147  else {
148  return -3;
149  }
150  }
151 
155 };
156 
157 
158 #endif // RUN_STATS_TABLE
159 
clarg::argString filename
int write_to_file(const CSVStringTable &table, string filename)
RunStatsRecorder stats
int read_from_file(CSVStringTable &table, string filename)
void write(ostream &os) const
Definition: csvtable.h:189
int read(istream &is)
Definition: csvtable.h:214
RunStatsTable(const char *arg, const char *desc)
Contains macros and functions to support execution statistics recording.
Definition: stats.py:1
bool was_set() const
Definition: arglib.h:138
const T & get_value() const
Definition: arglib.h:177
bool was_set() const