Loading...
Searching...
No Matches
ledger::balance_t Class Reference

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_tamounts_map
 

Public Member Functions

balance_t abs () const
 
 balance_t ()
 Constructors.
 
 balance_t (const amount_t &amt)
 
 balance_t (const balance_t &bal)
 Assignment and copy operators.
 
 balance_t (const char *val)
 
 balance_t (const double val)
 
 balance_t (const long val)
 
 balance_t (const string &val)
 
 balance_t (const unsigned long val)
 
balance_t ceilinged () const
 
optional< amount_tcommodity_amount (const optional< const commodity_t & > &commodity=none) const
 
std::size_t commodity_count () const
 Commodity-related methods.
 
void dump (std::ostream &out) const
 Debugging methods.
 
amounts_map::iterator find_by_name (const commodity_t &comm)
 
amounts_map::const_iterator find_by_name (const commodity_t &comm) const
 
balance_t floored () const
 
void in_place_ceiling ()
 
void in_place_floor ()
 
void in_place_negate ()
 
void in_place_reduce ()
 
void in_place_round ()
 
void in_place_roundto (int places)
 
void in_place_truncate ()
 
void in_place_unreduce ()
 
void in_place_unround ()
 
bool is_empty () const
 
bool is_nonzero () const
 
bool is_realzero () const
 
bool is_zero () const
 
void map_sorted_amounts (function< void(const amount_t &)> fn) const
 Iteration primitives.
 
balance_t negated () const
 Unary arithmetic operators.
 
balance_t number () const
 
 operator bool () const
 Truth tests.
 
 operator string () const
 Conversion methods.
 
balance_toperator*= (const amount_t &amt)
 
balance_toperator*= (const double val)
 
balance_toperator*= (const long val)
 
balance_toperator*= (const unsigned long val)
 
balance_toperator+= (const amount_t &amt)
 
balance_toperator+= (const balance_t &bal)
 Binary arithmetic operators.
 
balance_toperator+= (const double val)
 
balance_toperator+= (const long val)
 
balance_toperator+= (const unsigned long val)
 
balance_t operator- () const
 
balance_toperator-= (const amount_t &amt)
 
balance_toperator-= (const balance_t &bal)
 
balance_toperator-= (const double val)
 
balance_toperator-= (const long val)
 
balance_toperator-= (const unsigned long val)
 
balance_toperator/= (const amount_t &amt)
 
balance_toperator/= (const double val)
 
balance_toperator/= (const long val)
 
balance_toperator/= (const unsigned long val)
 
balance_toperator= (const amount_t &amt)
 
balance_toperator= (const balance_t &bal)
 
balance_toperator= (const char *str)
 
balance_toperator= (const string &str)
 
bool operator== (const amount_t &amt) const
 
bool operator== (const balance_t &bal) const
 Comparison operators.
 
template<typename T >
bool operator== (const T &val) const
 
void print (std::ostream &out, const int first_width=-1, const int latter_width=-1, const uint_least8_t flags=0x00) const
 Printing methods.
 
balance_t reduced () const
 
balance_t rounded () const
 
balance_t roundto (int places) const
 
bool single_amount () const
 
void sorted_amounts (amounts_array &sorted) const
 Given a balance, insert a commodity-wise sort of the amounts into the given amounts_array.
 
balance_t strip_annotations (const keep_details_t &what_to_keep) const
 Annotated commodity methods.
 
amount_t to_amount () const
 
string to_string () const
 
balance_t truncated () const
 
balance_t unreduced () const
 
balance_t unrounded () const
 
bool valid () const
 
optional< balance_tvalue (const datetime_t &moment=datetime_t(), const commodity_t *in_terms_of=NULL) const
 
 ~balance_t ()
 Destructor.
 

Public Attributes

amounts_map amounts
 

Detailed Description

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.

Definition at line 65 of file balance.h.

Member Typedef Documentation

◆ amounts_array

Definition at line 83 of file balance.h.

◆ amounts_map

Definition at line 82 of file balance.h.

Constructor & Destructor Documentation

◆ balance_t() [1/8]

ledger::balance_t::balance_t ( )
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=().

+ Here is the caller graph for this function:

◆ balance_t() [2/8]

ledger::balance_t::balance_t ( const amount_t & amt)
inline

Definition at line 110 of file balance.h.

References amounts, ledger::downcast(), throw_, and TRACE_CTOR.

+ Here is the call graph for this function:

◆ balance_t() [3/8]

ledger::balance_t::balance_t ( const double val)

◆ balance_t() [4/8]

ledger::balance_t::balance_t ( const unsigned long val)

◆ balance_t() [5/8]

ledger::balance_t::balance_t ( const long val)

◆ balance_t() [6/8]

ledger::balance_t::balance_t ( const string & val)
inlineexplicit

Definition at line 122 of file balance.h.

References amounts, ledger::downcast(), and TRACE_CTOR.

+ Here is the call graph for this function:

◆ balance_t() [7/8]

ledger::balance_t::balance_t ( const char * val)
inlineexplicit

Definition at line 127 of file balance.h.

References amounts, ledger::downcast(), and TRACE_CTOR.

+ Here is the call graph for this function:

◆ ~balance_t()

ledger::balance_t::~balance_t ( )
inline

Destructor.

Destroys all of the accumulated amounts in the balance.

Definition at line 137 of file balance.h.

References TRACE_DTOR.

◆ balance_t() [8/8]

ledger::balance_t::balance_t ( const balance_t & bal)
inline

Assignment and copy operators.

An balance may be assigned or copied.

Definition at line 144 of file balance.h.

References TRACE_CTOR.

Member Function Documentation

◆ abs()

balance_t ledger::balance_t::abs ( ) const
inline

Definition at line 304 of file balance.h.

References abs(), amounts, and ledger::downcast().

Referenced by abs().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ceilinged()

balance_t ledger::balance_t::ceilinged ( ) const
inline

Definition at line 352 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ commodity_amount()

optional< amount_t > ledger::balance_t::commodity_amount ( const optional< const commodity_t & > & commodity = none) const

◆ commodity_count()

std::size_t ledger::balance_t::commodity_count ( ) 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.

◆ dump()

void ledger::balance_t::dump ( std::ostream & out) const
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().

+ Here is the call graph for this function:

◆ find_by_name() [1/2]

amounts_map::iterator ledger::balance_t::find_by_name ( const commodity_t & comm)

◆ find_by_name() [2/2]

amounts_map::const_iterator ledger::balance_t::find_by_name ( const commodity_t & comm) const

◆ floored()

balance_t ledger::balance_t::floored ( ) const
inline

Definition at line 342 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ in_place_ceiling()

void ledger::balance_t::in_place_ceiling ( )
inline

Definition at line 357 of file balance.h.

References amounts.

◆ in_place_floor()

void ledger::balance_t::in_place_floor ( )
inline

Definition at line 347 of file balance.h.

References amounts.

◆ in_place_negate()

void ledger::balance_t::in_place_negate ( )
inline

Definition at line 296 of file balance.h.

References amounts.

◆ in_place_reduce()

void ledger::balance_t::in_place_reduce ( )
inline

Definition at line 378 of file balance.h.

References amounts, ledger::downcast(), and reduced().

+ Here is the call graph for this function:

◆ in_place_round()

void ledger::balance_t::in_place_round ( )
inline

Definition at line 316 of file balance.h.

References amounts.

◆ in_place_roundto()

void ledger::balance_t::in_place_roundto ( int places)
inline

Definition at line 327 of file balance.h.

References amounts, and ledger::downcast().

+ Here is the call graph for this function:

◆ in_place_truncate()

void ledger::balance_t::in_place_truncate ( )
inline

Definition at line 337 of file balance.h.

References amounts.

◆ in_place_unreduce()

void ledger::balance_t::in_place_unreduce ( )
inline

Definition at line 392 of file balance.h.

References amounts, ledger::downcast(), and unreduced().

+ Here is the call graph for this function:

◆ in_place_unround()

void ledger::balance_t::in_place_unround ( )
inline

Definition at line 368 of file balance.h.

References amounts.

◆ is_empty()

bool ledger::balance_t::is_empty ( ) const
inline

Definition at line 457 of file balance.h.

References amounts.

Referenced by is_nonzero(), is_realzero(), is_zero(), and to_amount().

+ Here is the caller graph for this function:

◆ is_nonzero()

bool ledger::balance_t::is_nonzero ( ) const
inline

Definition at line 427 of file balance.h.

References amounts, and is_empty().

Referenced by operator bool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_realzero()

bool ledger::balance_t::is_realzero ( ) const
inline

Definition at line 447 of file balance.h.

References amounts, and is_empty().

+ Here is the call graph for this function:

◆ is_zero()

bool ledger::balance_t::is_zero ( ) const
inline

Definition at line 437 of file balance.h.

References amounts, and is_empty().

+ Here is the call graph for this function:

◆ map_sorted_amounts()

void ledger::balance_t::map_sorted_amounts ( function< void(const amount_t &)> fn) const

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.

◆ negated()

balance_t ledger::balance_t::negated ( ) const
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-().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ number()

balance_t ledger::balance_t::number ( ) const
inline

Definition at line 511 of file balance.h.

References amounts, ledger::downcast(), and number().

Referenced by number().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator bool()

ledger::balance_t::operator bool ( ) const
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().

+ Here is the call graph for this function:

◆ operator string()

ledger::balance_t::operator string ( ) const
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().

+ Here is the call graph for this function:

◆ operator*=() [1/4]

balance_t & ledger::balance_t::operator*= ( const amount_t & amt)

◆ operator*=() [2/4]

balance_t & ledger::balance_t::operator*= ( const double val)
inline

Definition at line 236 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator*=() [3/4]

balance_t & ledger::balance_t::operator*= ( const long val)
inline

Definition at line 242 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator*=() [4/4]

balance_t & ledger::balance_t::operator*= ( const unsigned long val)
inline

Definition at line 239 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator+=() [1/5]

balance_t & ledger::balance_t::operator+= ( const amount_t & amt)

◆ operator+=() [2/5]

balance_t & ledger::balance_t::operator+= ( const balance_t & bal)

Binary arithmetic operators.

Balances support addition and subtraction of other balances or amounts, but multiplication and division are restricted to uncommoditized amounts only.

◆ operator+=() [3/5]

balance_t & ledger::balance_t::operator+= ( const double val)
inline

Definition at line 213 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator+=() [4/5]

balance_t & ledger::balance_t::operator+= ( const long val)
inline

Definition at line 219 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator+=() [5/5]

balance_t & ledger::balance_t::operator+= ( const unsigned long val)
inline

Definition at line 216 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator-()

balance_t ledger::balance_t::operator- ( ) const
inline

Definition at line 300 of file balance.h.

References negated().

+ Here is the call graph for this function:

◆ operator-=() [1/5]

balance_t & ledger::balance_t::operator-= ( const amount_t & amt)

◆ operator-=() [2/5]

balance_t & ledger::balance_t::operator-= ( const balance_t & bal)

◆ operator-=() [3/5]

balance_t & ledger::balance_t::operator-= ( const double val)
inline

Definition at line 225 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator-=() [4/5]

balance_t & ledger::balance_t::operator-= ( const long val)
inline

Definition at line 231 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator-=() [5/5]

balance_t & ledger::balance_t::operator-= ( const unsigned long val)
inline

Definition at line 228 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator/=() [1/4]

balance_t & ledger::balance_t::operator/= ( const amount_t & amt)

◆ operator/=() [2/4]

balance_t & ledger::balance_t::operator/= ( const double val)
inline

Definition at line 247 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator/=() [3/4]

balance_t & ledger::balance_t::operator/= ( const long val)
inline

Definition at line 253 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator/=() [4/4]

balance_t & ledger::balance_t::operator/= ( const unsigned long val)
inline

Definition at line 250 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ operator=() [1/4]

balance_t & ledger::balance_t::operator= ( const amount_t & amt)
inline

Definition at line 153 of file balance.h.

References amounts, ledger::downcast(), and throw_.

+ Here is the call graph for this function:

◆ operator=() [2/4]

balance_t & ledger::balance_t::operator= ( const balance_t & bal)
inline

Definition at line 148 of file balance.h.

References amounts, and ledger::downcast().

+ Here is the call graph for this function:

◆ operator=() [3/4]

balance_t & ledger::balance_t::operator= ( const char * str)
inline

Definition at line 168 of file balance.h.

References balance_t().

+ Here is the call graph for this function:

◆ operator=() [4/4]

balance_t & ledger::balance_t::operator= ( const string & str)
inline

Definition at line 165 of file balance.h.

References balance_t().

+ Here is the call graph for this function:

◆ operator==() [1/3]

bool ledger::balance_t::operator== ( const amount_t & amt) const
inline

Definition at line 190 of file balance.h.

References amounts, ledger::downcast(), and throw_.

+ Here is the call graph for this function:

◆ operator==() [2/3]

bool ledger::balance_t::operator== ( const balance_t & bal) const
inline

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().

+ Here is the call graph for this function:

◆ operator==() [3/3]

template<typename T >
bool ledger::balance_t::operator== ( const T & val) const
inline

Definition at line 202 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ print()

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().

+ Here is the caller graph for this function:

◆ reduced()

balance_t ledger::balance_t::reduced ( ) const
inline

Definition at line 373 of file balance.h.

References ledger::downcast().

Referenced by in_place_reduce().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rounded()

balance_t ledger::balance_t::rounded ( ) const
inline

Definition at line 311 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ roundto()

balance_t ledger::balance_t::roundto ( int places) const
inline

Definition at line 321 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ single_amount()

bool ledger::balance_t::single_amount ( ) const
inline

Definition at line 460 of file balance.h.

References amounts.

◆ sorted_amounts()

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.

◆ strip_annotations()

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.

◆ to_amount()

amount_t ledger::balance_t::to_amount ( ) const
inline

Definition at line 477 of file balance.h.

References amounts, ledger::downcast(), is_empty(), and throw_.

+ Here is the call graph for this function:

◆ to_string()

string ledger::balance_t::to_string ( ) const
inline

Definition at line 471 of file balance.h.

References print().

Referenced by operator string().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ truncated()

balance_t ledger::balance_t::truncated ( ) const
inline

Definition at line 332 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ unreduced()

balance_t ledger::balance_t::unreduced ( ) const
inline

Definition at line 387 of file balance.h.

References ledger::downcast().

Referenced by in_place_unreduce().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ unrounded()

balance_t ledger::balance_t::unrounded ( ) const
inline

Definition at line 363 of file balance.h.

References ledger::downcast().

+ Here is the call graph for this function:

◆ valid()

bool ledger::balance_t::valid ( ) const
inline

Definition at line 591 of file balance.h.

References amounts, and DEBUG.

◆ value()

optional< balance_t > ledger::balance_t::value ( const datetime_t & moment = datetime_t(),
const commodity_t * in_terms_of = NULL ) const

Member Data Documentation

◆ amounts


The documentation for this class was generated from the following file: