mirror of
https://github.com/triqs/dft_tools
synced 2024-11-01 03:33:50 +01:00
84 lines
2.8 KiB
C++
84 lines
2.8 KiB
C++
|
#include "./_field.hpp"
|
||
|
#include <boost/lexical_cast.hpp>
|
||
|
|
||
|
namespace triqs {
|
||
|
namespace params {
|
||
|
|
||
|
std::map<std::type_index, std::string> _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 <typename T> T lex_cast_from_string(_field const &obj) {
|
||
|
std::string s = extract<std::string>(obj);
|
||
|
try {
|
||
|
return boost::lexical_cast<T>(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<double>()) return *static_cast<const double *>(obj.get());
|
||
|
if (obj.has_type<std::string>()) {
|
||
|
return lex_cast_from_string<double>(obj);
|
||
|
}
|
||
|
#define TRANSFORM_TYPE(T) \
|
||
|
if (obj.has_type<T>()) return extract<T>(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<long>()) return *static_cast<const long *>(obj.get());
|
||
|
if (obj.has_type<std::string>()) {
|
||
|
return lex_cast_from_string<long>(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<T>(*this); }
|
||
|
BOOST_PP_SEQ_FOR_EACH(CAST_OPERATOR, nil, TRIQS_UTIL_OPAQUE_OBJECT_PREDEFINED_CAST);
|
||
|
#undef CAST_OPERATOR
|
||
|
}
|
||
|
}
|
||
|
|