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

View File

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