42 #ifndef TEUCHOS_AS_HPP
43 #define TEUCHOS_AS_HPP
78 #ifdef HAVE_TEUCHOS_COMPLEX
80 #endif // HAVE_TEUCHOS_COMPLEX
82 #ifdef HAVE_TEUCHOS_QD
83 #include <qd/qd_real.h>
84 #include <qd/dd_real.h>
85 #endif // HAVE_TEUCHOS_QD
178 template<
class TypeTo,
class TypeFrom>
286 template<
class TypeTo,
class TypeFrom>
287 inline TypeTo
as(
const TypeFrom& t );
355 template<
class TypeTo,
class TypeFrom>
356 inline TypeTo
asSafe(
const TypeFrom& t );
371 template <
class TypeTo>
376 template <
class TypeFrom>
378 return as<TypeTo>(t);
403 template<
class IntType>
405 intToString (
const std::string& t,
406 IntType (*rawConvert) (
const char*,
char**,
int),
407 const char* intTypeName)
425 const char* t_ptr = t.c_str ();
433 const IntType val = rawConvert (t_ptr, &endptr, 10);
435 const IntType minVal = std::numeric_limits<IntType>::min ();
436 const IntType maxVal = std::numeric_limits<IntType>::max ();
438 errno == ERANGE && (val == minVal || val == maxVal),
440 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
441 "The integer value in the given string \"" << t <<
"\" overflows " << intTypeName <<
".");
443 errno != 0 && val == 0,
444 std::invalid_argument,
445 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
446 "The conversion function was unable to convert the given string \"" << t <<
"\" to " << intTypeName <<
".");
449 std::invalid_argument,
450 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
451 "The conversion function was unable to read any integer digits from the given string "
467 template<
class OutputRealType,
class InputRealType>
469 realToReal (
const InputRealType& x,
const bool doBoundsChecking)
473 if (doBoundsChecking) {
478 const OutputRealType minVal = -std::numeric_limits<OutputRealType>::max ();
479 const OutputRealType maxVal = std::numeric_limits<OutputRealType>::max ();
485 x < as<InputRealType>(minVal) || x > as<InputRealType>(maxVal),
487 "realToReal<" << TypeNameTraits<OutputRealType>::name () <<
", "
488 << TypeNameTraits<InputRealType>::name () <<
">: "
489 "Input value x = " << x <<
" is out of the valid range [" << minVal
490 <<
", " << maxVal <<
"] for conversion to the output type.");
497 return as<OutputRealType> (x);
525 template<
class RealType>
527 stringToReal (
const std::string& t,
528 RealType (*rawConvert) (
const char*,
char**),
529 const char* realTypeName)
531 if (rawConvert == NULL) {
532 std::istringstream in (t);
542 const char* t_ptr = t.c_str ();
550 const RealType val = rawConvert (t_ptr, &endptr);
553 errno == ERANGE && (val != 0),
555 "Teuchos::ValueTypeConversionTraits<" << realTypeName
556 <<
", std::string>::convert: "
557 "The value in the given string \"" << t <<
"\" overflows "
558 << realTypeName <<
".");
563 errno == ERANGE && val == 0,
564 std::invalid_argument,
565 "Teuchos::ValueTypeConversionTraits<" << realTypeName
566 <<
", std::string>::convert: "
567 "The value in the given string \"" << t <<
"\" underflows "
568 << realTypeName <<
".");
571 std::invalid_argument,
572 "Teuchos::ValueTypeConversionTraits<" << realTypeName
573 <<
", std::string>::convert: "
574 "The conversion function was unable to read any floating-point data "
575 "from the given string \"" << t <<
"\".");
604 template<
class OutType>
611 static OutType
convert (
const std::string& t) {
612 std::istringstream in (t);
628 static double convert (
const std::string& t) {
629 return stringToReal<double> (t, &strtod,
"double");
633 return stringToReal<double> (t, &strtod,
"double");
642 #ifdef _ISOC99_SOURCE
643 return stringToReal<float> (t, &strtof,
"float");
647 const double d = stringToReal<double> (t, &strtod,
"double");
648 return realToReal<float, double> (d,
false);
649 #endif // _ISOC99_SOURCE
653 #ifdef _ISOC99_SOURCE
654 return stringToReal<float> (t, &strtof,
"float");
658 const double d = stringToReal<double> (t, &strtod,
"double");
659 return realToReal<float, double> (d,
true);
660 #endif // _ISOC99_SOURCE
668 static long double convert (
const std::string& t) {
669 #ifdef _ISOC99_SOURCE
670 return stringToReal<long double> (t, &strtold,
"long double");
674 return stringToReal<long double> (t, NULL,
"long double");
675 #endif // _ISOC99_SOURCE
700 #if defined(_MSC_VER)
706 return intToString<long long> (t, &_strtoi64,
"long long");
708 return intToString<long long> (t, &strtoll,
"long long");
709 #endif // defined(_MSC_VER)
713 static long long convert (
const std::string& t) {
731 #if defined(_MSC_VER)
736 const char intTypeName[] =
"unsigned long long";
737 std::istringstream istr (t);
738 unsigned long long i = 0;
741 ! istr, std::invalid_argument,
742 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::"
743 "convert: Unable to convert the given string \"" << t <<
"\" to " <<
744 intTypeName <<
". Windows lacks strtoull(), so we had to resort to a "
745 "fall-back conversion. The fall-back method does not know how to test "
749 return intToString<unsigned long long> (t, &strtoull,
"unsigned long long");
750 #endif // defined(_MSC_VER)
754 static unsigned long long convert (
const std::string& t) {
772 return intToString<long> (t, &strtol,
"long");
794 return intToString<unsigned long> (t, &strtoul,
"unsigned long");
798 static unsigned long convert (
const std::string& t) {
805 #ifdef HAVE_TEUCHOS___INT64
810 class ValueTypeConversionTraits<unsigned __int64, std::string> {
817 static unsigned __int64
safeConvert(
const std::string& t) {
818 unsigned __int64 output;
819 std::istringstream stream(t);
825 static unsigned __int64
convert(
const std::string& t) {
826 unsigned __int64 output;
827 std::istringstream stream(t);
832 #endif // HAVE_TEUCHOS___INT64
846 }
catch (std::range_error&) {
850 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
851 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into int.");
852 }
catch (std::invalid_argument& e) {
855 std::invalid_argument,
856 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
857 "Intermediate conversion from std::string to long failed, with the following error message: "
870 return asSafe<int> (safeConvertToLong (t));
875 return as<int> (safeConvertToLong (t));
888 unsigned long val = 0;
890 val = as<unsigned long> (t);
891 }
catch (std::range_error&) {
895 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
896 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
897 }
catch (std::invalid_argument& e) {
900 std::invalid_argument,
901 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
902 "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
915 return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
919 static unsigned int convert (
const std::string& t) {
920 return as<unsigned int> (safeConvertToUnsignedLong (t));
936 }
catch (std::range_error&) {
940 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
941 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into short.");
942 }
catch (std::invalid_argument& e) {
945 std::invalid_argument,
946 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
947 "Intermediate conversion from std::string to long failed, with the following error message: "
960 return asSafe<short> (safeConvertToLong (t));
965 return as<short> (safeConvertToLong (t));
978 unsigned long val = 0;
980 val = as<unsigned long> (t);
981 }
catch (std::range_error&) {
985 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
986 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
987 }
catch (std::invalid_argument& e) {
990 std::invalid_argument,
991 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
992 "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
1005 return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
1009 static unsigned short convert (
const std::string& t) {
1010 return as<unsigned short> (safeConvertToUnsignedLong (t));
1032 const float minVal = -std::numeric_limits<float>::max ();
1033 const float maxVal = std::numeric_limits<float>::max ();
1039 t < minVal || t > maxVal,
1041 "Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: "
1042 "Input double t = " << t <<
" is out of the valid range [" << minVal
1043 <<
", " << maxVal <<
"] for conversion to float.");
1046 return static_cast<float> (t);
1050 return static_cast<float> (t);
1068 const float minVal = -std::numeric_limits<float>::max ();
1069 const float maxVal = std::numeric_limits<float>::max ();
1075 t < minVal || t > maxVal,
1077 "Teuchos::ValueTypeConversionTraits<float, long double>::safeConvert: "
1078 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1079 <<
", " << maxVal <<
"] for conversion to float.");
1082 return static_cast<float> (t);
1086 return static_cast<float> (t);
1104 const double minVal = -std::numeric_limits<double>::max ();
1105 const double maxVal = std::numeric_limits<double>::max ();
1111 t < minVal || t > maxVal,
1113 "Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: "
1114 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1115 <<
", " << maxVal <<
"] for conversion to double.");
1118 return static_cast<double> (t);
1122 return static_cast<double> (t);
1145 return static_cast<short> (t);
1150 const short minVal = std::numeric_limits<short>::min ();
1151 const short maxVal = std::numeric_limits<short>::max ();
1168 if (
sizeof (
short) <
sizeof (
double)) {
1170 t < minVal || t > maxVal,
1172 "Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: "
1173 "Input double t = " << t <<
" is out of the valid range [" << minVal
1174 <<
", " << maxVal <<
"] for conversion to short.");
1176 return static_cast<short> (t);
1189 return static_cast<unsigned short> (t);
1194 const unsigned short minVal = 0;
1195 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1198 t < minVal || t > maxVal,
1200 "Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: "
1201 "Input double t = " << t <<
" is out of the valid range [" << minVal
1202 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1204 return static_cast<unsigned short> (t);
1221 return static_cast<int> (t);
1226 const int minVal = std::numeric_limits<int>::min ();
1227 const int maxVal = std::numeric_limits<int>::max ();
1240 if (
sizeof (
int) <
sizeof (
double)) {
1242 t < minVal || t > maxVal,
1244 "Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: "
1245 "Input double t = " << t <<
" is out of the valid range [" << minVal
1246 <<
", " << maxVal <<
"] for conversion to int.");
1248 return static_cast<int> (t);
1261 return static_cast<unsigned int> (t);
1266 const unsigned int minVal = 0;
1267 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1270 t < minVal || t > maxVal,
1272 "Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: "
1273 "Input double t = " << t <<
" is out of the valid range [" << minVal
1274 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1276 return static_cast<unsigned int> (t);
1289 return static_cast<long> (t);
1294 const long minVal = std::numeric_limits<long>::min ();
1295 const long maxVal = std::numeric_limits<long>::max ();
1312 if (
sizeof (
long) <
sizeof (
double)) {
1314 t < minVal || t > maxVal,
1316 "Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: "
1317 "Input double t = " << t <<
" is out of the valid range [" << minVal
1318 <<
", " << maxVal <<
"] for conversion to long.");
1320 return static_cast<long> (t);
1333 return static_cast<unsigned long> (t);
1338 const unsigned long minVal = 0;
1339 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1342 t < minVal || t > maxVal,
1344 "Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: "
1345 "Input double t = " << t <<
" is out of the valid range [" << minVal
1346 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1348 return static_cast<unsigned long> (t);
1360 return static_cast<long long> (t);
1375 return static_cast<long long> (t);
1385 static unsigned long long convert (
const double t) {
1388 return static_cast<unsigned long long> (t);
1393 const unsigned long long minVal = 0;
1394 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1397 t < minVal || t > maxVal,
1399 "Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: "
1400 "Input double t = " << t <<
" is out of the valid range [" << minVal
1401 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1403 return static_cast<unsigned long long> (t);
1420 return static_cast<short> (t);
1425 const short minVal = std::numeric_limits<short>::min ();
1426 const short maxVal = std::numeric_limits<short>::max ();
1445 if (
sizeof (
short) <
sizeof (
float)) {
1447 t < minVal || t > maxVal,
1449 "Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: "
1450 "Input float t = " << t <<
" is out of the valid range [" << minVal
1451 <<
", " << maxVal <<
"] for conversion to short.");
1454 return static_cast<short> (t);
1467 return static_cast<unsigned short> (t);
1472 const unsigned short minVal = 0;
1473 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1476 t < minVal || t > maxVal,
1478 "Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: "
1479 "Input float t = " << t <<
" is out of the valid range [" << minVal
1480 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1482 return static_cast<unsigned short> (t);
1497 return static_cast<int> (t);
1502 const int minVal = std::numeric_limits<int>::min ();
1503 const int maxVal = std::numeric_limits<int>::max ();
1517 if (
sizeof (
int) <
sizeof (
float)) {
1519 t < minVal || t > maxVal,
1521 "Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: "
1522 "Input float t = " << t <<
" is out of the valid range ["
1523 << minVal <<
", " << maxVal <<
"] for conversion to int.");
1525 return static_cast<int> (t);
1538 return static_cast<unsigned int> (t);
1543 const unsigned int minVal = 0;
1544 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1547 t < minVal || t > maxVal,
1549 "Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: "
1550 "Input double t = " << t <<
" is out of the valid range [" << minVal
1551 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1553 return static_cast<unsigned int> (t);
1568 return static_cast<long> (t);
1573 const long minVal = std::numeric_limits<long>::min ();
1574 const long maxVal = std::numeric_limits<long>::max ();
1594 if (
sizeof (
long) <
sizeof (
float)) {
1596 t < minVal || t > maxVal,
1598 "Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: "
1599 "Input float t = " << t <<
" is out of the valid range ["
1600 << minVal <<
", " << maxVal <<
"] for conversion to long.");
1602 return static_cast<long> (t);
1615 return static_cast<unsigned long> (t);
1620 const unsigned long minVal = 0;
1621 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1624 t < minVal || t > maxVal,
1626 "Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: "
1627 <<
"Input float t = " << t <<
" is out of the valid range [" << minVal
1628 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1630 return static_cast<unsigned long> (t);
1640 return static_cast<long long> (t);
1647 return static_cast<long long> (t);
1657 static unsigned long long convert (
const float t) {
1658 return static_cast<unsigned long long> (t);
1663 const unsigned long long minVal = 0;
1664 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1667 t < minVal || t > maxVal,
1669 "Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: "
1670 "Input float t = " << t <<
" is out of the valid range [" << minVal
1671 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1673 return static_cast<unsigned long long> (t);
1687 template<
class SignedIntType,
class Un
signedIntType>
1688 class UnsignedToSignedValueTypeConversionTraits {
1697 static SignedIntType convert (
const UnsignedIntType t) {
1700 return static_cast<SignedIntType
> (t);
1704 static SignedIntType safeConvert (
const UnsignedIntType t) {
1706 const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
1711 const SignedIntType signedVal =
static_cast<SignedIntType
> (t);
1715 "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<SignedIntType>::name ()
1716 <<
", " << TypeNameTraits<UnsignedIntType>::name () <<
">::safeConvert: "
1717 "Input " << TypeNameTraits<UnsignedIntType>::name () <<
" t = " << t
1718 <<
" is out of the valid range [0, " <<
", " << maxSigned
1719 <<
"] for conversion to " << TypeNameTraits<SignedIntType>::name () <<
".");
1727 template<
class Un
signedIntType,
class SignedIntType>
1728 class SignedToUnsignedValueTypeConversionTraits {
1731 static UnsignedIntType convert (
const SignedIntType t) {
1734 return static_cast<UnsignedIntType
> (t);
1738 static UnsignedIntType safeConvert (
const SignedIntType t) {
1745 t < static_cast<SignedIntType> (0),
1747 "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<UnsignedIntType>::name ()
1748 <<
", " << TypeNameTraits<SignedIntType>::name () <<
">::safeConvert: "
1749 "Input " << TypeNameTraits<SignedIntType>::name () <<
" t = " << t
1750 <<
" is negative, so it cannot be correctly converted to the unsigned type "
1751 << TypeNameTraits<UnsignedIntType>::name () <<
".");
1753 return static_cast<UnsignedIntType
> (t);
1765 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1769 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1779 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1783 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1793 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1797 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1807 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1811 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1821 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1825 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1835 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
1839 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
1848 static long long convert (
const unsigned long long t) {
1849 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
1853 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
1862 static unsigned long long convert (
const long long t) {
1863 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
1867 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
1888 return static_cast<short> (t);
1893 const short minShort = std::numeric_limits<short>::min ();
1894 const short maxShort = std::numeric_limits<short>::max ();
1899 t < static_cast<int> (minShort) ||
1900 t > static_cast<int> (maxShort),
1902 "Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: "
1903 "Input int t = " << t <<
" is out of the valid range [" << minShort
1904 <<
", " << maxShort <<
"] for conversion to short.");
1906 return static_cast<short> (t);
1923 return static_cast<short> (t);
1928 const short minShort = std::numeric_limits<short>::min ();
1929 const short maxShort = std::numeric_limits<short>::max ();
1934 t < static_cast<long> (minShort) ||
1935 t > static_cast<long> (maxShort),
1937 "Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: "
1938 "Input long t = " << t <<
" is out of the valid range [" << minShort
1939 <<
", " << maxShort <<
"] for conversion to short.");
1941 return static_cast<short> (t);
1958 return static_cast<int> (t);
1963 const int minInt = std::numeric_limits<int>::min ();
1964 const int maxInt = std::numeric_limits<int>::max ();
1969 t < static_cast<long> (minInt) ||
1970 t > static_cast<long> (maxInt),
1972 "Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: "
1973 "Input long t = " << t <<
" is out of the valid range [" << minInt
1974 <<
", " << maxInt <<
"] for conversion to int.");
1978 return static_cast<int> (t);
1996 return static_cast<int> (t);
2001 const int minInt = std::numeric_limits<int>::min ();
2002 const int maxInt = std::numeric_limits<int>::max ();
2016 #if INT_MAX == LONG_MAX
2022 static_cast<int> (t) < static_cast<int> (0),
2024 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
2025 "Input unsigned long t = " << t <<
" is out of the valid range ["
2026 << minInt <<
", " << maxInt <<
"] for conversion to int.");
2027 #else // INT_MAX < LONG_MAX
2031 t > static_cast<unsigned long> (maxInt),
2033 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
2034 "Input unsigned long t = " << t <<
" is out of the valid range ["
2035 << minInt <<
", " << maxInt <<
"] for conversion to int. An unchecked "
2036 "cast would have resulted in " << static_cast<int> (t) <<
".");
2037 #endif // INT_MAX == LONG_MAX
2041 return static_cast<int> (t);
2060 return static_cast<long> (t);
2077 #if UINT_MAX == LONG_MAX
2078 const long minLong = std::numeric_limits<long>::min ();
2079 const long maxLong = std::numeric_limits<long>::max ();
2087 static_cast<long> (t) < static_cast<long> (0),
2089 "Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: "
2090 "Input unsigned int t = " << t <<
" is out of the valid range ["
2091 << minLong <<
", " << maxLong <<
"] for conversion to long.");
2092 #endif // UINT_MAX == LONG_MAX
2094 return static_cast<long> (t);
2112 return static_cast<unsigned int> (t);
2124 if (
sizeof (
unsigned int) <
sizeof (
long)) {
2125 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2128 t < static_cast<long> (0) || t > static_cast<long> (maxInt),
2130 "Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: "
2131 "Input long t = " << t <<
" is out of the valid range [0, "
2132 << maxInt <<
"] for conversion to unsigned int.");
2136 return static_cast<unsigned int> (t);
2151 static unsigned int convert (
const unsigned long t) {
2154 return static_cast<unsigned int> (t);
2159 const unsigned int minInt = 0;
2160 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2164 t > static_cast<unsigned long> (maxInt),
2166 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: "
2167 "Input unsigned long t = " << t <<
" is out of the valid range [" << minInt
2168 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2172 return static_cast<unsigned int> (t);
2186 static unsigned short convert (
const unsigned long t) {
2189 return static_cast<unsigned short> (t);
2194 const unsigned short minShort = 0;
2195 const unsigned short maxShort = std::numeric_limits<unsigned short>::max ();
2199 t > static_cast<unsigned long> (maxShort),
2201 "Teuchos::ValueTypeConversionTraits<unsigned short, unsigned long>::safeConvert: "
2202 "Input unsigned long t = " << t <<
" is out of the valid range [" << minShort
2203 <<
", " << maxShort <<
"] for conversion to unsigned short.");
2207 return static_cast<unsigned short> (t);
2223 return static_cast<int> (t);
2228 const int minInt = std::numeric_limits<int>::min ();
2229 const int maxInt = std::numeric_limits<int>::max ();
2232 t < static_cast<long long> (minInt) ||
2233 t > static_cast<long long> (maxInt),
2235 "Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: "
2236 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2237 <<
", " << maxInt <<
"] for conversion to int.");
2241 return static_cast<int> (t);
2259 return static_cast<unsigned int> (t);
2264 const unsigned int minInt = 0;
2265 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2268 t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
2270 "Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: "
2271 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2272 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2276 return static_cast<unsigned int> (t);
2293 return static_cast<int> (t);
2298 const int minInt = std::numeric_limits<int>::min ();
2299 const int maxInt = std::numeric_limits<int>::max ();
2303 t > static_cast<unsigned long long> (maxInt),
2304 std::invalid_argument,
2305 "Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: "
2306 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2307 <<
", " << maxInt <<
"] for conversion to int.");
2311 return static_cast<int> (t);
2326 static unsigned int convert (
const unsigned long long t) {
2329 return static_cast<unsigned int> (t);
2334 const unsigned int minInt = 0;
2335 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2339 t > static_cast<unsigned long long> (maxInt),
2340 std::invalid_argument,
2341 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: "
2342 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2343 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2347 return static_cast<unsigned int> (t);
2368 return static_cast<float> (t);
2378 const float minFloat = -std::numeric_limits<float>::max ();
2379 const float maxFloat = std::numeric_limits<float>::max ();
2391 t < minFloat || t > maxFloat,
2393 "Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: "
2394 "Input long long t = " << t <<
" is out of the valid range [" << minFloat
2395 <<
", " << maxFloat <<
"] for conversion to float.");
2399 return static_cast<float> (t);
2413 static float convert (
const unsigned long long t) {
2416 return static_cast<float> (t);
2426 const float minFloat = -std::numeric_limits<float>::max ();
2427 const float maxFloat = std::numeric_limits<float>::max ();
2436 std::invalid_argument,
2437 "Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: "
2438 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minFloat
2439 <<
", " << maxFloat <<
"] for conversion to float.");
2443 return static_cast<float> (t);
2456 {
return std::string(t); }
2458 {
return std::string(t); }
2465 #ifdef HAVE_TEUCHOS_COMPLEX
2468 template<
class RealType>
2469 class ValueTypeConversionTraits<std::complex<RealType>, short> {
2471 inline static std::complex<RealType>
convert (
const short t) {
2473 return std::complex<RealType> (t, as<RealType> (0));
2475 static std::complex<RealType>
safeConvert (
const short t) {
2477 return std::complex<RealType> (t, asSafe<RealType> (0));
2482 template<
class RealType>
2483 class ValueTypeConversionTraits<std::complex<RealType>, unsigned short> {
2485 inline static std::complex<RealType>
convert (
const unsigned short t) {
2487 return std::complex<RealType> (t, as<RealType> (0));
2489 static std::complex<RealType>
safeConvert (
const unsigned short t) {
2491 return std::complex<RealType> (t, asSafe<RealType> (0));
2496 template<
class RealType>
2497 class ValueTypeConversionTraits<std::complex<RealType>, int> {
2499 inline static std::complex<RealType>
convert (
const int t) {
2501 return std::complex<RealType> (t, as<RealType> (0));
2503 static std::complex<RealType>
safeConvert (
const int t) {
2505 return std::complex<RealType> (t, asSafe<RealType> (0));
2510 template<
class RealType>
2511 class ValueTypeConversionTraits<std::complex<RealType>, unsigned int> {
2513 inline static std::complex<RealType>
convert (
const unsigned int t) {
2515 return std::complex<RealType> (t, as<RealType> (0));
2517 static std::complex<RealType>
safeConvert (
const unsigned int t) {
2519 return std::complex<RealType> (t, asSafe<RealType> (0));
2524 template<
class RealType>
2525 class ValueTypeConversionTraits<std::complex<RealType>, long> {
2527 inline static std::complex<RealType>
convert (
const long t) {
2529 return std::complex<RealType> (t, as<RealType> (0));
2531 static std::complex<RealType>
safeConvert (
const long t) {
2533 return std::complex<RealType> (t, asSafe<RealType> (0));
2538 template<
class RealType>
2539 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long> {
2541 inline static std::complex<RealType>
convert (
const unsigned long t) {
2543 return std::complex<RealType> (t, as<RealType> (0));
2545 static std::complex<RealType>
safeConvert (
const unsigned long t) {
2547 return std::complex<RealType> (t, asSafe<RealType> (0));
2552 template<
class RealType>
2553 class ValueTypeConversionTraits<std::complex<RealType>, long long> {
2555 inline static std::complex<RealType>
convert (
const long long t) {
2557 return std::complex<RealType> (t, as<RealType> (0));
2559 static std::complex<RealType>
safeConvert (
const long long t) {
2561 return std::complex<RealType> (t, asSafe<RealType> (0));
2566 template<
class RealType>
2567 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long long> {
2569 inline static std::complex<RealType>
convert (
const unsigned long long t) {
2571 return std::complex<RealType> (t, as<RealType> (0));
2573 static std::complex<RealType>
safeConvert (
const unsigned long long t) {
2575 return std::complex<RealType> (t, asSafe<RealType> (0));
2579 #endif // HAVE_TEUCHOS_COMPLEX
2585 #ifdef HAVE_TEUCHOS_QD
2589 class ValueTypeConversionTraits<double, qd_real> {
2591 inline static double convert (
const qd_real t) {
2592 return to_double (t);
2600 const qd_real minVal = -std::numeric_limits<double>::max ();
2601 const qd_real maxVal = std::numeric_limits<double>::max ();
2604 t < minVal || t > maxVal,
2606 "Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: "
2607 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2608 <<
", " << maxVal <<
"] for conversion to double.");
2610 return to_double (t);
2616 class ValueTypeConversionTraits<float, qd_real> {
2618 inline static float convert (
const qd_real t) {
2621 return as<float> (to_double (t));
2633 const qd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2634 const qd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2637 t < minVal || t > maxVal,
2639 "Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: "
2640 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2641 <<
", " << maxVal <<
"] for conversion to float.");
2644 return asSafe<float> (to_double (t));
2650 class ValueTypeConversionTraits<int, qd_real> {
2652 inline static int convert (
const qd_real t) {
2657 const qd_real minVal = std::numeric_limits<int>::min ();
2658 const qd_real maxVal = std::numeric_limits<int>::max ();
2661 t < minVal || t > maxVal,
2663 "Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: "
2664 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2665 <<
", " << maxVal <<
"] for conversion to int.");
2672 class ValueTypeConversionTraits<dd_real, qd_real> {
2674 inline static dd_real
convert (
const qd_real t) {
2675 return to_dd_real(t);
2687 const qd_real minVal = -std::numeric_limits<dd_real>::max ();
2688 const qd_real maxVal = std::numeric_limits<dd_real>::max ();
2691 t < minVal || t > maxVal,
2693 "Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: "
2694 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2695 <<
", " << maxVal <<
"] for conversion to dd_real.");
2697 return to_dd_real (t);
2703 class ValueTypeConversionTraits<double, dd_real> {
2705 inline static double convert (
const dd_real t) {
2706 return to_double (t);
2716 const dd_real minVal = -std::numeric_limits<double>::max ();
2717 const dd_real maxVal = std::numeric_limits<double>::max ();
2720 t < minVal || t > maxVal,
2722 "Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: "
2723 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2724 <<
", " << maxVal <<
"] for conversion to double.");
2726 return to_double (t);
2732 class ValueTypeConversionTraits<float, dd_real> {
2734 inline static float convert (
const dd_real t) {
2736 return as<float> (to_double (t));
2747 const dd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2748 const dd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2751 t < minVal || t > maxVal,
2753 "Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: "
2754 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2755 <<
", " << maxVal <<
"] for conversion to float.");
2758 return as<float> (to_double (t));
2764 class ValueTypeConversionTraits<int, dd_real> {
2766 inline static int convert (
const dd_real t) {
2771 const dd_real minVal = std::numeric_limits<int>::min ();
2772 const dd_real maxVal = std::numeric_limits<int>::max ();
2775 t < minVal || t > maxVal,
2777 "Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: "
2778 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2779 <<
", " << maxVal <<
"] for conversion to int.");
2786 class ValueTypeConversionTraits<qd_real, long unsigned int> {
2788 inline static qd_real
convert(
const long unsigned int t ) {
2798 inline static qd_real
safeConvert(
const long unsigned int t )
2804 class ValueTypeConversionTraits<dd_real, long unsigned int> {
2806 inline static dd_real
convert(
const long unsigned int t ) {
2816 inline static dd_real
safeConvert(
const long unsigned int t )
2820 #endif // HAVE_TEUCHOS_QD
2824 template<
class TypeTo,
class TypeFrom>
2825 inline TypeTo
as(
const TypeFrom& t )
2827 #ifdef HAVE_TEUCHOS_DEBUG
2831 #endif // HAVE_TEUCHOS_DEBUG
2834 template<
class TypeTo,
class TypeFrom>
2843 #endif // TEUCHOS_AS_HPP
static short convert(const int t)
Convert the given int to a short.
static unsigned int convert(const int t)
static unsigned int convert(const double t)
Convert the given double to an unsigned int.
static int safeConvert(const unsigned long t)
Convert from unsigned long to int, checking for overflow first.
static unsigned int convert(const float t)
Convert the given float to an unsigned int.
static unsigned long safeConvert(const float t)
Convert the given float to an unsigned long, checking first or under- or overflow.
static unsigned int convert(const unsigned long long t)
Convert the given unsigned long long to an unsigned int.
static unsigned long long convert(const long long t)
TypeTo operator()(const TypeFrom &t)
static long long safeConvert(const std::string &t)
Convert the given std::string to a long long, with checks.
static short safeConvert(const long t)
Convert from long to short, checking for overflow first.
static short safeConvert(const double t)
Convert the given double to a short, checking for overflow first.
static long long safeConvert(const unsigned long long t)
static OutType safeConvert(const std::string &t)
static float safeConvert(const long long t)
Convert from long long to float, checking for overflow first.
static short safeConvert(const float t)
Convert the given float to a short, checking for overflow first.
static int convert(const unsigned int t)
static int safeConvert(const std::string &t)
Convert the given std::string to an int, with checks.
static long safeConvert(const std::string &t)
Convert the given std::string to a long, with checks.
static long convert(const float t)
Convert the given float to an long.
static int convert(const long long t)
Convert the given long long to an int.
static unsigned short convert(const float t)
Convert the given float to an unsigned short.
TypeTo asSafe(const TypeFrom &t)
Convert from one value type to another, with validity checks if appropriate.
static long convert(const unsigned long t)
static unsigned int safeConvert(const std::string &t)
Convert the given std::string to an unsigned int, with checks.
static short convert(const std::string &t)
Convert the given std::string to a short.
static TypeTo safeConvert(const TypeFrom t)
Convert t from a TypeFrom object to a TypeTo object, with checks for validity.
static long double convert(const std::string &t)
static float safeConvert(const std::string &t)
static int safeConvert(const unsigned long long t)
Convert from unsigned long long to int, checking for overflow first.
static unsigned long safeConvert(const std::string &t)
Convert the given std::string to an unsigned long, with checks.
static unsigned long safeConvertToUnsignedLong(const std::string &t)
Convert the given std::string to an intermediate unsigned long, with checks.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static TypeTo convert(const TypeFrom t)
Convert t from a TypeFrom object to a TypeTo object.
static unsigned int convert(const long long t)
Convert the given long long to an unsigned int.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
static int convert(const std::string &t)
Convert the given std::string to an int.
static int convert(const float t)
Convert the given float to an int.
static long convert(const std::string &t)
Convert the given std::string to a long.
static long convert(const double t)
Convert the given double to long.
static float safeConvert(const double t)
static std::string safeConvert(const char t[])
static unsigned long convert(const std::string &t)
Convert the given std::string to an unsigned long.
static int convert(const long t)
Convert the given long to an int.
static float safeConvert(const unsigned long long t)
Convert from unsigned long long to float, checking for overflow first.
static unsigned int convert(const unsigned long t)
Convert the given unsigned long to an unsigned int.
static short convert(const unsigned short t)
static int convert(const unsigned long long t)
Convert the given unsigned long long to an int.
static unsigned long long safeConvert(const std::string &t)
Convert the given std::string to an unsigned long long, with checks.
static float convert(const double t)
static long safeConvert(const double t)
Convert the given double to long, checking for overflow first.
static double safeConvert(const long double t)
static short safeConvert(const unsigned short t)
static int safeConvert(const double t)
Convert the given double to an int, checking for overflow first.
static unsigned int safeConvert(const long long t)
Convert from long long to unsigned int, checking for overflow first.
static unsigned short safeConvert(const double t)
Convert the given double to an unsigned short, checking for overflow first.
static unsigned short safeConvert(const float t)
Convert the given float to an unsigned short, checking for overflow first.
static unsigned short convert(const unsigned long t)
Convert the given unsigned long to an unsigned short.
static std::string convert(const char t[])
static unsigned short safeConvert(const unsigned long t)
Convert from unsigned long to unsigned short, checking for overflow first.
static short convert(const double t)
Convert the given double to a short.
static long safeConvert(const unsigned int t)
Convert from unsigned int to long, checking for overflow first.
static int safeConvert(const float t)
Convert the given float to an int.
static unsigned int safeConvert(const float t)
Convert the given float to an unsigned int, checking first or under- or overflow. ...
static short convert(const float t)
Convert the given float to a short.
static unsigned int safeConvert(const double t)
Convert the given double to an unsigned int, checking for overflow first.
static unsigned short convert(const std::string &t)
Convert the given std::string to an unsigned short.
static long safeConvertToLong(const std::string &t)
Convert the given std::string to an intermediate long, with checks.
static long safeConvert(const float t)
Convert the given float to an long, checking first for overflow.
Function object wrapper for as().
static unsigned int safeConvert(const long t)
Convert from long to unsigned int, checking for underflow or overflow first.
static int convert(const double t)
Convert the given double to an int.
static unsigned short convert(const short t)
static unsigned long long convert(const double t)
Convert the given double to unsigned long long.
static long long safeConvert(const float t)
Convert the given float to a long long, checking first for overflow.
static unsigned long long convert(const std::string &t)
Convert the given std::string to an unsigned long long.
static unsigned long safeConvertToUnsignedLong(const std::string &t)
Convert the given std::string to an intermediate unsigned long, with checks.
static long long convert(const unsigned long long t)
static long long convert(const std::string &t)
Convert the given std::string to a long long.
static long double safeConvert(const std::string &t)
static long long convert(const double t)
Convert the given double to long long.
static float convert(const long double t)
Default traits class for all conversions between value types.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
static unsigned int convert(const std::string &t)
Convert the given std::string to an unsigned int.
static long safeConvertToLong(const std::string &t)
Convert the given std::string to an intermediate long, with checks.
static short convert(const long t)
Convert the given long to a short.
static unsigned short safeConvert(const std::string &t)
Convert the given std::string to an unsigned short, with checks.
static float convert(const long long t)
Convert the given long long to a float.
static double safeConvert(const std::string &t)
static unsigned int safeConvert(const unsigned long t)
Convert from unsigned long to unsigned int, checking for overflow first.
Default traits class that just returns typeid(T).name().
static unsigned long convert(const long t)
static float convert(const unsigned long long t)
Convert the given unsigned long long to a float.
static int safeConvert(const long long t)
Convert from long long to int, checking for overflow first.
static int convert(const unsigned long t)
Convert the given unsigned long to an int.
static int safeConvert(const long t)
Convert from long to int, checking for overflow first.
static unsigned int safeConvert(const unsigned long long t)
Convert from unsigned long long to unsigned int, checking for overflow first.
static int safeConvert(const unsigned int t)
static unsigned short safeConvert(const short t)
static float convert(const std::string &t)
static unsigned long convert(const double t)
Convert the given double to an unsigned long.
static long convert(const unsigned int t)
Convert the given unsigned int to a long.
static unsigned int safeConvert(const int t)
static long long convert(const float t)
Convert the given float to a long long.
static unsigned long long safeConvert(const long long t)
static unsigned long safeConvert(const long t)
static OutType convert(const std::string &t)
static double convert(const std::string &t)
static unsigned long safeConvert(const double t)
Convert the given double to an unsigned long, checking for overflow first.
static unsigned long long convert(const float t)
Convert the given float to an unsigned long long.
static short safeConvert(const int t)
Convert from int to short, checking for overflow first.
static long long safeConvert(const double t)
Convert the given double to long long, checking for overflow first.
static unsigned short convert(const double t)
Convert the given double to an unsigned short.
static short safeConvert(const std::string &t)
Convert the given std::string to a short, with checks.
static long safeConvert(const unsigned long t)
static unsigned long convert(const float t)
Convert the given float to an unsigned long.
static unsigned long long safeConvert(const double t)
Convert the given double to unsigned long long, checking for overflow first.
static double convert(const long double t)
static float safeConvert(const long double t)
static unsigned long long safeConvert(const float t)
Convert the given float to an unsigned long long, checking first for overflow.
static unsigned int convert(const long t)
Convert the given long to an unsigned int.