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 )
289 #ifdef HAVE_TEUCHOS_DEBUG 293 #endif // HAVE_TEUCHOS_DEBUG 362 template<
class TypeTo,
class TypeFrom>
363 inline TypeTo
asSafe(
const TypeFrom& t )
381 template <
class TypeTo>
386 template <
class TypeFrom>
387 inline TypeTo operator()(
const TypeFrom &t) {
388 return as<TypeTo>(t);
413 template<
class IntType>
415 intToString (
const std::string& t,
416 IntType (*rawConvert) (
const char*,
char**,
int),
417 const char* intTypeName)
435 const char* t_ptr = t.c_str ();
443 const IntType val = rawConvert (t_ptr, &endptr, 10);
445 const IntType minVal = std::numeric_limits<IntType>::min ();
446 const IntType maxVal = std::numeric_limits<IntType>::max ();
448 errno == ERANGE && (val == minVal || val == maxVal),
450 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: " 451 "The integer value in the given string \"" << t <<
"\" overflows " << intTypeName <<
".");
453 errno != 0 && val == 0,
454 std::invalid_argument,
455 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: " 456 "The conversion function was unable to convert the given string \"" << t <<
"\" to " << intTypeName <<
".");
459 std::invalid_argument,
460 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: " 461 "The conversion function was unable to read any integer digits from the given string " 477 template<
class OutputRealType,
class InputRealType>
479 realToReal (
const InputRealType& x,
const bool doBoundsChecking)
483 if (doBoundsChecking) {
488 const OutputRealType minVal = -std::numeric_limits<OutputRealType>::max ();
489 const OutputRealType maxVal = std::numeric_limits<OutputRealType>::max ();
495 x < minVal || x > maxVal,
499 "Input value x = " << x <<
" is out of the valid range [" << minVal
500 <<
", " << maxVal <<
"] for conversion to the output type.");
507 return as<OutputRealType> (x);
535 template<
class RealType>
537 stringToReal (
const std::string& t,
538 RealType (*rawConvert) (
const char*,
char**),
539 const char* realTypeName)
541 if (rawConvert == NULL) {
542 std::istringstream in (t);
552 const char* t_ptr = t.c_str ();
560 const RealType val = rawConvert (t_ptr, &endptr);
563 errno == ERANGE && (val != 0),
565 "Teuchos::ValueTypeConversionTraits<" << realTypeName
566 <<
", std::string>::convert: " 567 "The value in the given string \"" << t <<
"\" overflows " 568 << realTypeName <<
".");
573 errno == ERANGE && val == 0,
574 std::invalid_argument,
575 "Teuchos::ValueTypeConversionTraits<" << realTypeName
576 <<
", std::string>::convert: " 577 "The value in the given string \"" << t <<
"\" underflows " 578 << realTypeName <<
".");
581 std::invalid_argument,
582 "Teuchos::ValueTypeConversionTraits<" << realTypeName
583 <<
", std::string>::convert: " 584 "The conversion function was unable to read any floating-point data " 585 "from the given string \"" << t <<
"\".");
614 template<
class OutType>
617 static OutType
safeConvert (
const std::string& t) {
621 static OutType
convert (
const std::string& t) {
622 std::istringstream in (t);
638 static double convert (
const std::string& t) {
639 return stringToReal<double> (t, &strtod,
"double");
643 return stringToReal<double> (t, &strtod,
"double");
651 static float convert (
const std::string& t) {
652 #ifdef _ISOC99_SOURCE 653 return stringToReal<float> (t, &strtof,
"float");
657 const double d = stringToReal<double> (t, &strtod,
"double");
658 return realToReal<float, double> (d,
false);
659 #endif // _ISOC99_SOURCE 663 #ifdef _ISOC99_SOURCE 664 return stringToReal<float> (t, &strtof,
"float");
668 const double d = stringToReal<double> (t, &strtod,
"double");
669 return realToReal<float, double> (d,
true);
670 #endif // _ISOC99_SOURCE 678 static long double convert (
const std::string& t) {
679 #ifdef _ISOC99_SOURCE 680 return stringToReal<long double> (t, &strtold,
"long double");
684 return stringToReal<long double> (t, NULL,
"long double");
685 #endif // _ISOC99_SOURCE 688 static long double safeConvert (
const std::string& t) {
698 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 711 static long long safeConvert (
const std::string& t) {
712 #if defined(_MSC_VER) 718 return intToString<long long> (t, &_strtoi64,
"long long");
720 return intToString<long long> (t, &strtoll,
"long long");
721 #endif // defined(_MSC_VER) 725 static long long convert (
const std::string& t) {
742 static unsigned long long safeConvert (
const std::string& t) {
743 #if defined(_MSC_VER) 748 const char intTypeName[] =
"unsigned long long";
749 std::istringstream istr (t);
750 unsigned long long i = 0;
753 ! istr, std::invalid_argument,
754 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::" 755 "convert: Unable to convert the given string \"" << t <<
"\" to " <<
756 intTypeName <<
". Windows lacks strtoull(), so we had to resort to a " 757 "fall-back conversion. The fall-back method does not know how to test " 761 return intToString<unsigned long long> (t, &strtoull,
"unsigned long long");
762 #endif // defined(_MSC_VER) 766 static unsigned long long convert (
const std::string& t) {
771 #endif // HAVE_TEUCHOS_LONG_LONG_INT 786 return intToString<long> (t, &strtol,
"long");
808 return intToString<unsigned long> (t, &strtoul,
"unsigned long");
812 static unsigned long convert (
const std::string& t) {
825 static long safeConvertToLong (
const std::string& t) {
829 }
catch (std::range_error&) {
833 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: " 834 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into int.");
835 }
catch (std::invalid_argument& e) {
838 std::invalid_argument,
839 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: " 840 "Intermediate conversion from std::string to long failed, with the following error message: " 853 return asSafe<int> (safeConvertToLong (t));
858 return as<int> (safeConvertToLong (t));
870 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
871 unsigned long val = 0;
873 val = as<unsigned long> (t);
874 }
catch (std::range_error&) {
878 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: " 879 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
880 }
catch (std::invalid_argument& e) {
883 std::invalid_argument,
884 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: " 885 "Intermediate conversion from std::string to unsigned long failed, with the following error message: " 898 return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
902 static unsigned int convert (
const std::string& t) {
903 return as<unsigned int> (safeConvertToUnsignedLong (t));
915 static long safeConvertToLong (
const std::string& t) {
919 }
catch (std::range_error&) {
923 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: " 924 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into short.");
925 }
catch (std::invalid_argument& e) {
928 std::invalid_argument,
929 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: " 930 "Intermediate conversion from std::string to long failed, with the following error message: " 943 return asSafe<short> (safeConvertToLong (t));
948 return as<short> (safeConvertToLong (t));
960 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
961 unsigned long val = 0;
963 val = as<unsigned long> (t);
964 }
catch (std::range_error&) {
968 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: " 969 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
970 }
catch (std::invalid_argument& e) {
973 std::invalid_argument,
974 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: " 975 "Intermediate conversion from std::string to unsigned long failed, with the following error message: " 988 return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
992 static unsigned short convert (
const std::string& t) {
993 return as<unsigned short> (safeConvertToUnsignedLong (t));
1015 const float minVal = -std::numeric_limits<float>::max ();
1016 const float maxVal = std::numeric_limits<float>::max ();
1022 t < minVal || t > maxVal,
1024 "Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: " 1025 "Input double t = " << t <<
" is out of the valid range [" << minVal
1026 <<
", " << maxVal <<
"] for conversion to float.");
1029 return static_cast<float> (t);
1032 static float convert (
const double t) {
1033 return static_cast<float> (t);
1051 const float minVal = -std::numeric_limits<float>::max ();
1052 const float maxVal = std::numeric_limits<float>::max ();
1058 t < minVal || t > maxVal,
1060 "Teuchos::ValueTypeConversionTraits<float, long double>::safeConvert: " 1061 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1062 <<
", " << maxVal <<
"] for conversion to float.");
1065 return static_cast<float> (t);
1068 static float convert (
const long double t) {
1069 return static_cast<float> (t);
1087 const double minVal = -std::numeric_limits<double>::max ();
1088 const double maxVal = std::numeric_limits<double>::max ();
1094 t < minVal || t > maxVal,
1096 "Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: " 1097 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1098 <<
", " << maxVal <<
"] for conversion to double.");
1101 return static_cast<double> (t);
1104 static double convert (
const long double t) {
1105 return static_cast<double> (t);
1128 return static_cast<short> (t);
1133 const short minVal = std::numeric_limits<short>::min ();
1134 const short maxVal = std::numeric_limits<short>::max ();
1151 if (
sizeof (
short) <
sizeof (
double)) {
1153 t < minVal || t > maxVal,
1155 "Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: " 1156 "Input double t = " << t <<
" is out of the valid range [" << minVal
1157 <<
", " << maxVal <<
"] for conversion to short.");
1159 return static_cast<short> (t);
1172 return static_cast<unsigned short> (t);
1177 const unsigned short minVal = 0;
1178 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1181 t < minVal || t > maxVal,
1183 "Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: " 1184 "Input double t = " << t <<
" is out of the valid range [" << minVal
1185 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1187 return static_cast<unsigned short> (t);
1204 return static_cast<int> (t);
1209 const int minVal = std::numeric_limits<int>::min ();
1210 const int maxVal = std::numeric_limits<int>::max ();
1223 if (
sizeof (
int) <
sizeof (
double)) {
1225 t < minVal || t > maxVal,
1227 "Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: " 1228 "Input double t = " << t <<
" is out of the valid range [" << minVal
1229 <<
", " << maxVal <<
"] for conversion to int.");
1231 return static_cast<int> (t);
1244 return static_cast<unsigned int> (t);
1249 const unsigned int minVal = 0;
1250 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1253 t < minVal || t > maxVal,
1255 "Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: " 1256 "Input double t = " << t <<
" is out of the valid range [" << minVal
1257 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1259 return static_cast<unsigned int> (t);
1272 return static_cast<long> (t);
1277 const long minVal = std::numeric_limits<long>::min ();
1278 const long maxVal = std::numeric_limits<long>::max ();
1295 if (
sizeof (
long) <
sizeof (
double)) {
1297 t < minVal || t > maxVal,
1299 "Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: " 1300 "Input double t = " << t <<
" is out of the valid range [" << minVal
1301 <<
", " << maxVal <<
"] for conversion to long.");
1303 return static_cast<long> (t);
1316 return static_cast<unsigned long> (t);
1321 const unsigned long minVal = 0;
1322 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1325 t < minVal || t > maxVal,
1327 "Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: " 1328 "Input double t = " << t <<
" is out of the valid range [" << minVal
1329 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1331 return static_cast<unsigned long> (t);
1335 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1342 static long long convert (
const double t) {
1345 return static_cast<long long> (t);
1360 return static_cast<long long> (t);
1370 static unsigned long long convert (
const double t) {
1373 return static_cast<unsigned long long> (t);
1377 static unsigned long long safeConvert (
const double t) {
1378 const unsigned long long minVal = 0;
1379 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1382 t < minVal || t > maxVal,
1384 "Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: " 1385 "Input double t = " << t <<
" is out of the valid range [" << minVal
1386 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1388 return static_cast<unsigned long long> (t);
1392 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1407 return static_cast<short> (t);
1412 const short minVal = std::numeric_limits<short>::min ();
1413 const short maxVal = std::numeric_limits<short>::max ();
1432 if (
sizeof (
short) <
sizeof (
float)) {
1434 t < minVal || t > maxVal,
1436 "Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: " 1437 "Input float t = " << t <<
" is out of the valid range [" << minVal
1438 <<
", " << maxVal <<
"] for conversion to short.");
1441 return static_cast<short> (t);
1454 return static_cast<unsigned short> (t);
1459 const unsigned short minVal = 0;
1460 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1463 t < minVal || t > maxVal,
1465 "Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: " 1466 "Input float t = " << t <<
" is out of the valid range [" << minVal
1467 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1469 return static_cast<unsigned short> (t);
1484 return static_cast<int> (t);
1489 const int minVal = std::numeric_limits<int>::min ();
1490 const int maxVal = std::numeric_limits<int>::max ();
1504 if (
sizeof (
int) <
sizeof (
float)) {
1506 t < minVal || t > maxVal,
1508 "Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: " 1509 "Input float t = " << t <<
" is out of the valid range [" 1510 << minVal <<
", " << maxVal <<
"] for conversion to int.");
1512 return static_cast<int> (t);
1525 return static_cast<unsigned int> (t);
1530 const unsigned int minVal = 0;
1531 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1534 t < minVal || t > maxVal,
1536 "Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: " 1537 "Input double t = " << t <<
" is out of the valid range [" << minVal
1538 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1540 return static_cast<unsigned int> (t);
1555 return static_cast<long> (t);
1560 const long minVal = std::numeric_limits<long>::min ();
1561 const long maxVal = std::numeric_limits<long>::max ();
1581 if (
sizeof (
long) <
sizeof (
float)) {
1583 t < minVal || t > maxVal,
1585 "Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: " 1586 "Input float t = " << t <<
" is out of the valid range [" 1587 << minVal <<
", " << maxVal <<
"] for conversion to long.");
1589 return static_cast<long> (t);
1602 return static_cast<unsigned long> (t);
1607 const unsigned long minVal = 0;
1608 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1611 t < minVal || t > maxVal,
1613 "Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: " 1614 <<
"Input float t = " << t <<
" is out of the valid range [" << minVal
1615 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1617 return static_cast<unsigned long> (t);
1621 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1628 static long long convert (
const float t) {
1629 return static_cast<long long> (t);
1636 return static_cast<long long> (t);
1646 static unsigned long long convert (
const float t) {
1647 return static_cast<unsigned long long> (t);
1651 static unsigned long long safeConvert (
const float t) {
1652 const unsigned long long minVal = 0;
1653 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1656 t < minVal || t > maxVal,
1658 "Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: " 1659 "Input float t = " << t <<
" is out of the valid range [" << minVal
1660 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1662 return static_cast<unsigned long long> (t);
1666 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1678 template<
class SignedIntType,
class Un
signedIntType>
1679 class UnsignedToSignedValueTypeConversionTraits {
1688 static SignedIntType
convert (
const UnsignedIntType t) {
1691 return static_cast<SignedIntType
> (t);
1695 static SignedIntType
safeConvert (
const UnsignedIntType t) {
1697 const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
1702 const SignedIntType signedVal =
static_cast<SignedIntType
> (t);
1709 <<
" is out of the valid range [0, " <<
", " << maxSigned
1718 template<
class Un
signedIntType,
class SignedIntType>
1719 class SignedToUnsignedValueTypeConversionTraits {
1722 static UnsignedIntType
convert (
const SignedIntType t) {
1725 return static_cast<UnsignedIntType
> (t);
1729 static UnsignedIntType
safeConvert (
const SignedIntType t) {
1736 t < static_cast<SignedIntType> (0),
1741 <<
" is negative, so it cannot be correctly converted to the unsigned type " 1744 return static_cast<UnsignedIntType
> (t);
1755 static short convert (
const unsigned short t) {
1756 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1759 static short safeConvert (
const unsigned short t) {
1760 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1769 static unsigned short convert (
const short t) {
1770 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1773 static unsigned short safeConvert (
const short t) {
1774 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1783 static int convert (
const unsigned int t) {
1784 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1788 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1797 static unsigned int convert (
const int t) {
1798 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1802 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1811 static long convert (
const unsigned long t) {
1812 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1816 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1825 static unsigned long convert (
const long t) {
1826 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
1830 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
1835 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 1841 static long long convert (
const unsigned long long t) {
1842 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
1845 static long long safeConvert (
const unsigned long long t) {
1846 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
1855 static unsigned long long convert (
const long long t) {
1856 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
1859 static unsigned long long safeConvert (
const long long t) {
1860 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
1864 #endif // HAVE_TEUCHOS_LONG_LONG_INT 1883 return static_cast<short> (t);
1888 const short minShort = std::numeric_limits<short>::min ();
1889 const short maxShort = std::numeric_limits<short>::max ();
1894 t < static_cast<int> (minShort) ||
1895 t > static_cast<int> (maxShort),
1897 "Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: " 1898 "Input int t = " << t <<
" is out of the valid range [" << minShort
1899 <<
", " << maxShort <<
"] for conversion to short.");
1901 return static_cast<short> (t);
1918 return static_cast<short> (t);
1923 const short minShort = std::numeric_limits<short>::min ();
1924 const short maxShort = std::numeric_limits<short>::max ();
1929 t < static_cast<long> (minShort) ||
1930 t > static_cast<long> (maxShort),
1932 "Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: " 1933 "Input long t = " << t <<
" is out of the valid range [" << minShort
1934 <<
", " << maxShort <<
"] for conversion to short.");
1936 return static_cast<short> (t);
1953 return static_cast<int> (t);
1958 const int minInt = std::numeric_limits<int>::min ();
1959 const int maxInt = std::numeric_limits<int>::max ();
1964 t < static_cast<long> (minInt) ||
1965 t > static_cast<long> (maxInt),
1967 "Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: " 1968 "Input long t = " << t <<
" is out of the valid range [" << minInt
1969 <<
", " << maxInt <<
"] for conversion to int.");
1973 return static_cast<int> (t);
1991 return static_cast<int> (t);
1996 const int minInt = std::numeric_limits<int>::min ();
1997 const int maxInt = std::numeric_limits<int>::max ();
2011 #if INT_MAX == LONG_MAX 2017 static_cast<int> (t) < static_cast<int> (0),
2019 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: " 2020 "Input unsigned long t = " << t <<
" is out of the valid range [" 2021 << minInt <<
", " << maxInt <<
"] for conversion to int.");
2022 #else // INT_MAX < LONG_MAX 2026 t > static_cast<unsigned long> (maxInt),
2028 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: " 2029 "Input unsigned long t = " << t <<
" is out of the valid range [" 2030 << minInt <<
", " << maxInt <<
"] for conversion to int. An unchecked " 2031 "cast would have resulted in " << static_cast<int> (t) <<
".");
2032 #endif // INT_MAX == LONG_MAX 2036 return static_cast<int> (t);
2055 return static_cast<long> (t);
2072 #if UINT_MAX == LONG_MAX 2073 const long minLong = std::numeric_limits<long>::min ();
2074 const long maxLong = std::numeric_limits<long>::max ();
2082 static_cast<long> (t) < static_cast<long> (0),
2084 "Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: " 2085 "Input unsigned int t = " << t <<
" is out of the valid range [" 2086 << minLong <<
", " << maxLong <<
"] for conversion to long.");
2087 #endif // UINT_MAX == LONG_MAX 2089 return static_cast<long> (t);
2107 return static_cast<unsigned int> (t);
2119 if (
sizeof (
unsigned int) <
sizeof (
long)) {
2120 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2123 t < static_cast<long> (0) || t > static_cast<long> (maxInt),
2125 "Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: " 2126 "Input long t = " << t <<
" is out of the valid range [0, " 2127 << maxInt <<
"] for conversion to unsigned int.");
2131 return static_cast<unsigned int> (t);
2146 static unsigned int convert (
const unsigned long t) {
2149 return static_cast<unsigned int> (t);
2154 const unsigned int minInt = 0;
2155 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2159 t > static_cast<unsigned long> (maxInt),
2161 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: " 2162 "Input unsigned long t = " << t <<
" is out of the valid range [" << minInt
2163 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2167 return static_cast<unsigned int> (t);
2171 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2182 static int convert (
const long long t) {
2185 return static_cast<int> (t);
2190 const int minInt = std::numeric_limits<int>::min ();
2191 const int maxInt = std::numeric_limits<int>::max ();
2194 t < static_cast<long long> (minInt) ||
2195 t > static_cast<long long> (maxInt),
2197 "Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: " 2198 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2199 <<
", " << maxInt <<
"] for conversion to int.");
2203 return static_cast<int> (t);
2218 static unsigned int convert (
const long long t) {
2221 return static_cast<unsigned int> (t);
2225 static unsigned int safeConvert (
const long long t) {
2226 const unsigned int minInt = 0;
2227 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2230 t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
2232 "Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: " 2233 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2234 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2238 return static_cast<unsigned int> (t);
2252 static int convert (
const unsigned long long t) {
2255 return static_cast<int> (t);
2259 static int safeConvert (
const unsigned long long t) {
2260 const int minInt = std::numeric_limits<int>::min ();
2261 const int maxInt = std::numeric_limits<int>::max ();
2265 t > static_cast<unsigned long long> (maxInt),
2266 std::invalid_argument,
2267 "Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: " 2268 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2269 <<
", " << maxInt <<
"] for conversion to int.");
2273 return static_cast<int> (t);
2288 static unsigned int convert (
const unsigned long long t) {
2291 return static_cast<unsigned int> (t);
2295 static unsigned int safeConvert (
const unsigned long long t) {
2296 const unsigned int minInt = 0;
2297 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2301 t > static_cast<unsigned long long> (maxInt),
2302 std::invalid_argument,
2303 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: " 2304 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2305 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2309 return static_cast<unsigned int> (t);
2313 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2320 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2331 static float convert (
const long long t) {
2334 return static_cast<float> (t);
2344 const float minFloat = -std::numeric_limits<float>::max ();
2345 const float maxFloat = std::numeric_limits<float>::max ();
2357 t < minFloat || t > maxFloat,
2359 "Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: " 2360 "Input long long t = " << t <<
" is out of the valid range [" << minFloat
2361 <<
", " << maxFloat <<
"] for conversion to float.");
2365 return static_cast<float> (t);
2379 static float convert (
const unsigned long long t) {
2382 return static_cast<float> (t);
2386 static float safeConvert (
const unsigned long long t) {
2392 const float minFloat = -std::numeric_limits<float>::max ();
2393 const float maxFloat = std::numeric_limits<float>::max ();
2402 std::invalid_argument,
2403 "Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: " 2404 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minFloat
2405 <<
", " << maxFloat <<
"] for conversion to float.");
2409 return static_cast<float> (t);
2413 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2423 static std::string
convert(
const char t[] )
2424 {
return std::string(t); }
2426 {
return std::string(t); }
2433 #ifdef HAVE_TEUCHOS_COMPLEX 2436 template<
class RealType>
2439 inline static std::complex<RealType>
convert (
const short t) {
2441 return std::complex<RealType> (t, as<RealType> (0));
2443 static std::complex<RealType>
safeConvert (
const short t) {
2445 return std::complex<RealType> (t, asSafe<RealType> (0));
2450 template<
class RealType>
2453 inline static std::complex<RealType>
convert (
const unsigned short t) {
2455 return std::complex<RealType> (t, as<RealType> (0));
2457 static std::complex<RealType>
safeConvert (
const unsigned short t) {
2459 return std::complex<RealType> (t, asSafe<RealType> (0));
2464 template<
class RealType>
2467 inline static std::complex<RealType>
convert (
const int t) {
2469 return std::complex<RealType> (t, as<RealType> (0));
2471 static std::complex<RealType>
safeConvert (
const int t) {
2473 return std::complex<RealType> (t, asSafe<RealType> (0));
2478 template<
class RealType>
2481 inline static std::complex<RealType>
convert (
const unsigned int t) {
2483 return std::complex<RealType> (t, as<RealType> (0));
2485 static std::complex<RealType>
safeConvert (
const unsigned int t) {
2487 return std::complex<RealType> (t, asSafe<RealType> (0));
2492 template<
class RealType>
2495 inline static std::complex<RealType>
convert (
const long t) {
2497 return std::complex<RealType> (t, as<RealType> (0));
2499 static std::complex<RealType>
safeConvert (
const long t) {
2501 return std::complex<RealType> (t, asSafe<RealType> (0));
2506 template<
class RealType>
2509 inline static std::complex<RealType>
convert (
const unsigned long t) {
2511 return std::complex<RealType> (t, as<RealType> (0));
2513 static std::complex<RealType>
safeConvert (
const unsigned long t) {
2515 return std::complex<RealType> (t, asSafe<RealType> (0));
2519 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 2522 template<
class RealType>
2525 inline static std::complex<RealType>
convert (
const long long t) {
2527 return std::complex<RealType> (t, as<RealType> (0));
2529 static std::complex<RealType>
safeConvert (
const long long t) {
2531 return std::complex<RealType> (t, asSafe<RealType> (0));
2536 template<
class RealType>
2539 inline static std::complex<RealType>
convert (
const unsigned long long t) {
2541 return std::complex<RealType> (t, as<RealType> (0));
2543 static std::complex<RealType>
safeConvert (
const unsigned long long t) {
2545 return std::complex<RealType> (t, asSafe<RealType> (0));
2549 #endif // HAVE_TEUCHOS_LONG_LONG_INT 2550 #endif // HAVE_TEUCHOS_COMPLEX 2556 #ifdef HAVE_TEUCHOS_QD 2562 inline static double convert (
const qd_real t) {
2563 return to_double (t);
2571 const qd_real minVal = -std::numeric_limits<double>::max ();
2572 const qd_real maxVal = std::numeric_limits<double>::max ();
2575 t < minVal || t > maxVal,
2577 "Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: " 2578 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2579 <<
", " << maxVal <<
"] for conversion to double.");
2581 return to_double (t);
2589 inline static float convert (
const qd_real t) {
2592 return as<float> (to_double (t));
2604 const qd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2605 const qd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2608 t < minVal || t > maxVal,
2610 "Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: " 2611 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2612 <<
", " << maxVal <<
"] for conversion to float.");
2615 return asSafe<float> (to_double (t));
2623 inline static int convert (
const qd_real t) {
2628 const qd_real minVal = std::numeric_limits<int>::min ();
2629 const qd_real maxVal = std::numeric_limits<int>::max ();
2632 t < minVal || t > maxVal,
2634 "Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: " 2635 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2636 <<
", " << maxVal <<
"] for conversion to int.");
2645 inline static dd_real
convert (
const qd_real t) {
2646 return to_dd_real(t);
2658 const qd_real minVal = -std::numeric_limits<dd_real>::max ();
2659 const qd_real maxVal = std::numeric_limits<dd_real>::max ();
2662 t < minVal || t > maxVal,
2664 "Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: " 2665 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2666 <<
", " << maxVal <<
"] for conversion to dd_real.");
2668 return to_dd_real (t);
2676 inline static double convert (
const dd_real t) {
2677 return to_double (t);
2687 const dd_real minVal = -std::numeric_limits<double>::max ();
2688 const dd_real maxVal = std::numeric_limits<double>::max ();
2691 t < minVal || t > maxVal,
2693 "Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: " 2694 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2695 <<
", " << maxVal <<
"] for conversion to double.");
2697 return to_double (t);
2705 inline static float convert (
const dd_real t) {
2707 return as<float> (to_double (t));
2718 const dd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2719 const dd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2722 t < minVal || t > maxVal,
2724 "Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: " 2725 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2726 <<
", " << maxVal <<
"] for conversion to float.");
2729 return as<float> (to_double (t));
2737 inline static int convert (
const dd_real t) {
2742 const dd_real minVal = std::numeric_limits<int>::min ();
2743 const dd_real maxVal = std::numeric_limits<int>::max ();
2746 t < minVal || t > maxVal,
2748 "Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: " 2749 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2750 <<
", " << maxVal <<
"] for conversion to int.");
2759 inline static qd_real
convert(
const long unsigned int t ) {
2769 inline static qd_real
safeConvert(
const long unsigned int t )
2777 inline static dd_real
convert(
const long unsigned int t ) {
2787 inline static dd_real
safeConvert(
const long unsigned int t )
2791 #endif // HAVE_TEUCHOS_QD 2799 #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 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 unsigned int convert(const long t)
Convert the given long to an unsigned int.