queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::BoostInputOptionsParser Class Reference

#include <BoostInputOptionsParser.h>

Inheritance diagram for QUESO::BoostInputOptionsParser:
QUESO::BaseInputOptionsParser

Public Member Functions

 BoostInputOptionsParser (const std::string &filename)
 Constructor that parses file filename. More...
 
 BoostInputOptionsParser ()
 Default constructor that sets m_filename to "". More...
 
virtual ~BoostInputOptionsParser ()
 Destructor. More...
 
void scanInputFile ()
 This is the method that parses the input file. More...
 
template<class T >
void registerOption (const std::string &name, const T &defaultValue, const std::string &description)
 Call this to register an option with the parser. More...
 
void registerOption (const std::string &name, const std::string &description)
 For flags without values. Like a help message, for example. More...
 
template<class T >
void getOption (const std::string &name, T &value) const
 Get option name from the parser and set value to the parsed value. More...
 
template<>
void getOption (const std::string &name, std::set< unsigned int, std::less< unsigned int >, std::allocator< unsigned int > > &value) const
 
- Public Member Functions inherited from QUESO::BaseInputOptionsParser
 BaseInputOptionsParser ()
 Default constructor. More...
 
virtual ~BaseInputOptionsParser ()
 Destructor. More...
 

Protected Attributes

const std::string m_filename
 
ScopedPtr
< boost::program_options::options_description >
::Type 
m_optionsDescription
 
ScopedPtr
< boost::program_options::variables_map >
::Type 
m_optionsMap
 

Private Attributes

bool m_scannedInputFile
 

Friends

std::ostream & operator<< (std::ostream &os, const BoostInputOptionsParser &parser)
 Helpful stream operator for printing the parser state. More...
 

Detailed Description

Definition at line 45 of file BoostInputOptionsParser.h.

Constructor & Destructor Documentation

QUESO::BoostInputOptionsParser::BoostInputOptionsParser ( const std::string &  filename)

Constructor that parses file filename.

Definition at line 35 of file BoostInputOptionsParser.C.

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 }
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
ScopedPtr< boost::program_options::variables_map >::Type m_optionsMap
QUESO::BoostInputOptionsParser::BoostInputOptionsParser ( )

Default constructor that sets m_filename to "".

The use-case that options are not read in from a file but are instead wholly provided by the user.

Definition at line 45 of file BoostInputOptionsParser.C.

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 }
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
ScopedPtr< boost::program_options::variables_map >::Type m_optionsMap
QUESO::BoostInputOptionsParser::~BoostInputOptionsParser ( )
virtual

Destructor.

Deletes m_optionsDescription

Definition at line 55 of file BoostInputOptionsParser.C.

56 {
57  queso_deprecated();
58  // Do nothing
59 }

Member Function Documentation

template<typename T >
void QUESO::BoostInputOptionsParser::getOption ( const std::string &  name,
T &  value 
) const

Get option name from the parser and set value to the parsed value.

Definition at line 109 of file BoostInputOptionsParser.C.

References m_scannedInputFile.

Referenced by QUESO::MLSamplingLevelOptions::getAllOptions(), QUESO::InfiniteDimensionalMCMCSamplerOptions::InfiniteDimensionalMCMCSamplerOptions(), QUESO::MLSamplingOptions::MLSamplingOptions(), and QUESO::OptimizerOptions::OptimizerOptions().

110 {
111  queso_deprecated();
112  if (m_scannedInputFile) {
113  value = (*m_optionsMap)[name].as<T>();
114  }
115 }
template<>
template void QUESO::BoostInputOptionsParser::getOption< std::set< unsigned int, std::less< unsigned int >, std::allocator< unsigned int > > > ( const std::string &  name,
std::set< unsigned int, std::less< unsigned int >, std::allocator< unsigned int > > &  value 
) const

Definition at line 119 of file BoostInputOptionsParser.C.

References m_scannedInputFile, and QUESO::MiscReadDoublesFromString().

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 }
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40
template<typename T >
void QUESO::BoostInputOptionsParser::registerOption ( const std::string &  name,
const T &  defaultValue,
const std::string &  description 
)

Call this to register an option with the parser.

The name of the option to look for in the input file is given by name is given by. If no option is present in the input file, then use defaultValue is used for the default value for the option. Describe the option with a helpful message using description.

Definition at line 86 of file BoostInputOptionsParser.C.

References m_optionsDescription.

Referenced by QUESO::MLSamplingLevelOptions::defineAllOptions(), QUESO::InfiniteDimensionalMCMCSamplerOptions::InfiniteDimensionalMCMCSamplerOptions(), QUESO::MLSamplingOptions::MLSamplingOptions(), and QUESO::OptimizerOptions::OptimizerOptions().

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 }
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
void QUESO::BoostInputOptionsParser::registerOption ( const std::string &  name,
const std::string &  description 
)

For flags without values. Like a help message, for example.

Definition at line 98 of file BoostInputOptionsParser.C.

References m_optionsDescription.

100 {
101  queso_deprecated();
102  m_optionsDescription->add_options()
103  (name.c_str(),
104  description.c_str());
105 }
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
void QUESO::BoostInputOptionsParser::scanInputFile ( )

This is the method that parses the input file.

It parses the input file and sets the m_optionsMap member.

Definition at line 62 of file BoostInputOptionsParser.C.

References m_filename, m_optionsDescription, m_optionsMap, and m_scannedInputFile.

Referenced by QUESO::InfiniteDimensionalMCMCSamplerOptions::InfiniteDimensionalMCMCSamplerOptions(), QUESO::MLSamplingLevelOptions::MLSamplingLevelOptions(), QUESO::MLSamplingOptions::MLSamplingOptions(), QUESO::OptimizerOptions::OptimizerOptions(), and QUESO::MLSamplingLevelOptions::scanOptionsValues().

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 }
ScopedPtr< boost::program_options::options_description >::Type m_optionsDescription
ScopedPtr< boost::program_options::variables_map >::Type m_optionsMap

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  os,
const BoostInputOptionsParser parser 
)
friend

Helpful stream operator for printing the parser state.

Definition at line 157 of file BoostInputOptionsParser.C.

158 {
159  queso_deprecated();
160  os << *(parser.m_optionsDescription);
161  return os;
162 }

Member Data Documentation

const std::string QUESO::BoostInputOptionsParser::m_filename
protected

Definition at line 97 of file BoostInputOptionsParser.h.

Referenced by scanInputFile().

ScopedPtr<boost::program_options::options_description>::Type QUESO::BoostInputOptionsParser::m_optionsDescription
protected

Definition at line 99 of file BoostInputOptionsParser.h.

Referenced by QUESO::operator<<(), registerOption(), and scanInputFile().

ScopedPtr<boost::program_options::variables_map>::Type QUESO::BoostInputOptionsParser::m_optionsMap
protected

Definition at line 100 of file BoostInputOptionsParser.h.

Referenced by scanInputFile().

bool QUESO::BoostInputOptionsParser::m_scannedInputFile
private

Definition at line 103 of file BoostInputOptionsParser.h.

Referenced by getOption(), and scanInputFile().


The documentation for this class was generated from the following files:

Generated on Tue Jun 5 2018 19:49:05 for queso-0.57.1 by  doxygen 1.8.5