CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
ReadParameters.h
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli and Tommaso Ronconi *
3  * federico.marulli3@unibo.it, tommaso.ronconi@outlook.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
34 #ifndef __READPARA__
35 #define __READPARA__
36 
37 #include "Kernel.h"
38 
39 
40 // ============================================================================
41 
42 
43 namespace cbl {
44 
45  namespace glob {
46 
55  {
56 
57  public:
58 
63 
67  ReadParameters () = default;
68 
75  ReadParameters (const std::string parameter_file);
76 
80  ~ReadParameters () = default;
81 
83 
84 
89 
99  template <class T> T find (const std::string key) const;
100 
111  template <class T> T find (const std::string key, const T default_value) const;
112 
122  template <class T> std::vector< T > find_vector (const std::string key) const;
123 
134  template <class T> std::vector< T > find_vector (const std::string key, const std::vector<T> default_value) const;
135 
137 
138 
139  private:
140 
142  std::unordered_map<std::string, std::string> m_parameters;
143 
145  std::unordered_map<std::string, std::vector<std::string>> m_vectors;
146 
155  std::string m_trim (const std::string inStr);
156 
166  std::vector<std::string> m_trim_vect (const std::string inStr);
167 
168  }; // class ReadParameters
169 
170 
172  template <class T> T ReadParameters::find (const std::string key) const
173  {
174  T value;
175 
176  if (m_parameters.find(key) != m_parameters.end()) {
177  std::stringstream tmpVal(m_parameters.at(key));
178  tmpVal >> std::boolalpha >> value ;
179  }
180  else
181  ErrorCBL("[ReadParameters] Parameter "+key+" not found", "find", "ReadParameters.h");
182 
183  return value;
184  }
185 
187  template <class T> T ReadParameters::find (const std::string key, const T default_value) const
188  {
189  T value;
190 
191  if (m_parameters.find(key) != m_parameters.end()) {
192  std::stringstream tmpVal(m_parameters.at(key));
193  tmpVal >> std::boolalpha >> value;
194  }
195  else {
196  coutCBL <<"Parameter " << key << " not found --> using default: " << default_value << std::endl;
197  value = default_value;
198  }
199 
200  return value;
201  }
202 
203 
205  template <class T> std::vector < T > ReadParameters::find_vector (const std::string key) const
206  {
207  std::vector< T > vect;
208 
209  if (m_vectors.find(key) != m_vectors.end()) {
210  for (size_t ii=0; ii<m_vectors.at(key).size(); ii++) {
211  T value;
212  std::stringstream tmpVal(m_vectors.at(key)[ii]);
213  tmpVal >> std::boolalpha >> value ;
214  vect.push_back(value);
215  }
216  }
217  else
218  ErrorCBL("[ReadParameters] Parameter "+key+" not found", "find_vector", "ReadParameters.h");
219 
220  return vect;
221  }
222 
224  template <class T> std::vector< T > ReadParameters::find_vector (const std::string key, const std::vector<T> default_vect) const
225  {
226  std::vector< T > vect;
227 
228  if (m_vectors.find(key) != m_vectors.end()) {
229  std::vector< T > vect;
230  for (size_t ii=0; ii<m_vectors.at(key).size(); ii++) {
231  T value;
232  std::stringstream tmpVal(m_vectors.at(key)[ii]);
233  tmpVal >> std::boolalpha >> value ;
234  vect.push_back(value);
235  }
236  }
237  else {
238  coutCBL <<"Parameter " << key << " not found --> using default vector." << std::endl;
239  vect = default_vect;
240  }
241 
242  return vect;
243  }
244 
245  } // namespace glob
246 } // namespace cbl
247 
248 
249 #endif //__READPARA__
Useful generic functions.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class ReadParameters.
std::string m_trim(const std::string inStr)
Remove white spaces treading and leading each std::string (private function)
ReadParameters()=default
Default empty constructor.
std::unordered_map< std::string, std::string > m_parameters
map with all the parameter name/value couples
~ReadParameters()=default
Default destructor.
std::unordered_map< std::string, std::vector< std::string > > m_vectors
map with all the vector type parameter name/value couples
T find(const std::string key) const
Template method to get parameter value.
std::vector< std::string > m_trim_vect(const std::string inStr)
Stores values contained in between curly brackets in a vector of std::string (private function)
std::vector< T > find_vector(const std::string key) const
Template method to get a vector parameter.
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780