42 #ifndef TEUCHOS_AS_HPP 43 #define TEUCHOS_AS_HPP 72 #include "Teuchos_Assert.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>
377 inline TypeTo operator()(
const TypeFrom &t) {
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 < minVal || x > maxVal,
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>
607 static OutType
safeConvert (
const std::string& t) {
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");
641 static float convert (
const std::string& t) {
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 678 static long double safeConvert (
const std::string& t) {
688 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 701 static long long safeConvert (
const std::string& t) {
702 #if defined(_MSC_VER) 708 return intToString<long long> (t, &_strtoi64,
"long long");
710 return intToString<long long> (t, &strtoll,
"long long");
711 #endif // defined(_MSC_VER) 715 static long long convert (
const std::string& t) {
725 class ValueTypeConversionTraits<unsigned long long, std::string> {
732 static unsigned long long safeConvert (
const std::string& t) {
733 #if defined(_MSC_VER) 738 const char intTypeName[] =
"unsigned long long";
739 std::istringstream istr (t);
740 unsigned long long i = 0;
743 ! istr, std::invalid_argument,
744 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::" 745 "convert: Unable to convert the given string \"" << t <<
"\" to " <<
746 intTypeName <<
". Windows lacks strtoull(), so we had to resort to a " 747 "fall-back conversion. The fall-back method does not know how to test " 751 return intToString<unsigned long long> (t, &strtoull,
"unsigned long long");
752 #endif // defined(_MSC_VER) 756 static unsigned long long convert (
const std::string& t) {
761 #endif // HAVE_TEUCHOS_LONG_LONG_INT 776 return intToString<long> (t, &strtol,
"long");
798 return intToString<unsigned long> (t, &strtoul,
"unsigned long");
802 static unsigned long convert (
const std::string& t) {
809 #ifdef HAVE_TEUCHOS___INT64 814 class ValueTypeConversionTraits<unsigned __int64, std::string> {
821 static unsigned __int64
safeConvert(
const std::string& t) {
822 unsigned __int64 output;
823 std::istringstream stream(t);
829 static unsigned __int64
convert(
const std::string& t) {
830 unsigned __int64 output;
831 std::istringstream stream(t);
836 #endif // HAVE_TEUCHOS___INT64 846 static long safeConvertToLong (
const std::string& t) {
850 }
catch (std::range_error&) {
854 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: " 855 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into int.");
856 }
catch (std::invalid_argument& e) {
859 std::invalid_argument,
860 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: " 861 "Intermediate conversion from std::string to long failed, with the following error message: " 874 return asSafe<int> (safeConvertToLong (t));
879 return as<int> (safeConvertToLong (t));
891 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
892 unsigned long val = 0;
894 val = as<unsigned long> (t);
895 }
catch (std::range_error&) {
899 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: " 900 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
901 }
catch (std::invalid_argument& e) {
904 std::invalid_argument,
905 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: " 906 "Intermediate conversion from std::string to unsigned long failed, with the following error message: " 919 return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
923 static unsigned int convert (
const std::string& t) {
924 return as<unsigned int> (safeConvertToUnsignedLong (t));
936 static long safeConvertToLong (
const std::string& t) {
940 }
catch (std::range_error&) {
944 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: " 945 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into short.");
946 }
catch (std::invalid_argument& e) {
949 std::invalid_argument,
950 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: " 951 "Intermediate conversion from std::string to long failed, with the following error message: " 964 return asSafe<short> (safeConvertToLong (t));
969 return as<short> (safeConvertToLong (t));
981 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
982 unsigned long val = 0;
984 val = as<unsigned long> (t);
985 }
catch (std::range_error&) {
989 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: " 990 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
991 }
catch (std::invalid_argument& e) {
994 std::invalid_argument,
995 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: " 996 "Intermediate conversion from std::string to unsigned long failed, with the following error message: " 1009 return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
1013 static unsigned short convert (
const std::string& t) {
1014 return as<unsigned short> (safeConvertToUnsignedLong (t));
1036 const float minVal = -std::numeric_limits<float>::max ();
1037 const float maxVal = std::numeric_limits<float>::max ();
1043 t < minVal || t > maxVal,
1045 "Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: " 1046 "Input double t = " << t <<
" is out of the valid range [" << minVal
1047 <<
", " << maxVal <<
"] for conversion to float.");
1050 return static_cast<float> (t);
1053 static float convert (
const double t) {
1054 return static_cast<float> (t);
1072 const float minVal = -std::numeric_limits<float>::max ();
1073 const float maxVal = std::numeric_limits<float>::max ();
1079 t < minVal || t > maxVal,
1081 "Teuchos::ValueTypeConversionTraits<float, long double>::safeConvert: " 1082 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1083 <<
", " << maxVal <<
"] for conversion to float.");
1086 return static_cast<float> (t);
1089 static float convert (
const long double t) {
1090 return static_cast<float> (t);
1108 const double minVal = -std::numeric_limits<double>::max ();
1109 const double maxVal = std::numeric_limits<double>::max ();
1115 t < minVal || t > maxVal,
1117 "Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: " 1118 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1119 <<
", " << maxVal <<
"] for conversion to double.");
1122 return static_cast<double> (t);
1125 static double convert (
const long double t) {
1126 return static_cast<double> (t);
1149 return static_cast<short> (t);
1154 const short minVal = std::numeric_limits<short>::min ();
1155 const short maxVal = std::numeric_limits<short>::max ();
1172 if (
sizeof (
short) <
sizeof (
double)) {
1174 t < minVal || t > maxVal,
1176 "Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: " 1177 "Input double t = " << t <<
" is out of the valid range [" << minVal
1178 <<
", " << maxVal <<
"] for conversion to short.");
1180 return static_cast<short> (t);
1193 return static_cast<unsigned short> (t);
1198 const unsigned short minVal = 0;
1199 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1202 t < minVal || t > maxVal,
1204 "Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: " 1205 "Input double t = " << t <<
" is out of the valid range [" << minVal
1206 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1208 return static_cast<unsigned short> (t);
1225 return static_cast<int> (t);
1230 const int minVal = std::numeric_limits<int>::min ();
1231 const int maxVal = std::numeric_limits<int>::max ();
1244 if (
sizeof (
int) <
sizeof (
double)) {
1246 t < minVal || t > maxVal,
1248 "Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: " 1249 "Input double t = " << t <<
" is out of the valid range [" << minVal
1250 <<
", " << maxVal <<
"] for conversion to int.");
1252 return static_cast<int> (t);
1265 return static_cast<unsigned int> (t);
1270 const unsigned int minVal = 0;
1271 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1274 t < minVal || t > maxVal,
1276 "Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: " 1277 "Input double t = " << t <<
" is out of the valid range [" << minVal
1278 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1280 return static_cast<unsigned int> (t);
1293 return static_cast<long> (t);
1298 const long minVal = std::numeric_limits<long>::min ();
1299 const long maxVal = std::numeric_limits<long>::max ();
1316 if (
sizeof (
long) <
sizeof (
double)) {
1318 t < minVal || t > maxVal,
1320 "Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: " 1321 "Input double t = " << t <<
" is out of the valid range [" << minVal
1322 <<
", " << maxVal <<
"] for conversion to long.");
1324 return static_cast<long> (t);
1337 return static_cast<unsigned long> (t);
1342 const unsigned long minVal = 0;
1343 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1346 t < minVal || t > maxVal,
1348 "Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: " 1349 "Input double t = " << t <<
" is out of the valid range [" << minVal
1350 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1352 return static_cast<unsigned long> (t);
1356 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1360 class ValueTypeConversionTraits<long long, double> {
1363 static long long convert (
const double t) {
1366 return static_cast<long long> (t);
1381 return static_cast<long long> (t);
1388 class ValueTypeConversionTraits<unsigned long long, double> {
1391 static unsigned long long convert (
const double t) {
1394 return static_cast<unsigned long long> (t);
1398 static unsigned long long safeConvert (
const double t) {
1399 const unsigned long long minVal = 0;
1400 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1403 t < minVal || t > maxVal,
1405 "Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: " 1406 "Input double t = " << t <<
" is out of the valid range [" << minVal
1407 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1409 return static_cast<unsigned long long> (t);
1413 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1428 return static_cast<short> (t);
1433 const short minVal = std::numeric_limits<short>::min ();
1434 const short maxVal = std::numeric_limits<short>::max ();
1453 if (
sizeof (
short) <
sizeof (
float)) {
1455 t < minVal || t > maxVal,
1457 "Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: " 1458 "Input float t = " << t <<
" is out of the valid range [" << minVal
1459 <<
", " << maxVal <<
"] for conversion to short.");
1462 return static_cast<short> (t);
1475 return static_cast<unsigned short> (t);
1480 const unsigned short minVal = 0;
1481 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1484 t < minVal || t > maxVal,
1486 "Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: " 1487 "Input float t = " << t <<
" is out of the valid range [" << minVal
1488 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1490 return static_cast<unsigned short> (t);
1505 return static_cast<int> (t);
1510 const int minVal = std::numeric_limits<int>::min ();
1511 const int maxVal = std::numeric_limits<int>::max ();
1525 if (
sizeof (
int) <
sizeof (
float)) {
1527 t < minVal || t > maxVal,
1529 "Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: " 1530 "Input float t = " << t <<
" is out of the valid range [" 1531 << minVal <<
", " << maxVal <<
"] for conversion to int.");
1533 return static_cast<int> (t);
1546 return static_cast<unsigned int> (t);
1551 const unsigned int minVal = 0;
1552 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1555 t < minVal || t > maxVal,
1557 "Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: " 1558 "Input double t = " << t <<
" is out of the valid range [" << minVal
1559 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1561 return static_cast<unsigned int> (t);
1576 return static_cast<long> (t);
1581 const long minVal = std::numeric_limits<long>::min ();
1582 const long maxVal = std::numeric_limits<long>::max ();
1602 if (
sizeof (
long) <
sizeof (
float)) {
1604 t < minVal || t > maxVal,
1606 "Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: " 1607 "Input float t = " << t <<
" is out of the valid range [" 1608 << minVal <<
", " << maxVal <<
"] for conversion to long.");
1610 return static_cast<long> (t);
1623 return static_cast<unsigned long> (t);
1628 const unsigned long minVal = 0;
1629 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1632 t < minVal || t > maxVal,
1634 "Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: " 1635 <<
"Input float t = " << t <<
" is out of the valid range [" << minVal
1636 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1638 return static_cast<unsigned long> (t);
1642 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1646 class ValueTypeConversionTraits<long long, float> {
1649 static long long convert (
const float t) {
1650 return static_cast<long long> (t);
1657 return static_cast<long long> (t);
1664 class ValueTypeConversionTraits<unsigned long long, float> {
1667 static unsigned long long convert (
const float t) {
1668 return static_cast<unsigned long long> (t);
1672 static unsigned long long safeConvert (
const float t) {
1673 const unsigned long long minVal = 0;
1674 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1677 t < minVal || t > maxVal,
1679 "Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: " 1680 "Input float t = " << t <<
" is out of the valid range [" << minVal
1681 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1683 return static_cast<unsigned long long> (t);
1687 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1699 template<
class SignedIntType,
class Un
signedIntType>
1700 class UnsignedToSignedValueTypeConversionTraits {
1709 static SignedIntType convert (
const UnsignedIntType t) {
1712 return static_cast<SignedIntType
> (t);
1716 static SignedIntType safeConvert (
const UnsignedIntType t) {
1718 const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
1723 const SignedIntType signedVal =
static_cast<SignedIntType
> (t);
1730 <<
" is out of the valid range [0, " <<
", " << maxSigned
1739 template<
class Un
signedIntType,
class SignedIntType>
1740 class SignedToUnsignedValueTypeConversionTraits {
1743 static UnsignedIntType convert (
const SignedIntType t) {
1746 return static_cast<UnsignedIntType
> (t);
1750 static UnsignedIntType safeConvert (
const SignedIntType t) {
1757 t < static_cast<SignedIntType> (0),
1762 <<
" is negative, so it cannot be correctly converted to the unsigned type " 1765 return static_cast<UnsignedIntType
> (t);
1776 static short convert (
const unsigned short t) {
1777 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1780 static short safeConvert (
const unsigned short t) {
1781 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1790 static unsigned short convert (
const short t) {
1791 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1794 static unsigned short safeConvert (
const short t) {
1795 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1804 static int convert (
const unsigned int t) {
1805 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1809 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1818 static unsigned int convert (
const int t) {
1819 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1823 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1832 static long convert (
const unsigned long t) {
1833 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1837 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1846 static unsigned long convert (
const long t) {
1847 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
1851 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
1856 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1862 static long long convert (
const unsigned long long t) {
1863 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
1866 static long long safeConvert (
const unsigned long long t) {
1867 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
1874 class ValueTypeConversionTraits<unsigned long long, long long> {
1876 static unsigned long long convert (
const long long t) {
1877 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
1880 static unsigned long long safeConvert (
const long long t) {
1881 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
1885 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1904 return static_cast<short> (t);
1909 const short minShort = std::numeric_limits<short>::min ();
1910 const short maxShort = std::numeric_limits<short>::max ();
1915 t < static_cast<int> (minShort) ||
1916 t > static_cast<int> (maxShort),
1918 "Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: " 1919 "Input int t = " << t <<
" is out of the valid range [" << minShort
1920 <<
", " << maxShort <<
"] for conversion to short.");
1922 return static_cast<short> (t);
1939 return static_cast<short> (t);
1944 const short minShort = std::numeric_limits<short>::min ();
1945 const short maxShort = std::numeric_limits<short>::max ();
1950 t < static_cast<long> (minShort) ||
1951 t > static_cast<long> (maxShort),
1953 "Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: " 1954 "Input long t = " << t <<
" is out of the valid range [" << minShort
1955 <<
", " << maxShort <<
"] for conversion to short.");
1957 return static_cast<short> (t);
1974 return static_cast<int> (t);
1979 const int minInt = std::numeric_limits<int>::min ();
1980 const int maxInt = std::numeric_limits<int>::max ();
1985 t < static_cast<long> (minInt) ||
1986 t > static_cast<long> (maxInt),
1988 "Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: " 1989 "Input long t = " << t <<
" is out of the valid range [" << minInt
1990 <<
", " << maxInt <<
"] for conversion to int.");
1994 return static_cast<int> (t);
2012 return static_cast<int> (t);
2017 const int minInt = std::numeric_limits<int>::min ();
2018 const int maxInt = std::numeric_limits<int>::max ();
2032 #if INT_MAX == LONG_MAX 2038 static_cast<int> (t) < static_cast<int> (0),
2040 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: " 2041 "Input unsigned long t = " << t <<
" is out of the valid range [" 2042 << minInt <<
", " << maxInt <<
"] for conversion to int.");
2043 #else // INT_MAX < LONG_MAX 2047 t > static_cast<unsigned long> (maxInt),
2049 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: " 2050 "Input unsigned long t = " << t <<
" is out of the valid range [" 2051 << minInt <<
", " << maxInt <<
"] for conversion to int. An unchecked " 2052 "cast would have resulted in " << static_cast<int> (t) <<
".");
2053 #endif // INT_MAX == LONG_MAX 2057 return static_cast<int> (t);
2076 return static_cast<long> (t);
2093 #if UINT_MAX == LONG_MAX 2094 const long minLong = std::numeric_limits<long>::min ();
2095 const long maxLong = std::numeric_limits<long>::max ();
2103 static_cast<long> (t) < static_cast<long> (0),
2105 "Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: " 2106 "Input unsigned int t = " << t <<
" is out of the valid range [" 2107 << minLong <<
", " << maxLong <<
"] for conversion to long.");
2108 #endif // UINT_MAX == LONG_MAX 2110 return static_cast<long> (t);
2128 return static_cast<unsigned int> (t);
2140 if (
sizeof (
unsigned int) <
sizeof (
long)) {
2141 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2144 t < static_cast<long> (0) || t > static_cast<long> (maxInt),
2146 "Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: " 2147 "Input long t = " << t <<
" is out of the valid range [0, " 2148 << maxInt <<
"] for conversion to unsigned int.");
2152 return static_cast<unsigned int> (t);
2167 static unsigned int convert (
const unsigned long t) {
2170 return static_cast<unsigned int> (t);
2175 const unsigned int minInt = 0;
2176 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2180 t > static_cast<unsigned long> (maxInt),
2182 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: " 2183 "Input unsigned long t = " << t <<
" is out of the valid range [" << minInt
2184 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2188 return static_cast<unsigned int> (t);
2202 static unsigned short convert (
const unsigned long t) {
2205 return static_cast<unsigned short> (t);
2210 const unsigned short minShort = 0;
2211 const unsigned short maxShort = std::numeric_limits<unsigned short>::max ();
2215 t > static_cast<unsigned long> (maxShort),
2217 "Teuchos::ValueTypeConversionTraits<unsigned short, unsigned long>::safeConvert: " 2218 "Input unsigned long t = " << t <<
" is out of the valid range [" << minShort
2219 <<
", " << maxShort <<
"] for conversion to unsigned short.");
2223 return static_cast<unsigned short> (t);
2227 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2231 class ValueTypeConversionTraits<int, long long> {
2238 static int convert (
const long long t) {
2241 return static_cast<int> (t);
2246 const int minInt = std::numeric_limits<int>::min ();
2247 const int maxInt = std::numeric_limits<int>::max ();
2250 t < static_cast<long long> (minInt) ||
2251 t > static_cast<long long> (maxInt),
2253 "Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: " 2254 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2255 <<
", " << maxInt <<
"] for conversion to int.");
2259 return static_cast<int> (t);
2266 class ValueTypeConversionTraits<unsigned int, long long> {
2274 static unsigned int convert (
const long long t) {
2277 return static_cast<unsigned int> (t);
2281 static unsigned int safeConvert (
const long long t) {
2282 const unsigned int minInt = 0;
2283 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2286 t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
2288 "Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: " 2289 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2290 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2294 return static_cast<unsigned int> (t);
2301 class ValueTypeConversionTraits<int, unsigned long long> {
2308 static int convert (
const unsigned long long t) {
2311 return static_cast<int> (t);
2315 static int safeConvert (
const unsigned long long t) {
2316 const int minInt = std::numeric_limits<int>::min ();
2317 const int maxInt = std::numeric_limits<int>::max ();
2321 t > static_cast<unsigned long long> (maxInt),
2322 std::invalid_argument,
2323 "Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: " 2324 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2325 <<
", " << maxInt <<
"] for conversion to int.");
2329 return static_cast<int> (t);
2336 class ValueTypeConversionTraits<unsigned int, unsigned long long> {
2344 static unsigned int convert (
const unsigned long long t) {
2347 return static_cast<unsigned int> (t);
2351 static unsigned int safeConvert (
const unsigned long long t) {
2352 const unsigned int minInt = 0;
2353 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2357 t > static_cast<unsigned long long> (maxInt),
2358 std::invalid_argument,
2359 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: " 2360 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2361 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2365 return static_cast<unsigned int> (t);
2369 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2376 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2380 class ValueTypeConversionTraits<float, long long> {
2387 static float convert (
const long long t) {
2390 return static_cast<float> (t);
2400 const float minFloat = -std::numeric_limits<float>::max ();
2401 const float maxFloat = std::numeric_limits<float>::max ();
2413 t < minFloat || t > maxFloat,
2415 "Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: " 2416 "Input long long t = " << t <<
" is out of the valid range [" << minFloat
2417 <<
", " << maxFloat <<
"] for conversion to float.");
2421 return static_cast<float> (t);
2428 class ValueTypeConversionTraits<float, unsigned long long> {
2435 static float convert (
const unsigned long long t) {
2438 return static_cast<float> (t);
2442 static float safeConvert (
const unsigned long long t) {
2448 const float minFloat = -std::numeric_limits<float>::max ();
2449 const float maxFloat = std::numeric_limits<float>::max ();
2458 std::invalid_argument,
2459 "Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: " 2460 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minFloat
2461 <<
", " << maxFloat <<
"] for conversion to float.");
2465 return static_cast<float> (t);
2469 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2479 static std::string
convert(
const char t[] )
2480 {
return std::string(t); }
2482 {
return std::string(t); }
2489 #ifdef HAVE_TEUCHOS_COMPLEX 2492 template<
class RealType>
2495 inline static std::complex<RealType>
convert (
const short t) {
2497 return std::complex<RealType> (t, as<RealType> (0));
2499 static std::complex<RealType>
safeConvert (
const short t) {
2501 return std::complex<RealType> (t, asSafe<RealType> (0));
2506 template<
class RealType>
2507 class ValueTypeConversionTraits<std::complex<RealType>, unsigned short> {
2509 inline static std::complex<RealType>
convert (
const unsigned short t) {
2511 return std::complex<RealType> (t, as<RealType> (0));
2513 static std::complex<RealType>
safeConvert (
const unsigned short t) {
2515 return std::complex<RealType> (t, asSafe<RealType> (0));
2520 template<
class RealType>
2521 class ValueTypeConversionTraits<std::complex<RealType>, int> {
2523 inline static std::complex<RealType>
convert (
const int t) {
2525 return std::complex<RealType> (t, as<RealType> (0));
2527 static std::complex<RealType>
safeConvert (
const int t) {
2529 return std::complex<RealType> (t, asSafe<RealType> (0));
2534 template<
class RealType>
2535 class ValueTypeConversionTraits<std::complex<RealType>, unsigned int> {
2537 inline static std::complex<RealType>
convert (
const unsigned int t) {
2539 return std::complex<RealType> (t, as<RealType> (0));
2541 static std::complex<RealType>
safeConvert (
const unsigned int t) {
2543 return std::complex<RealType> (t, asSafe<RealType> (0));
2548 template<
class RealType>
2549 class ValueTypeConversionTraits<std::complex<RealType>, long> {
2551 inline static std::complex<RealType>
convert (
const long t) {
2553 return std::complex<RealType> (t, as<RealType> (0));
2555 static std::complex<RealType>
safeConvert (
const long t) {
2557 return std::complex<RealType> (t, asSafe<RealType> (0));
2562 template<
class RealType>
2563 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long> {
2565 inline static std::complex<RealType>
convert (
const unsigned long t) {
2567 return std::complex<RealType> (t, as<RealType> (0));
2569 static std::complex<RealType>
safeConvert (
const unsigned long t) {
2571 return std::complex<RealType> (t, asSafe<RealType> (0));
2575 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2578 template<
class RealType>
2579 class ValueTypeConversionTraits<std::complex<RealType>, long long> {
2581 inline static std::complex<RealType>
convert (
const long long t) {
2583 return std::complex<RealType> (t, as<RealType> (0));
2585 static std::complex<RealType>
safeConvert (
const long long t) {
2587 return std::complex<RealType> (t, asSafe<RealType> (0));
2592 template<
class RealType>
2593 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long long> {
2595 inline static std::complex<RealType>
convert (
const unsigned long long t) {
2597 return std::complex<RealType> (t, as<RealType> (0));
2599 static std::complex<RealType>
safeConvert (
const unsigned long long t) {
2601 return std::complex<RealType> (t, asSafe<RealType> (0));
2605 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2606 #endif // HAVE_TEUCHOS_COMPLEX 2612 #ifdef HAVE_TEUCHOS_QD 2616 class ValueTypeConversionTraits<double, qd_real> {
2618 inline static double convert (
const qd_real t) {
2619 return to_double (t);
2627 const qd_real minVal = -std::numeric_limits<double>::max ();
2628 const qd_real maxVal = std::numeric_limits<double>::max ();
2631 t < minVal || t > maxVal,
2633 "Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: " 2634 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2635 <<
", " << maxVal <<
"] for conversion to double.");
2637 return to_double (t);
2643 class ValueTypeConversionTraits<float, qd_real> {
2645 inline static float convert (
const qd_real t) {
2648 return as<float> (to_double (t));
2660 const qd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2661 const qd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2664 t < minVal || t > maxVal,
2666 "Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: " 2667 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2668 <<
", " << maxVal <<
"] for conversion to float.");
2671 return asSafe<float> (to_double (t));
2677 class ValueTypeConversionTraits<int, qd_real> {
2679 inline static int convert (
const qd_real t) {
2684 const qd_real minVal = std::numeric_limits<int>::min ();
2685 const qd_real maxVal = std::numeric_limits<int>::max ();
2688 t < minVal || t > maxVal,
2690 "Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: " 2691 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2692 <<
", " << maxVal <<
"] for conversion to int.");
2699 class ValueTypeConversionTraits<dd_real, qd_real> {
2701 inline static dd_real
convert (
const qd_real t) {
2702 return to_dd_real(t);
2714 const qd_real minVal = -std::numeric_limits<dd_real>::max ();
2715 const qd_real maxVal = std::numeric_limits<dd_real>::max ();
2718 t < minVal || t > maxVal,
2720 "Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: " 2721 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2722 <<
", " << maxVal <<
"] for conversion to dd_real.");
2724 return to_dd_real (t);
2730 class ValueTypeConversionTraits<double, dd_real> {
2732 inline static double convert (
const dd_real t) {
2733 return to_double (t);
2743 const dd_real minVal = -std::numeric_limits<double>::max ();
2744 const dd_real maxVal = std::numeric_limits<double>::max ();
2747 t < minVal || t > maxVal,
2749 "Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: " 2750 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2751 <<
", " << maxVal <<
"] for conversion to double.");
2753 return to_double (t);
2759 class ValueTypeConversionTraits<float, dd_real> {
2761 inline static float convert (
const dd_real t) {
2763 return as<float> (to_double (t));
2774 const dd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2775 const dd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2778 t < minVal || t > maxVal,
2780 "Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: " 2781 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2782 <<
", " << maxVal <<
"] for conversion to float.");
2785 return as<float> (to_double (t));
2791 class ValueTypeConversionTraits<int, dd_real> {
2793 inline static int convert (
const dd_real t) {
2798 const dd_real minVal = std::numeric_limits<int>::min ();
2799 const dd_real maxVal = std::numeric_limits<int>::max ();
2802 t < minVal || t > maxVal,
2804 "Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: " 2805 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2806 <<
", " << maxVal <<
"] for conversion to int.");
2813 class ValueTypeConversionTraits<qd_real, long unsigned int> {
2815 inline static qd_real
convert(
const long unsigned int t ) {
2825 inline static qd_real
safeConvert(
const long unsigned int t )
2831 class ValueTypeConversionTraits<dd_real, long unsigned int> {
2833 inline static dd_real
convert(
const long unsigned int t ) {
2843 inline static dd_real
safeConvert(
const long unsigned int t )
2847 #endif // HAVE_TEUCHOS_QD 2851 template<
class TypeTo,
class TypeFrom>
2852 inline TypeTo
as(
const TypeFrom& t )
2854 #ifdef HAVE_TEUCHOS_DEBUG 2858 #endif // HAVE_TEUCHOS_DEBUG 2861 template<
class TypeTo,
class TypeFrom>
2870 #endif // TEUCHOS_AS_HPP static short convert(const int t)
Convert the given int to a short.
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 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 short safeConvert(const float t)
Convert the given float to a short, checking for overflow first.
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 unsigned short convert(const float t)
Convert the given float to an unsigned short.
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 unsigned long safeConvert(const std::string &t)
Convert the given std::string to an 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.
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 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 unsigned int convert(const unsigned long t)
Convert the given unsigned long to an unsigned int.
static long safeConvert(const double t)
Convert the given double to long, checking for overflow first.
TypeTo asSafe(const TypeFrom &t)
Convert from one value type to another, with validity checks if appropriate.
static int safeConvert(const double t)
Convert the given double to an 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 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 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.
Default traits class for all conversions between value types.
static unsigned int convert(const std::string &t)
Convert the given std::string to an unsigned int.
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 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 int convert(const unsigned long t)
Convert the given unsigned long to an int.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
static int safeConvert(const long t)
Convert from long to int, checking for overflow first.
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.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
static unsigned long safeConvert(const double t)
Convert the given double to an unsigned long, checking for overflow first.
static short safeConvert(const int t)
Convert from int to short, 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 unsigned long convert(const float t)
Convert the given float to an unsigned long.
static std::string name()
static unsigned int convert(const long t)
Convert the given long to an unsigned int.