summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-02-27 02:15:27 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-02-27 02:15:27 -0400
commitfbb0d2583173073e11976c7cd7e570d4829f0621 (patch)
tree7c83465d30bd45f89f82757d5f3041a9e3903f1f
parentecb1ca71c12f7772d345f19b6df821da123d3b57 (diff)
downloadfork-ledger-fbb0d2583173073e11976c7cd7e570d4829f0621.tar.gz
fork-ledger-fbb0d2583173073e11976c7cd7e570d4829f0621.tar.bz2
fork-ledger-fbb0d2583173073e11976c7cd7e570d4829f0621.zip
Gave round/unround/truncate all in_place_ variants
-rw-r--r--src/amount.cc22
-rw-r--r--src/amount.h23
-rw-r--r--src/balance.h21
-rw-r--r--src/value.cc42
-rw-r--r--src/value.h23
5 files changed, 89 insertions, 42 deletions
diff --git a/src/amount.cc b/src/amount.cc
index 885eaf43..d7cdbacb 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -464,32 +464,26 @@ amount_t amount_t::inverted() const
return t;
}
-amount_t amount_t::rounded() const
+void amount_t::in_place_round()
{
if (! quantity)
throw_(amount_error, _("Cannot set rounding for an uninitialized amount"));
else if (! keep_precision())
- return *this;
-
- amount_t t(*this);
- t._dup();
- t.set_keep_precision(false);
+ return;
- return t;
+ _dup();
+ set_keep_precision(false);
}
-amount_t amount_t::unrounded() const
+void amount_t::in_place_unround()
{
if (! quantity)
throw_(amount_error, _("Cannot unround an uninitialized amount"));
else if (keep_precision())
- return *this;
-
- amount_t t(*this);
- t._dup();
- t.set_keep_precision(true);
+ return;
- return t;
+ _dup();
+ set_keep_precision(true);
}
void amount_t::in_place_reduce()
diff --git a/src/amount.h b/src/amount.h
index 90f1359c..52cb2be9 100644
--- a/src/amount.h
+++ b/src/amount.h
@@ -320,17 +320,32 @@ public:
default state of an amount, but if one has become unrounded, this
sets the "keep precision" state back to false.
@see set_keep_precision */
- amount_t rounded() const;
+ amount_t rounded() const {
+ amount_t temp(*this);
+ temp.in_place_round();
+ return temp;
+ }
+ void in_place_round();
/** Yields an amount which has lost all of its extra precision, beyond what
the display precision of the commodity would have printed. */
amount_t truncated() const {
- return amount_t(to_string());
+ amount_t temp(*this);
+ temp.in_place_truncate();
+ return temp;
}
-
+ void in_place_truncate() {
+ *this = amount_t(to_string());
+ }
+
/** Yields an amount whose display precision is never truncated, even
though its commodity normally displays only rounded values. */
- amount_t unrounded() const;
+ amount_t unrounded() const {
+ amount_t temp(*this);
+ temp.in_place_unround();
+ return temp;
+ }
+ void in_place_unround();
/** reduces a value to its most basic commodity form, for amounts that
utilize "scaling commodities". For example, an amount of \c 1h
diff --git a/src/balance.h b/src/balance.h
index f8e4f25c..71fc41be 100644
--- a/src/balance.h
+++ b/src/balance.h
@@ -312,24 +312,39 @@ public:
}
balance_t rounded() const {
+ balance_t temp(*this);
+ temp.in_place_round();
+ return temp;
+ }
+ void in_place_round() {
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.rounded();
- return temp;
+ *this = temp;
}
balance_t truncated() const {
+ balance_t temp(*this);
+ temp.in_place_truncate();
+ return temp;
+ }
+ void in_place_truncate() {
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.truncated();
- return temp;
+ *this = temp;
}
balance_t unrounded() const {
+ balance_t temp(*this);
+ temp.in_place_unround();
+ return temp;
+ }
+ void in_place_unround() {
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.unrounded();
- return temp;
+ *this = temp;
}
balance_t reduced() const {
diff --git a/src/value.cc b/src/value.cc
index 755c18bb..556fc152 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -1280,73 +1280,79 @@ value_t value_t::abs() const
return NULL_VALUE;
}
-value_t value_t::rounded() const
+void value_t::in_place_round()
{
switch (type()) {
case INTEGER:
- return *this;
+ return;
case AMOUNT:
- return as_amount().rounded();
+ as_amount_lval().in_place_round();
+ return;
case BALANCE:
- return as_balance().rounded();
+ as_balance_lval().in_place_round();
+ return;
case SEQUENCE: {
value_t temp;
foreach (const value_t& value, as_sequence())
temp.push_back(value.rounded());
- return temp;
+ *this = temp;
+ return;
}
default:
break;
}
throw_(value_error, _("Cannot set rounding for %1") << label());
- return NULL_VALUE;
}
-value_t value_t::truncated() const
+void value_t::in_place_truncate()
{
switch (type()) {
case INTEGER:
- return *this;
+ return;
case AMOUNT:
- return as_amount().truncated();
+ as_amount_lval().in_place_truncate();
+ return;
case BALANCE:
- return as_balance().truncated();
+ as_balance_lval().in_place_truncate();
+ return;
case SEQUENCE: {
value_t temp;
foreach (const value_t& value, as_sequence())
temp.push_back(value.truncated());
- return temp;
+ *this = temp;
+ return;
}
default:
break;
}
throw_(value_error, _("Cannot truncate %1") << label());
- return NULL_VALUE;
}
-value_t value_t::unrounded() const
+void value_t::in_place_unround()
{
switch (type()) {
case INTEGER:
- return *this;
+ return;
case AMOUNT:
- return as_amount().unrounded();
+ as_amount_lval().unrounded();
+ return;
case BALANCE:
- return as_balance().unrounded();
+ as_balance_lval().unrounded();
+ return;
case SEQUENCE: {
value_t temp;
foreach (const value_t& value, as_sequence())
temp.push_back(value.unrounded());
- return temp;
+ *this = temp;
+ return;
}
default:
break;
}
throw_(value_error, _("Cannot unround %1") << label());
- return NULL_VALUE;
}
void value_t::annotate(const annotation_t& details)
diff --git a/src/value.h b/src/value.h
index 4d019450..35d7f6a8 100644
--- a/src/value.h
+++ b/src/value.h
@@ -409,9 +409,26 @@ public:
value_t abs() const;
- value_t rounded() const;
- value_t truncated() const;
- value_t unrounded() const;
+ value_t rounded() const {
+ value_t temp(*this);
+ temp.in_place_round();
+ return temp;
+ }
+ void in_place_round();
+
+ value_t truncated() const {
+ value_t temp(*this);
+ temp.in_place_truncate();
+ return temp;
+ }
+ void in_place_truncate();
+
+ value_t unrounded() const {
+ value_t temp(*this);
+ temp.in_place_unround();
+ return temp;
+ }
+ void in_place_unround();
value_t reduced() const {
value_t temp(*this);