Reconfigure some of the operators.

This commit is contained in:
John Wiegley 2007-05-07 10:27:29 +00:00
parent d849837203
commit 6ec2f6b59b
5 changed files with 29 additions and 40 deletions

View file

@ -1,30 +0,0 @@
/*
* Copyright (c) 2003-2007, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

View file

@ -41,7 +41,9 @@ class balance_t
equality_comparable<balance_t, amount_t,
additive<balance_t,
additive<balance_t, amount_t,
multiplicative<balance_t, amount_t> > > > >
multiplicative<balance_t, amount_t,
multiplicative<balance_t, unsigned long,
multiplicative<balance_t, long> > > > > > >
{
public:
typedef std::map<const commodity_t *, amount_t> amounts_map;
@ -235,7 +237,9 @@ class balance_pair_t
additive<balance_pair_t,
additive<balance_pair_t, balance_t,
additive<balance_pair_t, amount_t,
multiplicative<balance_pair_t, amount_t> > > > > > >
multiplicative<balance_pair_t, unsigned long,
multiplicative<balance_pair_t, long,
multiplicative<balance_pair_t, amount_t> > > > > > > > >
{
balance_t quantity;
optional<balance_t> cost;
@ -298,6 +302,9 @@ public:
bool operator==(const balance_t& bal) const {
return quantity == bal;
}
bool operator==(const amount_t& amt) const {
return quantity == amt;
}
balance_pair_t& operator*=(const amount_t& amt) {
quantity *= amt;

View file

@ -598,7 +598,7 @@ value_t& value_t::operator*=(const value_t& val)
case BALANCE:
switch (val.type) {
case INTEGER:
as_balance() *= val.to_amount();
as_balance() *= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
@ -612,7 +612,7 @@ value_t& value_t::operator*=(const value_t& val)
case BALANCE_PAIR:
switch (val.type) {
case INTEGER:
as_balance_pair() *= val.to_amount();
as_balance_pair() *= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
@ -667,7 +667,7 @@ value_t& value_t::operator/=(const value_t& val)
case BALANCE:
switch (val.type) {
case INTEGER:
as_balance() /= val.to_amount();
as_balance() /= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
@ -681,7 +681,7 @@ value_t& value_t::operator/=(const value_t& val)
case BALANCE_PAIR:
switch (val.type) {
case INTEGER:
as_balance_pair() /= val.to_amount();
as_balance_pair() /= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
@ -727,7 +727,7 @@ bool value_t::operator==(const value_t& val) const
case BALANCE:
return val.as_balance() == to_amount();
case BALANCE_PAIR:
return val.as_balance_pair() == to_balance();
return val.as_balance_pair() == to_amount();
default:
break;
}
@ -742,7 +742,7 @@ bool value_t::operator==(const value_t& val) const
case BALANCE:
return val.as_balance() == as_amount();
case BALANCE_PAIR:
return val.as_balance_pair() == to_balance();
return val.as_balance_pair() == as_amount();
default:
break;
}
@ -766,9 +766,9 @@ bool value_t::operator==(const value_t& val) const
case BALANCE_PAIR:
switch (val.type) {
case INTEGER:
return as_balance_pair() == val.to_balance();
return as_balance_pair() == val.to_amount();
case AMOUNT:
return as_balance_pair() == val.to_balance();
return as_balance_pair() == val.as_amount();
case BALANCE:
return as_balance_pair() == val.as_balance();
case BALANCE_PAIR:

View file

@ -149,6 +149,13 @@ void BasicAmountTestCase::testComparisons()
CPPUNIT_ASSERT(x3 < x1);
CPPUNIT_ASSERT(x3 < x4);
CPPUNIT_ASSERT(x1 < 100L);
CPPUNIT_ASSERT(x1 < 100UL);
CPPUNIT_ASSERT(x1 < 100.0);
CPPUNIT_ASSERT(100L > x1);
CPPUNIT_ASSERT(100UL > x1);
CPPUNIT_ASSERT(100.0 > x1);
CPPUNIT_ASSERT(x0.valid());
CPPUNIT_ASSERT(x1.valid());
CPPUNIT_ASSERT(x2.valid());

View file

@ -475,6 +475,11 @@ class BasicAmountTestCase(unittest.TestCase):
self.assertTrue(x3 < x1)
self.assertTrue(x3 < x4)
self.assertTrue(x1 < 100)
self.assertTrue(x1 < 100.0)
self.assertTrue(100 > x1)
self.assertTrue(100.0 > x1)
self.assertTrue(x0.valid())
self.assertTrue(x1.valid())
self.assertTrue(x2.valid())