diff options
author | John Wiegley <johnw@newartisans.com> | 2007-05-02 04:46:06 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 03:38:36 -0400 |
commit | f0508a9f86be63fc4f98e9943ce2f226339e6309 (patch) | |
tree | efe3bce9d361b64b1fc39ec450241f0e7dbb92c1 /src | |
parent | c676527270e2e5b23de0e4b99e85010c76eb72b0 (diff) | |
download | fork-ledger-f0508a9f86be63fc4f98e9943ce2f226339e6309.tar.gz fork-ledger-f0508a9f86be63fc4f98e9943ce2f226339e6309.tar.bz2 fork-ledger-f0508a9f86be63fc4f98e9943ce2f226339e6309.zip |
In the middle of revising commodities.
Diffstat (limited to 'src')
-rw-r--r-- | src/amount.cc | 9 | ||||
-rw-r--r-- | src/commodity.cc | 4 | ||||
-rw-r--r-- | src/commodity.h | 66 | ||||
-rw-r--r-- | src/journal.h | 6 |
4 files changed, 42 insertions, 43 deletions
diff --git a/src/amount.cc b/src/amount.cc index 348fda64..b1ef1c03 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -5,8 +5,9 @@ * * @brief Types for handling commoditized math. * - * This file defines member functions for amount_t and the various - * flavors of commodity_t. + * This file defines member functions for amount_t, and also defines a + * helper class, bigint_t, which is used as a refcounted wrapper + * around libgmp's mpz_t type. */ /* @@ -1142,8 +1143,8 @@ void amount_t::parse_conversion(const string& larger_str, { amount_t larger, smaller; - larger.parse(larger_str.c_str(), AMOUNT_PARSE_NO_REDUCE); - smaller.parse(smaller_str.c_str(), AMOUNT_PARSE_NO_REDUCE); + larger.parse(larger_str, AMOUNT_PARSE_NO_REDUCE); + smaller.parse(smaller_str, AMOUNT_PARSE_NO_REDUCE); larger *= smaller.number(); diff --git a/src/commodity.cc b/src/commodity.cc index 397e4667..dba1eb98 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -59,7 +59,7 @@ void commodity_base_t::add_price(const moment_t& date, const amount_t& price) { if (! history) - history = new history_t; + history = history_t(); history_map::iterator i = history->prices.find(date); if (i != history->prices.end()) { @@ -77,7 +77,7 @@ bool commodity_base_t::remove_price(const moment_t& date) history_map::size_type n = history->prices.erase(date); if (n > 0) { if (history->prices.empty()) - history = NULL; + history.reset(); return true; } } diff --git a/src/commodity.h b/src/commodity.h index e3a89333..def41778 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -6,7 +6,7 @@ * @brief Types for handling commodities. * * This file contains one of the most basic types in Ledger: - * commodity_t, and its derived cousin, annotated_commodity_t. + * commodity_t, and its annotated cousin, annotated_commodity_t. */ /* @@ -61,25 +61,28 @@ class commodity_base_t; typedef std::map<const string, commodity_base_t *> base_commodities_map; typedef std::pair<const string, commodity_base_t *> base_commodities_pair; -class commodity_base_t +class commodity_base_t : public noncopyable { - public: +public: friend class commodity_t; friend class annotated_commodity_t; - typedef unsigned long ident_t; + friend void amount_t::initialize(); + friend void amount_t::shutdown(); + + friend void checked_delete<commodity_base_t>(commodity_base_t *); + + typedef uint_least32_t ident_t; - ident_t ident; - string name; - string note; - unsigned char precision; - unsigned char flags; - amount_t * smaller; - amount_t * larger; - - commodity_base_t() - : precision(0), flags(COMMODITY_STYLE_DEFAULTS), - smaller(NULL), larger(NULL), history(NULL) { + ident_t ident; + string name; + string note; + amount_t::precision_t precision; + unsigned char flags; + optional<amount_t> smaller; + optional<amount_t> larger; + + commodity_base_t() : precision(0), flags(COMMODITY_STYLE_DEFAULTS) { TRACE_CTOR(commodity_base_t, ""); } @@ -91,19 +94,16 @@ class commodity_base_t commodity_base_t(const string& _symbol, unsigned int _precision = 0, unsigned int _flags = COMMODITY_STYLE_DEFAULTS) - : precision(_precision), flags(_flags), - smaller(NULL), larger(NULL), symbol(_symbol), history(NULL) { + : precision(_precision), flags(_flags), symbol(_symbol) { TRACE_CTOR(commodity_base_t, "const string&, unsigned int, unsigned int"); } ~commodity_base_t() { TRACE_DTOR(commodity_base_t); - if (history) checked_delete(history); - if (smaller) checked_delete(smaller); - if (larger) checked_delete(larger); } static base_commodities_map commodities; + static commodity_base_t * create(const string& symbol); string symbol; @@ -113,14 +113,15 @@ class commodity_base_t ptime last_lookup; history_t() : last_lookup() {} }; - history_t * history; + optional<history_t> history; void add_price(const moment_t& date, const amount_t& price); bool remove_price(const moment_t& date); amount_t value(const moment_t& moment = now); +public: class updater_t { - public: + public: virtual ~updater_t() {} virtual void operator()(commodity_base_t& commodity, const moment_t& moment, @@ -138,11 +139,12 @@ typedef std::pair<const string, commodity_t *> commodities_pair; typedef std::vector<commodity_t *> commodities_array; -class commodity_t : public equality_comparable<commodity_t> +class commodity_t + : public equality_comparable<commodity_t, noncopyable> { friend class annotated_commodity_t; - public: +public: // This map remembers all commodities that have been defined. static commodities_map commodities; @@ -169,7 +171,7 @@ class commodity_t : public equality_comparable<commodity_t> string qualified_symbol; bool annotated; - public: +public: explicit commodity_t() : base(NULL), annotated(false) { TRACE_CTOR(commodity_t, ""); } @@ -236,25 +238,21 @@ class commodity_t : public equality_comparable<commodity_t> base->flags &= ~arg; } - amount_t * smaller() const { + optional<amount_t> smaller() const { return base->smaller; } void set_smaller(const amount_t& arg) { - if (base->smaller) - checked_delete(base->smaller); - base->smaller = new amount_t(arg); + base->smaller = arg; } - amount_t * larger() const { + optional<amount_t> larger() const { return base->larger; } void set_larger(const amount_t& arg) { - if (base->larger) - checked_delete(base->larger); - base->larger = new amount_t(arg); + base->larger = arg; } - commodity_base_t::history_t * history() const { + optional<commodity_base_t::history_t> history() const { return base->history; } diff --git a/src/journal.h b/src/journal.h index 5ac7c48c..f9393677 100644 --- a/src/journal.h +++ b/src/journal.h @@ -32,15 +32,15 @@ class transaction_t enum state_t { UNCLEARED, CLEARED, PENDING }; entry_t * entry; + unsigned short flags; + state_t state; + account_t * account; optional<moment_t> _date; optional<moment_t> _date_eff; - account_t * account; optional<amount_t> amount; optional<string> amount_expr; optional<amount_t> cost; optional<string> cost_expr; - state_t state; - unsigned short flags; optional<string> note; unsigned long beg_pos; unsigned long beg_line; |