diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-16 05:37:26 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:52 -0400 |
commit | 52822604713b73160ac497bc170eb45d9a594306 (patch) | |
tree | 867027ab9186c04e7ecb2f101247f16307116e86 /src/value.cc | |
parent | d89f6e1c447fc5e7fcd0d6e5a236298323f9596e (diff) | |
download | fork-ledger-52822604713b73160ac497bc170eb45d9a594306.tar.gz fork-ledger-52822604713b73160ac497bc170eb45d9a594306.tar.bz2 fork-ledger-52822604713b73160ac497bc170eb45d9a594306.zip |
Modified value_t to use copy-on-write semantics.
Diffstat (limited to 'src/value.cc')
-rw-r--r-- | src/value.cc | 817 |
1 files changed, 383 insertions, 434 deletions
diff --git a/src/value.cc b/src/value.cc index b3578ae9..4089e0c3 100644 --- a/src/value.cc +++ b/src/value.cc @@ -34,9 +34,162 @@ namespace ledger { +intrusive_ptr<value_t::storage_t> value_t::true_value; +intrusive_ptr<value_t::storage_t> value_t::false_value; + +void value_t::storage_t::destroy() +{ + switch (type) { + case AMOUNT: + ((amount_t *)data)->~amount_t(); + break; + case BALANCE: + ((balance_t *)data)->~balance_t(); + break; + case BALANCE_PAIR: + ((balance_pair_t *)data)->~balance_pair_t(); + break; + case STRING: + ((string *)data)->~string(); + break; + case SEQUENCE: + ((sequence_t *)data)->~sequence_t(); + break; + + default: + break; + } + type = VOID; +} + +void value_t::initialize() +{ + true_value = new storage_t; + true_value->type = BOOLEAN; + *(bool *) true_value->data = true; + + false_value = new storage_t; + false_value->type = BOOLEAN; + *(bool *) false_value->data = false; +} + +void value_t::shutdown() +{ + true_value = intrusive_ptr<storage_t>(); + false_value = intrusive_ptr<storage_t>(); +} + +value_t& value_t::operator=(const value_t& val) +{ + if (this == &val || storage == val.storage) + return *this; + + if (type() == val.type()) + switch (type()) { + case BOOLEAN: + as_boolean_lval() = val.as_boolean(); + return *this; + case INTEGER: + as_long_lval() = val.as_long(); + return *this; + case DATETIME: + as_datetime_lval() = val.as_datetime(); + return *this; + case AMOUNT: + as_amount_lval() = val.as_amount(); + return *this; + case BALANCE: + as_balance_lval() = val.as_balance(); + return *this; + case BALANCE_PAIR: + as_balance_pair_lval() = val.as_balance_pair(); + return *this; + case STRING: + as_string_lval() = val.as_string(); + return *this; + case SEQUENCE: + as_sequence_lval() = val.as_sequence(); + return *this; + } + + switch (val.type()) { + case VOID: + set_type(VOID); + break; + + case BOOLEAN: + set_boolean(val.as_boolean()); + break; + case INTEGER: + set_long(val.as_long()); + break; + case DATETIME: + set_datetime(val.as_datetime()); + break; + case AMOUNT: + set_amount(val.as_amount()); + break; + case BALANCE: + set_balance(val.as_balance()); + break; + case BALANCE_PAIR: + set_balance_pair(val.as_balance_pair()); + break; + case STRING: + set_string(val.as_string()); + break; + case SEQUENCE: + set_sequence(val.as_sequence()); + break; + case XML_NODE: + set_xml_node(val.as_xml_node()); + break; + case POINTER: + set_pointer(val.as_pointer()); + break; + + default: + assert(false); + break; + } + + return *this; +} + +value_t::operator bool() const +{ + switch (type()) { + case BOOLEAN: + return as_boolean(); + case INTEGER: + return as_long(); + case DATETIME: + return is_valid_moment(as_datetime()); + case AMOUNT: + return as_amount(); + case BALANCE: + return as_balance(); + case BALANCE_PAIR: + return as_balance_pair(); + case STRING: + return ! as_string().empty(); + case SEQUENCE: + return ! as_sequence().empty(); + case XML_NODE: + return as_xml_node()->to_value(); + case POINTER: + return as_pointer() != NULL; + default: + assert(false); + break; + } + assert(false); + return 0; +} + bool value_t::to_boolean() const { - if (type == BOOLEAN) { + if (is_boolean()) { return as_boolean(); } else { value_t temp(*this); @@ -47,7 +200,7 @@ bool value_t::to_boolean() const long value_t::to_long() const { - if (type == INTEGER) { + if (is_long()) { return as_long(); } else { value_t temp(*this); @@ -58,7 +211,7 @@ long value_t::to_long() const moment_t value_t::to_datetime() const { - if (type == DATETIME) { + if (is_datetime()) { return as_datetime(); } else { value_t temp(*this); @@ -69,7 +222,7 @@ moment_t value_t::to_datetime() const amount_t value_t::to_amount() const { - if (type == AMOUNT) { + if (is_amount()) { return as_amount(); } else { value_t temp(*this); @@ -80,7 +233,7 @@ amount_t value_t::to_amount() const balance_t value_t::to_balance() const { - if (type == BALANCE) { + if (is_balance()) { return as_balance(); } else { value_t temp(*this); @@ -91,7 +244,7 @@ balance_t value_t::to_balance() const balance_pair_t value_t::to_balance_pair() const { - if (type == BALANCE_PAIR) { + if (is_balance_pair()) { return as_balance_pair(); } else { value_t temp(*this); @@ -102,7 +255,7 @@ balance_pair_t value_t::to_balance_pair() const string value_t::to_string() const { - if (type == STRING) { + if (is_string()) { return as_string(); } else { value_t temp(*this); @@ -113,7 +266,7 @@ string value_t::to_string() const value_t::sequence_t value_t::to_sequence() const { - if (type == SEQUENCE) { + if (is_sequence()) { return as_sequence(); } else { value_t temp(*this); @@ -123,211 +276,99 @@ value_t::sequence_t value_t::to_sequence() const } -void value_t::destroy() -{ - switch (type) { - case AMOUNT: - ((amount_t *)data)->~amount_t(); - break; - case BALANCE: - ((balance_t *)data)->~balance_t(); - break; - case BALANCE_PAIR: - ((balance_pair_t *)data)->~balance_pair_t(); - break; - case STRING: - ((string *)data)->~string(); - break; - case SEQUENCE: - ((sequence_t *)data)->~sequence_t(); - break; - - default: - break; - } -} - void value_t::in_place_simplify() { LOGGER("amounts.values.simplify"); if (is_realzero()) { - DEBUG_("Zeroing type " << type); - destroy(); - type = INTEGER; - as_long() = 0L; + DEBUG_("Zeroing type " << type()); + set_long(0L); return; } - if (type == BALANCE_PAIR && + if (is_type(BALANCE_PAIR) && (! as_balance_pair().cost || as_balance_pair().cost->is_realzero())) { DEBUG_("Reducing balance pair to balance"); in_place_cast(BALANCE); } - if (type == BALANCE && as_balance().amounts.size() == 1) { + if (is_type(BALANCE) && as_balance().amounts.size() == 1) { DEBUG_("Reducing balance to amount"); in_place_cast(AMOUNT); } - if (type == AMOUNT && ! as_amount().has_commodity() && +#if 0 + if (is_type(AMOUNT) && ! as_amount().has_commodity() && as_amount().fits_in_long()) { DEBUG_("Reducing amount to integer"); in_place_cast(INTEGER); } -} - -value_t& value_t::operator=(const value_t& val) -{ - if (this == &val) - return *this; - - if (type == val.type) - switch (type) { - case BOOLEAN: - as_boolean() = val.as_boolean(); - return *this; - case INTEGER: - as_long() = val.as_long(); - return *this; - case DATETIME: - as_datetime() = val.as_datetime(); - return *this; - case AMOUNT: - as_amount() = val.as_amount(); - return *this; - case BALANCE: - as_balance() = val.as_balance(); - return *this; - case BALANCE_PAIR: - as_balance_pair() = val.as_balance_pair(); - return *this; - case STRING: - as_string() = val.as_string(); - return *this; - case SEQUENCE: - as_sequence() = val.as_sequence(); - return *this; - } - - destroy(); - - type = val.type; - - switch (val.type) { - case VOID: - break; - - case BOOLEAN: - as_boolean() = val.as_boolean(); - break; - - case INTEGER: - as_long() = val.as_long(); - break; - - case DATETIME: - new((moment_t *) data) moment_t(val.as_datetime()); - break; - - case AMOUNT: - new((amount_t *)data) amount_t(val.as_amount()); - break; - - case BALANCE: - new((balance_t *)data) balance_t(val.as_balance()); - break; - - case BALANCE_PAIR: - new((balance_pair_t *)data) balance_pair_t(val.as_balance_pair()); - break; - - case STRING: - new((string *)data) string(val.as_string()); - break; - - case SEQUENCE: - new((sequence_t *)data) sequence_t(val.as_sequence()); - break; - - case XML_NODE: - as_xml_node() = val.as_xml_node(); - break; - - case POINTER: - as_pointer() = val.as_pointer(); - break; - - default: - assert(false); - break; - } - - return *this; +#endif } value_t& value_t::operator+=(const value_t& val) { - if (type == STRING) { - if (val.type == STRING) - as_string() += val.as_string(); + if (is_type(STRING)) { + if (val.is_type(STRING)) + as_string_lval() += val.as_string(); else - as_string() += val.to_string(); + as_string_lval() += val.to_string(); return *this; } - else if (type == SEQUENCE) { - if (val.type == SEQUENCE) - as_sequence().insert(as_sequence().end(), - val.as_sequence().begin(), - val.as_sequence().end()); - else - as_sequence().push_back(val); + else if (is_type(SEQUENCE)) { + if (val.is_type(SEQUENCE)) { + sequence_t& seq(as_sequence_lval()); + seq.insert(seq.end(), val.as_sequence().begin(), + val.as_sequence().end()); + } else { + as_sequence_lval().push_back(val); + } return *this; } - if (val.type == XML_NODE) // recurse + if (val.is_type(XML_NODE)) // recurse return *this += val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case DATETIME: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_datetime() += date_duration(val.as_long()); + as_datetime_lval() += date_duration(val.as_long()); return *this; case AMOUNT: - as_datetime() += date_duration(val.as_amount().to_long()); + as_datetime_lval() += date_duration(val.as_amount().to_long()); return *this; } break; case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_long() += val.as_long(); + as_long_lval() += val.as_long(); return *this; case AMOUNT: in_place_cast(AMOUNT); - as_amount() += val.as_amount(); + as_amount_lval() += val.as_amount(); return *this; case BALANCE: in_place_cast(BALANCE); - as_balance() += val.as_balance(); + as_balance_lval() += val.as_balance(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() += val.as_balance_pair(); + as_balance_pair_lval() += val.as_balance_pair(); return *this; } break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: if (as_amount().has_commodity()) { in_place_cast(BALANCE); return *this += val; } else { - as_amount() += val.as_long(); + as_amount_lval() += val.as_long(); return *this; } break; @@ -337,125 +378,125 @@ value_t& value_t::operator+=(const value_t& val) in_place_cast(BALANCE); return *this += val; } else { - as_amount() += val.as_amount(); + as_amount_lval() += val.as_amount(); return *this; } break; case BALANCE: in_place_cast(BALANCE); - as_balance() += val.as_balance(); + as_balance_lval() += val.as_balance(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() += val.as_balance_pair(); + as_balance_pair_lval() += val.as_balance_pair(); return *this; } break; case BALANCE: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance() += val.to_amount(); + as_balance_lval() += val.to_amount(); return *this; case AMOUNT: - as_balance() += val.as_amount(); + as_balance_lval() += val.as_amount(); return *this; case BALANCE: - as_balance() += val.as_balance(); + as_balance_lval() += val.as_balance(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() += val.as_balance_pair(); + as_balance_pair_lval() += val.as_balance_pair(); return *this; } break; case BALANCE_PAIR: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance_pair() += val.to_amount(); + as_balance_pair_lval() += val.to_amount(); return *this; case AMOUNT: - as_balance_pair() += val.as_amount(); + as_balance_pair_lval() += val.as_amount(); return *this; case BALANCE: - as_balance_pair() += val.as_balance(); + as_balance_pair_lval() += val.as_balance(); return *this; case BALANCE_PAIR: - as_balance_pair() += val.as_balance_pair(); + as_balance_pair_lval() += val.as_balance_pair(); return *this; } break; } - throw_(value_error, - "Cannot add " << label() << " to " << val.label()); + throw_(value_error, "Cannot add " << label() << " to " << val.label()); + return *this; } value_t& value_t::operator-=(const value_t& val) { - if (type == SEQUENCE) { - if (val.type == SEQUENCE) { + if (is_type(SEQUENCE)) { + sequence_t& seq(as_sequence_lval()); + + if (val.is_type(SEQUENCE)) { for (sequence_t::const_iterator i = val.as_sequence().begin(); i != val.as_sequence().end(); i++) { - sequence_t::iterator j = - std::find(as_sequence().begin(), as_sequence().end(), *i); - if (j != as_sequence().end()) - as_sequence().erase(j); + sequence_t::iterator j = std::find(seq.begin(), seq.end(), *i); + if (j != seq.end()) + seq.erase(j); } } else { - sequence_t::iterator i = - std::find(as_sequence().begin(), as_sequence().end(), val); - if (i != as_sequence().end()) - as_sequence().erase(i); + sequence_t::iterator i = std::find(seq.begin(), seq.end(), val); + if (i != seq.end()) + seq.erase(i); } return *this; } - if (val.type == XML_NODE) // recurse + if (val.is_type(XML_NODE)) // recurse return *this -= val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case DATETIME: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_datetime() -= date_duration(val.as_long()); + as_datetime_lval() -= date_duration(val.as_long()); return *this; case AMOUNT: - as_datetime() -= date_duration(val.as_amount().to_long()); + as_datetime_lval() -= date_duration(val.as_amount().to_long()); return *this; } break; case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_long() -= val.as_long(); + as_long_lval() -= val.as_long(); return *this; case AMOUNT: in_place_cast(AMOUNT); - as_amount() -= val.as_amount(); + as_amount_lval() -= val.as_amount(); in_place_simplify(); return *this; case BALANCE: in_place_cast(BALANCE); - as_balance() -= val.as_balance(); + as_balance_lval() -= val.as_balance(); in_place_simplify(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() -= val.as_balance_pair(); + as_balance_pair_lval() -= val.as_balance_pair(); in_place_simplify(); return *this; } break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: if (as_amount().has_commodity()) { in_place_cast(BALANCE); @@ -463,7 +504,7 @@ value_t& value_t::operator-=(const value_t& val) in_place_simplify(); return *this; } else { - as_amount() -= val.as_long(); + as_amount_lval() -= val.as_long(); in_place_simplify(); return *this; } @@ -476,7 +517,7 @@ value_t& value_t::operator-=(const value_t& val) in_place_simplify(); return *this; } else { - as_amount() -= val.as_amount(); + as_amount_lval() -= val.as_amount(); in_place_simplify(); return *this; } @@ -484,78 +525,78 @@ value_t& value_t::operator-=(const value_t& val) case BALANCE: in_place_cast(BALANCE); - as_balance() -= val.as_balance(); + as_balance_lval() -= val.as_balance(); in_place_simplify(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() -= val.as_balance_pair(); + as_balance_pair_lval() -= val.as_balance_pair(); in_place_simplify(); return *this; } break; case BALANCE: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance() -= val.to_amount(); + as_balance_lval() -= val.to_amount(); in_place_simplify(); return *this; case AMOUNT: - as_balance() -= val.as_amount(); + as_balance_lval() -= val.as_amount(); in_place_simplify(); return *this; case BALANCE: - as_balance() -= val.as_balance(); + as_balance_lval() -= val.as_balance(); in_place_simplify(); return *this; case BALANCE_PAIR: in_place_cast(BALANCE_PAIR); - as_balance_pair() -= val.as_balance_pair(); + as_balance_pair_lval() -= val.as_balance_pair(); in_place_simplify(); return *this; } break; case BALANCE_PAIR: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance_pair() -= val.to_amount(); + as_balance_pair_lval() -= val.to_amount(); in_place_simplify(); return *this; case AMOUNT: - as_balance_pair() -= val.as_amount(); + as_balance_pair_lval() -= val.as_amount(); in_place_simplify(); return *this; case BALANCE: - as_balance_pair() -= val.as_balance(); + as_balance_pair_lval() -= val.as_balance(); in_place_simplify(); return *this; case BALANCE_PAIR: - as_balance_pair() -= val.as_balance_pair(); + as_balance_pair_lval() -= val.as_balance_pair(); in_place_simplify(); return *this; } break; } - throw_(value_error, - "Cannot subtract " << label() << " from " << val.label()); + throw_(value_error, "Cannot subtract " << label() << " from " << val.label()); + return *this; } value_t& value_t::operator*=(const value_t& val) { - if (type == STRING) { + if (is_type(STRING)) { string temp; long count = val.to_long(); for (long i = 0; i < count; i++) temp += as_string(); - as_string() = temp; + set_string(temp); return *this; } - else if (type == SEQUENCE) { + else if (is_type(SEQUENCE)) { value_t temp; long count = val.to_long(); for (long i = 0; i < count; i++) @@ -563,34 +604,31 @@ value_t& value_t::operator*=(const value_t& val) return *this = temp; } - if (val.type == XML_NODE) // recurse + if (val.is_type(XML_NODE)) // recurse return *this *= val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_long() *= val.as_long(); + as_long_lval() *= val.as_long(); return *this; - case AMOUNT: { - long temp = as_long(); - in_place_cast(AMOUNT); - as_amount() = val.as_amount() * temp; + case AMOUNT: + set_amount(val.as_amount() * as_long()); return *this; } - } break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_amount() *= val.as_long(); + as_amount_lval() *= val.as_long(); return *this; case AMOUNT: if (as_amount().commodity() == val.as_amount().commodity() || ! val.as_amount().has_commodity()) { - as_amount() *= val.as_amount(); + as_amount_lval() *= val.as_amount(); return *this; } break; @@ -598,13 +636,13 @@ value_t& value_t::operator*=(const value_t& val) break; case BALANCE: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance() *= val.as_long(); + as_balance_lval() *= val.as_long(); return *this; case AMOUNT: if (! val.as_amount().has_commodity()) { - as_balance() *= val.as_amount(); + as_balance_lval() *= val.as_amount(); return *this; } break; @@ -612,13 +650,13 @@ value_t& value_t::operator*=(const value_t& val) break; case BALANCE_PAIR: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance_pair() *= val.as_long(); + as_balance_pair_lval() *= val.as_long(); return *this; case AMOUNT: if (! val.as_amount().has_commodity()) { - as_balance_pair() *= val.as_amount(); + as_balance_pair_lval() *= val.as_amount(); return *this; } break; @@ -626,41 +664,38 @@ value_t& value_t::operator*=(const value_t& val) break; } - throw_(value_error, - "Cannot multiply " << label() << " with " << val.label()); + throw_(value_error, "Cannot multiply " << label() << " with " << val.label()); + return *this; } value_t& value_t::operator/=(const value_t& val) { - if (val.type == XML_NODE) // recurse + if (val.is_type(XML_NODE)) // recurse return *this /= val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_long() /= val.as_long(); + as_long_lval() /= val.as_long(); return *this; - case AMOUNT: { - long temp = as_long(); - in_place_cast(AMOUNT); - as_amount() = val.as_amount() / temp; + case AMOUNT: + set_amount(val.as_amount() / as_long()); return *this; } - } break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_amount() /= val.as_long(); + as_amount_lval() /= val.as_long(); return *this; case AMOUNT: if (as_amount().commodity() == val.as_amount().commodity() || ! val.as_amount().has_commodity()) { - as_amount() /= val.as_amount(); + as_amount_lval() /= val.as_amount(); return *this; } break; @@ -668,13 +703,13 @@ value_t& value_t::operator/=(const value_t& val) break; case BALANCE: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance() /= val.as_long(); + as_balance_lval() /= val.as_long(); return *this; case AMOUNT: if (! val.as_amount().has_commodity()) { - as_balance() /= val.as_amount(); + as_balance_lval() /= val.as_amount(); return *this; } break; @@ -682,13 +717,13 @@ value_t& value_t::operator/=(const value_t& val) break; case BALANCE_PAIR: - switch (val.type) { + switch (val.type()) { case INTEGER: - as_balance_pair() /= val.as_long(); + as_balance_pair_lval() /= val.as_long(); return *this; case AMOUNT: if (! val.as_amount().has_commodity()) { - as_balance_pair() /= val.as_amount(); + as_balance_pair_lval() /= val.as_amount(); return *this; } break; @@ -696,34 +731,34 @@ value_t& value_t::operator/=(const value_t& val) break; } - throw_(value_error, - "Cannot divide " << label() << " by " << val.label()); + throw_(value_error, "Cannot divide " << label() << " by " << val.label()); + return *this; } bool value_t::operator==(const value_t& val) const { - if (type == XML_NODE && val.type == XML_NODE) + if (is_type(XML_NODE) && val.is_type(XML_NODE)) return as_xml_node() == val.as_xml_node(); - else if (type == XML_NODE) + else if (is_type(XML_NODE)) return as_xml_node()->to_value() == val; - else if (val.type == XML_NODE) + else if (val.is_type(XML_NODE)) return *this == val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case BOOLEAN: - if (val.type == BOOLEAN) + if (val.is_type(BOOLEAN)) return as_boolean() == val.as_boolean(); break; case DATETIME: - if (val.type == DATETIME) + if (val.is_type(DATETIME)) return as_datetime() == val.as_datetime(); break; case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_long() == val.as_long(); case AMOUNT: @@ -738,7 +773,7 @@ bool value_t::operator==(const value_t& val) const break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_amount() == val.as_long(); case AMOUNT: @@ -753,7 +788,7 @@ bool value_t::operator==(const value_t& val) const break; case BALANCE: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_balance() == val.to_amount(); case AMOUNT: @@ -768,7 +803,7 @@ bool value_t::operator==(const value_t& val) const break; case BALANCE_PAIR: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_balance_pair() == val.to_amount(); case AMOUNT: @@ -783,17 +818,17 @@ bool value_t::operator==(const value_t& val) const break; case STRING: - if (val.type == STRING) + if (val.is_type(STRING)) return as_string() == val.as_string(); break; case SEQUENCE: - if (val.type == SEQUENCE) + if (val.is_type(SEQUENCE)) return as_sequence() == val.as_sequence(); break; case POINTER: - if (val.type == POINTER) + if (val.is_type(POINTER)) return as_pointer() == val.as_pointer(); break; @@ -801,29 +836,28 @@ bool value_t::operator==(const value_t& val) const break; } - throw_(value_error, - "Cannot compare " << label() << " to " << val.label()); + throw_(value_error, "Cannot compare " << label() << " to " << val.label()); return *this; } bool value_t::operator<(const value_t& val) const { - if (type == XML_NODE && val.type == XML_NODE) + if (is_type(XML_NODE) && val.is_type(XML_NODE)) return as_xml_node() < val.as_xml_node(); - else if (type == XML_NODE) + else if (is_type(XML_NODE)) return as_xml_node()->to_value() < val; - else if (val.type == XML_NODE) + else if (val.is_type(XML_NODE)) return *this < val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case DATETIME: - if (val.type == DATETIME) + if (val.is_type(DATETIME)) return as_datetime() < val.as_datetime(); break; case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_long() < val.as_long(); case AMOUNT: @@ -834,7 +868,7 @@ bool value_t::operator<(const value_t& val) const break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_amount() < val.as_long(); case AMOUNT: @@ -845,12 +879,12 @@ bool value_t::operator<(const value_t& val) const break; case STRING: - if (val.type == STRING) + if (val.is_type(STRING)) return as_string() < val.as_string(); break; case POINTER: - if (val.type == POINTER) + if (val.is_type(POINTER)) return as_pointer() < val.as_pointer(); break; @@ -858,8 +892,7 @@ bool value_t::operator<(const value_t& val) const break; } - throw_(value_error, - "Cannot compare " << label() << " to " << val.label()); + throw_(value_error, "Cannot compare " << label() << " to " << val.label()); return *this; } @@ -867,21 +900,21 @@ bool value_t::operator<(const value_t& val) const #if 0 bool value_t::operator>(const value_t& val) const { - if (type == XML_NODE && val.type == XML_NODE) + if (is_type(XML_NODE) && val.is_type(XML_NODE)) return as_xml_node() > val.as_xml_node(); - else if (type == XML_NODE) + else if (is_type(XML_NODE)) return as_xml_node()->to_value() > val; - else if (val.type == XML_NODE) + else if (val.is_type(XML_NODE)) return *this > val.as_xml_node()->to_value(); - switch (type) { + switch (type()) { case DATETIME: - if (val.type == DATETIME) + if (val.is_type(DATETIME)) return as_datetime() > val.as_datetime(); break; case INTEGER: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_long() > val.as_long(); case AMOUNT: @@ -892,7 +925,7 @@ bool value_t::operator>(const value_t& val) const break; case AMOUNT: - switch (val.type) { + switch (val.type()) { case INTEGER: return as_amount() > val.as_long(); case AMOUNT: @@ -903,12 +936,12 @@ bool value_t::operator>(const value_t& val) const break; case STRING: - if (val.type == STRING) + if (val.is_type(STRING)) return as_string() > val.as_string(); break; case POINTER: - if (val.type == POINTER) + if (val.is_type(POINTER)) return as_pointer() > val.as_pointer(); break; @@ -923,72 +956,35 @@ bool value_t::operator>(const value_t& val) const } #endif -value_t::operator bool() const -{ - switch (type) { - case BOOLEAN: - return as_boolean(); - case INTEGER: - return as_long(); - case DATETIME: - return is_valid_moment(as_datetime()); - case AMOUNT: - return as_amount(); - case BALANCE: - return as_balance(); - case BALANCE_PAIR: - return as_balance_pair(); - case STRING: - return ! as_string().empty(); - case SEQUENCE: - return ! as_sequence().empty(); - case XML_NODE: - return as_xml_node()->to_value(); - case POINTER: - return as_pointer() != NULL; - default: - assert(false); - break; - } - assert(false); - return 0; -} - void value_t::in_place_cast(type_t cast_type) { - if (type == cast_type) + if (type() == cast_type) return; if (cast_type == BOOLEAN) { - bool truth(*this); - destroy(); - type = BOOLEAN; - as_boolean() = truth; + set_boolean(bool(*this)); return; } else if (cast_type == SEQUENCE) { - value_t temp(*this); - destroy(); - type = SEQUENCE; - new((sequence_t *)data) sequence_t; - as_sequence().push_back(temp); + sequence_t temp; + temp.push_back(*this); + set_sequence(temp); return; } // This must came after the if's above, otherwise it would be // impossible to turn an XML node into a sequence containing that // same XML node. - if (type == XML_NODE) { + if (is_type(XML_NODE)) { *this = as_xml_node()->to_value().cast(cast_type); return; } - switch (type) { + switch (type()) { case BOOLEAN: switch (cast_type) { case STRING: - new((string *)data) string(as_boolean() ? "true" : "false"); - type = cast_type; + set_string(as_boolean() ? "true" : "false"); return; } break; @@ -996,132 +992,90 @@ void value_t::in_place_cast(type_t cast_type) case INTEGER: switch (cast_type) { case AMOUNT: - new((amount_t *)data) amount_t(as_long()); - type = cast_type; + set_amount(as_long()); return; case BALANCE: - new((balance_t *)data) balance_t(as_long()); - type = cast_type; + set_balance(to_amount()); return; case BALANCE_PAIR: - new((balance_pair_t *)data) balance_pair_t(as_long()); - type = cast_type; + set_balance_pair(to_amount()); return; case STRING: - new((string *)data) string(lexical_cast<string>(as_long())); - type = cast_type; + set_string(lexical_cast<string>(as_long())); return; } break; case AMOUNT: switch (cast_type) { - case INTEGER: { - long temp = as_amount().to_long(); - destroy(); - type = cast_type; - as_long() = temp; + case INTEGER: + set_long(as_amount().to_long()); return; - } - case BALANCE: { - amount_t temp = as_amount(); - destroy(); - type = cast_type; - new((balance_t *)data) balance_t(temp); + case BALANCE: + set_balance(as_amount()); return; - } - case BALANCE_PAIR: { - amount_t temp = as_amount(); - destroy(); - type = cast_type; - new((balance_pair_t *)data) balance_pair_t(temp); + case BALANCE_PAIR: + set_balance_pair(as_amount()); return; - } - case STRING: { - amount_t temp = as_amount(); - destroy(); - type = cast_type; - new((string *)data) string(temp.to_string()); + case STRING: + set_string(as_amount().to_string()); return; } - } break; case BALANCE: switch (cast_type) { case AMOUNT: { - balance_t& temp(as_balance()); + const balance_t& temp(as_balance()); if (temp.amounts.size() == 1) { - amount_t amt = (*temp.amounts.begin()).second; - destroy(); - type = cast_type; - new((amount_t *)data) amount_t(amt); + set_amount((*temp.amounts.begin()).second); return; } else if (temp.amounts.size() == 0) { - destroy(); - type = cast_type; - new((amount_t *)data) amount_t(0L); + set_amount(0L); return; } else { - throw_(value_error, - "Cannot convert " << label() << + throw_(value_error, "Cannot convert " << label() << " with multiple commodities to " << label(cast_type)); } break; } - case BALANCE_PAIR: { - balance_t temp = as_balance(); - destroy(); - type = cast_type; - new((balance_pair_t *)data) balance_pair_t(temp); + case BALANCE_PAIR: + set_balance_pair(as_balance()); return; } - } break; case BALANCE_PAIR: switch (cast_type) { case AMOUNT: { - balance_t& temp(as_balance_pair().quantity); + const balance_t& temp(as_balance_pair().quantity); if (temp.amounts.size() == 1) { - amount_t amt = (*temp.amounts.begin()).second; - destroy(); - type = cast_type; - new((amount_t *)data) amount_t(amt); + set_amount((*temp.amounts.begin()).second); return; } else if (temp.amounts.size() == 0) { - type = cast_type; - new((amount_t *)data) amount_t(0L); + set_amount(0L); return; } else { - throw_(value_error, - "Cannot convert " << label() << + throw_(value_error, "Cannot convert " << label() << " with multiple commodities to " << label(cast_type)); } break; } - case BALANCE: { - balance_t temp = as_balance_pair().quantity; - destroy(); - type = cast_type; - new((balance_t *)data) balance_t(temp); + case BALANCE: + set_balance(as_balance_pair().quantity); return; } - } break; case STRING: switch (cast_type) { case INTEGER: { if (all(as_string(), is_digit())) { - long temp = lexical_cast<long>(as_string()); - destroy(); - type = cast_type; - as_long() = temp; + set_long(lexical_cast<long>(as_string())); return; } else { throw_(value_error, @@ -1130,14 +1084,10 @@ void value_t::in_place_cast(type_t cast_type) break; } - case AMOUNT: { - amount_t temp(as_string()); - destroy(); - type = cast_type; - new((amount_t *)data) amount_t(temp); + case AMOUNT: + set_amount(as_string()); return; } - } break; } @@ -1147,21 +1097,21 @@ void value_t::in_place_cast(type_t cast_type) void value_t::in_place_negate() { - switch (type) { + switch (type()) { case BOOLEAN: - as_boolean() = ! as_boolean(); + set_boolean(! as_boolean()); return; case INTEGER: - as_long() = - as_long(); + set_long(- as_long()); return; case AMOUNT: - as_amount().in_place_negate(); + as_amount_lval().in_place_negate(); return; case BALANCE: - as_balance().in_place_negate(); + as_balance_lval().in_place_negate(); return; case BALANCE_PAIR: - as_balance_pair().in_place_negate(); + as_balance_pair_lval().in_place_negate(); return; case XML_NODE: *this = as_xml_node()->to_value(); @@ -1174,7 +1124,7 @@ void value_t::in_place_negate() bool value_t::is_realzero() const { - switch (type) { + switch (type()) { case BOOLEAN: return ! as_boolean(); case INTEGER: @@ -1207,7 +1157,7 @@ bool value_t::is_realzero() const value_t value_t::value(const optional<moment_t>& moment) const { - switch (type) { + switch (type()) { case INTEGER: return *this; @@ -1237,17 +1187,17 @@ value_t value_t::value(const optional<moment_t>& moment) const void value_t::in_place_reduce() { - switch (type) { + switch (type()) { case INTEGER: break; case AMOUNT: - as_amount().in_place_reduce(); + as_amount_lval().in_place_reduce(); break; case BALANCE: - as_balance().in_place_reduce(); + as_balance_lval().in_place_reduce(); break; case BALANCE_PAIR: - as_balance_pair().in_place_reduce(); + as_balance_pair_lval().in_place_reduce(); break; case XML_NODE: *this = as_xml_node()->to_value(); @@ -1260,7 +1210,7 @@ void value_t::in_place_reduce() value_t value_t::round() const { - switch (type) { + switch (type()) { case INTEGER: return *this; case AMOUNT: @@ -1279,7 +1229,7 @@ value_t value_t::round() const value_t value_t::unround() const { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot un-round a boolean"); case DATETIME: @@ -1307,7 +1257,7 @@ value_t value_t::unround() const value_t value_t::annotated_price() const { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot find the annotated price of a boolean"); case INTEGER: @@ -1347,7 +1297,7 @@ value_t value_t::annotated_price() const value_t value_t::annotated_date() const { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot find the annotated date of a boolean"); case INTEGER: @@ -1388,7 +1338,7 @@ value_t value_t::annotated_date() const value_t value_t::annotated_tag() const { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot find the annotated tag of a boolean"); case INTEGER: @@ -1431,7 +1381,7 @@ value_t value_t::strip_annotations(const bool keep_price, const bool keep_date, const bool keep_tag) const { - switch (type) { + switch (type()) { case BOOLEAN: case INTEGER: case DATETIME: @@ -1448,14 +1398,12 @@ value_t value_t::strip_annotations(const bool keep_price, } case AMOUNT: - return as_amount().strip_annotations - (keep_price, keep_date, keep_tag); + return as_amount().strip_annotations(keep_price, keep_date, keep_tag); case BALANCE: - return as_balance().strip_annotations - (keep_price, keep_date, keep_tag); + return as_balance().strip_annotations(keep_price, keep_date, keep_tag); case BALANCE_PAIR: - return as_balance_pair().quantity.strip_annotations - (keep_price, keep_date, keep_tag); + return as_balance_pair().quantity.strip_annotations(keep_price, + keep_date, keep_tag); default: assert(false); @@ -1467,7 +1415,7 @@ value_t value_t::strip_annotations(const bool keep_price, value_t value_t::cost() const { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot find the cost of a boolean"); case INTEGER: @@ -1503,27 +1451,28 @@ value_t value_t::cost() const value_t& value_t::add(const amount_t& amount, const optional<amount_t>& tcost) { - switch (type) { + switch (type()) { case BOOLEAN: throw_(value_error, "Cannot add an amount to a boolean"); case DATETIME: throw_(value_error, "Cannot add an amount to a date/time"); + case INTEGER: case AMOUNT: if (tcost) { in_place_cast(BALANCE_PAIR); return add(amount, tcost); } - else if ((type == AMOUNT && + else if ((is_type(AMOUNT) && as_amount().commodity() != amount.commodity()) || - (type != AMOUNT && amount.commodity())) { + (! is_type(AMOUNT) && amount.commodity())) { in_place_cast(BALANCE); return add(amount, tcost); } - else if (type != AMOUNT) { + else if (! is_type(AMOUNT)) { in_place_cast(AMOUNT); } - as_amount() += amount; + *this += amount; break; case BALANCE: @@ -1531,11 +1480,11 @@ value_t& value_t::add(const amount_t& amount, const optional<amount_t>& tcost) in_place_cast(BALANCE_PAIR); return add(amount, tcost); } - as_balance() += amount; + *this += amount; break; case BALANCE_PAIR: - as_balance_pair().add(amount, tcost); + as_balance_pair_lval().add(amount, tcost); break; case STRING: @@ -1558,7 +1507,7 @@ value_t& value_t::add(const amount_t& amount, const optional<amount_t>& tcost) void value_t::print(std::ostream& out, const int first_width, const int latter_width) const { - switch (type) { + switch (type()) { case BOOLEAN: case DATETIME: case INTEGER: @@ -1601,7 +1550,7 @@ void value_t::print(std::ostream& out, const int first_width, std::ostream& operator<<(std::ostream& out, const value_t& val) { - switch (val.type) { + switch (val.type()) { case value_t::BOOLEAN: out << (val.as_boolean() ? "true" : "false"); break; @@ -1676,7 +1625,7 @@ void value_context::describe(std::ostream& out) const throw() out << std::right; out.width(20); - switch (bal->type) { + switch (bal->type()) { case value_t::BOOLEAN: out << (*((bool *) bal->data) ? "true" : "false"); break; |