3
0
mirror of https://github.com/triqs/dft_tools synced 2024-12-25 22:03:43 +01:00

Serialization boost/h5 and clean parameters

- parameters : clean, add back serialization. Clean whitespace

- serialization : depending on the version of the hdf5 lib,
  uses h5 or boost.

- TODO : test it on a machine with new hdf5.
This commit is contained in:
Olivier Parcollet 2014-05-21 14:33:07 +02:00
parent 945116a54b
commit 27af80c04e
8 changed files with 237 additions and 205 deletions

View File

@ -3,7 +3,6 @@ from wrap_generator import *
# The module # The module
module = module_(full_name = "pytriqs.parameters.parameters", doc = "TO BE WRITTEN") module = module_(full_name = "pytriqs.parameters.parameters", doc = "TO BE WRITTEN")
module.add_include("<triqs/parameters/parameters.hpp>") module.add_include("<triqs/parameters/parameters.hpp>")
module.add_include("<triqs/utility/formatted_output.hpp>")
module.add_using("namespace triqs::params") module.add_using("namespace triqs::params")
# one class # one class
@ -20,7 +19,7 @@ g = class_(
#add a constructor #add a constructor
#g.add_constructor(doc = "DOC of constructor", args = []) #g.add_constructor(doc = "DOC of constructor", args = [])
g.add_method(py_name = "help", calling_pattern = "auto result = triqs::utility::print_formatted(self_c.generate_help())", signature = "std::string()", doc = "help") g.add_method(py_name = "help", calling_pattern = "auto result = self_c.help()", signature = "std::string()", doc = "help")
# add getitem/setitem ... # add getitem/setitem ...
g.add_getitem(signature = "PyObject *(const char * key)", g.add_getitem(signature = "PyObject *(const char * key)",

View File

@ -45,18 +45,10 @@ namespace params {
if (obj.has_type<std::string>()) { if (obj.has_type<std::string>()) {
return lex_cast_from_string<double>(obj); return lex_cast_from_string<double>(obj);
} }
#define TRANSFORM_TYPE(T) \ try {
if (obj.has_type<T>()) return extract<T>(obj) if (obj.has_type<long>()) return extract<long>(obj);
TRANSFORM_TYPE(int); }
// TRANSFORM_TYPE(unsigned int); catch(...){}
TRANSFORM_TYPE(long);
// TRANSFORM_TYPE(unsigned long);
TRANSFORM_TYPE(short);
// TRANSFORM_TYPE(unsigned short);
TRANSFORM_TYPE(long long);
// TRANSFORM_TYPE(unsigned long long);
// TRANSFORM_TYPE(float);
#undef TRANSFORM_TYPE
TRIQS_RUNTIME_ERROR << "extraction of " << obj.name() << " impossible : type mismatch. Got " << obj.type_name() TRIQS_RUNTIME_ERROR << "extraction of " << obj.name() << " impossible : type mismatch. Got " << obj.type_name()
<< ", while I am supposed to extract a double"; << ", while I am supposed to extract a double";
} }

View File

@ -19,32 +19,28 @@
* *
******************************************************************************/ ******************************************************************************/
#pragma once #pragma once
#include <triqs/utility/first_include.hpp> #include <triqs/arrays.hpp>
#ifdef TRIQS_WITH_PYTHON_SUPPORT
#include <triqs/python_tools/wrapper_tools.hpp>
#endif
#include <string> #include <string>
#include <complex> #include <complex>
#include <memory> #include <memory>
#include <map> #include <map>
#include <typeindex> #include <typeindex>
#include <boost/preprocessor/seq/for_each.hpp> #include <boost/preprocessor/seq/for_each.hpp>
// serialization will use hdf5 with a failsafe to boost for old hdf5 libs.
//#include <triqs/utility/serialization.hpp> #include <triqs/utility/serialization.hpp>
#include <triqs/arrays.hpp>
#include <triqs/python_tools/wrapper_tools.hpp>
namespace triqs { namespace triqs {
namespace params { namespace params {
using triqs::get_triqs_hdf5_data_scheme; using triqs::get_triqs_hdf5_data_scheme; // bring it into name resolution
template <typename T> std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) { template <typename T> std::ostream &operator<<(std::ostream &out, std::vector<T> const &v) {
out << "["; out << "[";
if (v.size() > 3)
out << v[0] << ", ..., " << v[v.size()-1];
else {
int c = 0; int c = 0;
for (auto const &x : v) out << (c++ ? ", " : "") << x; for (auto const &x : v) out << (c++ ? ", " : "") << x;
}
return out << "]"; return out << "]";
} }
@ -52,21 +48,13 @@ namespace params {
#define TRIQS_UTIL_OPAQUE_OBJECT_PREDEFINED_CAST \ #define TRIQS_UTIL_OPAQUE_OBJECT_PREDEFINED_CAST \
(int)(long)(long long)(unsigned int)(unsigned long)(unsigned long long)(double)(bool)(std::string) (int)(long)(long long)(unsigned int)(unsigned long)(unsigned long long)(double)(bool)(std::string)
// a trait to compute the type actually stored in the opaque object. // the type actually stored
// T except for integers, which are all stored as long template <typename T, typename Enable = void> struct storage_t_impl : std::conditional<std::is_integral<T>::value, long, T>{};
template <typename T, typename Enable = void> struct storage_t_impl {
using type = T;
};
template <typename T> using storage_t = typename storage_t_impl<std::c14::decay_t<T>>::type; template <typename T> using storage_t = typename storage_t_impl<std::c14::decay_t<T>>::type;
template <typename T> struct storage_t_impl<T, std::c14::enable_if_t<std::is_integral<T>::value>> {
using type = long;
};
// differentiate value and view ?
template <typename A> struct storage_t_impl<A, std::c14::enable_if_t<arrays::is_amv_value_or_view_class<A>::value>> { template <typename A> struct storage_t_impl<A, std::c14::enable_if_t<arrays::is_amv_value_or_view_class<A>::value>> {
using type = arrays::array<typename A::value_type, A::rank>; using type = arrays::array<typename A::value_type, A::rank>;
}; };
template<> struct storage_t_impl<const char *> {using type = std::string;}; template<> struct storage_t_impl<const char *> {using type = std::string;};
template<> struct storage_t_impl<char *> {using type = std::string;}; template<> struct storage_t_impl<char *> {using type = std::string;};
@ -80,14 +68,14 @@ namespace params {
virtual _data *clone() const = 0; virtual _data *clone() const = 0;
virtual void h5_write_(h5::group, std::string const &) const = 0; virtual void h5_write_(h5::group, std::string const &) const = 0;
virtual void h5_read_(h5::group, std::string const &) = 0; virtual void h5_read_(h5::group, std::string const &) = 0;
virtual std::string serialize() const = 0;
//virtual std::string serialize() const = 0; virtual void deserialize(std::string const &) = 0;
//virtual void deserialize(std::string const &) = 0;
virtual std::ostream &print(std::ostream &out) const = 0; virtual std::ostream &print(std::ostream &out) const = 0;
#ifdef TRIQS_WITH_PYTHON_SUPPORT
virtual bool from_python_convertible(PyObject *) const = 0; virtual bool from_python_convertible(PyObject *) const = 0;
virtual void set_from_python(PyObject *) = 0; virtual void set_from_python(PyObject *) = 0;
virtual PyObject *to_python() const = 0; virtual PyObject *to_python() const = 0;
#endif
}; };
template <typename T> struct _data_impl : _data { template <typename T> struct _data_impl : _data {
@ -101,19 +89,18 @@ namespace params {
virtual _data *clone() const override { return new _data_impl(*this); } virtual _data *clone() const override { return new _data_impl(*this); }
virtual void h5_write_(h5::group f, std::string const &name) const override { h5_write(f, name, x); } virtual void h5_write_(h5::group f, std::string const &name) const override { h5_write(f, name, x); }
virtual void h5_read_(h5::group f, std::string const &name) override { h5_read(f, name, x); } virtual void h5_read_(h5::group f, std::string const &name) override { h5_read(f, name, x); }
/*
virtual std::string serialize() const override { return triqs::serialize(x); } virtual std::string serialize() const override { return triqs::serialize(x); }
virtual void deserialize(std::string const &s) override { virtual void deserialize(std::string const &s) override {
x = triqs::deserialize<T>(s); x = triqs::deserialize<T>(s);
}; };
*/
virtual std::ostream &print(std::ostream &out) const override { return out << x; } virtual std::ostream &print(std::ostream &out) const override { return out << x; }
#ifdef TRIQS_WITH_PYTHON_SUPPORT
virtual bool from_python_convertible(PyObject *ob) const override { virtual bool from_python_convertible(PyObject *ob) const override {
return py_tools::py_converter<T>::is_convertible(ob, true); return py_tools::py_converter<T>::is_convertible(ob, true);
} }
virtual void set_from_python(PyObject *ob) override { x = py_tools::py_converter<T>::py2c(ob); } virtual void set_from_python(PyObject *ob) override { x = py_tools::py_converter<T>::py2c(ob); }
virtual PyObject *to_python() const override { return py_tools::py_converter<T>::c2py(x); } virtual PyObject *to_python() const override { return py_tools::py_converter<T>::c2py(x); }
#endif
}; };
std::type_index index; std::type_index index;
@ -121,18 +108,28 @@ namespace params {
static std::map<std::type_index, std::string> type_names; static std::map<std::type_index, std::string> type_names;
std::string name_; // for_error_messages std::string name_; // for_error_messages
bool modified = false; bool modified = false;
bool no_default_value = false;
// only parameters will construct _field // only parameters will construct _field
template <typename T> template <typename T>
_field(T obj, std::string n, bool modification_required) _field(T obj, std::string n, bool without_default_value)
: index(typeid(storage_t<T>)), p(new _data_impl<storage_t<T>>{std::move(obj)}), name_(n), modified(!modification_required) { : index(typeid(storage_t<T>)), p(new _data_impl<storage_t<T>>{std::move(obj)}), name_(n), no_default_value(without_default_value) {
type_names.insert({index, get_triqs_hdf5_data_scheme(storage_t<T>{})}); type_names.insert({index, get_triqs_hdf5_data_scheme(storage_t<T>{})});
} }
std::string type_name() const { return type_name(index); }
static std::string type_name(std::type_index i);
const void *get() const { return (p ? p->get() : nullptr); }
template <typename T> friend T extract(_field const &ob);
template <typename T> bool has_type() const { return index == typeid(T); }
public: public:
//_field() : index(typeid(void)) {} // BREAKS invariant : only used for BOOST serialization... #ifdef TRIQS_SERIALIZATION_WITH_BOOST
_field() : index(typeid(void)) {} // BREAKS invariant : only used for BOOST serialization.
#endif
_field(_field &&c) = default; _field(_field &&c) = default;
_field(_field const &x) : index(x.index), p(x.p ? x.p->clone() : nullptr), name_(x.name_), modified(x.modified) {} // regular type _field(_field const &x) : index(x.index), p(x.p ? x.p->clone() : nullptr), name_(x.name_), modified(x.modified), no_default_value(x.no_default_value) {} // regular type
_field &operator=(_field &&c) = default; _field &operator=(_field &&c) = default;
_field &operator=(_field const &x) { return operator=(_field(x)); } _field &operator=(_field const &x) { return operator=(_field(x)); }
@ -147,33 +144,38 @@ namespace params {
return *this; return *this;
} }
// rewrite a few cases for convenience ... // rewrite a few cases for practical convenience ...
_field &operator=(int rhs) { // a special case where we can correct : int -> double _field &operator=(int rhs) { // a special case where we can correct : int -> double
if (index == typeid(double)) return operator=(double(rhs)); if (index == typeid(double)) return operator=(double(rhs));
return operator=(long(rhs));// beware infinite loop! return operator=(long(rhs));// beware infinite loop! Works because int != long ... Clean this ...
} }
// special treatment for const char *: fall back to string
_field &operator=(const char *rhs) { return operator=(std::string(rhs)); } _field &operator=(const char *rhs) { return operator=(std::string(rhs)); }
// for subgroups only : implemented after parameters. Check type at runtime // In the case where the data is itself a parameters (used to implement subgroups)
// we suplement _field with a few parameters methods
// Implemented after parameters (we need the type).
// Strict check type at runtime.
template <typename... T> _field &add_field(T &&... x); template <typename... T> _field &add_field(T &&... x);
_field &operator[](const char * key); _field &operator[](const char * key);
_field const &operator[](const char * key) const; _field const &operator[](const char * key) const;
friend bool is_parameter(_field const & f); friend bool is_parameter(_field const & f);
_field& add_group(std::string const& key, std::string const& doc); _field& add_group(std::string const& key, std::string const& doc);
std::string type_name() const { return type_name(index); } /// Name of the field
std::string const &name() const { return name_; } std::string const &name() const { return name_; }
bool is_modified() const { return modified; }
const void *get() const { return (p ? p->get() : nullptr); }
template <typename T> bool has_type() const { return index == typeid(T); }
static std::string type_name(std::type_index i);
/// Has the field been modified
bool is_modified() const { return modified; }
/// Is a modification required for this field
bool is_modification_required() const { return no_default_value && (!modified); }
#ifdef TRIQS_WITH_PYTHON_SUPPORT
/// Convertions python <-> C++
bool from_python_convertible(PyObject *ob) const { return p->from_python_convertible(ob); } bool from_python_convertible(PyObject *ob) const { return p->from_python_convertible(ob); }
void set_from_python(PyObject *ob) { p->set_from_python(ob); } void set_from_python(PyObject *ob) { p->set_from_python(ob); }
PyObject *to_python() const { return p->to_python(); } PyObject *to_python() const { return p->to_python(); }
#endif
// implemented later, since it needs the extract function ... // implemented later, since it needs the extract function ...
#define CAST_OPERATOR(r, data, T) operator T() const; #define CAST_OPERATOR(r, data, T) operator T() const;
@ -181,20 +183,17 @@ namespace params {
#undef CAST_OPERATOR #undef CAST_OPERATOR
// ----- Boost serialisation // ----- Boost serialisation
/*
template<class Archive> template <class Archive> void save(Archive &ar, const unsigned int version) const {
void save(Archive & ar, const unsigned int version) const {
std::string s = p->serialize(); std::string s = p->serialize();
ar << TRIQS_MAKE_NVP("seria_str", s); ar << TRIQS_MAKE_NVP("seria_str", s);
} }
template<class Archive> template <class Archive> void load(Archive &ar, const unsigned int version) {
void load(Archive & ar, const unsigned int version) {
std::string s; std::string s;
ar >> TRIQS_MAKE_NVP("seria_str", s); ar >> TRIQS_MAKE_NVP("seria_str", s);
p->deserialize(s); p->deserialize(s);
} }
BOOST_SERIALIZATION_SPLIT_MEMBER(); BOOST_SERIALIZATION_SPLIT_MEMBER();
*/
friend std::ostream &operator<<(std::ostream &out, _field const &ob) { return ob.p->print(out); } friend std::ostream &operator<<(std::ostream &out, _field const &ob) { return ob.p->print(out); }

View File

@ -1,5 +1,4 @@
#include <triqs/utility/first_include.hpp> #include <triqs/utility/first_include.hpp>
#include <triqs/utility/formatted_output.hpp>
#include <boost/serialization/string.hpp> #include <boost/serialization/string.hpp>
#include "./parameters.hpp" #include "./parameters.hpp"
#include <triqs/utility/serialization.hpp> #include <triqs/utility/serialization.hpp>
@ -29,6 +28,8 @@ namespace params {
A(4); A(4);
} }
} }
#undef A
#undef R
parameters::_data_t::iterator parameters::find(std::string const& key) { parameters::_data_t::iterator parameters::find(std::string const& key) {
return std::find_if(_data.begin(), _data.end(), [&key](_data_elem const& x) { return x.key == key; }); return std::find_if(_data.begin(), _data.end(), [&key](_data_elem const& x) { return x.key == key; });
@ -76,9 +77,9 @@ namespace params {
void parameters::update(parameters const& other) { void parameters::update(parameters const& other) {
for (auto const& kfd : other._data) { for (auto const& kfd : other._data) {
auto it = find(kfd.key); auto it = find(kfd.key);
if (it !=_data.end()) { if (it !=_data.end()) { // already there
if (it->f.index != kfd.f.index) TRIQS_RUNTIME_ERROR << "Index mismatch in merging parameters"; if (it->f.index != kfd.f.index) TRIQS_RUNTIME_ERROR << "Index mismatch in merging parameters";
if (kfd.doc.size()>0) it->doc = kfd.doc; if (kfd.doc.size() > 0) it->doc = kfd.doc; // update doc iif there is a doc ...
} else } else
_data.push_back(kfd); _data.push_back(kfd);
} }
@ -86,45 +87,73 @@ namespace params {
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
std::vector<std::vector<std::string>> parameters::generate_help() const { std::vector<std::vector<std::string>> parameters::generate_help(bool with_header) const {
std::vector<std::vector<std::string>> str; std::vector<std::vector<std::string>> str;
str.push_back({"parameter:", "type:", "value:", "description:"}); if (with_header) str.push_back({"parameter:", "type:", "value:", "description:"});
for (auto const& s : _data) for (auto const& s : _data)
if (!s.f.is_modified()) str.push_back({s.key, s.f.type_name(), "-", s.doc}); if (s.f.is_modification_required()) str.push_back({s.key, s.f.type_name(), "-", s.doc});
for (auto const& s : _data) { for (auto const& s : _data) {
if (is_parameter(s.f)) continue; // no subgroup if (is_parameter(s.f)) continue; // no subgroup
if (s.f.is_modified()) { if (!s.f.is_modification_required()) {
std::ostringstream val; std::ostringstream val;
val << s.f; val << s.f;
auto sv = val.str(); auto sv = val.str();
boost::algorithm::trim(sv); boost::algorithm::trim(sv);
int size_max = 30; int size_max = 30;
std::replace( sv.begin(), sv.end(), '\n', ','); // replace all '\n' with ' '
if (sv.size()>size_max) { if (sv.size()>size_max) {
auto s2 = std::string(size_max+5,'.'); auto s2 = std::string(size_max+5,'.');
std::copy(sv.begin(), sv.begin()+size_max/2, s2.begin()); std::copy(sv.begin(), sv.begin()+size_max/2, s2.begin());
std::copy(sv.end()-size_max/2, sv.end(), s2.end()-size_max/2); std::copy(sv.end()-size_max/2, sv.end(), s2.end()-size_max/2);
sv = s2; sv = s2;
} }
std::replace( sv.begin(), sv.end(), '\n', ','); // replace all 'x' to 'y'
str.push_back({s.key, s.f.type_name(), sv, s.doc}); str.push_back({s.key, s.f.type_name(), sv, s.doc});
} }
} }
// all sub groups after // all sub groups after
for (auto const& s : _data) { for (auto const& s : _data) {
if (!is_parameter(s.f)) continue; if (!is_parameter(s.f)) continue;
auto p = dynamic_cast<_field::_data_impl<parameters>*>(s.f.p.get()); auto* p = dynamic_cast<_field::_data_impl<parameters>*>(s.f.p.get());
auto str2 = p->x.generate_help(); auto str2 = p->x.generate_help(false);
for (auto & x : str2) x[0] = " " + x[0]; for (auto & x : str2) x[0] = " " + x[0];
str2.insert(str2.begin(), {"Sub-group : ", s.key, " ", ""}); str2.insert(str2.begin(), {"Sub-group : ", s.key, " ", ""});
str.insert(str.end(), str2.begin(), str2.end()); str.insert(str.end(), str2.begin(), str2.end());
} }
return str; return str;
} }
//-----------------------------------------------------------------------
//
std::string print_formatted(std::vector<std::vector<std::string>> const &out){
std::vector<std::size_t> max_len;
std::ostringstream str;
for(auto const &s : out){
max_len.resize(out[0].size(),0);
for(std::size_t i=0; i<s.size(); ++i)
if(s[i].length()>max_len[i]) max_len[i]=s[i].length();
}
for(auto const &s : out){
for(std::size_t i=0; i<s.size(); ++i)
str << std::left << std::setw(max_len[i]+2) << s[i];
str << std::endl;
}
return str.str();
}
//-----------------------------------------------------------------------
/// help as a string
std::string parameters::help() const {
return print_formatted(generate_help());
}
//-----------------------------------------------------------------------
std::ostream& operator<<(std::ostream& out, parameters const& p) { std::ostream& operator<<(std::ostream& out, parameters const& p) {
out << utility::print_formatted(p.generate_help()); return out << p.help();
return out;
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -24,15 +24,23 @@
namespace triqs { namespace triqs {
namespace params { namespace params {
/// For add_field method of parameters.
template<typename T> struct no_default {}; template<typename T> struct no_default {};
/** /**
* Class for storing program parameters. * DOC to be written.
* Parameters can be added to and extracted from the parameter object using the element access operator []. *
* Each element is stored by means of an object of type _field, which also stores the original type (all * Provide a form for program parameters.
* integral types are collapsed to long and char* is collapsed to std::string). *
* When accessing elements, a typecheck is performed. Typecasts are allowed and are similar to the C++ rules * Fields of the form can be added with add_field method (only in C++), with a name, a type, a doc, and optionally a default value.
* for casts. If the lvalue has type double, a cast from any integral type is allowed. If the lvalue has *
* arithmetic type, a boost::lexical_cast is attempted. * They can be retrieved from and put into the form, using the [] operator, in C++ and python.
*
* The data is stored with a strict type checking at runtime, with some type collapse (all integers into a long,
* all string, const char * into a std::string, etc).
*
* Basic type cast to basic types are provided.
*
* The class is boost-serializable and implements hdf5 I/O operations. * The class is boost-serializable and implements hdf5 I/O operations.
*/ */
class parameters { class parameters {
@ -56,51 +64,70 @@ namespace params {
public: public:
parameters(); parameters();
/// calls can be chained for multiple parameters /// Add a field with a default value
template <typename T> parameters& add_field(std::string const& key, T&& x, std::string const& doc) { template <typename T> parameters& add_field(std::string const& key, T&& x, std::string const& doc) {
insert(key, _field{std::forward<T>(x),key, false}, doc); insert(key, _field{std::forward<T>(x),key, false}, doc);
return *this; return *this;
} }
// add a field without a default value // Add a field without a default value
template <typename T> parameters& add_field(std::string const& key, no_default<T>, std::string const& doc) { template <typename T> parameters& add_field(std::string const& key, no_default<T>, std::string const& doc) {
insert(key, _field{T{}, key, true}, doc); insert(key, _field{T{}, key, true}, doc);
return *this; return *this;
} }
/// Add a subgroup (a field which it itself a parameter).
parameters& add_group(std::string const& key, std::string const& doc) { parameters& add_group(std::string const& key, std::string const& doc) {
insert(key, _field{parameters{}, key, false}, doc); insert(key, _field{parameters{}, key, false}, doc);
return *this; return *this;
} }
/// Sort with the key. By default the list of fields is ordered as in the order of their addition to the parameter.
void sort_by_key(); void sort_by_key();
bool has_key(std::string const& k) const; /// Does the form have the key ?
bool has_key(std::string const& key) const;
/// Access the parameter key, which must be present (or it throws an exception). /**
* Access the parameter key.
* Key must be a valid key or a TRIQS_RUNTIME_ERROR is thrown.
*/
_field& operator[](const char * key); _field& operator[](const char * key);
/**
* Access the parameter key.
* Key must be a valid key or a TRIQS_RUNTIME_ERROR is thrown.
*/
_field const& operator[](const char * key) const; _field const& operator[](const char * key) const;
/// generate help in form of a table of strings containing a list of required and optional parameters /// generate help in form of a table of strings containing a list of required and optional parameters
std::vector<std::vector<std::string>> generate_help() const; std::vector<std::vector<std::string>> generate_help(bool with_header = true) const;
friend std::string get_triqs_hdf5_data_scheme(parameters) { return ""; } /// help as a string
std::string help() const;
/// hdf5
friend std::string get_triqs_hdf5_data_scheme(parameters const &) { return ""; }
friend void h5_write(h5::group F, std::string const& subgroup_name, parameters const& p); friend void h5_write(h5::group F, std::string const& subgroup_name, parameters const& p);
friend void h5_read(h5::group F, std::string const& subgroup_name, parameters& p); friend void h5_read(h5::group F, std::string const& subgroup_name, parameters& p);
/// Ostream
friend std::ostream& operator<<(std::ostream& out, parameters const& p); friend std::ostream& operator<<(std::ostream& out, parameters const& p);
/** /**
* Update with another parameter set. * Update with another parameter set.
* If a key is present in other and not in this, add parameter to this. * If a key is present in other and not in this, add parameter to this.
* If a key is present in both, overwrite parameter in this without any check (Python-like behaviour). * If a key is present in both, overwrite parameter with a strict type checking.
*/ */
void update(parameters const&); void update(parameters const&);
}; };
/// Another form of update, more pythonic
inline parameters operator+(parameters p1, parameters const& p2) { inline parameters operator+(parameters p1, parameters const& p2) {
p1.update(p2); p1.update(p2);
return p1; return p1;
} }
// can only be implemented after complete declaration of parameters // can only be implemented after complete declaration of parameters
template <typename... T> _field& _field::add_field(T&&... x) { template <typename... T> _field& _field::add_field(T&&... x) {
auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get()); auto* pp = dynamic_cast<_data_impl<parameters>*>(p.get());
@ -110,4 +137,9 @@ namespace params {
} }
} }
} }
#ifdef TRIQS_WITH_PYTHON_SUPPORT
// include the generated python converter
#include <pytriqs/converters/parameters.hpp> #include <pytriqs/converters/parameters.hpp>
#endif

View File

@ -0,0 +1,74 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2012 by M. Ferrero, O. Parcollet
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#ifndef TRIQS_SERIAL_H
#define TRIQS_SERIAL_H
#include <triqs/utility/first_include.hpp>
#include <triqs/utility/view_tools.hpp>
//#include <vector>
//#include <string>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/complex.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
namespace triqs {
// serialize obj into an std::string
template <typename T> std::string serialize (T const & obj) {
std::string serial_str;
boost::iostreams::back_insert_device<std::string> inserter(serial_str);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter);
//boost::archive::binary_oarchive oa(s);
boost::archive::text_oarchive oa(s);
oa << obj;
s.flush();
return serial_str;
}
template <typename T> T deserialize (std::string const & serial_str) {
T obj;
// wrap buffer inside a stream and deserialize serial_str into obj
boost::iostreams::basic_array_source<char> device(serial_str.data(), serial_str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s(device);
//boost::archive::binary_iarchive ia(s);
boost::archive::text_iarchive ia(s);
ia >> obj;
return obj;
}
template <typename T> void deserialize_into_view (std::string const & serial_str, T & x) {
typename regular_type_if_exists_else_type<T>::type obj;
// wrap buffer inside a stream and deserialize serial_str into obj
boost::iostreams::basic_array_source<char> device(serial_str.data(), serial_str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s(device);
boost::archive::text_iarchive ia(s);
//boost::archive::binary_iarchive ia(s);
ia >> obj;
x= typename view_type_if_exists_else_type<T>::type (obj);
}
}
#endif

View File

@ -1,54 +0,0 @@
/*******************************************************************************
*
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
*
* Copyright (C) 2013 by H. Hafermann
*
* TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#ifndef TRIQS_FORMATTED_OUTPUT_H
#define TRIQS_FORMATTED_OUTPUT_H
#include<string>
#include<vector>
#include<iostream>
#include<iomanip>
#include<sstream>
namespace triqs { namespace utility {
inline std::string print_formatted(std::vector<std::vector<std::string>> const &out){
std::vector<std::size_t> max_len;
std::ostringstream str;
for(auto const &s : out){
max_len.resize(out[0].size(),0);
for(std::size_t i=0; i<s.size(); ++i)
if(s[i].length()>max_len[i]) max_len[i]=s[i].length();
}
for(auto const &s : out){
for(std::size_t i=0; i<s.size(); ++i)
str << std::left << std::setw(max_len[i]+2) << s[i];
str << std::endl;
}
return str.str();
}
}}
#endif

View File

@ -2,7 +2,7 @@
* *
* TRIQS: a Toolbox for Research in Interacting Quantum Systems * TRIQS: a Toolbox for Research in Interacting Quantum Systems
* *
* Copyright (C) 2012 by M. Ferrero, O. Parcollet * Copyright (C) 2014 by O. Parcollet
* *
* TRIQS is free software: you can redistribute it and/or modify it under the * TRIQS is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software * terms of the GNU General Public License as published by the Free Software
@ -18,57 +18,18 @@
* TRIQS. If not, see <http://www.gnu.org/licenses/>. * TRIQS. If not, see <http://www.gnu.org/licenses/>.
* *
******************************************************************************/ ******************************************************************************/
#ifndef TRIQS_SERIAL_H #pragma once
#define TRIQS_SERIAL_H
#include <triqs/utility/first_include.hpp> #if defined(TRIQS_SERIALIZATION_USE_BOOST) or not H5_VERSION_GE(1,8,9)
#include <triqs/utility/view_tools.hpp> #include "./boost_serialization.hpp"
//#include <vector>
//#include <string> #define TRIQS_SERIALIZATION_WITH_BOOST
#include <boost/iostreams/stream.hpp> #else
#include <boost/iostreams/device/back_inserter.hpp> #include "../h5/serialization.hpp"
#include <boost/archive/binary_oarchive.hpp> #define TRIQS_SERIALIZATION_WITH_HDF5
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/complex.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
namespace triqs { namespace triqs {
using h5::serialize;
// serialize obj into an std::string using h5::deserialize;
template <typename T> std::string serialize (T const & obj) {
std::string serial_str;
boost::iostreams::back_insert_device<std::string> inserter(serial_str);
boost::iostreams::stream<boost::iostreams::back_insert_device<std::string> > s(inserter);
//boost::archive::binary_oarchive oa(s);
boost::archive::text_oarchive oa(s);
oa << obj;
s.flush();
return serial_str;
}
template <typename T> T deserialize (std::string const & serial_str) {
T obj;
// wrap buffer inside a stream and deserialize serial_str into obj
boost::iostreams::basic_array_source<char> device(serial_str.data(), serial_str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s(device);
//boost::archive::binary_iarchive ia(s);
boost::archive::text_iarchive ia(s);
ia >> obj;
return obj;
}
template <typename T> void deserialize_into_view (std::string const & serial_str, T & x) {
typename regular_type_if_exists_else_type<T>::type obj;
// wrap buffer inside a stream and deserialize serial_str into obj
boost::iostreams::basic_array_source<char> device(serial_str.data(), serial_str.size());
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > s(device);
boost::archive::text_iarchive ia(s);
//boost::archive::binary_iarchive ia(s);
ia >> obj;
x= typename view_type_if_exists_else_type<T>::type (obj);
}
} }
#endif #endif