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
23{
31 template <class T>
33 {
34 protected:
35 std::string m_name;
36 std::string m_helpString;
37 std::vector<T> m_valueArray;
38
40 {
41 auto parser = OptionsParser::getMe();
42 auto line = parser->find( m_name );
43 if( line == parser->end() ) return false ;
44 const std::vector<std::string>& vsl = line->second;
45 if ( vsl.size() < 2 ) return false; // first element is parameter name
46 m_valueArray.clear();
47 m_valueArray.resize( vsl.size() - 1 );
48 for ( unsigned int i = 1; i < vsl.size(); i++ ) {
49 bool status = true;
50 T tmpVal = lexical_cast<T>( vsl[i], status );
51 if ( status == false ) {
52 ERROR( "Failed to parse token: " << vsl[i] << " for parameter: " << m_name );
53 continue;
54 }
55 setVal( tmpVal, i - 1 );
56 }
57 return true;
58 }
59
60 public:
61 NamedParameter( const std::string& name, const T& def=T(), const std::string& helpString="" ) :
62 m_name(name),
63 m_helpString(helpString)
64 {
65 setVal( def );
67 if ( OptionsParser::printHelp() ) help(def);
68 DEBUG( *this );
69 }
70 NamedParameter(const std::string& name, const std::vector<T>& defVec, const std::string& helpString="")
71 : m_name(name),
72 m_helpString(helpString)
73 {
74 setVal( defVec );
76 if ( OptionsParser::printHelp() ) help( defVec.size() > 0 ? defVec[0] : T() );
77 }
78 void help(const T& def){
79 std::string type = type_string<T>();
80 if( type == "std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >" ) type = "string";
81 std::cout << " " << bold_on << std::left << std::setw(27) << m_name << bold_off
82 << std::setw(20) << "[" + type + "]" ;
83 auto tokens = split( m_helpString, '\n' );
84 if( tokens.size() == 0 ) std::cout << std::endl;
85 for( size_t i = 0 ; i < tokens.size(); ++i){
86 if( i == 0 ){
87 std::cout << tokens[i];
88 if( def != T() ) std::cout << " (default = " << def << ")";
89 std::cout << std::endl;
90 }
91 else std::cout << std::string(48,' ') << tokens[i] << std::endl;
92 }
93 }
94
95 size_t size() const { return m_valueArray.size(); }
96
97 const T getVal( int i = 0 ) const
98 {
99 if ( i < 0 || i >= (int)m_valueArray.size() ) {
100 ERROR( "Parameter name \"" << name() << "\". Index: " << i << " out of range [ " << 0 << " , "
101 << m_valueArray.size() );
102 throw std::runtime_error( "array index out of bounds" );
103 }
104 return m_valueArray[i];
105 }
106
107 operator T() const { return getVal(); }
108 operator T() { return getVal(); }
109 template <class G> bool operator==(const G& other) const { return getVal() == other; }
110 template <class G> bool operator!=(const G& other) const { return getVal() != other; }
111 const std::vector<T>& getVector() const { return m_valueArray; }
112
113 void setVal( const T& val, int i = 0 )
114 {
115 if ( i < 0 ) return;
116 if ( i >= (int)m_valueArray.size() ) {
117 m_valueArray.resize( i + 1 );
118 }
119 m_valueArray[i] = val;
120 }
121 void setVal( const std::vector<T>& valList )
122 {
123 m_valueArray = valList;
124 }
125
126 operator std::vector<T>() const { return getVector(); }
128 {
129 setVal( d, 0 );
130 return *this;
131 }
132 NamedParameter<T>& operator=( const std::vector<T>& v )
133 {
134 setVal( v );
135 return *this;
136 }
137 const std::string& name() const { return m_name ; }
138
139 static std::vector<T> getVectorArgument( const std::string& name, const T& default_value )
140 {
141 std::vector<T> return_container;
142 unsigned int x = 0;
143 T obj = default_value;
144 do {
145 obj = AmpGen::NamedParameter<T>( name + std::to_string( x++ ), default_value );
146 if ( obj != T() ) return_container.push_back( obj );
147 } while ( obj != default_value );
148 return return_container;
149 }
150 };
151 template <typename T> std::ostream& operator<<( std::ostream& os, const NamedParameter<T>& np );
152 template <typename ...T> std::string optionalHelpString(const std::string& header, const T&... args);
153}
154
155template <typename T>
156std::ostream& AmpGen::operator<<( std::ostream& os, const AmpGen::NamedParameter<T>& np )
157{
158 os << np.name() ;
159 for ( size_t i = 0; i < np.size(); i++ ) {
160 if( i == 0 ) os << " = ";
161 os << np.getVal(i);
162 if ( i != np.size() ) os << " ";
163 }
164 return os;
165}
166
167template <typename ...T> std::string AmpGen::optionalHelpString(const std::string& header, const T&... args )
168{
169 std::stringstream rt;
170 rt << header;
171 for_each( std::make_tuple(args...), [&rt](const auto& f) mutable {
172 rt << "\n\033[3m " << f.first << "\033[0m: " << f.second;
173 });
174 return rt.str();
175}
176
177#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:80
#define DEBUG(X)
Used for printing verbose debugging messages, only if DEBUGLEVEL is defined.
Definition MsgService.h:66
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::string optionalHelpString(const std::string &header, const T &... args)
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:18
return_type lexical_cast(const std::string &word, bool &status)
Definition Utilities.h:87
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:39