public
Description: A double-entry accounting system with a command-line reporting interface
Homepage: http://www.newartisans.com/software/ledger.html
Clone URL: git://github.com/jwiegley/ledger.git
Search Repo:
*** no comment ***
jwiegley (author)
Sun Mar 19 15:16:31 -0800 2006
commit  f60717d3f4bd3f3c04e08edbe459495d00936d0c
tree    b573b4e23dbcec06d7f4b79835127a2d64db5db7
parent  7adb262823377bc40085ba5b66a6448a3d58db0b
...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
502
503
504
505
506
507
508
 
 
 
 
 
 
509
510
511
...
473
474
475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
 
 
 
 
 
514
515
516
517
518
519
520
521
522
0
@@ -473,39 +473,50 @@ int amount_t::sign() const
0
   return quantity ? mpz_sgn(MPZ(quantity)) : 0;
0
 }
0
 
0
-// comparisons between amounts
0
-#define AMOUNT_CMP_AMOUNT(OP) \
0
-bool amount_t::operator OP(const amount_t& amt) const \
0
-{ \
0
- if (! quantity) \
0
- return 0 OP amt; \
0
- if (! amt.quantity) \
0
- return *this OP 0; \
0
- \
0
- if (commodity() && amt.commodity() && \
0
- commodity() != amt.commodity()) \
0
- return false; \
0
- \
0
- if (quantity->prec == amt.quantity->prec) { \
0
- return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)) OP 0; \
0
- } \
0
- else if (quantity->prec < amt.quantity->prec) { \
0
- amount_t temp = *this; \
0
- temp._resize(amt.quantity->prec); \
0
- return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)) OP 0; \
0
- } \
0
- else { \
0
- amount_t temp = amt; \
0
- temp._resize(quantity->prec); \
0
- return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)) OP 0; \
0
- } \
0
+int amount_t::compare(const amount_t& amt) const
0
+{
0
+ if (! quantity) {
0
+ if (! amt.quantity)
0
+ return 0;
0
+ return - amt.sign();
0
+ }
0
+ if (! amt.quantity)
0
+ return sign();
0
+
0
+ if (commodity() && amt.commodity() &&
0
+ commodity() != amt.commodity())
0
+ throw new amount_error
0
+ (std::string("Cannot compare amounts with different commodities: ") +
0
+ commodity().symbol + " and " + amt.commodity().symbol);
0
+
0
+ if (quantity->prec == amt.quantity->prec) {
0
+ return mpz_cmp(MPZ(quantity), MPZ(amt.quantity));
0
+ }
0
+ else if (quantity->prec < amt.quantity->prec) {
0
+ amount_t temp = *this;
0
+ temp._resize(amt.quantity->prec);
0
+ return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity));
0
+ }
0
+ else {
0
+ amount_t temp = amt;
0
+ temp._resize(quantity->prec);
0
+ return mpz_cmp(MPZ(quantity), MPZ(temp.quantity));
0
+ }
0
+}
0
+
0
+bool amount_t::operator==(const amount_t& amt) const
0
+{
0
+ if (commodity() != amt.commodity())
0
+ return false;
0
+ return compare(amt) == 0;
0
 }
0
 
0
-AMOUNT_CMP_AMOUNT(<)
0
-AMOUNT_CMP_AMOUNT(<=)
0
-AMOUNT_CMP_AMOUNT(>)
0
-AMOUNT_CMP_AMOUNT(>=)
0
-AMOUNT_CMP_AMOUNT(==)
0
+bool amount_t::operator!=(const amount_t& amt) const
0
+{
0
+ if (commodity() != amt.commodity())
0
+ return true;
0
+ return compare(amt) != 0;
0
+}
0
 
0
 amount_t::operator bool() const
0
 {
...
181
182
183
184
185
186
187
188
189
190
191
192
 
 
 
 
 
 
 
193
 
 
 
 
 
 
 
 
194
195
196
...
181
182
183
 
 
 
 
 
 
 
 
 
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
0
@@ -181,16 +181,22 @@ class amount_t
0
   operator double() const;
0
 
0
   // comparisons between amounts
0
- bool operator<(const amount_t& amt) const;
0
- bool operator<=(const amount_t& amt) const;
0
- bool operator>(const amount_t& amt) const;
0
- bool operator>=(const amount_t& amt) const;
0
- bool operator==(const amount_t& amt) const;
0
- bool operator!=(const amount_t& amt) const {
0
- if (commodity_ != amt.commodity_)
0
- return true;
0
- return ! (*this == amt);
0
+ int compare(const amount_t& amt) const;
0
+
0
+ bool operator<(const amount_t& amt) const {
0
+ return compare(amt) < 0;
0
+ }
0
+ bool operator<=(const amount_t& amt) const {
0
+ return compare(amt) <= 0;
0
   }
0
+ bool operator>(const amount_t& amt) const {
0
+ return compare(amt) > 0;
0
+ }
0
+ bool operator>=(const amount_t& amt) const {
0
+ return compare(amt) >= 0;
0
+ }
0
+ bool operator==(const amount_t& amt) const;
0
+ bool operator!=(const amount_t& amt) const;
0
 
0
   template <typename T>
0
   void parse_num(T num) {

Comments

    No one has commented yet.