diff --git a/Foundation/include/Poco/DynamicAny.h b/Foundation/include/Poco/DynamicAny.h index 107a5cb88..3bff46328 100644 --- a/Foundation/include/Poco/DynamicAny.h +++ b/Foundation/include/Poco/DynamicAny.h @@ -177,28 +177,28 @@ public: template DynamicAny& operator = (const T& other) - /// Assignment operator + /// Assignment operator for assigning POD to DynamicAny { DynamicAny tmp(other); swap(tmp); return *this; } - + DynamicAny& operator = (const DynamicAny& other); /// Assignment operator specialization for DynamicAny template - DynamicAny operator + (const T& other) const - /// Addition operator + const DynamicAny operator + (const T& other) const + /// Addition operator for adding POD to DynamicAny { return convert() + other; } - DynamicAny operator + (const DynamicAny& other) const; + const DynamicAny operator + (const DynamicAny& other) const; /// Addition operator specialization for DynamicAny - DynamicAny operator + (const char* other) const; - /// Addition operator specialization for const char* + const DynamicAny operator + (const char* other) const; + /// Addition operator specialization for adding const char* to DynamicAny DynamicAny& operator ++ (); /// Pre-increment operator @@ -214,7 +214,7 @@ public: template DynamicAny& operator += (const T& other) - /// Addition asignment operator + /// Addition asignment operator for addition/assignment of POD to DynamicAny. { return *this = convert() + other; } @@ -226,13 +226,13 @@ public: /// Addition asignment operator specialization for const char* template - DynamicAny operator - (const T& other) const - /// Subtraction operator + const DynamicAny operator - (const T& other) const + /// Subtraction operator for subtracting POD from DynamicAny { return convert() - other; } - DynamicAny operator - (const DynamicAny& other) const; + const DynamicAny operator - (const DynamicAny& other) const; /// Subtraction operator specialization for DynamicAny template @@ -246,13 +246,13 @@ public: /// Subtraction asignment operator specialization for DynamicAny template - DynamicAny operator * (const T& other) const - /// Multiplication operator + const DynamicAny operator * (const T& other) const + /// Multiplication operator for multiplying DynamicAny with POD { return convert() * other; } - DynamicAny operator * (const DynamicAny& other) const; + const DynamicAny operator * (const DynamicAny& other) const; /// Multiplication operator specialization for DynamicAny template @@ -266,13 +266,13 @@ public: /// Multiplication asignment operator specialization for DynamicAny template - DynamicAny operator / (const T& other) const - /// Division operator + const DynamicAny operator / (const T& other) const + /// Division operator for dividing DynamicAny with POD { return convert() / other; } - DynamicAny operator / (const DynamicAny& other) const; + const DynamicAny operator / (const DynamicAny& other) const; /// Division operator specialization for DynamicAny template @@ -295,6 +295,9 @@ public: bool operator == (const char* other) const; /// Equality operator specialization for const char* + bool operator == (const DynamicAny& other) const; + /// Equality operator specialization for DynamicAny + template bool operator != (const T& other) const /// Inequality operator @@ -302,6 +305,9 @@ public: return convert() != other; } + bool operator != (const DynamicAny& other) const; + /// Inequality operator specialization for DynamicAny + bool operator != (const char* other) const; /// Inequality operator specialization for const char* @@ -387,6 +393,15 @@ private: }; +/// +/// inlines +/// + + +/// +/// DynamicAny members +/// + inline void DynamicAny::swap(DynamicAny& ptr) { std::swap(_pHolder, ptr._pHolder); @@ -399,7 +414,7 @@ inline const std::type_info& DynamicAny::type() const } -inline DynamicAny DynamicAny::operator + (const char* other) const +inline const DynamicAny DynamicAny::operator + (const char* other) const { return convert() + other; } @@ -411,12 +426,24 @@ inline DynamicAny& DynamicAny::operator += (const char*other) } +inline bool DynamicAny::operator == (const DynamicAny& other) const +{ + return convert() == other.convert(); +} + + inline bool DynamicAny::operator == (const char* other) const { return convert() == other; } +inline bool DynamicAny::operator != (const DynamicAny& other) const +{ + return convert() != other.convert(); +} + + inline bool DynamicAny::operator != (const char* other) const { return convert() != other; @@ -459,6 +486,98 @@ inline bool DynamicAny::isString() const } +/// +/// DynamicAny non-member functions +/// + +template +inline const DynamicAny operator + (const T& other, const DynamicAny& da) + /// Addition operator for adding DynamicAny to POD +{ + return da.convert() + other; +} + + +inline const DynamicAny operator + (const char* other, const DynamicAny& da) + /// Addition operator for adding DynamicAny to const char* +{ + std::string tmp = other; + return other + da.convert(); +} + + +template +inline const DynamicAny operator - (const T& other, const DynamicAny& da) + /// Subtraction operator for subtracting DynamicAny from POD +{ + DynamicAny tmp = other; + return tmp - da.convert(); +} + + +template +inline const DynamicAny operator * (const T& other, const DynamicAny& da) + /// Multiplication operator for multiplying POD with DynamicAny +{ + return da.convert() * other; +} + + +template +inline const DynamicAny operator / (const T& other, const DynamicAny& da) + /// Division operator for dividing POD with DynamicAny +{ + DynamicAny tmp = other; + return tmp / da.convert(); +} + + +template +inline T& operator += (T& other, const DynamicAny& da) + /// Addition asignment operator for addition/assignment of DynamicAny to POD. +{ + return other += da.convert(); +} + + +template +inline T& operator -= (T& other, const DynamicAny& da) + /// Subtraction asignment operator for subtraction/assignment of DynamicAny to POD. +{ + return other -= da.convert(); +} + + +template +inline T& operator *= (T& other, const DynamicAny& da) + /// Multiplication asignment operator for multiplication/assignment of DynamicAny to POD. +{ + return other *= da.convert(); +} + + +template +inline T& operator /= (T& other, const DynamicAny& da) + /// Division asignment operator for division/assignment of DynamicAny to POD. +{ + return other /= da.convert(); +} + + +inline bool operator == (const std::string& other, const DynamicAny& da) + /// Equality operator for DynamicAny comparison to std::string +{ + return da.convert() == other; +} + + +inline bool operator != (const std::string& other, const DynamicAny& da) + /// Equality operator for DynamicAny comparison to std::string +{ + return da.convert() != other; +} + + } // namespace Poco diff --git a/Foundation/src/DynamicAny.cpp b/Foundation/src/DynamicAny.cpp index ae537adff..35cd200fa 100644 --- a/Foundation/src/DynamicAny.cpp +++ b/Foundation/src/DynamicAny.cpp @@ -77,7 +77,7 @@ DynamicAny& DynamicAny::operator = (const DynamicAny& other) } -DynamicAny DynamicAny::operator + (const DynamicAny& other) const +const DynamicAny DynamicAny::operator + (const DynamicAny& other) const { if (isInteger()) { @@ -113,7 +113,7 @@ DynamicAny& DynamicAny::operator += (const DynamicAny& other) } -DynamicAny DynamicAny::operator - (const DynamicAny& other) const +const DynamicAny DynamicAny::operator - (const DynamicAny& other) const { if (isInteger()) { @@ -145,7 +145,7 @@ DynamicAny& DynamicAny::operator -= (const DynamicAny& other) } -DynamicAny DynamicAny::operator * (const DynamicAny& other) const +const DynamicAny DynamicAny::operator * (const DynamicAny& other) const { if (isInteger()) { @@ -177,7 +177,7 @@ DynamicAny& DynamicAny::operator *= (const DynamicAny& other) } -DynamicAny DynamicAny::operator / (const DynamicAny& other) const +const DynamicAny DynamicAny::operator / (const DynamicAny& other) const { if (isInteger()) { diff --git a/Foundation/testsuite/src/DynamicAnyTest.cpp b/Foundation/testsuite/src/DynamicAnyTest.cpp index dd5b292c1..6e7397425 100644 --- a/Foundation/testsuite/src/DynamicAnyTest.cpp +++ b/Foundation/testsuite/src/DynamicAnyTest.cpp @@ -1327,16 +1327,20 @@ void DynamicAnyTest::testULong() assert (a3 == 64); } - + void DynamicAnyTest::testConversionOperator() { DynamicAny any("42"); int i = any; assert (i == 42); - + assert (any == i); + any = 123; std::string s = any; assert (s == "123"); + assert (s == any); + assert (any == s); + assert ("123" == any); any = 321; s = any.convert(); @@ -1344,10 +1348,29 @@ void DynamicAnyTest::testConversionOperator() any = "456"; assert (any == "456"); + assert ("456" == any); DynamicAny any2 = "1.5"; double d = any2; assert (d == 1.5); + assert (any2 == d); +} + + +void DynamicAnyTest::testComparisonOperators() +{ + DynamicAny any1 = 1; + DynamicAny any2 = "1"; + assert (any1 == any2); + assert (any1 == 1); + assert (any1 == "1"); + assert ("1" == any1); + + any1 = "2"; + assert (any1 != any2); + assert (any1 != 1); + assert (any1 != "1"); + assert ("1" != any1); } @@ -1357,27 +1380,42 @@ void DynamicAnyTest::testArithmeticOperators() DynamicAny any2 = 2; DynamicAny any3 = any1 + any2; assert (any3 == 3); + int i = 1; + i += any1; + assert (2 == i); any1 = 3; + assert ((5 - any1) == 2); any2 = 5; any3 = any2 - any1; assert (any3 == 2); any3 -= 1; assert (any3 == 1); + i = 5; + i -= any1; + assert (2 == i); any1 = 3; + assert ((5 * any1) == 15); any2 = 5; any3 = any1 * any2; assert (any3 == 15); any3 *= 3; assert (any3 == 45); + i = 5; + i *= any1; + assert (15 == i); any1 = 3; + assert ((9 / any1) == 3); any2 = 9; any3 = any2 / any1; assert (any3 == 3); any3 /= 3; assert (any3 == 1); + i = 9; + i /= any1; + assert (3 == i); any1 = 1.0f; any2 = .5f; @@ -1402,6 +1440,7 @@ void DynamicAnyTest::testArithmeticOperators() any2 = "4"; any3 += any2; assert (any3 == 7); + assert (1 + any3 == 8); any1 = "123"; any2 = "456"; @@ -1410,6 +1449,7 @@ void DynamicAnyTest::testArithmeticOperators() any2 = "789"; any3 += any2; assert (any3 == "123456789"); + assert (("xyz" + any3) == "xyz123456789"); try { any3 = any1 - any2; fail ("must fail"); } catch (InvalidArgumentException&){} @@ -2054,8 +2094,9 @@ void DynamicAnyTest::testGetIdxMustThrow(DynamicAny& a1, std::vector { try { - DynamicAny& val1 = a1[n]; + DynamicAny& val1 = a1[n]; fail("bad cast - must throw"); + val1 = 0; // silence the compiler } catch (Poco::BadCastException&) { @@ -2064,8 +2105,9 @@ void DynamicAnyTest::testGetIdxMustThrow(DynamicAny& a1, std::vector try { const DynamicAny& c1 = a1; - const DynamicAny& cval1 = c1[n]; + const DynamicAny& cval1 = c1[n]; fail("bad const cast - must throw"); + assert (cval1 == c1); // silence the compiler } catch (Poco::BadCastException&) { @@ -2103,6 +2145,7 @@ CppUnit::Test* DynamicAnyTest::suite() CppUnit_addTest(pSuite, DynamicAnyTest, testLong); CppUnit_addTest(pSuite, DynamicAnyTest, testULong); CppUnit_addTest(pSuite, DynamicAnyTest, testConversionOperator); + CppUnit_addTest(pSuite, DynamicAnyTest, testComparisonOperators); CppUnit_addTest(pSuite, DynamicAnyTest, testArithmeticOperators); CppUnit_addTest(pSuite, DynamicAnyTest, testLimitsInt); CppUnit_addTest(pSuite, DynamicAnyTest, testLimitsFloat); diff --git a/Foundation/testsuite/src/DynamicAnyTest.h b/Foundation/testsuite/src/DynamicAnyTest.h index d9341dd82..7990dd51c 100644 --- a/Foundation/testsuite/src/DynamicAnyTest.h +++ b/Foundation/testsuite/src/DynamicAnyTest.h @@ -63,6 +63,7 @@ public: void testULong(); void testString(); void testConversionOperator(); + void testComparisonOperators(); void testArithmeticOperators(); void testLimitsInt(); void testLimitsFloat(); @@ -96,6 +97,7 @@ private: assert (val1 == expectedResult); const Poco::DynamicAny& c1 = a1; + assert (a1 == c1); // silence the compiler const Poco::DynamicAny& cval1 = a1[n]; assert (cval1 == expectedResult); }