A wrapper around amount_t allowing addition of multiple commodities. More...
#include <balance.h>
Inherits equality_comparable< balance_t, equality_comparable< balance_t, amount_t, equality_comparable< balance_t, double, equality_comparable< balance_t, unsigned long, equality_comparable< balance_t, long, additive< balance_t, additive< balance_t, amount_t, additive< balance_t, double, additive< balance_t, unsigned long, additive< balance_t, long, multiplicative< balance_t, amount_t, multiplicative< balance_t, double, multiplicative< balance_t, unsigned long, multiplicative< balance_t, long > > > > > > > > > > > > > >.
Public Types | |
typedef std::vector< const amount_t * > | amounts_array |
typedef std::unordered_map< commodity_t *, amount_t > | amounts_map |
Public Attributes | |
amounts_map | amounts |
A wrapper around amount_t allowing addition of multiple commodities.
The balance_t class is appropriate for keeping a running balance where amounts of multiple commodities may be involved.
typedef std::vector<const amount_t *> ledger::balance_t::amounts_array |
typedef std::unordered_map<commodity_t *, amount_t> ledger::balance_t::amounts_map |
|
inline |
Constructors.
balance_t supports similar forms of construction to amount_t.
balance_t() creates an empty balance to which amounts or other balances may be added or subtracted.
balance_t(amount_t) constructs a balance whose starting value is equal to the given amount.
balance_t(double), balance_t(unsigned long) and balance_t(long) will construct an amount from their arguments and then construct a balance whose starting value is equal to that amount. This initial balance will have no commodity.
balance_t(string) and balance_t(const char *) both convert from a string representation of an amount to a balance whose initial value is that amount. This is the proper way to initialize a balance like '$100.00'.
Definition at line 107 of file balance.h.
References TRACE_CTOR.
Referenced by operator=(), and operator=().
Definition at line 110 of file balance.h.
References amounts, ledger::downcast(), throw_, and TRACE_CTOR.
Definition at line 122 of file balance.h.
References amounts, ledger::downcast(), and TRACE_CTOR.
Definition at line 127 of file balance.h.
References amounts, ledger::downcast(), and TRACE_CTOR.
|
inline |
Destructor.
Destroys all of the accumulated amounts in the balance.
Definition at line 137 of file balance.h.
References TRACE_DTOR.
Assignment and copy operators.
An balance may be assigned or copied.
Definition at line 144 of file balance.h.
References TRACE_CTOR.
|
inline |
|
inline |
Definition at line 352 of file balance.h.
References ledger::downcast().
optional< amount_t > ledger::balance_t::commodity_amount | ( | const optional< const commodity_t & > & | commodity = none | ) | const |
|
inline |
Commodity-related methods.
Balances support two commodity-related methods:
commodity_count() returns the number of different commodities stored in the balance.
commodity_amount(optional<commodity_t>) returns an (optional) amount for the given commodity within the balance; if no commodity is specified, it returns the (optional) uncommoditized component of the balance. If no matching element can be found, boost::none is returned.
Definition at line 501 of file balance.h.
References amounts.
|
inline |
Debugging methods.
There are two methods defined to help with debugging:
dump(ostream) dumps a balance to an output stream. There is little different from print(), it simply surrounds the display value with a marker, for example "BALANCE($1.00, DM 12.00)". This code is used by other dumping code elsewhere in Ledger.
valid() returns true if the amounts within the balance are valid.
Definition at line 578 of file balance.h.
References amounts, and ledger::downcast().
amounts_map::iterator ledger::balance_t::find_by_name | ( | const commodity_t & | comm | ) |
amounts_map::const_iterator ledger::balance_t::find_by_name | ( | const commodity_t & | comm | ) | const |
|
inline |
Definition at line 342 of file balance.h.
References ledger::downcast().
|
inline |
|
inline |
|
inline |
|
inline |
Definition at line 378 of file balance.h.
References amounts, ledger::downcast(), and reduced().
|
inline |
Definition at line 327 of file balance.h.
References amounts, and ledger::downcast().
|
inline |
|
inline |
Definition at line 392 of file balance.h.
References amounts, ledger::downcast(), and unreduced().
|
inline |
|
inline |
Definition at line 457 of file balance.h.
References amounts.
Referenced by is_nonzero(), is_realzero(), is_zero(), and to_amount().
|
inline |
Definition at line 427 of file balance.h.
References amounts, and is_empty().
Referenced by operator bool().
|
inline |
Definition at line 447 of file balance.h.
References amounts, and is_empty().
|
inline |
Definition at line 437 of file balance.h.
References amounts, and is_empty().
Iteration primitives.
‘map_sorted_amounts’ allows one to visit each amount in balance in the proper order for displaying to the user. Mostly used by ‘print’ and other routinse where the sort order of the amounts' commodities is significant.
|
inline |
Unary arithmetic operators.
There are only a few unary methods support on balance:
negate(), also unary minus (- x), returns a balance all of whose component amounts have been negated. In order words, it inverts the sign of all member amounts.
abs() returns a balance where no component amount is negative.
reduce() reduces the values in a balance to their most basic commodity forms, for amounts that utilize "scaling commodities". For example, a balance of 1h and 1m after reduction will be 3660s.
unreduce(), if used with amounts that use "scaling commodities", yields the most compact form greater than 1.0 for each component amount. That is, a balance of 10m and 1799s will unreduce to 39.98m.
value(optional<datetime_t>) returns the total historical value for a balance – the default moment returns a value based on the most recently known price – based on the price history of its component commodities. See amount_t::value for an example.
Further, for the sake of efficiency and avoiding temporary objects, the following methods support "in-place" variants act on the balance itself and return a reference to the result (‘*this’):
in_place_negate() in_place_reduce() in_place_unreduce()
Definition at line 291 of file balance.h.
References ledger::downcast().
Referenced by operator-().
|
inline |
|
inline |
Truth tests.
An balance may be truth test in two ways:
is_nonzero(), or operator bool, returns true if a balance's display value is not zero.
is_zero() returns true if an balance's display value is zero. Thus, a balance containing $0.0001 is considered zero if the current display precision for dollars is two decimal places.
is_realzero() returns true if an balance's actual value is zero. Thus, a balance containing $0.0001 is never considered realzero.
is_empty() returns true if a balance has no amounts within it. This can occur after a balance has been default initialized, or if the exact amount it contains is subsequently subtracted from it.
Definition at line 423 of file balance.h.
References is_nonzero().
|
inline |
Conversion methods.
A balance can be converted to an amount, but only if contains a single component amount.
Definition at line 468 of file balance.h.
References to_string().
Definition at line 236 of file balance.h.
References ledger::downcast().
Definition at line 242 of file balance.h.
References ledger::downcast().
Definition at line 239 of file balance.h.
References ledger::downcast().
Binary arithmetic operators.
Balances support addition and subtraction of other balances or amounts, but multiplication and division are restricted to uncommoditized amounts only.
Definition at line 213 of file balance.h.
References ledger::downcast().
Definition at line 219 of file balance.h.
References ledger::downcast().
Definition at line 216 of file balance.h.
References ledger::downcast().
|
inline |
Definition at line 225 of file balance.h.
References ledger::downcast().
Definition at line 231 of file balance.h.
References ledger::downcast().
Definition at line 228 of file balance.h.
References ledger::downcast().
Definition at line 247 of file balance.h.
References ledger::downcast().
Definition at line 253 of file balance.h.
References ledger::downcast().
Definition at line 250 of file balance.h.
References ledger::downcast().
Definition at line 153 of file balance.h.
References amounts, ledger::downcast(), and throw_.
Definition at line 148 of file balance.h.
References amounts, and ledger::downcast().
Definition at line 168 of file balance.h.
References balance_t().
Definition at line 165 of file balance.h.
References balance_t().
Definition at line 190 of file balance.h.
References amounts, ledger::downcast(), and throw_.
Comparison operators.
Balances are fairly restrictive in terms of how they may be compared. They may be compared for equality or inequality, but this is all, since the concept of "less than" or "greater than" makes no sense when amounts of multiple commodities are involved.
Balances may also be compared to amounts, in which case the sum of the balance must equal the amount exactly.
If a comparison between balances is desired, the balances must first be rendered to value equivalent amounts using the ‘value’ method, to determine a market valuation at some specific moment in time.
Definition at line 187 of file balance.h.
References amounts, and ledger::downcast().
Definition at line 202 of file balance.h.
References ledger::downcast().
void ledger::balance_t::print | ( | std::ostream & | out, |
const int | first_width = -1, | ||
const int | latter_width = -1, | ||
const uint_least8_t | flags = 0x00 ) const |
Printing methods.
A balance may be output to a stream using the ‘print’ method. There is also a global operator<< defined which simply calls print for a balance on the given stream. There is one form of the print method, which takes two required arguments and one arguments with a default value:
print(ostream, int first_width, int latter_width) prints a balance to the given output stream, using each commodity's default display characteristics. The first_width parameter specifies the width that should be used for printing amounts (since they are likely to vary in width). The latter_width, if specified, gives the width to be used for each line after the first. This is useful when printing in a column which falls at the right-hand side of the screen.
In addition to the width constraints, balances will also print with commodities in alphabetized order, regardless of the relative amounts of those commodities. There is no option to change this behavior.
Referenced by to_string().
|
inline |
Definition at line 373 of file balance.h.
References ledger::downcast().
Referenced by in_place_reduce().
|
inline |
Definition at line 311 of file balance.h.
References ledger::downcast().
Definition at line 321 of file balance.h.
References ledger::downcast().
|
inline |
void ledger::balance_t::sorted_amounts | ( | amounts_array & | sorted | ) | const |
Given a balance, insert a commodity-wise sort of the amounts into the given amounts_array.
balance_t ledger::balance_t::strip_annotations | ( | const keep_details_t & | what_to_keep | ) | const |
Annotated commodity methods.
The amounts contained by a balance may use annotated commodities. The ‘strip_annotations’ method will return a balance all of whose component amount have had their commodity annotations likewise stripped. See amount_t::strip_annotations for more details.
|
inline |
Definition at line 477 of file balance.h.
References amounts, ledger::downcast(), is_empty(), and throw_.
|
inline |
Definition at line 471 of file balance.h.
References print().
Referenced by operator string().
|
inline |
Definition at line 332 of file balance.h.
References ledger::downcast().
|
inline |
Definition at line 387 of file balance.h.
References ledger::downcast().
Referenced by in_place_unreduce().
|
inline |
Definition at line 363 of file balance.h.
References ledger::downcast().
|
inline |
optional< balance_t > ledger::balance_t::value | ( | const datetime_t & | moment = datetime_t(), |
const commodity_t * | in_terms_of = NULL ) const |
amounts_map ledger::balance_t::amounts |
Definition at line 85 of file balance.h.
Referenced by abs(), balance_t(), balance_t(), balance_t(), commodity_count(), dump(), in_place_ceiling(), in_place_floor(), in_place_negate(), in_place_reduce(), in_place_round(), in_place_roundto(), in_place_truncate(), in_place_unreduce(), in_place_unround(), is_empty(), is_nonzero(), is_realzero(), is_zero(), number(), operator=(), operator=(), operator==(), operator==(), single_amount(), to_amount(), and valid().