#include "./_field.hpp" #include namespace triqs { namespace params { std::map _field::type_names; std::string _field::type_name(std::type_index i) { try { return type_names.at(i); } catch (...) { return i.name(); } } //----------------------------------------------------------------------- void h5_write(h5::group g, std::string const &name, _field const &obj) { obj.p->h5_write_(g, name); }; //----------------------------------------------------------------------- void h5_read(h5::group g, std::string const &name, _field &obj) { obj.p->h5_read_(g, name); obj.modified = true; }; template T lex_cast_from_string(_field const &obj) { std::string s = extract(obj); try { return boost::lexical_cast(s); } catch (boost::bad_lexical_cast &) { TRIQS_RUNTIME_ERROR << " extraction : can not read the string " << s << " into a " << typeid(T).name(); } } //----------------------------------------------------- template <> // specialize for double since we can make int -> double conversion double extract(_field const &obj) { if (obj.has_type()) return *static_cast(obj.get()); if (obj.has_type()) { return lex_cast_from_string(obj); } #define TRANSFORM_TYPE(T) \ if (obj.has_type()) return extract(obj) TRANSFORM_TYPE(int); // TRANSFORM_TYPE(unsigned int); 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() << ", while I am supposed to extract a double"; } //----------------------------------------------------- template <> // specialize for double since we can make int -> double conversion long extract(_field const &obj) { if (obj.has_type()) return *static_cast(obj.get()); if (obj.has_type()) { return lex_cast_from_string(obj); } TRIQS_RUNTIME_ERROR << "extraction of " << obj.name() << " impossible : type mismatch. Got " << obj.type_name() << ", while I am supposed to extract a double"; } // --------------- _field cast op implementation --------------------------------------- #define CAST_OPERATOR(r, data, T) \ _field::operator T() const { return extract(*this); } BOOST_PP_SEQ_FOR_EACH(CAST_OPERATOR, nil, TRIQS_UTIL_OPAQUE_OBJECT_PREDEFINED_CAST); #undef CAST_OPERATOR } }