Simulation Parameters.
More...
#include <setup.h>
|
template<typename Ttype > |
void | add (string, string, string) |
| Add a parameter to the map without a default value. More...
|
|
template<typename Ttype > |
void | add (string, string, string, const Ttype) |
| Add a parameter to the map with a default value. More...
|
|
template<typename Ttype > |
void | set (const string &, const Ttype) |
| Set a parameter from a value. More...
|
|
template<typename Ttype > |
void | set (const string &, const pt::ptree &) |
| Set a parameter from an xml node. More...
|
|
void | setupCommandLine (boost::ptr_map< string, po::options_description > &) |
| Insert options into the options_description data structure to be read from the command line. More...
|
|
void | update (int, char *[], po::options_description &) |
| Update parameters from command line. More...
|
|
void | update (const pt::ptree &) |
| Update parameters from an xml file.
|
|
void | print () |
| print out the parameter map More...
|
|
const po::variables_map & | operator() () const |
| Parameters() returns the full map.
|
|
po::variables_map & | operator() () |
|
const bool | operator() (const string &key) const |
| Parmaters(key) returns the existence of a parameter.
|
|
const po::variable_value & | operator[] (const string &key) const |
| Parmaters[key] returns the variable value at key.
|
|
po::variable_value & | operator[] (const string &key) |
|
Simulation Parameters.
- See also
- http://stackoverflow.com/questions/7174781/boost-program-options-notifier-for-options-with-no-value in the future to change default value of no value options to be bool
Definition at line 44 of file setup.h.
◆ add() [1/2]
template<typename Ttype >
void Parameters::add |
( |
string |
_label, |
|
|
string |
_helpMessage, |
|
|
string |
_pClass |
|
) |
| |
Add a parameter to the map without a default value.
Initialize a parameter in the map.
- Parameters
-
_label | longName,shortName where shortName is optional |
_helpMessage | a command line help message |
_pClass | the class of parameter |
Definition at line 125 of file setup.h.
129 vector<string> label = split(_label,
',');
130 string key = label[0];
131 if (label.size() > 1)
132 shortName.emplace(key,label[1]);
134 shortName.emplace(key,
"");
137 helpMessage.emplace(key,_helpMessage);
140 pClass.emplace(key,_pClass);
143 type.emplace(pair<string,const type_info&>(key,
typeid(Ttype)));
146 extract.emplace(pair<
string,
void(*)(
const po::variable_value &)>
147 (key,[](
const po::variable_value& v) {cout << v.as<Ttype>();}));
150 state.emplace(key,UNSET);
153 params.insert(make_pair(key, po::variable_value()));
◆ add() [2/2]
template<typename Ttype >
void Parameters::add |
( |
string |
_label, |
|
|
string |
_helpMessage, |
|
|
string |
_pClass, |
|
|
const Ttype |
_defaultValue |
|
) |
| |
Add a parameter to the map with a default value.
Initialize a parameter in the map with a default value.
- Parameters
-
_label | longName,shortName where shortName is optional |
_helpMessage | a command line help message |
_pClass | the class of parameter |
_defaultValue | the default value of the parameter |
Definition at line 168 of file setup.h.
172 add<Ttype>(_label,_helpMessage,_pClass);
175 vector<string> label = split(_label,
',');
176 string key = label[0];
179 set<Ttype>(key,_defaultValue);
182 state.at(key) = DEFAULTED;
◆ print()
void Parameters::print |
( |
| ) |
|
◆ set() [1/2]
template<typename Ttype >
void Parameters::set |
( |
const string & |
key, |
|
|
const pt::ptree & |
xml |
|
) |
| |
Set a parameter from an xml node.
Set the value of a parameter in the map from an xml node.
- Parameters
-
key | name of the parameter |
xml | node |
Definition at line 216 of file setup.h.
221 if (state[key] == UNSET || state[key] == DEFAULTED) {
222 set<Ttype>(key,xml.get<Ttype>(key));
◆ set() [2/2]
template<typename Ttype >
void Parameters::set |
( |
const string & |
key, |
|
|
const Ttype |
val |
|
) |
| |
Set a parameter from a value.
Set the value of a parameter in the map from a value.
- Parameters
-
key | name of the parameter |
val | value of the parameter |
Definition at line 192 of file setup.h.
193 if (params.count(key)) {
194 po::variables_map::iterator it(params.find(key));
195 po::variable_value & v(it->second);
199 params.insert(std::make_pair(key, po::variable_value(val,
false)));
203 type.emplace(pair<string,const type_info&>(key,
typeid(Ttype)));
◆ setupCommandLine()
void Parameters::setupCommandLine |
( |
boost::ptr_map< string, po::options_description > & |
_options | ) |
|
Insert options into the options_description data structure to be read from the command line.
Insert all parameters into a pointer map of options.
At present we can accept bool, double, int, uint32, string and vector<string> options. This needs to be modified for each new option data type.
Definition at line 97 of file setup.cpp.
100 for (
auto & par : params) {
103 string key = par.first;
106 po::options_description &option = _options[pClass[key]];
110 if (shortName[key] !=
"")
111 label +=
"," + shortName[key];
114 if (type.at(key) ==
typeid(
bool))
115 option.add_options()(label.c_str(),helpMessage[key].c_str());
117 else if (type.at(key) ==
typeid(double))
118 option.add_options()(label.c_str(),initValue<double>(key),helpMessage[key].c_str());
119 else if (type.at(key) ==
typeid(int))
120 option.add_options()(label.c_str(),initValue<int>(key),helpMessage[key].c_str());
121 else if (type.at(key) ==
typeid(
uint32))
122 option.add_options()(label.c_str(),initValue<uint32>(key),helpMessage[key].c_str());
123 else if (type.at(key) ==
typeid(string))
124 option.add_options()(label.c_str(),initValue<string>(key),helpMessage[key].c_str());
125 else if (type.at(key) ==
typeid(vector<string>)) {
127 if (state[key] == DEFAULTED) {
129 vector<string> ops = par.second.as<vector<string>>();
130 option.add_options()(label.c_str(), po::value<vector<string>>()->
131 default_value(ops,
getList(ops))->composing(), helpMessage[key].c_str());
134 option.add_options()(label.c_str(),
135 po::value<vector<string>>()->composing(), helpMessage[key].c_str());
138 cerr <<
"insertOption Failed to find a valid type.";
unsigned long uint32
Unsigned integer type, at least 32 bits.
string getList(const vector< string > &options)
Create a comma separated list from a vector of strings.
◆ update()
void Parameters::update |
( |
int |
argc, |
|
|
char * |
argv[], |
|
|
po::options_description & |
cmdLineOptions |
|
) |
| |
Update parameters from command line.
Update the state of all parameters.
We check both the commmand line options as well as any parameters specified in an XML file. The command line takes precedence.
!!NB!! XML options needed to be correctly sorted in the file or they will be ignored. This should be fixed to exit gracefully with an error message.
Definition at line 151 of file setup.cpp.
154 po::variables_map cmdparams;
157 po::store(po::parse_command_line(argc, argv, cmdLineOptions), cmdparams);
158 po::notify(cmdparams);
162 for (
auto & par : params) {
165 string key = par.first;
166 auto &val = par.second;
169 if (cmdparams.count(key)) {
171 val = cmdparams[key];
174 if (cmdparams[key].empty())
176 else if (cmdparams[key].defaulted())
177 state[key] = DEFAULTED;
185 if (!params[
"param_file"].empty()) {
189 pt::read_xml(params[
"param_file"].as<string>(),xmlParams);
190 xmlParams = xmlParams.get_child(
"pimc");
194 for (
auto & par : params) {
197 string key = par.first;
200 string paramClass = pClass[key] +
"_parameters";
204 if (getNode(xmlParams,xml,paramClass)) {
206 if (type.at(key) ==
typeid(
bool)){
209 else if (type.at(key) ==
typeid(
double)) {
210 set<double>(key,xml);
212 else if (type.at(key) ==
typeid(
int)) {
215 else if (type.at(key) ==
typeid(
uint32)) {
216 set<uint32>(key,xml);
218 else if (type.at(key) ==
typeid(
string)) {
219 set<string>(key,xml);
221 else if (type.at(key) ==
typeid(vector<string>)) {
223 string pluralKey = key +
"s";
224 if (xml.count(pluralKey) != 0) {
230 for (
auto const &opName : xml.get_child(pluralKey))
231 ops.push_back(opName.second.data());
234 set<vector<string>>(key,ops);
239 cerr <<
"xmlInsertOption Failed to find a valid type.";
245 cerr <<
"Cannot read parameter file: " << params[
"param_file"].as<
string>() << endl;
The documentation for this class was generated from the following files: