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) {
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
842 static long safeConvertToLong (
const std::string& t) {
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));
887 static unsigned long safeConvertToUnsignedLong (
const std::string& 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));
932 static long safeConvertToLong (
const std::string& 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));
977 static unsigned long safeConvertToUnsignedLong (
const std::string& 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);
1049 static float convert (
const double 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);
1085 static float convert (
const long double 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);
1121 static double convert (
const long 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);
1718 <<
" is out of the valid range [0, " <<
", " << maxSigned
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),
1750 <<
" is negative, so it cannot be correctly converted to the unsigned type "
1753 return static_cast<UnsignedIntType> (t);
1764 static short convert (
const unsigned short t) {
1765 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1768 static short safeConvert (
const unsigned short t) {
1769 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1778 static unsigned short convert (
const short t) {
1779 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1782 static unsigned short safeConvert (
const short t) {
1783 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1792 static int convert (
const unsigned int t) {
1793 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1797 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1806 static unsigned int convert (
const int t) {
1807 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1811 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1820 static long convert (
const unsigned long t) {
1821 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1825 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1834 static unsigned long convert (
const long 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);
1852 static long long safeConvert (
const unsigned long long 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);
1866 static unsigned long long safeConvert (
const long long 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);
2455 static std::string
convert(
const char t[] )
2456 {
return std::string(t); }
2458 {
return std::string(t); }
2465 #ifdef HAVE_TEUCHOS_COMPLEX
2468 template<
class RealType>
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