queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
BoostInputOptionsParser.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2017 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/config_queso.h>
26 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
27 #include <boost/program_options.hpp>
28 
29 #include <queso/BoostInputOptionsParser.h>
30 #include <queso/Miscellaneous.h>
31 #include <queso/Defines.h>
32 
33 namespace QUESO {
34 
36  :
37  m_filename(filename),
38  m_optionsDescription(new boost::program_options::options_description("Input options")),
39  m_optionsMap(new boost::program_options::variables_map()),
40  m_scannedInputFile(false)
41 {
42  queso_deprecated();
43 }
44 
46  :
47  m_filename(""),
48  m_optionsDescription(new boost::program_options::options_description("Input options")),
49  m_optionsMap(new boost::program_options::variables_map()),
50  m_scannedInputFile(false)
51 {
52  queso_deprecated();
53 }
54 
56 {
57  queso_deprecated();
58  // Do nothing
59 }
60 
61 void
63 {
64  queso_deprecated();
65  queso_require_msg(m_optionsDescription, "m_optionsDescription variable is NULL");
66 
67  // If it's the empty string then the defaults are used
68  if (m_filename != "") {
69  std::ifstream ifs;
70  ifs.open(m_filename.c_str());
71 
72  queso_require_msg(m_optionsMap, "m_allOptionsMap variable is NULL");
73  boost::program_options::store(
74  boost::program_options::parse_config_file(
75  ifs, *m_optionsDescription, true), *m_optionsMap);
76  boost::program_options::notify(*m_optionsMap);
77 
78  ifs.close();
79 
80  m_scannedInputFile = true;
81  }
82 }
83 
84 template <typename T>
85 void
86 BoostInputOptionsParser::registerOption(const std::string & name,
87  const T & defaultValue,
88  const std::string & description)
89 {
90  queso_deprecated();
91  m_optionsDescription->add_options()
92  (name.c_str(),
93  boost::program_options::value<T>()->default_value(defaultValue),
94  description.c_str());
95 }
96 
97 void
98 BoostInputOptionsParser::registerOption(const std::string & name,
99  const std::string & description)
100 {
101  queso_deprecated();
102  m_optionsDescription->add_options()
103  (name.c_str(),
104  description.c_str());
105 }
106 
107 template <typename T>
108 void
109 BoostInputOptionsParser::getOption(const std::string & name, T & value) const
110 {
111  queso_deprecated();
112  if (m_scannedInputFile) {
113  value = (*m_optionsMap)[name].as<T>();
114  }
115 }
116 
117 template <>
118 void
119 BoostInputOptionsParser::getOption(const std::string & name, std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> > & value) const
120 {
121  queso_deprecated();
122  if (m_scannedInputFile) {
123  // Clear before putting things in it
124  value.clear();
125 
126  // Get the option as a string
127  // DM: Why do it this way? Doesn't boost support vectors as input
128  // options?
129  std::vector<double> tmpVec(0, 0.0);
130  std::string optionValue;
131  this->getOption<std::string>(name, optionValue);
132  MiscReadDoublesFromString(optionValue, tmpVec);
133 
134  for (unsigned int i = 0; i < tmpVec.size(); i++) {
135  value.insert((unsigned int) tmpVec[i]); // Why cast?!
136  }
137  }
138 }
139 
140 template <>
141 void
142 BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(const std::string & name, std::vector<double, std::allocator<double> > & value) const
143 {
144  queso_deprecated();
145  if (m_scannedInputFile) {
146  // Need to reset value?
147 
148  // Get the option as a string
149  // DM: Why do it this way? Doesn't boost support vectors as input options?
150  std::string optionValue;
151  this->getOption<std::string>(name, optionValue);
152  MiscReadDoublesFromString(optionValue, value);
153  }
154 }
155 
156 std::ostream &
157 operator<<(std::ostream & os, const BoostInputOptionsParser & parser)
158 {
159  queso_deprecated();
160  os << *(parser.m_optionsDescription);
161  return os;
162 }
163 
164 template void BoostInputOptionsParser::registerOption<int>(const std::string &, const int &, const std::string &);
165 template void BoostInputOptionsParser::registerOption<unsigned int>(const std::string &, const unsigned int &, const std::string &);
166 template void BoostInputOptionsParser::registerOption<bool>(const std::string &, const bool &, const std::string &);
167 template void BoostInputOptionsParser::registerOption<double>(const std::string &, const double &, const std::string &);
168 template void BoostInputOptionsParser::registerOption<std::string>(const std::string &, const std::string &, const std::string &);
169 
170 template void BoostInputOptionsParser::getOption<int>(const std::string&, int&) const;
171 template void BoostInputOptionsParser::getOption<unsigned int>(const std::string&, unsigned int&) const;
172 template void BoostInputOptionsParser::getOption<double>(const std::string&, double&) const;
173 template void BoostInputOptionsParser::getOption<bool>(const std::string&, bool&) const;
174 template void BoostInputOptionsParser::getOption<std::string>(const std::string&, std::string&) const;
175 template void BoostInputOptionsParser::getOption<std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> > >(const std::string&, std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> >&) const;
176 template void BoostInputOptionsParser::getOption<std::vector<unsigned int, std::allocator<unsigned int> > >(const std::string&, std::vector<unsigned int, std::allocator<unsigned int> >&) const;
177 template void BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(const std::string&, std::vector<double, std::allocator<double> >&) const;
178 
179 } // End namespace QUESO
180 #endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
BoostInputOptionsParser()
Default constructor that sets m_filename to &quot;&quot;.
void getOption(const std::string &name, T &value) const
Get option name from the parser and set value to the parsed value.
std::ostream & operator<<(std::ostream &os, const SequenceStatisticalOptions &obj)
virtual ~BoostInputOptionsParser()
Destructor.
void registerOption(const std::string &name, const T &defaultValue, const std::string &description)
Call this to register an option with the parser.
m_parser getOption< bool >(m_option_computeSolution, m_computeSolution)
void scanInputFile()
This is the method that parses the input file.
SfpOptionsValues::SfpOptionsValues() m_parser registerOption< bool >(m_option_computeSolution, UQ_SFP_COMPUTE_SOLUTION_ODV,"compute solution process")
ScopedPtr< boost::program_options::variables_map >::Type m_optionsMap
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40

Generated on Tue Jun 5 2018 19:48:54 for queso-0.57.1 by  doxygen 1.8.5