3
0
mirror of https://github.com/triqs/dft_tools synced 2024-10-31 19:23:45 +01:00

[API Change] parameters: required, optional

- now chain the required

- pdef.optional( "B", short(0), " short ")
  .optional( "C", 1u, " unsigned short ")
  .optional( "D", int(2), " int ")
  .optional( "E", 3u, " unsigned int ")
  .optional( "F", long(4), " long ")
This commit is contained in:
Olivier Parcollet 2013-10-26 14:34:10 +02:00
parent 12cb2db2ba
commit 928ef222cc
2 changed files with 32 additions and 41 deletions

View File

@ -9,24 +9,21 @@ int main() {
parameters P,P2;
parameter_defaults pdef;
pdef.required
( "A", int(), "really ?")
;
pdef.required ( "A", int(), "really ?") ;
pdef.optional
( "B", short(0), " short ")
( "C", 1u, " unsigned short ")
( "D", int(2), " int ")
( "E", 3u, " unsigned int ")
( "F", long(4), " long ")
( "G", 5ll, " long ")
( "H", float(6), " float ")
( "I", double(7.8), " doube ")
// ( "K", std::complex<double>(12), " double complex ")
( "L", std::string("13"), " string ")
// ( "M", std::vector<double> { 1,4 }, " vector ")
( "N", double(15), "")
( "W", int(16), "")
pdef.optional( "B", short(0), " short ")
.optional( "C", 1u, " unsigned short ")
.optional( "D", int(2), " int ")
.optional( "E", 3u, " unsigned int ")
.optional( "F", long(4), " long ")
.optional( "G", 5ll, " long ")
.optional( "H", float(6), " float ")
.optional( "I", double(7.8), " doube ")
.optional( "K", std::complex<double>(12), " double complex ")
.optional( "L", std::string("13"), " string ")
.optional( "M", std::vector<double> { 1,4 }, " vector ")
.optional( "N", double(15), "")
.optional( "W", int(16), "")
;
P["a"] = long(1);

View File

@ -49,19 +49,13 @@ namespace triqs { namespace utility {
template<class Archive>
void serialize(Archive & ar, const unsigned int version) { ar & boost::serialization::make_nvp("object_map",object_map); }
struct _inserter {
parameter_defaults * p; bool opt;
_inserter(parameter_defaults *p_, bool opt_) : p(p_), opt(opt_) {}
template<typename T> _inserter operator()(std::string const & key, T && def_val, std::string const & doc) {
p->object_map[key] = std::forward<T>(def_val);
p->documentation[key] = doc;
p->is_optional[key] = opt;
template<typename T> parameter_defaults & insert(std::string const & key, T && def_val, std::string const & doc, bool opt) {
object_map[key] = std::forward<T>(def_val);
documentation[key] = doc;
is_optional[key] = opt;
return *this;
}
};
friend struct _inserter;
template<typename T> const T getter(std::map<std::string,T> const & m, std::string const & key) const {
auto it = m.find(key); assert(it !=m.end()); return it->second;
}
@ -79,18 +73,18 @@ namespace triqs { namespace utility {
bool is_required(std::string const & key) const { return (has_key(key) && (! getter(this->is_optional,key)));}
std::string doc(std::string const & key) const { return (has_key(key) ? getter(this->documentation,key) : "");}
///inserter for optional parameters;
///calls can be chained for multiple parameters
template<typename T>
_inserter optional (std::string const & key, T && def_val, std::string const & doc) {
return _inserter(this, true)(key,std::forward<T>(def_val), doc);
/// inserter for optional parameters;
/// calls can be chained for multiple parameters
template <typename T> parameter_defaults &optional(std::string const &key, T &&def_val, std::string const &doc) {
insert(key, std::forward<T>(def_val), doc, true);
return *this;
}
///inserter for required parameters;
///calls can be chained for multiple parameters
template<typename T>
_inserter required (std::string const & key, T && def_val, std::string const & doc) {
return _inserter(this, false)(key,std::forward<T>(def_val), doc);
/// inserter for required parameters;
/// calls can be chained for multiple parameters
template <typename T> parameter_defaults &required(std::string const &key, T &&def_val, std::string const &doc) {
insert(key, std::forward<T>(def_val), doc, false);
return *this;
}
///parameters class-like element access