AmpGen 2.1
Loading...
Searching...
No Matches
NamedParameter.h
Go to the documentation of this file.
1#ifndef AMPGEN_NAMEDPARAMETER_H
2#define AMPGEN_NAMEDPARAMETER_H
3// author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
4// status: Mon 9 Feb 2009 19:17:56 GMT
5
6#include <stddef.h>
7#include <iostream>
8#include <sstream>
9#include <stdexcept>
10#include <string>
11#include <utility>
12#include <vector>
13#include <iomanip>
14#include <map>
15#include <cstring>
16
17#include "AmpGen/MsgService.h"
19#include "AmpGen/Utilities.h"
20#include "AmpGen/MetaUtils.h"
21
22namespace AmpGen {
30 template <class T> class [[deprecated]] NamedParameter {
31 protected:
32 std::string m_name;
33 std::string m_helpString;
34 std::vector<T> m_valueArray;
35
37 auto parser = OptionsParser::getMe();
38 auto line = parser->find(m_name);
39 if(line == parser->end()) return false;
40 const std::vector<std::string> &vsl = line->second;
41 if(vsl.size() < 2) return false; // first element is parameter name
42 m_valueArray.clear();
43 m_valueArray.resize(vsl.size() - 1);
44 for(unsigned int i = 1; i < vsl.size(); i++) {
45 bool status = true;
46 T tmpVal = lexical_cast<T>(vsl[i], status);
47 if(status == false) {
48 ERROR("Failed to parse token: " << vsl[i] << " for parameter: " << m_name);
49 continue;
50 }
51 setVal(tmpVal, i - 1);
52 }
53 return true;
54 }
55
56 public:
57 NamedParameter(const std::string &name, const T &def = T(), const std::string &helpString = "") : m_name(name), m_helpString(helpString) {
58 setVal(def);
61 DEBUG(*this);
62 }
63 NamedParameter(const std::string &name, const std::vector<T> &defVec, const std::string &helpString = "") : m_name(name), m_helpString(helpString) {
64 setVal(defVec);
66 if(OptionsParser::printHelp()) help(defVec.size() > 0 ? defVec[0] : T());
67 }
68 void help(const T &def) {
69 std::string type = type_string<T>();
70 if(type == "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >") type = "string";
71 std::cout << " " << bold_on << std::left << std::setw(27) << m_name << bold_off << std::setw(20) << "[" + type + "]";
72 auto tokens = split(m_helpString, '\n');
73 if(tokens.size() == 0) std::cout << std::endl;
74 for(size_t i = 0; i < tokens.size(); ++i) {
75 if(i == 0) {
76 std::cout << tokens[i];
77 if(def != T()) std::cout << " (default = " << def << ")";
78 std::cout << std::endl;
79 } else
80 std::cout << std::string(48, ' ') << tokens[i] << std::endl;
81 }
82 }
83
84 size_t size() const { return m_valueArray.size(); }
85
86 const T getVal(int i = 0) const {
87 if(i < 0 || i >= (int)m_valueArray.size()) {
88 ERROR("Parameter name \"" << name() << "\". Index: " << i << " out of range [ " << 0 << " , " << m_valueArray.size());
89 throw std::runtime_error("array index out of bounds");
90 }
91 return m_valueArray[i];
92 }
93
94 operator T() const { return getVal(); }
95 operator T() { return getVal(); }
96 template <class G> bool operator==(const G &other) const { return getVal() == other; }
97 template <class G> bool operator!=(const G &other) const { return getVal() != other; }
98 const std::vector<T> &getVector() const { return m_valueArray; }
99
100 void setVal(const T &val, int i = 0) {
101 if(i < 0) return;
102 if(i >= (int)m_valueArray.size()) { m_valueArray.resize(i + 1); }
103 m_valueArray[i] = val;
104 }
105 void setVal(const std::vector<T> &valList) { m_valueArray = valList; }
106
107 operator std::vector<T>() const { return getVector(); }
109 setVal(d, 0);
110 return *this;
111 }
112 NamedParameter<T> &operator=(const std::vector<T> &v) {
113 setVal(v);
114 return *this;
115 }
116 const std::string &name() const { return m_name; }
117
118 static std::vector<T> getVectorArgument(const std::string &name, const T &default_value) {
119 std::vector<T> return_container;
120 unsigned int x = 0;
121 T obj = default_value;
122 do {
123 obj = AmpGen::NamedParameter<T>(name + std::to_string(x++), default_value);
124 if(obj != T()) return_container.push_back(obj);
125 } while(obj != default_value);
126 return return_container;
127 }
128 };
129 template <typename T> std::ostream &operator<<(std::ostream &os, const NamedParameter<T> &np);
130 template <typename... T> std::string optionalHelpString(const std::string &header, const T &...args);
131}
132
133template <typename T> std::ostream &AmpGen::operator<<(std::ostream &os, const AmpGen::NamedParameter<T> &np) {
134 os << np.name();
135 for(size_t i = 0; i < np.size(); i++) {
136 if(i == 0) os << " = ";
137 os << np.getVal(i);
138 if(i != np.size()) os << " ";
139 }
140 return os;
141}
142
143template <typename... T> std::string AmpGen::optionalHelpString(const std::string &header, const T &...args) {
144 std::stringstream rt;
145 rt << header;
146 for_each(std::make_tuple(args...), [&rt](const auto &f) mutable { rt << "\n\033[3m " << f.first << "\033[0m: " << f.second; });
147 return rt.str();
148}
149
150#endif
A parameter with value specified by the user at runtime, either in an options file or via the command...
void setVal(const std::vector< T > &valList)
const T getVal(int i=0) const
std::vector< T > m_valueArray
< The helper string for this parameter, printed if the flag –help is used.
bool operator==(const G &other) const
bool operator!=(const G &other) const
const std::vector< T > & getVector() const
NamedParameter< T > & operator=(const T &d)
NamedParameter(const std::string &name, const T &def=T(), const std::string &helpString="")
NamedParameter(const std::string &name, const std::vector< T > &defVec, const std::string &helpString="")
bool setFromOptionsParser()
< The value (array) of this parameter.
static std::vector< T > getVectorArgument(const std::string &name, const T &default_value)
const std::string & name() const
NamedParameter< T > & operator=(const std::vector< T > &v)
std::string m_helpString
< Name of this parameter
void help(const T &def)
void setVal(const T &val, int i=0)
static OptionsParser * getMe()
static bool printHelp()
#define ERROR(X)
Used for printing errors messages, and will always be printed.
Definition MsgService.h:85
#define DEBUG(X)
Used for printing verbose debugging messages, only if DEBUGLEVEL is defined.
Definition MsgService.h:69
std::string optionalHelpString(const std::string &header, const T &...args)
std::vector< std::string > split(const std::string &, char, bool=true)
std::ostream & operator<<(std::ostream &os, const CompiledExpressionBase &expression)
std::ostream & bold_on(std::ostream &)
std::ostream & bold_off(std::ostream &)
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:17
return_type lexical_cast(const std::string &word, bool &status)
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:36