13 #include <boost/lexical_cast.hpp> 14 #include <boost/optional.hpp> 25 static boost::optional<T> apply(
const std::string& in) {
28 if (boost::conversion::try_lexical_convert(in, conv_result)) {
36 struct parse_string<
std::string> {
37 static boost::optional<std::string> apply(
const std::string& in) {
43 struct parse_string<bool> {
44 static boost::optional<bool> apply(std::string in) {
45 std::locale c_locale(
"C");
47 c=tolower(c, c_locale);
49 boost::optional<bool>
result;
50 if (in==
"true" || in==
"on" || in==
"yes" || in==
"1") result=
true;
51 if (in==
"false" || in==
"off" || in==
"no" || in==
"0") result=
false;
57 struct parse_string<
std::vector<T> > {
58 static boost::optional< std::vector<T> > apply(
const std::string& in) {
59 typedef std::vector<T> value_type;
60 typedef boost::optional<value_type> result_type;
61 typedef boost::optional<T> optional_el_type;
62 typedef std::string::const_iterator sit_type;
63 value_type result_vec;
65 sit_type it1=in.begin();
66 while (it1!=in.end()) {
67 sit_type it2=find(it1, in.end(),
',');
68 optional_el_type elem=parse_string<T>::apply(std::string(it1,it2));
70 result_vec.push_back(*elem);
71 if (it2!=in.end()) ++it2;
82 bool params::assign_to_name_(
const std::string& name,
const std::string& strval)
84 boost::optional<T> result=detail::parse_string<T>::apply(strval);
94 bool params::define_(
const std::string& name,
const std::string& descr)
96 if (this->exists(name) && !this->exists<T>(name))
97 throw exception::type_mismatch(name,
"Parameter already in dictionary with a different type");
99 td_map_type::iterator td_it=td_map_.find(name);
100 if (td_it!=td_map_.end()) {
101 if (td_it->second.typestr() != detail::make_typestr::apply<T>())
throw exception::type_mismatch(name,
"Parameter already defined with a different type");
102 td_it->second.descr()=descr;
105 td_map_.insert(std::make_pair(name, detail::td_type::make_pair<T>(descr, td_map_.size())));
107 strmap::const_iterator it=raw_kv_content_.find(name);
108 if (it==raw_kv_content_.end()) {
109 if (this->exists(name))
return true;
112 if (!assign_to_name_<T>(name, it->second)) {
113 err_status_.push_back(
"Cannot parse parameter '"+name+
"' as the requested type");
114 (*this)[name].clear();
119 template <
typename T>
122 if (!define_<T>(name, descr)) {
123 if (!this->exists<T>(name)) err_status_.push_back(
"Required parameter '"+name+
"' is missing");
128 template <
typename T>
131 if (!define_<T>(name, descr)) {
132 (*this)[name]=defval;
139 return raw_kv_content_.count(name);
144 return exists(name) && !supplied(name);
149 return td_map_.count(name)!=0 || exists(name);
155 swap(static_cast<dictionary&>(p1), static_cast<dictionary&>(p2));
156 swap(p1.raw_kv_content_, p2.raw_kv_content_);
157 swap(p1.td_map_, p2.td_map_);
158 swap(p1.err_status_, p2.err_status_);
159 swap(p1.origins_.data(), p2.origins_.data());
Parse sectioned INI file or HDF5 or command line, provide the results as dictionary.
bool defined(const std::string &name) const
Check whether a parameter was ever defined.
params & define(const std::string &name, const std::string &descr)
Defines a parameter; returns false on error, and records the error in the object. ...
void swap(params &p1, params &p2)
std::string origin_name(const params &p)
Convenience function to obtain the "origin" filename associated with the parameters object...
bool supplied(const std::string &name) const
True if the parameter is supplied via file or cmdline.
traits< Acc >::result_type result(const Acc &acc)
bool defaulted(const std::string &name) const
True if the parameter acquired its value by default.
std::string get_origin_name() const ALPS_DEPRECATED
Convenience method: returns the "origin name".