Reaktoro
A unified framework for modeling chemically reactive systems
Outputter.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright (C) 2014-2018 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <iomanip>
22 #include <iostream>
23 #include <list>
24 #include <sstream>
25 
26 // Reaktoro includes
27 #include <Reaktoro/Math/Matrix.hpp>
28 
29 namespace Reaktoro {
30 
32 {
33  bool active = false;
34 
35  bool fixed = false;
36 
37  bool scientific = false;
38 
39  unsigned precision = 6;
40 
41  unsigned width = 15;
42 
43  std::string separator = "|";
44 
46  auto operator=(bool active) -> OutputterOptions&;
47 };
48 
50 class Outputter
51 {
52 public:
53  struct Options;
54 
55  Outputter();
56 
57  void setOptions(const OutputterOptions& options);
58 
59  void addEntry(const std::string& name);
60 
61  void addEntries(const std::string& prefix, unsigned size);
62 
63  void addEntries(const std::string& prefix, unsigned size, const std::vector<std::string>& names);
64 
65  template<typename Iter>
66  void addEntries(const Iter& begin, const Iter& end);
67 
68  void addEntrySeparator();
69 
70  template<typename T>
71  void addValue(const T& val);
72 
73  template<typename Iter>
74  void addValues(const Iter& begin, const Iter& end);
75 
76  template<typename Vec>
77  void addValues(const Vec& vec);
78 
79  void addValueSeparator();
80 
81  void outputHeader();
82 
83  void outputState();
84 
85  void outputMessage(const std::string& message);
86 
87  template<typename T>
88  void outputMessage(const T& arg)
89  {
90  if(options.active) std::cout << arg;
91  }
92 
93  template<typename T, typename... Args>
94  void outputMessage(const T& arg, const Args&... args)
95  {
96  if(options.active)
97  {
98  std::cout << arg;
99  outputMessage(args...);
100  }
101  }
102 
103 private:
104  std::list<std::string> entries;
105 
106  std::list<std::string> values;
107 
108  OutputterOptions options;
109 };
110 
111 template<typename Iter>
112 void Outputter::addEntries(const Iter& begin, const Iter& end)
113 {
114  entries.insert(entries.end(), begin, end);
115 }
116 
117 template<typename T>
118 void Outputter::addValue(const T& val)
119 {
120  std::stringstream ss;
121  ss << std::setprecision(options.precision);
122  if(options.fixed) ss << std::fixed;
123  if(options.scientific) ss << std::scientific;
124  ss << val;
125  values.push_back(ss.str());
126 }
127 
128 template<typename Iter>
129 void Outputter::addValues(const Iter& begin, const Iter& end)
130 {
131  for(Iter iter = begin; iter != end; ++iter)
132  AddValue(*iter);
133 }
134 
135 template<typename Vec>
136 void Outputter::addValues(const Vec& vec)
137 {
138  for(unsigned i = 0; i < vec.size(); ++i)
139  addValue(vec[i]);
140 }
141 
142 } // namespace
A utility class for printing the progress of iterative calculations.
Definition: Outputter.hpp:51
auto begin(const Reaktoro::ReactionEquation &equation) -> decltype(equation.equation().begin())
Return begin const iterator of a ReactionEquation instance.
Definition: ReactionEquation.hpp:86
auto names(const NamedValues &values) -> std::vector< std::string >
Return the names of the entries in a container.
Definition: Utils.hxx:22
auto end(const Reaktoro::ReactionEquation &equation) -> decltype(equation.equation().end())
Return end const iterator of a ReactionEquation instance.
Definition: ReactionEquation.hpp:98
auto operator=(bool active) -> OutputterOptions &
Assign a boolean value to active member.
Definition: Outputter.cpp:40
The namespace containing all components of the Reaktoro library.
Definition: ChemicalScalar.hpp:24
Definition: Outputter.hpp:32