7 #ifndef ALPS_UTILITY_CAST_HPP 8 #define ALPS_UTILITY_CAST_HPP 10 #include <alps/config.hpp> 13 #include <boost/bind.hpp> 18 #include <type_traits> 26 bad_cast(std::string
const & arg) :
std::runtime_error(arg) {}
29 template<
typename U,
typename T>
inline U
cast(T
const &);
33 template<
typename U,
typename T>
struct is_cast {
36 static double check(...);
37 enum { value =
sizeof(check(t)) /
sizeof(
char) };
41 typename U,
typename T,
typename X
42 >
inline U cast_generic(T , X) {
44 std::string(
"cannot cast from ")
52 template<
typename U,
typename T>
inline U cast_generic(
53 T arg, std::integral_constant<int, 1>
const&
62 return detail::cast_generic<U, T>(
63 arg, std::integral_constant<int, detail::is_cast<U, T>::value>()
68 #define ALPS_CAST_STRING(T, p, c) \ 69 template<> struct cast_hook<std::string, T > { \ 70 static inline std::string apply( T arg) { \ 72 if (sprintf(buffer, "%" p "" c, arg) < 0) \ 73 throw std::runtime_error( \ 74 "error casting from " #T " to string" + ALPS_STACKTRACE \ 79 template<> struct cast_hook< T, std::string> { \ 80 static inline T apply(std::string arg) { \ 82 if (arg.size() && sscanf(arg.c_str(), "%" c, &value) < 0) \ 83 throw std::runtime_error( \ 84 "error casting from string to " #T ": " \ 85 + arg + ALPS_STACKTRACE \ 101 #undef ALPS_CAST_STRING 103 #define ALPS_CAST_STRING_CHAR(T, U) \ 104 template<> struct cast_hook<std::string, T > { \ 105 static inline std::string apply( T arg) { \ 106 return cast_hook<std::string, U>::apply(arg); \ 109 template<> struct cast_hook<T, std::string> { \ 110 static inline T apply(std::string arg) { \ 111 return cast_hook< U , std::string>::apply(arg); \ 118 #undef ALPS_CAST_STRING_CHAR 120 template<
typename U,
typename T>
struct cast_hook<U,
std::complex<T> > {
121 static inline U
apply(std::complex<T>
const & arg) {
122 return static_cast<U
>(arg.real());
126 template<
typename U,
typename T>
struct cast_hook<
std::complex<U>, T> {
127 static inline std::complex<U>
apply(T
const & arg) {
132 template<
typename U,
typename T>
struct cast_hook<
std::complex<U>, std::complex<T> > {
133 static inline std::complex<U>
apply(std::complex<T>
const & arg) {
134 return std::complex<U>(arg.real(), arg.imag());
139 static inline std::string
apply(std::complex<T>
const & arg) {
140 return cast<std::string>(arg.real()) +
"+" + cast<std::string>(arg.imag()) +
"i";
146 static inline std::complex<T>
apply(std::string
const & arg) {
151 template<
typename U,
typename T>
inline U
cast(T
const & arg) {
155 template<
typename U,
typename T>
inline void cast(
156 U
const * src, U
const * end, T * dest
158 for (U
const * it = src; it != end; ++it)
159 dest[it - src] = cast<T>(*it);
static std::string apply(std::complex< T > const &arg)
#define ALPS_CAST_STRING_CHAR(T, U)
static std::complex< U > apply(std::complex< T > const &arg)
static std::complex< U > apply(T const &arg)
bad_cast(std::string const &arg)
static U apply(std::complex< T > const &arg)
static std::complex< T > apply(std::string const &arg)
#define ALPS_CAST_STRING(T, p, c)