inputOutput.hh

Go to the documentation of this file.
1 
6 #ifndef inputOutput_hh
7 #define inputOutput_hh
8 
9 #include <iostream>
10 #include <string>
11 #include <cstring>
12 #include <map>
13 #include <complex>
14 #include <limits.h>
15 
16 #include "basics/exceptions.hh"
17 #include "basics/outputOperator.hh"
18 #include "toolbox/multiArray.hh"
19 
20 namespace concepts {
21 
22  // ****************************************************** MissingParameter **
23 
30  public:
34  MissingParameter(const std::string& parameter) throw();
35  protected:
36  virtual std::ostream& info(std::ostream& os) const throw();
37  };
38 
39  // ******************************************************* InOutParameters **
40 
41  struct ltstr {
42  bool operator()(const char* s1, const char* s2) const {
43  return std::strcmp(s1, s2) < 0;
44  }
45  };
46 
47  struct ltidx {
48  bool operator()(const MultiIndex<2>& i1, const MultiIndex<2>& i2) const {
49  return (i1[0] < i2[0]) || (i1[0] == i2[0] && i1[1] < i2[1]);
50  }
51  };
52 
75  class InOutParameters : public virtual OutputOperator {
76  public:
81  virtual ~InOutParameters();
86  void append(const InOutParameters& inout, bool arrayAppend = true);
87 
89  void addDouble(const char* name, const double value);
91  void addComplex(const char* name, const std::complex<double> value);
93  void addString(const char* name, const char* value);
95  void addInt(const char* name, const int value);
97  void addBool(const char* name, const int value);
98 
100  double getDouble(const char* name) const;
105  double getDouble(const char* name, const double value) const;
107  std::complex<double> getComplex(const char* name) const;
112  std::string getString(const char* name, const char* value = 0) const;
117  int getInt(const char* name, const int value = INT_MAX) const;
119  bool getBool(const char* name) const;
124  bool getBool(const char* name, const bool value) const;
125 
130  void addArrayDouble(const char* array, const bool newArray = false);
135  void addArrayComplex(const char* array, const bool newArray = false);
140  void addArrayString(const char* array, const bool newArray = false);
145  void addArrayInt(const char* array, const bool newArray = false);
150  void addArrayBool(const char* array, const bool newArray = false);
151 
160  void addArrayDouble(const char* array, const int number,
161  const double value);
170  void addArrayComplex(const char* array, const int number,
171  const std::complex<double> value);
180  void addArrayString(const char* array, const int number,
181  const char* value);
190  void addArrayInt(const char* array, const int number, const int value);
199  void addArrayBool(const char* array, const int number, const int value);
200 
206  double getArrayDouble(const char* array, const int number) const;
212  std::complex<double> getArrayComplex(const char* array,
213  const int number) const;
219  std::string getArrayString(const char* array, const int number) const;
225  int getArrayInt(const char* array, const int number) const;
231  bool getArrayBool(const char* array, const int number) const;
232 
236  const std::map<int, double>& getMapDouble(const char* array) const;
240  const std::map<int, std::complex<double> >&
241  getMapComplex(const char* array) const;
245  const std::map<int, std::string>& getMapString(const char* array) const;
249  const std::map<int, int>& getMapInt(const char* array) const;
253  const std::map<int, bool>& getMapBool(const char* array) const;
254 
255 
260  void addMatrixDouble(const char* array, const bool newMatrix = false);
265  void addMatrixInt(const char* array, const bool newMatrix = false);
266 
276  void addMatrixDouble(const char* matrix, const int i, const int j,
277  const double value);
287  void addMatrixInt(const char* matrix, const int i, const int j,
288  const int value);
289 
296  double getMatrixDouble(const char* array, const int i, const int j) const;
303  int getMatrixInt(const char* array, const int i, const int j) const;
304 
305 
307  void clear();
312  void storeMatlab(const char* filename,
313  const std::string description = "") const;
319  std::ostream& storeMatlab(std::ostream& os, const char* name,
320  const std::string description = "") const;
321  protected:
322  virtual std::ostream& info(std::ostream& os) const;
323  private:
325  std::map<const char*, double, ltstr> double_;
327  std::map<const char*, std::complex<double>, ltstr> complex_;
329  std::map<const char*, std::string, ltstr> string_;
331  std::map<const char*, int, ltstr> int_;
333  std::map<const char*, bool, ltstr> bool_;
334 
336  std::map<const char*, std::map<int, double>*, ltstr> doubleArrays_;
338  std::map
339  <const char*, std::map<int, std::complex<double> >*, ltstr> complexArrays_;
341  std::map<const char*, std::map<int, std::string>*, ltstr> stringArrays_;
343  std::map<const char*, std::map<int, int>*, ltstr> intArrays_;
345  std::map<const char*, std::map<int, bool>*, ltstr> boolArrays_;
346 
348  std::map<const char*,
349  std::map<MultiIndex<2>, double, ltidx>*, ltstr> doubleMatrices_;
351  std::map<const char*,
352  std::map<MultiIndex<2>, int, ltidx>*, ltstr> intMatrices_;
353 
355  template<class F>
356  void mapOutputMatlab_(std::ostream& os,
357  const std::map<const char*, F, ltstr>& map) const;
359  template<class F>
360  void mapArrayOutputMatlab_(std::ostream& os, const std::map
361  <const char*, std::map<int, F>*, ltstr> & map,
362  const std::string brackets = "[]") const;
364  template<class F>
366  (std::ostream& os, const std::map
367  <const char*, std::map<MultiIndex<2>, F, ltidx>*, ltstr> & map,
368  const std::string brackets = "[]") const;
369 
371  template<class F>
372  void add_(std::map<const char*, F, ltstr>& map, const char* name,
373  const F value);
374 
375  template<class F>
376  F get_(const std::map<const char*, F, ltstr>& map, const char* name) const;
377 
378  template<class F, class G, class H>
379  void addArray_(std::map<const char*, std::map<F, G, H>*, ltstr>& map,
380  const char* array, const bool newArray = false);
381 
382  template<class F, class G, class H>
383  void addArray_(std::map<const char*, std::map<F, G, H>*, ltstr>& map,
384  const char* array, const F number, const G value);
385 
386  template<class F, class G, class H>
387  G getArray_(const std::map<const char*, std::map<F, G, H>*, ltstr>& map,
388  const char* array, const F number) const;
389 
390  template<class F>
391  const std::map<int, F>&
392  getMap_(const std::map<const char*, std::map<int, F>*, ltstr>& map,
393  const char* array) const;
394 
396  template<class F>
397  void clear_(std::map<const char*, F, ltstr>& map);
398 
399  template<class F, class G>
400  void clear_(std::map<const char*, std::map<G, F>*, ltstr>& map);
401  };
402 
403  // *********************************************************** InputParser **
404 
443  class InputParser : public virtual OutputOperator {
444  public:
451  InputParser(bool quiet = false);
452 
453  virtual ~InputParser();
454 
456  void parse();
458  void parse(std::string file);
459 
462 
465 
467  typedef struct yy_buffer_state *YY_BUFFER_STATE;
468  protected:
469  virtual std::ostream& info(std::ostream& os) const;
470  private:
473 
476 
479 
481  bool quiet_;
482  };
483 
484 } // namespace
485 
486 #endif // inputOutput_hh
void addArrayBool(const char *array, const bool newArray=false)
Creates an emtpy array for bools if necessary.
void addMatrixDouble(const char *matrix, const int i, const int j, const double value)
Adds a double to an matrix.
std::map< const char *, bool, ltstr > bool_
Hash of bools.
Definition: inputOutput.hh:333
F get_(const std::map< const char *, F, ltstr > &map, const char *name) const
std::map< const char *, std::map< int, bool > *, ltstr > boolArrays_
Hash of arrays of bools.
Definition: inputOutput.hh:345
void addArrayInt(const char *array, const int number, const int value)
Adds an int to an array.
const std::map< int, std::complex< double > > & getMapComplex(const char *array) const
Returns a reference to the requested map.
void addArray_(std::map< const char *, std::map< F, G, H > *, ltstr > &map, const char *array, const bool newArray=false)
void addMatrixInt(const char *array, const bool newMatrix=false)
Creates an empty matrix for integer if necessary.
double getMatrixDouble(const char *array, const int i, const int j) const
Returns a double from an matrix.
void addComplex(const char *name, const std::complex< double > value)
Adds a complex double to the hash of doubles.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
std::complex< double > getArrayComplex(const char *array, const int number) const
Returns a complex double from an array.
std::map< const char *, std::string, ltstr > string_
Hash of strings.
Definition: inputOutput.hh:329
int getArrayInt(const char *array, const int number) const
Returns a int from an array.
void addArray_(std::map< const char *, std::map< F, G, H > *, ltstr > &map, const char *array, const F number, const G value)
void mapMatrixOutputMatlab_(std::ostream &os, const std::map< const char *, std::map< MultiIndex< 2 >, F, ltidx > *, ltstr > &map, const std::string brackets="[]") const
Matlab output of one mapping of an matrix.
bool operator()(const char *s1, const char *s2) const
Definition: inputOutput.hh:42
Indicates that a requested parameter is not present.
Definition: inputOutput.hh:29
Holds parameters in hashes.
Definition: inputOutput.hh:75
void addArrayComplex(const char *array, const int number, const std::complex< double > value)
Adds a complex double to an array.
std::complex< double > getComplex(const char *name) const
Returns a complex double from the hash of doubles.
std::map< const char *, std::map< MultiIndex< 2 >, int, ltidx > *, ltstr > intMatrices_
Hash of matrices of integer.
Definition: inputOutput.hh:352
std::map< const char *, double, ltstr > double_
Hash of doubles.
Definition: inputOutput.hh:325
std::map< const char *, std::map< int, int > *, ltstr > intArrays_
Hash of arrays of ints.
Definition: inputOutput.hh:343
const std::map< int, bool > & getMapBool(const char *array) const
Returns a reference to the requested map.
void mapOutputMatlab_(std::ostream &os, const std::map< const char *, F, ltstr > &map) const
Matlab output of one mapping.
void addArrayDouble(const char *array, const bool newArray=false)
Creates an empty array for doubles if necessary.
double getDouble(const char *name) const
Returns a double from the hash of doubles.
InOutParameters(const InOutParameters &i)
Copy constructor.
InOutParameters output_
Output data.
Definition: inputOutput.hh:478
MissingParameter(const std::string &parameter)
Constructor.
int getMatrixInt(const char *array, const int i, const int j) const
Returns a integer from an matrix.
std::map< const char *, int, ltstr > int_
Hash of ints.
Definition: inputOutput.hh:331
const std::map< int, F > & getMap_(const std::map< const char *, std::map< int, F > *, ltstr > &map, const char *array) const
void addMatrixDouble(const char *array, const bool newMatrix=false)
Creates an empty matrix for doubles if necessary.
int getInt(const char *name, const int value=INT_MAX) const
Returns an int from the hash of ints.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
void clear()
clears the object
void addArrayInt(const char *array, const bool newArray=false)
Creates an empty array for ints if necessary
void addString(const char *name, const char *value)
Adds a string to the hash of strings.
std::map< const char *, std::map< MultiIndex< 2 >, double, ltidx > *, ltstr > doubleMatrices_
Hash of matrices of doubles.
Definition: inputOutput.hh:349
void addMatrixInt(const char *matrix, const int i, const int j, const int value)
Adds a integer to an matrix.
void clear_(std::map< const char *, F, ltstr > &map)
clears one mapping
struct yy_buffer_state * YY_BUFFER_STATE
The buffer representing the input file (for the parser)
Definition: inputOutput.hh:467
void addBool(const char *name, const int value)
Adds a bool to the hash of bools.
Name traits for number types.
Definition: typedefs.hh:68
const std::map< int, double > & getMapDouble(const char *array) const
Returns a reference to the requested map.
InOutParameters input_
Input data.
Definition: inputOutput.hh:475
std::string getArrayString(const char *array, const int number) const
Returns a string from an array.
void append(const InOutParameters &inout, bool arrayAppend=true)
Appends inout.
Parses an input file and extracts parameters.
Definition: inputOutput.hh:443
double getDouble(const char *name, const double value) const
Returns a double from the hash of doubles.
bool getArrayBool(const char *array, const int number) const
Returns a bool from an array.
InOutParameters & inputParameters()
Returns the input data.
Definition: inputOutput.hh:461
void addInt(const char *name, const int value)
Adds an int to the hash of ints.
void parse(std::string file)
Parses the input file.
void addArrayDouble(const char *array, const int number, const double value)
Adds a double to an array.
InOutParameters & outputParameters()
Returns the output data.
Definition: inputOutput.hh:464
double getArrayDouble(const char *array, const int number) const
Returns a double from an array.
std::map< const char *, std::map< int, double > *, ltstr > doubleArrays_
Hash of arrays of doubles.
Definition: inputOutput.hh:336
void mapArrayOutputMatlab_(std::ostream &os, const std::map< const char *, std::map< int, F > *, ltstr > &map, const std::string brackets="[]") const
Matlab output of one mapping of an array.
bool getBool(const char *name, const bool value) const
Returns a bool from the hash of bools.
void addArrayString(const char *array, const bool newArray=false)
Creates an empty array for strings if necessary.
std::ostream & storeMatlab(std::ostream &os, const char *name, const std::string description="") const
Output as matlab function, which gives a object back.
const std::map< int, std::string > & getMapString(const char *array) const
Returns a reference to the requested map.
InputParser(bool quiet=false)
Default constructor.
void addArrayString(const char *array, const int number, const char *value)
Adds a string to an array.
std::string getString(const char *name, const char *value=0) const
Returns a string from the hash of strings.
void storeMatlab(const char *filename, const std::string description="") const
Output as matlab function, which gives a object back.
void clear_(std::map< const char *, std::map< G, F > *, ltstr > &map)
const std::map< int, int > & getMapInt(const char *array) const
Returns a reference to the requested map.
virtual std::ostream & info(std::ostream &os) const
Returns information in an output stream.
Exception class to express a missing feature.
Definition: exceptions.hh:206
void parse()
Parses the input file.
void add_(std::map< const char *, F, ltstr > &map, const char *name, const F value)
Adds a entrance value of type F to field name.
bool quiet_
Suppress output of current time, machine name etc. in the output.
Definition: inputOutput.hh:481
std::map< const char *, std::map< int, std::complex< double > > *, ltstr > complexArrays_
Hash of arrays of complex.
Definition: inputOutput.hh:339
InOutParameters()
Constructor.
Definition: inputOutput.hh:78
bool operator()(const MultiIndex< 2 > &i1, const MultiIndex< 2 > &i2) const
Definition: inputOutput.hh:48
void addArrayBool(const char *array, const int number, const int value)
Adds a bool to an array.
void addArrayComplex(const char *array, const bool newArray=false)
Creates an empty array for complex doubles if necessary.
YY_BUFFER_STATE current_buffer_
The current input buffer (for the parser)
Definition: inputOutput.hh:472
G getArray_(const std::map< const char *, std::map< F, G, H > *, ltstr > &map, const char *array, const F number) const
bool getBool(const char *name) const
Returns a bool from the hash of bools.
Class providing an output operator.
void addDouble(const char *name, const double value)
Adds a double to the hash of doubles.
std::map< const char *, std::complex< double >, ltstr > complex_
Hash of complex doubles.
Definition: inputOutput.hh:327
std::map< const char *, std::map< int, std::string > *, ltstr > stringArrays_
Hash of arrays of strings.
Definition: inputOutput.hh:341
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich