provide nicer type names in error messages

This commit is contained in:
River Tarnell 2008-08-13 16:42:18 +00:00
parent 901b47f58f
commit 0834126abc
2 changed files with 72 additions and 1 deletions

View file

@ -15,6 +15,7 @@
#include <boost/format.hpp>
#include "return_type.h"
#include "type_name.h"
/*
* Various functors used for datums. Unlike the standard functor, these are
@ -88,7 +89,7 @@ struct invalid_binary_operator {
operator() (T, U) const {
std::string s(str(boost::format(
"operator %s cannot be applied to the types ('%s', '%s')")
% opname_ % typeid(T).name() % typeid(U).name()));
% opname_ % type_name<T>::name() % type_name<U>::name()));
throw type_error(s);
}

View file

@ -0,0 +1,70 @@
/*
* Copyright (c) 2008 Andrew Garrett.
* Copyright (c) 2008 River Tarnell <river@wikimedia.org>
* Derived from public domain code contributed by Victor Vasiliev.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely. This software is provided 'as-is', without any express or
* implied warranty.
*/
#ifndef TYPE_NAME_H
#define TYPE_NAME_H
#include <boost/date_time.hpp>
#include <gmpxx.h>
#include "fray.h"
/*
* A helper class to provide nicer names for types than the compiler-generated
* ones, which look like 10__gmp_exprIA1_12__mpz_structS1_E.
*/
namespace afp {
template<typename T>
struct type_name {
static std::string name() {
return typeid(T).name();
}
};
template<>
struct type_name<mpf_class> {
static std::string name() {
return "float";
}
};
template<>
struct type_name<mpz_class> {
static std::string name() {
return "integer";
}
};
template<>
struct type_name<boost::posix_time::ptime> {
static std::string name() {
return "datetime";
}
};
template<>
struct type_name<boost::posix_time::time_duration> {
static std::string name() {
return "time_duration";
}
};
template<>
struct type_name<u32fray> {
static std::string name() {
return "string";
}
};
} // namespace afp
#endif /* !TYPE_NAME_H */