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

Generated on Thu Jun 11 2015 13:52:31 for queso-0.53.0 by  doxygen 1.8.5