queso-0.56.1
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-2015 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 {
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 {
52 }
53 
55 {
57  // Do nothing
58 }
59 
60 void
62 {
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(std::string name, T defaultValue,
86  std::string description)
87 {
89  m_optionsDescription->add_options()
90  (name.c_str(),
91  boost::program_options::value<T>()->default_value(defaultValue),
92  description.c_str());
93 }
94 
95 void
96 BoostInputOptionsParser::registerOption(std::string name, std::string description)
97 {
99  m_optionsDescription->add_options()
100  (name.c_str(),
101  description.c_str());
102 }
103 
104 template <typename T>
105 void
106 BoostInputOptionsParser::getOption(std::string & name, T & value)
107 {
109  if (m_scannedInputFile) {
110  value = (*m_optionsMap)[name].as<T>();
111  }
112 }
113 
114 template <>
115 void
116 BoostInputOptionsParser::getOption(std::string & name, std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> > & value)
117 {
119  if (m_scannedInputFile) {
120  // Clear before putting things in it
121  value.clear();
122 
123  // Get the option as a string
124  // DM: Why do it this way? Doesn't boost support vectors as input
125  // options?
126  std::vector<double> tmpVec(0, 0.0);
127  std::string optionValue;
128  this->getOption<std::string>(name, optionValue);
129  MiscReadDoublesFromString(optionValue, tmpVec);
130 
131  for (unsigned int i = 0; i < tmpVec.size(); i++) {
132  value.insert((unsigned int) tmpVec[i]); // Why cast?!
133  }
134  }
135 }
136 
137 template <>
138 void
139 BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(std::string & name, std::vector<double, std::allocator<double> > & value)
140 {
142  if (m_scannedInputFile) {
143  // Need to reset value?
144 
145  // Get the option as a string
146  // DM: Why do it this way? Doesn't boost support vectors as input options?
147  std::string optionValue;
148  this->getOption<std::string>(name, optionValue);
149  MiscReadDoublesFromString(optionValue, value);
150  }
151 }
152 
153 std::ostream &
154 operator<<(std::ostream & os, const BoostInputOptionsParser & parser)
155 {
157  os << *(parser.m_optionsDescription);
158  return os;
159 }
160 
161 template void BoostInputOptionsParser::registerOption<int>(std::string, int, std::string);
162 template void BoostInputOptionsParser::registerOption<unsigned int>(std::string, unsigned int, std::string);
163 template void BoostInputOptionsParser::registerOption<bool>(std::string, bool, std::string);
164 template void BoostInputOptionsParser::registerOption<double>(std::string, double, std::string);
165 template void BoostInputOptionsParser::registerOption<std::string>(std::string, std::string, std::string);
166 
167 template void BoostInputOptionsParser::getOption<int>(std::string&, int&);
168 template void BoostInputOptionsParser::getOption<unsigned int>(std::string&, unsigned int&);
169 template void BoostInputOptionsParser::getOption<double>(std::string&, double&);
170 template void BoostInputOptionsParser::getOption<bool>(std::string&, bool&);
171 template void BoostInputOptionsParser::getOption<std::string>(std::string&, std::string&);
172 template void BoostInputOptionsParser::getOption<std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> > >(std::string&, std::set<unsigned int, std::less<unsigned int>, std::allocator<unsigned int> >&);
173 template void BoostInputOptionsParser::getOption<std::vector<unsigned int, std::allocator<unsigned int> > >(std::string&, std::vector<unsigned int, std::allocator<unsigned int> >&);
174 template void BoostInputOptionsParser::getOption<std::vector<double, std::allocator<double> > >(std::string&, std::vector<double, std::allocator<double> >&);
175 
176 } // End namespace QUESO
177 #endif // DISABLE_BOOST_PROGRAM_OPTIONS
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
virtual ~BoostInputOptionsParser()
Destructor.
void getOption(std::string &name, T &value)
Get option name from the parser and set value to the parsed value.
ScopedPtr< boost::program_options::variables_map >::Type m_optionsMap
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40
BoostInputOptionsParser()
Default constructor that sets m_filename to &quot;&quot;.
#define queso_deprecated()
Definition: Defines.h:134
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
void scanInputFile()
This is the method that parses the input file.
void registerOption(std::string name, T defaultValue, std::string description)
Call this to register an option with the parser.
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69

Generated on Thu Dec 15 2016 13:23:10 for queso-0.56.1 by  doxygen 1.8.5