From 0a65eefd9a17f4b7ba577c41c9e88f8be146202c Mon Sep 17 00:00:00 2001 From: Aleksandar Fabijanic Date: Tue, 4 Sep 2007 20:14:40 +0000 Subject: [PATCH] Eliminate some gcc warnings --- Foundation/include/Poco/DynamicAnyHolder.h | 64 ++++++++++++++++----- Foundation/testsuite/src/DynamicAnyTest.cpp | 35 ++++++----- 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/Foundation/include/Poco/DynamicAnyHolder.h b/Foundation/include/Poco/DynamicAnyHolder.h index 76b0d56ac..ffd559638 100644 --- a/Foundation/include/Poco/DynamicAnyHolder.h +++ b/Foundation/include/Poco/DynamicAnyHolder.h @@ -116,7 +116,7 @@ public: protected: template void convertToSmaller(const F& from, T& to) const - /// This function is meant to convert signed integral values from + /// This function is meant to convert signed numeric values from /// larger to smaller type. It checks the upper and lower bound and /// if from value is within limits of type T (i.e. check calls do not throw), /// it is converted. @@ -126,7 +126,11 @@ protected: poco_static_assert (std::numeric_limits::is_signed); poco_static_assert (std::numeric_limits::is_signed); - checkUpperLimit(from, to); + if (std::numeric_limits::is_integer) + checkUpperLimit(from, to); + else + checkUpperLimitFloat(from, to); + checkLowerLimit(from, to); to = static_cast(from); } @@ -153,7 +157,7 @@ protected: /// This function is meant for converting signed integral data types to /// unsigned data types. Negative values can not be converted and if one is /// encountered, RangeException is thrown. - /// If uper limit is within the target data type limits, the converiosn is performed. + /// If uper limit is within the target data type limits, the conversion is performed. { poco_static_assert (std::numeric_limits::is_specialized); poco_static_assert (std::numeric_limits::is_specialized); @@ -166,12 +170,31 @@ protected: to = static_cast(from); } + template + void convertSignedFloatToUnsigned(const F& from, T& to) const + /// This function is meant for converting floating point data types to + /// unsigned integral data types. Negative values can not be converted and if one is + /// encountered, RangeException is thrown. + /// If uper limit is within the target data type limits, the conversion is performed. + { + poco_static_assert (std::numeric_limits::is_specialized); + poco_static_assert (std::numeric_limits::is_specialized); + poco_static_assert (!std::numeric_limits::is_integer); + poco_static_assert (std::numeric_limits::is_integer); + poco_static_assert (!std::numeric_limits::is_signed); + + if (from < 0) + throw RangeException("Value too small."); + checkUpperLimitFloat(from, to); + to = static_cast(from); + } + template void convertUnsignedToSigned(const F& from, T& to) const /// This function is meant for converting unsigned integral data types to /// unsigned data types. Negative values can not be converted and if one is /// encountered, RangeException is thrown. - /// If uper limit is within the target data type limits, the converiosn is performed. + /// If upper limit is within the target data type limits, the converiosn is performed. { poco_static_assert (std::numeric_limits::is_specialized); poco_static_assert (std::numeric_limits::is_specialized); @@ -186,7 +209,22 @@ private: template void checkUpperLimit(const F& from, T& to) const { - if (from > std::numeric_limits::max()) + if ((sizeof(T) < sizeof(F)) && + (from > static_cast(std::numeric_limits::max()))) + { + throw RangeException("Value too large."); + } + else + if (static_cast(from) > std::numeric_limits::max()) + { + throw RangeException("Value too large."); + } + } + + template + void checkUpperLimitFloat(const F& from, T& to) const + { + if (from > std::numeric_limits::max()) throw RangeException("Value too large."); } @@ -1505,22 +1543,22 @@ public: void convert(UInt8& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt16& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt32& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt64& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(bool& val) const @@ -1630,22 +1668,22 @@ public: void convert(UInt8& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt16& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt32& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(UInt64& val) const { - convertSignedToUnsigned(_val, val); + convertSignedFloatToUnsigned(_val, val); } void convert(bool& val) const diff --git a/Foundation/testsuite/src/DynamicAnyTest.cpp b/Foundation/testsuite/src/DynamicAnyTest.cpp index e0d2578bd..305ed5424 100644 --- a/Foundation/testsuite/src/DynamicAnyTest.cpp +++ b/Foundation/testsuite/src/DynamicAnyTest.cpp @@ -114,7 +114,7 @@ void DynamicAnyTest::testInt8() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -185,7 +185,7 @@ void DynamicAnyTest::testInt16() try { - Int32 value2 = a1.extract(); + Int32 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -256,7 +256,7 @@ void DynamicAnyTest::testInt32() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -327,7 +327,7 @@ void DynamicAnyTest::testInt64() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -398,7 +398,7 @@ void DynamicAnyTest::testUInt8() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -469,7 +469,7 @@ void DynamicAnyTest::testUInt16() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -540,7 +540,7 @@ void DynamicAnyTest::testUInt32() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -611,7 +611,7 @@ void DynamicAnyTest::testUInt64() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -682,7 +682,7 @@ void DynamicAnyTest::testBool() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -753,7 +753,7 @@ void DynamicAnyTest::testChar() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -824,7 +824,7 @@ void DynamicAnyTest::testFloat() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -895,7 +895,7 @@ void DynamicAnyTest::testDouble() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -961,7 +961,7 @@ void DynamicAnyTest::testString() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -1032,7 +1032,7 @@ void DynamicAnyTest::testLong() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -1103,7 +1103,7 @@ void DynamicAnyTest::testULong() try { - Int16 value2 = a1.extract(); + Int16 value2; value2 = a1.extract(); fail("bad cast - must throw"); } catch (Poco::BadCastException&) @@ -1123,9 +1123,8 @@ void DynamicAnyTest::testConversionOperator() assert (s == "123"); any = 321; - //fails on gcc 3.4.4. - //s = (std::string) any;//must cast to disambiguate char/string - //assert (s == "321"); + s = any.convert(); + assert (s == "321"); any = "456"; assert (any == "456");