Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CSVExporter Class Reference

#include <CSVExporter.h>

Public Member Functions

 CSVExporter (const std::string &output)
 
void addScalarVar (const std::string &name)
 
void addVectorVar (const std::string &name)
 
void setPrecision (int precision)
 
void exportVars (const VarDict &vars)
 

Protected Member Functions

void printHeader ()
 
void init (const VarDict &vars)
 

Protected Attributes

std::ofstream ofile
 
std::vector< std::stringscalVarNames
 
std::vector< std::stringvectVarNames
 
bool initialized
 

Detailed Description

Definition at line 10 of file CSVExporter.h.

Constructor & Destructor Documentation

CSVExporter::CSVExporter ( const std::string output)
explicit

Definition at line 40 of file CSVExporter.cxx.

41  : ofile(output), initialized(false)
42 {
43  if (! ofile) {
44  throw std::runtime_error("Failed to open output file");
45  }
46 
47  ofile << std::setprecision(std::numeric_limits<double>::max_digits10);
48 }
bool initialized
Definition: CSVExporter.h:21
std::ofstream ofile
Definition: CSVExporter.h:13

Member Function Documentation

void CSVExporter::addScalarVar ( const std::string name)

Definition at line 91 of file CSVExporter.cxx.

92 {
93  if (! initialized) {
94  scalVarNames.push_back(name);
95  }
96 }
static QCString name
Definition: declinfo.cpp:673
bool initialized
Definition: CSVExporter.h:21
std::vector< std::string > scalVarNames
Definition: CSVExporter.h:15
void CSVExporter::addVectorVar ( const std::string name)

Definition at line 98 of file CSVExporter.cxx.

99 {
100  if (! initialized) {
101  vectVarNames.push_back(name);
102  }
103 }
static QCString name
Definition: declinfo.cpp:673
bool initialized
Definition: CSVExporter.h:21
std::vector< std::string > vectVarNames
Definition: CSVExporter.h:16
void CSVExporter::exportVars ( const VarDict vars)

Definition at line 105 of file CSVExporter.cxx.

106 {
107  if (! initialized) {
108  init(vars);
109  }
110 
111  bool firstValuePrinted = false;
112 
113  firstValuePrinted = printSeparatedValues<std::string, char>(
114  ofile, scalVarNames, firstValuePrinted, ',',
115  [&vars] (auto &output, const auto &name)
116  {
117  auto it = vars.scalar.find(name);
118  if (it != vars.scalar.end()) {
119  output << it->second;
120  }
121  }
122  );
123 
124  firstValuePrinted = printSeparatedValues<std::string, char>(
125  ofile, vectVarNames, firstValuePrinted, ',',
126  [&vars] (auto &output, const auto &name)
127  {
128  output << "\"";
129  auto it = vars.vector.find(name);
130 
131  if (it != vars.vector.end()) {
132  printSeparatedValues<double, char>(
133  output, it->second, false, ',',
134  [] (auto &output, const auto x) { output << x; }
135  );
136  }
137 
138  output << "\"";
139  }
140  );
141 
142  ofile << std::endl;
143 }
static QCString name
Definition: declinfo.cpp:673
bool initialized
Definition: CSVExporter.h:21
std::ofstream ofile
Definition: CSVExporter.h:13
std::vector< std::string > scalVarNames
Definition: CSVExporter.h:15
void init(const VarDict &vars)
Definition: CSVExporter.cxx:72
std::unordered_map< std::string, std::vector< double > > vector
Definition: VarDict.h:11
std::vector< std::string > vectVarNames
Definition: CSVExporter.h:16
list x
Definition: train.py:276
QTextStream & endl(QTextStream &s)
std::unordered_map< std::string, double > scalar
Definition: VarDict.h:10
void CSVExporter::init ( const VarDict vars)
protected

Definition at line 72 of file CSVExporter.cxx.

73 {
74  if (scalVarNames.empty() && vectVarNames.empty()) {
75  for (const auto &kval : vars.scalar) {
76  scalVarNames.push_back(kval.first);
77  }
78 
79  for (const auto &kval : vars.vector) {
80  vectVarNames.push_back(kval.first);
81  }
82 
83  std::sort(scalVarNames.begin(), scalVarNames.end());
84  std::sort(vectVarNames.begin(), vectVarNames.end());
85  }
86 
87  printHeader();
88  initialized = true;
89 }
void printHeader()
Definition: CSVExporter.cxx:55
bool initialized
Definition: CSVExporter.h:21
std::vector< std::string > scalVarNames
Definition: CSVExporter.h:15
std::unordered_map< std::string, std::vector< double > > vector
Definition: VarDict.h:11
std::vector< std::string > vectVarNames
Definition: CSVExporter.h:16
std::unordered_map< std::string, double > scalar
Definition: VarDict.h:10
void CSVExporter::printHeader ( )
protected

Definition at line 55 of file CSVExporter.cxx.

56 {
57  bool firstValuePrinted = false;
58 
59  firstValuePrinted = printSeparatedValues<std::string, char>(
60  ofile, scalVarNames, firstValuePrinted, ',',
61  [] (auto &output, const auto &name) { output << name; }
62  );
63 
64  printSeparatedValues<std::string, char>(
65  ofile, vectVarNames, firstValuePrinted, ',',
66  [] (auto &output, const auto &name) { output << name; }
67  );
68 
69  ofile << std::endl;
70 }
static QCString name
Definition: declinfo.cpp:673
std::ofstream ofile
Definition: CSVExporter.h:13
std::vector< std::string > scalVarNames
Definition: CSVExporter.h:15
std::vector< std::string > vectVarNames
Definition: CSVExporter.h:16
QTextStream & endl(QTextStream &s)
void CSVExporter::setPrecision ( int  precision)

Definition at line 50 of file CSVExporter.cxx.

51 {
53 }
std::ofstream ofile
Definition: CSVExporter.h:13
float precision
Definition: makePolycone.py:48
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343

Member Data Documentation

bool CSVExporter::initialized
protected

Definition at line 21 of file CSVExporter.h.

std::ofstream CSVExporter::ofile
protected

Definition at line 13 of file CSVExporter.h.

std::vector<std::string> CSVExporter::scalVarNames
protected

Definition at line 15 of file CSVExporter.h.

std::vector<std::string> CSVExporter::vectVarNames
protected

Definition at line 16 of file CSVExporter.h.


The documentation for this class was generated from the following files: