summaryrefslogtreecommitdiff
path: root/value.h
diff options
context:
space:
mode:
Diffstat (limited to 'value.h')
-rw-r--r--value.h69
1 files changed, 44 insertions, 25 deletions
diff --git a/value.h b/value.h
index e2fbda66..b0687d64 100644
--- a/value.h
+++ b/value.h
@@ -37,7 +37,7 @@ class value_t
type = INTEGER;
}
- value_t(const value_t& value) {
+ value_t(const value_t& value) : type(INTEGER) {
DEBUG_PRINT("ledger.memory.ctors", "ctor value_t");
*this = value;
}
@@ -51,20 +51,17 @@ class value_t
*((unsigned int *) data) = value;
type = INTEGER;
}
- value_t(const amount_t& value) {
+ value_t(const amount_t& value) : type(INTEGER) {
DEBUG_PRINT("ledger.memory.ctors", "ctor value_t");
- new((amount_t *)data) amount_t(value);
- type = AMOUNT;
+ *this = value;
}
- value_t(const balance_t& value) {
+ value_t(const balance_t& value) : type(INTEGER) {
DEBUG_PRINT("ledger.memory.ctors", "ctor value_t");
- new((balance_t *)data) balance_t(value);
- type = BALANCE;
+ *this = value;
}
- value_t(const balance_pair_t& value) {
+ value_t(const balance_pair_t& value) : type(INTEGER) {
DEBUG_PRINT("ledger.memory.ctors", "ctor value_t");
- new((balance_pair_t *)data) balance_pair_t(value);
- type = BALANCE_PAIR;
+ *this = value;
}
~value_t() {
@@ -76,33 +73,55 @@ class value_t
value_t& operator=(const value_t& value);
value_t& operator=(const bool value) {
- destroy();
- *((bool *) data) = value;
- type = BOOLEAN;
+ if ((bool *) data != &value) {
+ destroy();
+ *((bool *) data) = value;
+ type = BOOLEAN;
+ }
return *this;
}
value_t& operator=(const unsigned int value) {
- destroy();
- *((unsigned int *) data) = value;
- type = INTEGER;
+ if ((unsigned int *) data != &value) {
+ destroy();
+ *((unsigned int *) data) = value;
+ type = INTEGER;
+ }
return *this;
}
value_t& operator=(const amount_t& value) {
- destroy();
- new((amount_t *)data) amount_t(value);
- type = AMOUNT;
+ if ((amount_t *) data != &value) {
+ if (! value) {
+ return *this = 0U;
+ } else {
+ destroy();
+ new((amount_t *)data) amount_t(value);
+ type = AMOUNT;
+ }
+ }
return *this;
}
value_t& operator=(const balance_t& value) {
- destroy();
- new((balance_t *)data) balance_t(value);
- type = BALANCE;
+ if ((balance_t *) data != &value) {
+ if (value.amounts.size() == 1) {
+ return *this = (*value.amounts.begin()).second;
+ } else {
+ destroy();
+ new((balance_t *)data) balance_t(value);
+ type = BALANCE;
+ }
+ }
return *this;
}
value_t& operator=(const balance_pair_t& value) {
- destroy();
- new((balance_pair_t *)data) balance_pair_t(value);
- type = BALANCE_PAIR;
+ if ((balance_pair_t *) data != &value) {
+ if (! value.cost) {
+ return *this = value.quantity;
+ } else {
+ destroy();
+ new((balance_pair_t *)data) balance_pair_t(value);
+ type = BALANCE_PAIR;
+ }
+ }
return *this;
}