++ and -- operators for DynamicAny

This commit is contained in:
Aleksandar Fabijanic 2007-09-06 11:16:27 +00:00
parent a86dc7a393
commit 009db61570
3 changed files with 76 additions and 0 deletions

View File

@ -79,6 +79,9 @@ class Foundation_API DynamicAny
/// - for integral and floating point numeric values, following operations are supported:
/// '+', '+=', '-', '-=', '*', '*=' , '/' and '/='
///
/// - for integral values, following operations are supported:
/// prefix and postfix increment (++) and decement (--)
///
/// - for all other types, InvalidArgumentException is thrown upon attempt of an arithmetic operation
///
/// A DynamicAny can be created from and converted to a value of any type for which a specialization of
@ -197,6 +200,18 @@ public:
DynamicAny operator + (const char* other) const;
/// Addition operator specialization for const char*
DynamicAny& operator ++ ();
/// Pre-increment operator
DynamicAny operator ++ (int);
/// Post-increment operator
DynamicAny& operator -- ();
/// Pre-decrement operator
DynamicAny operator -- (int);
/// Post-decrement operator
template <typename T>
DynamicAny& operator += (const T& other)
/// Addition asignment operator

View File

@ -209,6 +209,43 @@ DynamicAny& DynamicAny::operator /= (const DynamicAny& other)
}
DynamicAny& DynamicAny::operator ++ ()
{
if (!isInteger())
throw InvalidArgumentException("Invalid operation for this data type.");
return *this = *this + 1;
}
DynamicAny DynamicAny::operator ++ (int)
{
if (!isInteger())
throw InvalidArgumentException("Invalid operation for this data type.");
DynamicAny tmp(*this);
*this += 1;
return tmp;
}
DynamicAny& DynamicAny::operator -- ()
{
if (!isInteger())
throw InvalidArgumentException("Invalid operation for this data type.");
return *this = *this - 1;
}
DynamicAny DynamicAny::operator -- (int)
{
if (!isInteger())
throw InvalidArgumentException("Invalid operation for this data type.");
DynamicAny tmp(*this);
*this -= 1;
return tmp;
}
DynamicAny& DynamicAny::operator [] (std::vector<DynamicAny>::size_type n)
{
DynamicAnyHolderImpl<std::vector<DynamicAny> >* pHolder = dynamic_cast<DynamicAnyHolderImpl<std::vector<DynamicAny> > *>(_pHolder);

View File

@ -1427,6 +1427,30 @@ void DynamicAnyTest::testArithmeticOperators()
try { any3 /= any2; fail ("must fail"); }
catch (InvalidArgumentException&){}
any1 = 10;
assert (any1++ == 10);
assert (any1 == 11);
assert (++any1 == 12);
assert (any1-- == 12);
assert (any1 == 11);
assert (--any1 == 10);
any1 = 1.23;
try { ++any1; fail ("must fail"); }
catch (InvalidArgumentException&){}
try { any1++; fail ("must fail"); }
catch (InvalidArgumentException&){}
try { --any1; fail ("must fail"); }
catch (InvalidArgumentException&){}
try { any1--; fail ("must fail"); }
catch (InvalidArgumentException&){}
}