queso-0.57.0
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 #ifndef DISABLE_BOOST_PROGRAM_OPTIONS
26 #include <boost/program_options.hpp>
27 
28 #include <queso/BoostInputOptionsParser.h>
29 #include <queso/Miscellaneous.h>
30 #include <queso/Defines.h>
31 
32 namespace QUESO {
33 
35  :
36  m_filename(filename),
37  m_optionsDescription(new boost::program_options::options_description("Input options")),
38  m_optionsMap(new boost::program_options::variables_map()),
39  m_scannedInputFile(false)
40 {
41  queso_deprecated();
42 }
43 
45  :
46  m_filename(""),
47  m_optionsDescription(new boost::program_options::options_description("Input options")),
48  m_optionsMap(new boost::program_options::variables_map()),
49  m_scannedInputFile(false)
50 {
51  queso_deprecated();
52 }
53 
55 {
56  queso_deprecated();
57  // Do nothing
58 }
59 
60 void
62 {
63  queso_deprecated();
64  queso_require_msg(m_optionsDescription, "m_optionsDescription variable is NULL");
65 
66  // If it's the empty string then the defaults are used
67  if (m_filename != "") {
68  std::ifstream ifs;
69  ifs.open(m_filename.c_str());
70 
71  queso_require_msg(m_optionsMap, "m_allOptionsMap variable is NULL");
72  boost::program_options::store(
73  boost::program_options::parse_config_file(
74  ifs, *m_optionsDescription, true), *m_optionsMap);
75  boost::program_options::notify(*m_optionsMap);
76 
77  ifs.close();
78 
79  m_scannedInputFile = true;
80  }
81 }
82 
83 template <typename T>
84 void
85 BoostInputOptionsParser::registerOption(const std::string & name,
86  const T & defaultValue,
87  const std::string & description)
88 {
89  queso_deprecated();
90  m_optionsDescription->add_options()
91  (name.c_str(),
92  boost::program_options::value<T>()->default_value(defaultValue),
93  description.c_str());
94 }
95 
96 void
97 BoostInputOptionsParser::registerOption(const std::string & name,
98  const std::string & description)
99 {
100  queso_deprecated();
101  m_optionsDescription->add_options()
102  (name.c_str(),
103  description.c_str());
104 }
105 
106 template <typename T>
107 void
108 BoostInputOptionsParser::getOption(const std::string & name, T & value) const
109 {
110  queso_deprecated();
111  if (m_scannedInputFile) {
112  value = (*m_optionsMap)[name].as<T>();
113  }
114 }
115 
116 template <>
117 void
118 BoostInputOptionsParser::getOption(const std::string & name, std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> > & value) const
119 {
120  queso_deprecated();
121  if (m_scannedInputFile) {
122  // Clear before putting things in it
123  value.clear();
124 
125  // Get the option as a string
126  // DM: Why do it this way? Doesn't boost support vectors as input
127  // options?
128  std::vector<double> tmpVec(0, 0.0);
129  std::string optionValue;
130  this->getOption<std::string>(name, optionValue);
131  MiscReadDoublesFromString(optionValue, tmpVec);
132 
133  for (unsigned int i = 0; i < tmpVec.size(); i++) {
134  value.insert((unsigned int) tmpVec[i]); // Why cast?!
135  }
136  }
137 }
138 
139 template <>
140 void
141 BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(const std::string & name, std::vector<double, std::allocator<double> > & value) const
142 {
143  queso_deprecated();
144  if (m_scannedInputFile) {
145  // Need to reset value?
146 
147  // Get the option as a string
148  // DM: Why do it this way? Doesn't boost support vectors as input options?
149  std::string optionValue;
150  this->getOption<std::string>(name, optionValue);
151  MiscReadDoublesFromString(optionValue, value);
152  }
153 }
154 
155 std::ostream &
156 operator<<(std::ostream & os, const BoostInputOptionsParser & parser)
157 {
158  queso_deprecated();
159  os << *(parser.m_optionsDescription);
160  return os;
161 }
162 
163 template void BoostInputOptionsParser::registerOption<int>(const std::string &, const int &, const std::string &);
164 template void BoostInputOptionsParser::registerOption<unsigned int>(const std::string &, const unsigned int &, const std::string &);
165 template void BoostInputOptionsParser::registerOption<bool>(const std::string &, const bool &, const std::string &);
166 template void BoostInputOptionsParser::registerOption<double>(const std::string &, const double &, const std::string &);
167 template void BoostInputOptionsParser::registerOption<std::string>(const std::string &, const std::string &, const std::string &);
168 
169 template void BoostInputOptionsParser::getOption<int>(const std::string&, int&) const;
170 template void BoostInputOptionsParser::getOption<unsigned int>(const std::string&, unsigned int&) const;
171 template void BoostInputOptionsParser::getOption<double>(const std::string&, double&) const;
172 template void BoostInputOptionsParser::getOption<bool>(const std::string&, bool&) const;
173 template void BoostInputOptionsParser::getOption<std::string>(const std::string&, std::string&) const;
174 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;
175 template void BoostInputOptionsParser::getOption<std::vector<unsigned int, std::allocator<unsigned int> > >(const std::string&, std::vector<unsigned int, std::allocator<unsigned int> >&) const;
176 template void BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(const std::string&, std::vector<double, std::allocator<double> >&) const;
177 
178 } // End namespace QUESO
179 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
std::ostream & operator<<(std::ostream &os, const SequenceStatisticalOptions &obj)
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
void getOption(const std::string &name, T &value) const
Get option name from the parser and set value to the parsed value.
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.
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
m_parser getOption< bool >(m_option_computeSolution, m_computeSolution)
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40
void scanInputFile()
This is the method that parses the input file.
BoostInputOptionsParser()
Default constructor that sets m_filename to &quot;&quot;.

Generated on Sat Apr 22 2017 14:04:35 for queso-0.57.0 by  doxygen 1.8.5