10 #ifndef TEUCHOS_AS_HPP
11 #define TEUCHOS_AS_HPP
40 #include "Teuchos_Assert.hpp"
46 #ifdef HAVE_TEUCHOS_COMPLEX
48 #endif // HAVE_TEUCHOS_COMPLEX
50 #ifdef HAVE_TEUCHOS_QD
51 #include <qd/qd_real.h>
52 #include <qd/dd_real.h>
53 #endif // HAVE_TEUCHOS_QD
146 template<
class TypeTo,
class TypeFrom>
254 template<
class TypeTo,
class TypeFrom>
255 inline TypeTo
as(
const TypeFrom& t );
323 template<
class TypeTo,
class TypeFrom>
324 inline TypeTo
asSafe(
const TypeFrom& t );
339 template <
class TypeTo>
344 template <
class TypeFrom>
345 inline TypeTo operator()(
const TypeFrom &t) {
346 return as<TypeTo>(t);
371 template<
class IntType>
373 intToString (
const std::string& t,
374 IntType (*rawConvert) (
const char*,
char**,
int),
375 const char* intTypeName)
393 const char* t_ptr = t.c_str ();
401 const IntType val = rawConvert (t_ptr, &endptr, 10);
403 const IntType minVal = std::numeric_limits<IntType>::min ();
404 const IntType maxVal = std::numeric_limits<IntType>::max ();
406 errno == ERANGE && (val == minVal || val == maxVal),
408 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
409 "The integer value in the given string \"" << t <<
"\" overflows " << intTypeName <<
".");
411 errno != 0 && val == 0,
412 std::invalid_argument,
413 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
414 "The conversion function was unable to convert the given string \"" << t <<
"\" to " << intTypeName <<
".");
417 std::invalid_argument,
418 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::convert: "
419 "The conversion function was unable to read any integer digits from the given string "
435 template<
class OutputRealType,
class InputRealType>
437 realToReal (
const InputRealType& x,
const bool doBoundsChecking)
441 if (doBoundsChecking) {
446 const OutputRealType minVal = -std::numeric_limits<OutputRealType>::max ();
447 const OutputRealType maxVal = std::numeric_limits<OutputRealType>::max ();
453 x < as<InputRealType>(minVal) || x > as<InputRealType>(maxVal),
455 "realToReal<" << TypeNameTraits<OutputRealType>::name () <<
", "
456 << TypeNameTraits<InputRealType>::name () <<
">: "
457 "Input value x = " << x <<
" is out of the valid range [" << minVal
458 <<
", " << maxVal <<
"] for conversion to the output type.");
465 return as<OutputRealType> (x);
493 template<
class RealType>
495 stringToReal (
const std::string& t,
496 RealType (*rawConvert) (
const char*,
char**),
497 const char* realTypeName)
499 if (rawConvert == NULL) {
500 std::istringstream in (t);
510 const char* t_ptr = t.c_str ();
518 const RealType val = rawConvert (t_ptr, &endptr);
521 errno == ERANGE && (val != 0),
523 "Teuchos::ValueTypeConversionTraits<" << realTypeName
524 <<
", std::string>::convert: "
525 "The value in the given string \"" << t <<
"\" overflows "
526 << realTypeName <<
".");
531 errno == ERANGE && val == 0,
532 std::invalid_argument,
533 "Teuchos::ValueTypeConversionTraits<" << realTypeName
534 <<
", std::string>::convert: "
535 "The value in the given string \"" << t <<
"\" underflows "
536 << realTypeName <<
".");
539 std::invalid_argument,
540 "Teuchos::ValueTypeConversionTraits<" << realTypeName
541 <<
", std::string>::convert: "
542 "The conversion function was unable to read any floating-point data "
543 "from the given string \"" << t <<
"\".");
572 template<
class OutType>
575 static OutType
safeConvert (
const std::string& t) {
579 static OutType
convert (
const std::string& t) {
580 std::istringstream in (t);
596 static double convert (
const std::string& t) {
597 return stringToReal<double> (t, &strtod,
"double");
601 return stringToReal<double> (t, &strtod,
"double");
609 static float convert (
const std::string& t) {
610 #ifdef _ISOC99_SOURCE
611 return stringToReal<float> (t, &strtof,
"float");
615 const double d = stringToReal<double> (t, &strtod,
"double");
616 return realToReal<float, double> (d,
false);
617 #endif // _ISOC99_SOURCE
621 #ifdef _ISOC99_SOURCE
622 return stringToReal<float> (t, &strtof,
"float");
626 const double d = stringToReal<double> (t, &strtod,
"double");
627 return realToReal<float, double> (d,
true);
628 #endif // _ISOC99_SOURCE
636 static long double convert (
const std::string& t) {
637 #ifdef _ISOC99_SOURCE
638 return stringToReal<long double> (t, &strtold,
"long double");
642 return stringToReal<long double> (t, NULL,
"long double");
643 #endif // _ISOC99_SOURCE
646 static long double safeConvert (
const std::string& t) {
668 #if defined(_MSC_VER)
674 return intToString<long long> (t, &_strtoi64,
"long long");
676 return intToString<long long> (t, &strtoll,
"long long");
677 #endif // defined(_MSC_VER)
681 static long long convert (
const std::string& t) {
699 #if defined(_MSC_VER)
704 const char intTypeName[] =
"unsigned long long";
705 std::istringstream istr (t);
706 unsigned long long i = 0;
709 ! istr, std::invalid_argument,
710 "Teuchos::ValueTypeConversionTraits<" << intTypeName <<
", std::string>::"
711 "convert: Unable to convert the given string \"" << t <<
"\" to " <<
712 intTypeName <<
". Windows lacks strtoull(), so we had to resort to a "
713 "fall-back conversion. The fall-back method does not know how to test "
717 return intToString<unsigned long long> (t, &strtoull,
"unsigned long long");
718 #endif // defined(_MSC_VER)
722 static unsigned long long convert (
const std::string& t) {
740 return intToString<long> (t, &strtol,
"long");
762 return intToString<unsigned long> (t, &strtoul,
"unsigned long");
766 static unsigned long convert (
const std::string& t) {
773 #ifdef HAVE_TEUCHOS___INT64
778 class ValueTypeConversionTraits<unsigned __int64, std::string> {
785 static unsigned __int64
safeConvert(
const std::string& t) {
786 unsigned __int64 output;
787 std::istringstream stream(t);
793 static unsigned __int64
convert(
const std::string& t) {
794 unsigned __int64 output;
795 std::istringstream stream(t);
800 #endif // HAVE_TEUCHOS___INT64
810 static long safeConvertToLong (
const std::string& t) {
814 }
catch (std::range_error&) {
818 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
819 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into int.");
820 }
catch (std::invalid_argument& e) {
823 std::invalid_argument,
824 "Teuchos::ValueTypeConversionTraits<int, std::string>::convert: "
825 "Intermediate conversion from std::string to long failed, with the following error message: "
838 return asSafe<int> (safeConvertToLong (t));
843 return as<int> (safeConvertToLong (t));
855 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
856 unsigned long val = 0;
858 val = as<unsigned long> (t);
859 }
catch (std::range_error&) {
863 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
864 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned int.");
865 }
catch (std::invalid_argument& e) {
868 std::invalid_argument,
869 "Teuchos::ValueTypeConversionTraits<unsigned int, std::string>::convert: "
870 "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
883 return asSafe<unsigned int> (safeConvertToUnsignedLong (t));
887 static unsigned int convert (
const std::string& t) {
888 return as<unsigned int> (safeConvertToUnsignedLong (t));
900 static long safeConvertToLong (
const std::string& t) {
904 }
catch (std::range_error&) {
908 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
909 "The given std::string \"" << t <<
"\" is too big to fit into long, so there is no way it could fit into short.");
910 }
catch (std::invalid_argument& e) {
913 std::invalid_argument,
914 "Teuchos::ValueTypeConversionTraits<short, std::string>::convert: "
915 "Intermediate conversion from std::string to long failed, with the following error message: "
928 return asSafe<short> (safeConvertToLong (t));
933 return as<short> (safeConvertToLong (t));
945 static unsigned long safeConvertToUnsignedLong (
const std::string& t) {
946 unsigned long val = 0;
948 val = as<unsigned long> (t);
949 }
catch (std::range_error&) {
953 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
954 "The given std::string \"" << t <<
"\" is too big to fit into unsigned long, so there is no way it could fit into unsigned short.");
955 }
catch (std::invalid_argument& e) {
958 std::invalid_argument,
959 "Teuchos::ValueTypeConversionTraits<unsigned short, std::string>::convert: "
960 "Intermediate conversion from std::string to unsigned long failed, with the following error message: "
973 return asSafe<unsigned short> (safeConvertToUnsignedLong (t));
977 static unsigned short convert (
const std::string& t) {
978 return as<unsigned short> (safeConvertToUnsignedLong (t));
1000 const float minVal = -std::numeric_limits<float>::max ();
1001 const float maxVal = std::numeric_limits<float>::max ();
1007 t < minVal || t > maxVal,
1009 "Teuchos::ValueTypeConversionTraits<float, double>::safeConvert: "
1010 "Input double t = " << t <<
" is out of the valid range [" << minVal
1011 <<
", " << maxVal <<
"] for conversion to float.");
1014 return static_cast<float> (t);
1017 static float convert (
const double t) {
1018 return static_cast<float> (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, long double>::safeConvert: "
1046 "Input long 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 long double t) {
1054 return static_cast<float> (t);
1072 const double minVal = -std::numeric_limits<double>::max ();
1073 const double maxVal = std::numeric_limits<double>::max ();
1079 t < minVal || t > maxVal,
1081 "Teuchos::ValueTypeConversionTraits<double, long double>::safeConvert: "
1082 "Input long double t = " << t <<
" is out of the valid range [" << minVal
1083 <<
", " << maxVal <<
"] for conversion to double.");
1086 return static_cast<double> (t);
1089 static double convert (
const long double t) {
1090 return static_cast<double> (t);
1113 return static_cast<short> (t);
1118 const short minVal = std::numeric_limits<short>::min ();
1119 const short maxVal = std::numeric_limits<short>::max ();
1136 if (
sizeof (
short) <
sizeof (
double)) {
1138 t < minVal || t > maxVal,
1140 "Teuchos::ValueTypeConversionTraits<short, double>::safeConvert: "
1141 "Input double t = " << t <<
" is out of the valid range [" << minVal
1142 <<
", " << maxVal <<
"] for conversion to short.");
1144 return static_cast<short> (t);
1157 return static_cast<unsigned short> (t);
1162 const unsigned short minVal = 0;
1163 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1166 t < minVal || t > maxVal,
1168 "Teuchos::ValueTypeConversionTraits<unsigned short, double>::safeConvert: "
1169 "Input double t = " << t <<
" is out of the valid range [" << minVal
1170 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1172 return static_cast<unsigned short> (t);
1189 return static_cast<int> (t);
1194 const int minVal = std::numeric_limits<int>::min ();
1195 const int maxVal = std::numeric_limits<int>::max ();
1208 if (
sizeof (
int) <
sizeof (
double)) {
1210 t < minVal || t > maxVal,
1212 "Teuchos::ValueTypeConversionTraits<int, double>::safeConvert: "
1213 "Input double t = " << t <<
" is out of the valid range [" << minVal
1214 <<
", " << maxVal <<
"] for conversion to int.");
1216 return static_cast<int> (t);
1229 return static_cast<unsigned int> (t);
1234 const unsigned int minVal = 0;
1235 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1238 t < minVal || t > maxVal,
1240 "Teuchos::ValueTypeConversionTraits<unsigned int, double>::safeConvert: "
1241 "Input double t = " << t <<
" is out of the valid range [" << minVal
1242 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1244 return static_cast<unsigned int> (t);
1257 return static_cast<long> (t);
1262 const long minVal = std::numeric_limits<long>::min ();
1263 const long maxVal = std::numeric_limits<long>::max ();
1280 if (
sizeof (
long) <
sizeof (
double)) {
1282 t < minVal || t > maxVal,
1284 "Teuchos::ValueTypeConversionTraits<long, double>::safeConvert: "
1285 "Input double t = " << t <<
" is out of the valid range [" << minVal
1286 <<
", " << maxVal <<
"] for conversion to long.");
1288 return static_cast<long> (t);
1301 return static_cast<unsigned long> (t);
1306 const unsigned long minVal = 0;
1307 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1310 t < minVal || t > static_cast<double>(maxVal),
1312 "Teuchos::ValueTypeConversionTraits<unsigned long, double>::safeConvert: "
1313 "Input double t = " << t <<
" is out of the valid range [" << minVal
1314 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1316 return static_cast<unsigned long> (t);
1328 return static_cast<long long> (t);
1343 return static_cast<long long> (t);
1353 static unsigned long long convert (
const double t) {
1356 return static_cast<unsigned long long> (t);
1361 const unsigned long long minVal = 0;
1362 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1365 t < minVal || t > static_cast<double>(maxVal),
1367 "Teuchos::ValueTypeConversionTraits<unsigned long long, double>::safeConvert: "
1368 "Input double t = " << t <<
" is out of the valid range [" << minVal
1369 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1371 return static_cast<unsigned long long> (t);
1388 return static_cast<short> (t);
1393 const short minVal = std::numeric_limits<short>::min ();
1394 const short maxVal = std::numeric_limits<short>::max ();
1413 if (
sizeof (
short) <
sizeof (
float)) {
1415 t < minVal || t > maxVal,
1417 "Teuchos::ValueTypeConversionTraits<short, float>::safeConvert: "
1418 "Input float t = " << t <<
" is out of the valid range [" << minVal
1419 <<
", " << maxVal <<
"] for conversion to short.");
1422 return static_cast<short> (t);
1435 return static_cast<unsigned short> (t);
1440 const unsigned short minVal = 0;
1441 const unsigned short maxVal = std::numeric_limits<unsigned short>::max ();
1444 t < minVal || t > maxVal,
1446 "Teuchos::ValueTypeConversionTraits<unsigned short, float>::safeConvert: "
1447 "Input float t = " << t <<
" is out of the valid range [" << minVal
1448 <<
", " << maxVal <<
"] for conversion to unsigned short.");
1450 return static_cast<unsigned short> (t);
1465 return static_cast<int> (t);
1470 const int minVal = std::numeric_limits<int>::min ();
1471 const int maxVal = std::numeric_limits<int>::max ();
1485 if (
sizeof (
int) <
sizeof (
float)) {
1487 t < minVal || t > maxVal,
1489 "Teuchos::ValueTypeConversionTraits<int, float>::safeConvert: "
1490 "Input float t = " << t <<
" is out of the valid range ["
1491 << minVal <<
", " << maxVal <<
"] for conversion to int.");
1493 return static_cast<int> (t);
1506 return static_cast<unsigned int> (t);
1511 const unsigned int minVal = 0;
1512 const unsigned int maxVal = std::numeric_limits<unsigned int>::max ();
1515 t < minVal || t > static_cast<float>(maxVal),
1517 "Teuchos::ValueTypeConversionTraits<unsigned int, float>::safeConvert: "
1518 "Input double t = " << t <<
" is out of the valid range [" << minVal
1519 <<
", " << maxVal <<
"] for conversion to unsigned int.");
1521 return static_cast<unsigned int> (t);
1536 return static_cast<long> (t);
1541 const long minVal = std::numeric_limits<long>::min ();
1542 const long maxVal = std::numeric_limits<long>::max ();
1562 if (
sizeof (
long) <
sizeof (
float)) {
1564 t < minVal || t > static_cast<float>(maxVal),
1566 "Teuchos::ValueTypeConversionTraits<long, float>::safeConvert: "
1567 "Input float t = " << t <<
" is out of the valid range ["
1568 << minVal <<
", " << maxVal <<
"] for conversion to long.");
1570 return static_cast<long> (t);
1583 return static_cast<unsigned long> (t);
1588 const unsigned long minVal = 0;
1589 const unsigned long maxVal = std::numeric_limits<unsigned long>::max ();
1592 t < minVal || t > static_cast<float>(maxVal),
1594 "Teuchos::ValueTypeConversionTraits<unsigned long, float>::safeConvert: "
1595 <<
"Input float t = " << t <<
" is out of the valid range [" << minVal
1596 <<
", " << maxVal <<
"] for conversion to unsigned long.");
1598 return static_cast<unsigned long> (t);
1608 return static_cast<long long> (t);
1615 return static_cast<long long> (t);
1625 static unsigned long long convert (
const float t) {
1626 return static_cast<unsigned long long> (t);
1631 const unsigned long long minVal = 0;
1632 const unsigned long long maxVal = std::numeric_limits<unsigned long long>::max ();
1635 t < minVal || t > static_cast<float>(maxVal),
1637 "Teuchos::ValueTypeConversionTraits<unsigned long long, float>::safeConvert: "
1638 "Input float t = " << t <<
" is out of the valid range [" << minVal
1639 <<
", " << maxVal <<
"] for conversion to unsigned long long.");
1641 return static_cast<unsigned long long> (t);
1655 template<
class SignedIntType,
class Un
signedIntType>
1656 class UnsignedToSignedValueTypeConversionTraits {
1665 static SignedIntType convert (
const UnsignedIntType t) {
1668 return static_cast<SignedIntType
> (t);
1672 static SignedIntType safeConvert (
const UnsignedIntType t) {
1674 const SignedIntType maxSigned = std::numeric_limits<SignedIntType>::max ();
1679 const SignedIntType signedVal =
static_cast<SignedIntType
> (t);
1683 "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<SignedIntType>::name ()
1684 <<
", " << TypeNameTraits<UnsignedIntType>::name () <<
">::safeConvert: "
1685 "Input " << TypeNameTraits<UnsignedIntType>::name () <<
" t = " << t
1686 <<
" is out of the valid range [0, " <<
", " << maxSigned
1687 <<
"] for conversion to " << TypeNameTraits<SignedIntType>::name () <<
".");
1695 template<
class Un
signedIntType,
class SignedIntType>
1696 class SignedToUnsignedValueTypeConversionTraits {
1699 static UnsignedIntType convert (
const SignedIntType t) {
1702 return static_cast<UnsignedIntType
> (t);
1706 static UnsignedIntType safeConvert (
const SignedIntType t) {
1713 t < static_cast<SignedIntType> (0),
1715 "Teuchos::ValueTypeConversionTraits<" << TypeNameTraits<UnsignedIntType>::name ()
1716 <<
", " << TypeNameTraits<SignedIntType>::name () <<
">::safeConvert: "
1717 "Input " << TypeNameTraits<SignedIntType>::name () <<
" t = " << t
1718 <<
" is negative, so it cannot be correctly converted to the unsigned type "
1719 << TypeNameTraits<UnsignedIntType>::name () <<
".");
1721 return static_cast<UnsignedIntType
> (t);
1732 static short convert (
const unsigned short t) {
1733 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::convert (t);
1736 static short safeConvert (
const unsigned short t) {
1737 return UnsignedToSignedValueTypeConversionTraits<short, unsigned short>::safeConvert (t);
1746 static unsigned short convert (
const short t) {
1747 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::convert (t);
1750 static unsigned short safeConvert (
const short t) {
1751 return SignedToUnsignedValueTypeConversionTraits<unsigned short, short>::safeConvert (t);
1760 static int convert (
const unsigned int t) {
1761 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::convert (t);
1765 return UnsignedToSignedValueTypeConversionTraits<int, unsigned int>::safeConvert (t);
1774 static unsigned int convert (
const int t) {
1775 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::convert (t);
1779 return SignedToUnsignedValueTypeConversionTraits<unsigned int, int>::safeConvert (t);
1788 static long convert (
const unsigned long t) {
1789 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::convert (t);
1793 return UnsignedToSignedValueTypeConversionTraits<long, unsigned long>::safeConvert (t);
1802 static unsigned long convert (
const long t) {
1803 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::convert (t);
1807 return SignedToUnsignedValueTypeConversionTraits<unsigned long, long>::safeConvert (t);
1816 static long long convert (
const unsigned long long t) {
1817 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::convert (t);
1820 static long long safeConvert (
const unsigned long long t) {
1821 return UnsignedToSignedValueTypeConversionTraits<long long, unsigned long long>::safeConvert (t);
1830 static unsigned long long convert (
const long long t) {
1831 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::convert (t);
1834 static unsigned long long safeConvert (
const long long t) {
1835 return SignedToUnsignedValueTypeConversionTraits<unsigned long long, long long>::safeConvert (t);
1856 return static_cast<short> (t);
1861 const short minShort = std::numeric_limits<short>::min ();
1862 const short maxShort = std::numeric_limits<short>::max ();
1867 t < static_cast<int> (minShort) ||
1868 t > static_cast<int> (maxShort),
1870 "Teuchos::ValueTypeConversionTraits<short, int>::safeConvert: "
1871 "Input int t = " << t <<
" is out of the valid range [" << minShort
1872 <<
", " << maxShort <<
"] for conversion to short.");
1874 return static_cast<short> (t);
1891 return static_cast<short> (t);
1896 const short minShort = std::numeric_limits<short>::min ();
1897 const short maxShort = std::numeric_limits<short>::max ();
1902 t < static_cast<long> (minShort) ||
1903 t > static_cast<long> (maxShort),
1905 "Teuchos::ValueTypeConversionTraits<short, long>::safeConvert: "
1906 "Input long t = " << t <<
" is out of the valid range [" << minShort
1907 <<
", " << maxShort <<
"] for conversion to short.");
1909 return static_cast<short> (t);
1926 return static_cast<int> (t);
1931 const int minInt = std::numeric_limits<int>::min ();
1932 const int maxInt = std::numeric_limits<int>::max ();
1937 t < static_cast<long> (minInt) ||
1938 t > static_cast<long> (maxInt),
1940 "Teuchos::ValueTypeConversionTraits<int, long>::safeConvert: "
1941 "Input long t = " << t <<
" is out of the valid range [" << minInt
1942 <<
", " << maxInt <<
"] for conversion to int.");
1946 return static_cast<int> (t);
1964 return static_cast<int> (t);
1969 const int minInt = std::numeric_limits<int>::min ();
1970 const int maxInt = std::numeric_limits<int>::max ();
1984 #if INT_MAX == LONG_MAX
1990 static_cast<int> (t) < static_cast<int> (0),
1992 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
1993 "Input unsigned long t = " << t <<
" is out of the valid range ["
1994 << minInt <<
", " << maxInt <<
"] for conversion to int.");
1995 #else // INT_MAX < LONG_MAX
1999 t > static_cast<unsigned long> (maxInt),
2001 "Teuchos::ValueTypeConversionTraits<int, unsigned long>::safeConvert: "
2002 "Input unsigned long t = " << t <<
" is out of the valid range ["
2003 << minInt <<
", " << maxInt <<
"] for conversion to int. An unchecked "
2004 "cast would have resulted in " << static_cast<int> (t) <<
".");
2005 #endif // INT_MAX == LONG_MAX
2009 return static_cast<int> (t);
2028 return static_cast<long> (t);
2045 #if UINT_MAX == LONG_MAX
2046 const long minLong = std::numeric_limits<long>::min ();
2047 const long maxLong = std::numeric_limits<long>::max ();
2055 static_cast<long> (t) < static_cast<long> (0),
2057 "Teuchos::ValueTypeConversionTraits<long, unsigned int>::safeConvert: "
2058 "Input unsigned int t = " << t <<
" is out of the valid range ["
2059 << minLong <<
", " << maxLong <<
"] for conversion to long.");
2060 #endif // UINT_MAX == LONG_MAX
2062 return static_cast<long> (t);
2080 return static_cast<unsigned int> (t);
2092 if (
sizeof (
unsigned int) <
sizeof (
long)) {
2093 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2096 t < static_cast<long> (0) || t > static_cast<long> (maxInt),
2098 "Teuchos::ValueTypeConversionTraits<unsigned int, long>::safeConvert: "
2099 "Input long t = " << t <<
" is out of the valid range [0, "
2100 << maxInt <<
"] for conversion to unsigned int.");
2104 return static_cast<unsigned int> (t);
2119 static unsigned int convert (
const unsigned long t) {
2122 return static_cast<unsigned int> (t);
2127 const unsigned int minInt = 0;
2128 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2132 t > static_cast<unsigned long> (maxInt),
2134 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long>::safeConvert: "
2135 "Input unsigned long t = " << t <<
" is out of the valid range [" << minInt
2136 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2140 return static_cast<unsigned int> (t);
2154 static unsigned short convert (
const unsigned long t) {
2157 return static_cast<unsigned short> (t);
2162 const unsigned short minShort = 0;
2163 const unsigned short maxShort = std::numeric_limits<unsigned short>::max ();
2167 t > static_cast<unsigned long> (maxShort),
2169 "Teuchos::ValueTypeConversionTraits<unsigned short, unsigned long>::safeConvert: "
2170 "Input unsigned long t = " << t <<
" is out of the valid range [" << minShort
2171 <<
", " << maxShort <<
"] for conversion to unsigned short.");
2175 return static_cast<unsigned short> (t);
2191 return static_cast<int> (t);
2196 const int minInt = std::numeric_limits<int>::min ();
2197 const int maxInt = std::numeric_limits<int>::max ();
2200 t < static_cast<long long> (minInt) ||
2201 t > static_cast<long long> (maxInt),
2203 "Teuchos::ValueTypeConversionTraits<int, long long>::safeConvert: "
2204 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2205 <<
", " << maxInt <<
"] for conversion to int.");
2209 return static_cast<int> (t);
2227 return static_cast<unsigned int> (t);
2232 const unsigned int minInt = 0;
2233 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2236 t < static_cast<long long> (minInt) || t > static_cast<long long> (maxInt),
2238 "Teuchos::ValueTypeConversionTraits<unsigned int, long long>::safeConvert: "
2239 "Input long long t = " << t <<
" is out of the valid range [" << minInt
2240 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2244 return static_cast<unsigned int> (t);
2261 return static_cast<int> (t);
2266 const int minInt = std::numeric_limits<int>::min ();
2267 const int maxInt = std::numeric_limits<int>::max ();
2271 t > static_cast<unsigned long long> (maxInt),
2272 std::invalid_argument,
2273 "Teuchos::ValueTypeConversionTraits<int, unsigned long long>::safeConvert: "
2274 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2275 <<
", " << maxInt <<
"] for conversion to int.");
2279 return static_cast<int> (t);
2294 static unsigned int convert (
const unsigned long long t) {
2297 return static_cast<unsigned int> (t);
2302 const unsigned int minInt = 0;
2303 const unsigned int maxInt = std::numeric_limits<unsigned int>::max ();
2307 t > static_cast<unsigned long long> (maxInt),
2308 std::invalid_argument,
2309 "Teuchos::ValueTypeConversionTraits<unsigned int, unsigned long long>::safeConvert: "
2310 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minInt
2311 <<
", " << maxInt <<
"] for conversion to unsigned int.");
2315 return static_cast<unsigned int> (t);
2336 return static_cast<float> (t);
2346 const float minFloat = -std::numeric_limits<float>::max ();
2347 const float maxFloat = std::numeric_limits<float>::max ();
2359 t < minFloat || t > maxFloat,
2361 "Teuchos::ValueTypeConversionTraits<float, long long>::safeConvert: "
2362 "Input long long t = " << t <<
" is out of the valid range [" << minFloat
2363 <<
", " << maxFloat <<
"] for conversion to float.");
2367 return static_cast<float> (t);
2381 static float convert (
const unsigned long long t) {
2384 return static_cast<float> (t);
2394 const float minFloat = -std::numeric_limits<float>::max ();
2395 const float maxFloat = std::numeric_limits<float>::max ();
2404 std::invalid_argument,
2405 "Teuchos::ValueTypeConversionTraits<float, unsigned long long>::safeConvert: "
2406 "Input unsigned long long t = " << t <<
" is out of the valid range [" << minFloat
2407 <<
", " << maxFloat <<
"] for conversion to float.");
2411 return static_cast<float> (t);
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>
2451 class ValueTypeConversionTraits<std::complex<RealType>, unsigned short> {
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>
2465 class ValueTypeConversionTraits<std::complex<RealType>, int> {
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>
2479 class ValueTypeConversionTraits<std::complex<RealType>, unsigned int> {
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>
2493 class ValueTypeConversionTraits<std::complex<RealType>, long> {
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>
2507 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long> {
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));
2520 template<
class RealType>
2521 class ValueTypeConversionTraits<std::complex<RealType>, long long> {
2523 inline static std::complex<RealType>
convert (
const long long t) {
2525 return std::complex<RealType> (t, as<RealType> (0));
2527 static std::complex<RealType>
safeConvert (
const long long t) {
2529 return std::complex<RealType> (t, asSafe<RealType> (0));
2534 template<
class RealType>
2535 class ValueTypeConversionTraits<std::complex<RealType>, unsigned long long> {
2537 inline static std::complex<RealType>
convert (
const unsigned long long t) {
2539 return std::complex<RealType> (t, as<RealType> (0));
2541 static std::complex<RealType>
safeConvert (
const unsigned long long t) {
2543 return std::complex<RealType> (t, asSafe<RealType> (0));
2550 class ValueTypeConversionTraits<std::complex<float>, std::complex<double> > {
2552 inline static std::complex<float>
convert (
const std::complex<double> t) {
2554 return std::complex<float> (as<float> (t.real ()), as<float> (t.imag ()));
2556 static std::complex<float>
safeConvert (
const std::complex<double> t) {
2558 return std::complex<float> (asSafe<float> (t.real ()), asSafe<float> (t.imag ()));
2562 #endif // HAVE_TEUCHOS_COMPLEX
2568 #ifdef HAVE_TEUCHOS_QD
2572 class ValueTypeConversionTraits<double, qd_real> {
2574 inline static double convert (
const qd_real t) {
2575 return to_double (t);
2583 const qd_real minVal = -std::numeric_limits<double>::max ();
2584 const qd_real maxVal = std::numeric_limits<double>::max ();
2587 t < minVal || t > maxVal,
2589 "Teuchos::ValueTypeConversionTraits<double, qd_real>::safeConvert: "
2590 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2591 <<
", " << maxVal <<
"] for conversion to double.");
2593 return to_double (t);
2599 class ValueTypeConversionTraits<float, qd_real> {
2601 inline static float convert (
const qd_real t) {
2604 return as<float> (to_double (t));
2616 const qd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2617 const qd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2620 t < minVal || t > maxVal,
2622 "Teuchos::ValueTypeConversionTraits<float, qd_real>::safeConvert: "
2623 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2624 <<
", " << maxVal <<
"] for conversion to float.");
2627 return asSafe<float> (to_double (t));
2633 class ValueTypeConversionTraits<int, qd_real> {
2635 inline static int convert (
const qd_real t) {
2640 const qd_real minVal = std::numeric_limits<int>::min ();
2641 const qd_real maxVal = std::numeric_limits<int>::max ();
2644 t < minVal || t > maxVal,
2646 "Teuchos::ValueTypeConversionTraits<int, qd_real>::safeConvert: "
2647 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2648 <<
", " << maxVal <<
"] for conversion to int.");
2655 class ValueTypeConversionTraits<dd_real, qd_real> {
2657 inline static dd_real
convert (
const qd_real t) {
2658 return to_dd_real(t);
2670 const qd_real minVal = -std::numeric_limits<dd_real>::max ();
2671 const qd_real maxVal = std::numeric_limits<dd_real>::max ();
2674 t < minVal || t > maxVal,
2676 "Teuchos::ValueTypeConversionTraits<dd_real, qd_real>::safeConvert: "
2677 "Input qd_real t = " << t <<
" is out of the valid range [" << minVal
2678 <<
", " << maxVal <<
"] for conversion to dd_real.");
2680 return to_dd_real (t);
2686 class ValueTypeConversionTraits<double, dd_real> {
2688 inline static double convert (
const dd_real t) {
2689 return to_double (t);
2699 const dd_real minVal = -std::numeric_limits<double>::max ();
2700 const dd_real maxVal = std::numeric_limits<double>::max ();
2703 t < minVal || t > maxVal,
2705 "Teuchos::ValueTypeConversionTraits<double, dd_real>::safeConvert: "
2706 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2707 <<
", " << maxVal <<
"] for conversion to double.");
2709 return to_double (t);
2715 class ValueTypeConversionTraits<float, dd_real> {
2717 inline static float convert (
const dd_real t) {
2719 return as<float> (to_double (t));
2730 const dd_real minVal =
static_cast<double> (-std::numeric_limits<float>::max ());
2731 const dd_real maxVal =
static_cast<double> (std::numeric_limits<float>::max ());
2734 t < minVal || t > maxVal,
2736 "Teuchos::ValueTypeConversionTraits<float, dd_real>::safeConvert: "
2737 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2738 <<
", " << maxVal <<
"] for conversion to float.");
2741 return as<float> (to_double (t));
2747 class ValueTypeConversionTraits<int, dd_real> {
2749 inline static int convert (
const dd_real t) {
2754 const dd_real minVal = std::numeric_limits<int>::min ();
2755 const dd_real maxVal = std::numeric_limits<int>::max ();
2758 t < minVal || t > maxVal,
2760 "Teuchos::ValueTypeConversionTraits<int, dd_real>::safeConvert: "
2761 "Input dd_real t = " << t <<
" is out of the valid range [" << minVal
2762 <<
", " << maxVal <<
"] for conversion to int.");
2769 class ValueTypeConversionTraits<qd_real, long unsigned int> {
2771 inline static qd_real
convert(
const long unsigned int t ) {
2781 inline static qd_real
safeConvert(
const long unsigned int t )
2787 class ValueTypeConversionTraits<dd_real, long unsigned int> {
2789 inline static dd_real
convert(
const long unsigned int t ) {
2799 inline static dd_real
safeConvert(
const long unsigned int t )
2803 #endif // HAVE_TEUCHOS_QD
2807 template<
class TypeTo,
class TypeFrom>
2808 inline TypeTo
as(
const TypeFrom& t )
2810 #ifdef HAVE_TEUCHOS_DEBUG
2814 #endif // HAVE_TEUCHOS_DEBUG
2817 template<
class TypeTo,
class TypeFrom>
2826 #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 unsigned int convert(const unsigned long long t)
Convert the given unsigned long long to an unsigned int.
static long long safeConvert(const std::string &t)
Convert the given std::string to a long long, with checks.
static short safeConvert(const long t)
Convert from long to short, checking for overflow first.
static short safeConvert(const double t)
Convert the given double to a short, checking for overflow first.
static float safeConvert(const long long t)
Convert from long long to float, checking for overflow first.
static short safeConvert(const float t)
Convert the given float to a short, checking for overflow first.
static int safeConvert(const std::string &t)
Convert the given std::string to an int, with checks.
static long safeConvert(const std::string &t)
Convert the given std::string to a long, with checks.
static long convert(const float t)
Convert the given float to an long.
static int convert(const long long t)
Convert the given long long to an int.
static unsigned short convert(const float t)
Convert the given float to an unsigned short.
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 int safeConvert(const unsigned long long t)
Convert from unsigned long long to int, checking for overflow first.
static unsigned long safeConvert(const std::string &t)
Convert the given std::string to an unsigned long, with checks.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
static TypeTo convert(const TypeFrom t)
Convert t from a TypeFrom object to a TypeTo object.
static unsigned int convert(const long long t)
Convert the given long long to an unsigned int.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
static int convert(const std::string &t)
Convert the given std::string to an int.
static int convert(const float t)
Convert the given float to an int.
static long convert(const std::string &t)
Convert the given std::string to a long.
static long convert(const double t)
Convert the given double to long.
static unsigned long convert(const std::string &t)
Convert the given std::string to an unsigned long.
static int convert(const long t)
Convert the given long to an int.
static float safeConvert(const unsigned long long t)
Convert from unsigned long long to float, checking for overflow first.
static unsigned int convert(const unsigned long t)
Convert the given unsigned long to an unsigned int.
static int convert(const unsigned long long t)
Convert the given unsigned long long to an int.
static unsigned long long safeConvert(const std::string &t)
Convert the given std::string to an unsigned long long, with checks.
static 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 int safeConvert(const long long t)
Convert from long long to unsigned int, checking for overflow first.
static unsigned short safeConvert(const double t)
Convert the given double to an unsigned short, checking for overflow first.
static unsigned short safeConvert(const float t)
Convert the given float to an unsigned short, checking for overflow first.
static unsigned short convert(const unsigned long t)
Convert the given unsigned long to an unsigned short.
static 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.
static unsigned long long convert(const double t)
Convert the given double to unsigned long long.
static long long safeConvert(const float t)
Convert the given float to a long long, checking first for overflow.
static unsigned long long convert(const std::string &t)
Convert the given std::string to an unsigned long long.
static long long convert(const std::string &t)
Convert the given std::string to a long long.
static long long convert(const double t)
Convert the given double to long long.
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 float convert(const long long t)
Convert the given long long to a float.
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 float convert(const unsigned long long t)
Convert the given unsigned long long to a float.
static int safeConvert(const long long t)
Convert from long long to int, checking for overflow first.
static int convert(const unsigned long t)
Convert the given unsigned long to an int.
static int safeConvert(const long t)
Convert from long to int, checking for overflow first.
static unsigned int safeConvert(const unsigned long long t)
Convert from unsigned long long to unsigned int, checking for overflow first.
static 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 long long convert(const float t)
Convert the given float to a long long.
static unsigned long safeConvert(const double t)
Convert the given double to an unsigned long, checking for overflow first.
static unsigned long long convert(const float t)
Convert the given float to an unsigned long long.
static short safeConvert(const int t)
Convert from int to short, checking for overflow first.
static long long safeConvert(const double t)
Convert the given double to long long, checking for overflow first.
static unsigned short convert(const double t)
Convert the given double to an unsigned short.
static short safeConvert(const std::string &t)
Convert the given std::string to a short, with checks.
static unsigned long convert(const float t)
Convert the given float to an unsigned long.
static unsigned long long safeConvert(const double t)
Convert the given double to unsigned long long, checking for overflow first.
static unsigned long long safeConvert(const float t)
Convert the given float to an unsigned long long, checking first for overflow.
static unsigned int convert(const long t)
Convert the given long to an unsigned int.