From 9aaab88c618170a0cda2233a829f2595b2221037 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 29 Jan 2009 02:24:42 -0400 Subject: Update Doxygen documentation. Still much more work to be done. --- src/amount.h | 440 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 270 insertions(+), 170 deletions(-) (limited to 'src/amount.h') diff --git a/src/amount.h b/src/amount.h index 69f10e9d..0c49392d 100644 --- a/src/amount.h +++ b/src/amount.h @@ -30,24 +30,22 @@ */ /** - * @defgroup numerics Core numerics + * @defgroup math Core numerical objects */ /** * @file amount.h * @author John Wiegley - * @date Wed Apr 18 22:05:53 2007 * - * @brief Basic type for handling commoditized math: amount_t. + * @ingroup math * - * @ingroup numerics + * @brief Basic type for handling commoditized math: amount_t * - * This file contains the most basic numerical type in Ledger: - * amount_t, which relies upon commodity.h (commodity_t) for handling - * commoditized amounts. This class allows Ledger to handle - * mathematical expressions involving differing commodities, as well - * as math using no commodities at all (such as increasing a dollar - * amount by a multiplier). + * This file contains the most basic numerical type in Ledger, which + * relies upon commodity.h for handling commoditized amounts. This + * class allows Ledger to handle mathematical expressions involving + * differing commodities, as well as math using no commodities at all + * (such as increasing a dollar amount by a multiplier). */ #ifndef _AMOUNT_H #define _AMOUNT_H @@ -63,17 +61,14 @@ class commodity_pool_t; DECLARE_EXCEPTION(amount_error, std::runtime_error); /** - * @class amount_t + * @brief Encapsulate infinite-precision commoditized amounts * - * @brief Encapsulates infinite-precision commoditized amounts. - * - * The amount_t class can be used for commoditized infinite-precision - * math, and also for uncommoditized math. In the commoditized case, - * commodities keep track of how they are used, and will always - * display back to the user after the same fashion. For - * uncommoditized numbers, no display truncation is ever done. In - * both cases, internal precision is always kept to an excessive - * degree. + * This class can be used for commoditized infinite-precision math, and + * also for uncommoditized math. In the commoditized case, commodities + * keep track of how they are used, and will always display back to the + * user after the same fashion. For uncommoditized numbers, no display + * truncation is ever done. In both cases, internal precision is always + * kept to an excessive degree. */ class amount_t : public ordered_field_operators 0; } + /*@}*/ + + /** + * @name Binary arithmetic + */ + /*@{*/ + /** - * Binary arithmetic operators. Amounts support addition, - * subtraction, multiplication and division -- but not modulus, - * bitwise operations, or shifting. Arithmetic is also supported - * between amounts, double, long and unsigned long, in which case - * temporary amount are constructed for the life of the expression. + * Amounts support addition, subtraction, multiplication and division + * -- but not modulus, bitwise operations, or shifting. Arithmetic is + * also supported between amounts, double, long and unsigned long, in + * which case temporary amount are constructed for the life of the + * expression. * * Although only in-place operators are defined here, the remainder - * are provided by `boost::ordered_field_operators<>'. + * are provided by \code boost::ordered_field_operators<> \endcode. */ amount_t& operator+=(const amount_t& amt); amount_t& operator-=(const amount_t& amt); amount_t& operator*=(const amount_t& amt); amount_t& operator/=(const amount_t& amt); + /*@}*/ + /** - * Unary arithmetic operators. There are several unary methods - * support on amounts: - * - * precision() return an amount's current, internal precision. To - * find the precision it will be displayed at -- assuming it was not - * created using the static method `amount_t::exact' -- refer to - * commodity().precision. - * - * negate(), also unary minus (- x), returns the negated value of an - * amount. - * - * abs() returns the absolute value of an amount. It is equivalent - * to: `(x < 0) ? - x : x'. - * - * round(precision_t) and round() round an amount's internal value - * to the given precision, or to the commodity's current display - * precision if no precision value is given. This method changes - * the internal value of the amount, if it's internal precision was - * greater than the rounding precision. - * - * unround() yields an amount whose display precision is never - * truncated, even though its commodity normally displays only - * rounded values. - * - * reduce() reduces a value to its most basic commodity form, for - * amounts that utilize "scaling commodities". For example, an - * amount of 1h after reduction will be 3600s. - * - * unreduce(), if used with a "scaling commodity", yields the most - * compact form greater than 1.0. That is, 3599s will unreduce to - * 59.98m, while 3601 unreduces to 1h. - * - * value(optional, optional) returns the - * historical value for an amount -- the default moment returns the - * most recently known price -- based on the price history for the - * given commodity (or determined automatically, if none is provided). - * For example, if the amount were 10 AAPL, and on Apr 10, 2000 each - * share of AAPL was worth $10, then call value() for that moment in - * time would yield the amount $100.00. - * - * Further, for the sake of efficiency and avoiding temporary - * objects, the following methods support "in-place" variants that - * act on the amount itself and return a reference to the result - * (`*this'): - * - * in_place_negate() - * in_place_reduce() - * in_place_unreduce() + * @name Unary arithmetic + * There are several unary methods supported for amounts. + */ + /*@{*/ + + /** + * Return an amount's current, internal precision. To find the + * precision it will be displayed at -- assuming it was not created + * using the static method amount_t::exact(). + * @see commodity_t::precision() */ precision_t precision() const; + /** + * Returns the negated value of an amount. + * @see operator-() + */ amount_t negate() const { amount_t temp(*this); temp.in_place_negate(); @@ -384,12 +398,22 @@ public: return negate(); } + /** + * Returns the absolute value of an amount. Equivalent to: \code (x < + * 0) ? - x : x \endcode. + */ amount_t abs() const { if (sign() < 0) return negate(); return *this; } + /** + * An amount's internal value to the given precision, or to the + * commodity's current display precision if no precision value is + * given. This method changes the internal value of the amount, if + * it's internal precision was greater than the rounding precision. + */ amount_t round() const { amount_t temp(*this); temp.in_place_round(); @@ -404,8 +428,17 @@ public: } amount_t& in_place_round(precision_t prec); + /** + * Yields an amount whose display precision is never truncated, even + * though its commodity normally displays only rounded values. + */ amount_t unround() const; + /** + * reduces a value to its most basic commodity form, for amounts that + * utilize "scaling commodities". For example, an amount of \c 1h + * after reduction will be \code 3600s \endcode. + */ amount_t reduce() const { amount_t temp(*this); temp.in_place_reduce(); @@ -413,6 +446,12 @@ public: } amount_t& in_place_reduce(); + /** + * unreduce(), if used with a "scaling commodity", yields the most + * compact form greater than one. That is, \c 3599s will unreduce to + * \code 59.98m \endcode, while \c 3601 unreduces to \code 1h + * \endcode. + */ amount_t unreduce() const { amount_t temp(*this); temp.in_place_unreduce(); @@ -420,10 +459,26 @@ public: } amount_t& in_place_unreduce(); + /** + * Returns the historical value for an amount -- the default moment + * returns the most recently known price -- based on the price history + * for the given commodity (or determined automatically, if none is + * provided). For example, if the amount were \code 10 AAPL \encode, + * and on Apr 10, 2000 each share of \c AAPL was worth \code $10 + * \endcode, then calling value() for that moment in time would yield + * the amount \code $100.00 \endcode. + */ optional value(const optional& moment = none, const optional& in_terms_of = none) const; + /*@}*/ + + /** + * @name Truth tests + */ + /*@{*/ + /** * Truth tests. An amount may be truth test in several ways: * @@ -470,6 +525,13 @@ public: return false; } + /*@}*/ + + /** + * @name Conversion + */ + /*@{*/ + /** * Conversion methods. An amount may be converted to the same types * it can be constructed from -- with the exception of unsigned @@ -517,8 +579,15 @@ public: #endif bool fits_in_long() const; + /*@}*/ + + /** + * @name Commodity methods + */ + /*@{*/ + /** - * Commodity-related methods. The following methods relate to an + * The following methods relate to an * amount's commodity: * * commodity() returns an amount's commodity. If the amount has no @@ -559,8 +628,15 @@ public: return temp; } + /*@}*/ + + /** + * @name Commodity annotations + */ + /*@{*/ + /** - * Annotated commodity methods. An amount's commodity may be + * An amount's commodity may be * annotated with special details, such as the price it was * purchased for, when it was acquired, or an arbitrary note, * identifying perhaps the lot number of an item. @@ -596,33 +672,28 @@ public: const bool _keep_date = keep_date, const bool _keep_tag = keep_tag) const; + /*@}*/ + + /** + * @name Parsing + */ + /*@{*/ + /** - * Parsing methods. The method `parse' is used to parse an amount - * from an input stream or a string. A global operator>> is also - * defined which simply calls parse on the input stream. The - * `parse' method has two forms: - * - * parse(istream, flags_t) parses an amount from the given input - * stream. - * - * parse(string, flags_t) parses an amount from the given string. - * - * parse(string, flags_t) also parses an amount from a string. - * * The `flags' argument of both parsing may be one or more of the * following: * - * AMOUNT_PARSE_NO_MIGRATE means to not pay attention to the way an + * PARSE_NO_MIGRATE means to not pay attention to the way an * amount is used. Ordinarily, if an amount were $100.001, for * example, it would cause the default display precision for $ to be - * "widened" to three decimal places. If AMOUNT_PARSE_NO_MIGRATE is + * "widened" to three decimal places. If PARSE_NO_MIGRATE is * used, the commodity's default display precision is not changed. * - * AMOUNT_PARSE_NO_REDUCE means not to call in_place_reduce() on the + * PARSE_NO_REDUCE means not to call in_place_reduce() on the * resulting amount after it is parsed. * * These parsing methods observe the amounts they parse (unless - * AMOUNT_PARSE_NO_MIGRATE is true), and set the display details of + * PARSE_NO_MIGRATE is true), and set the display details of * the corresponding commodity accordingly. This way, amounts do * not require commodities to be pre-defined in any way, but merely * displays them back to the user in the same fashion as it saw them @@ -672,8 +743,15 @@ public: static void parse_conversion(const string& larger_str, const string& smaller_str); + /*@}*/ + + /** + * @name Printing + */ + /*@{*/ + /** - * Printing methods. An amount may be output to a stream using the + * An amount may be output to a stream using the * `print' method. There is also a global operator<< defined which * simply calls print for an amount on the given stream. There is * one form of the print method, which takes one required argument @@ -691,10 +769,17 @@ public: void print(std::ostream& out, bool omit_commodity = false, bool full_precision = false) const; + /*@}*/ + + /** + * @name Serialization + */ + /*@{*/ + /** - * Serialization methods. An amount may be deserialized from an - * input stream or a character pointer, and it may be serialized to - * an output stream. The methods used are: + * An amount may be deserialized from an input stream or a character + * pointer, and it may be serialized to an output stream. The methods + * used are: * * read(istream) reads an amount from the given input stream. It * must have been put there using `write(ostream)'. The required @@ -722,12 +807,25 @@ public: char ** pool_next = NULL); void write(std::ostream& out, unsigned int index = 0) const; + /*@}*/ + + /** + * @name XML Serialization + */ + /*@{*/ + void read_xml(std::istream& in); void write_xml(std::ostream& out, const int depth = 0) const; + /*@}*/ + + /** + * @name Debugging + */ + /*@{*/ + /** - * Debugging methods. There are two methods defined to help with - * debugging: + * There are two methods defined to help with debugging: * * dump(ostream) dumps an amount to an output stream. There is * little different from print(), it simply surrounds the display @@ -745,13 +843,15 @@ public: } bool valid() const; + + /*@}*/ }; extern amount_t * one; inline amount_t amount_t::exact(const string& value) { amount_t temp; - temp.parse(value, AMOUNT_PARSE_NO_MIGRATE); + temp.parse(value, PARSE_NO_MIGRATE); return temp; } -- cgit v1.2.3