From 991e3a3eaf2c31f856faa85cfed514bbb28be526 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 24 Jun 2009 16:43:46 +0100 Subject: Split commodity.h/cc into three files commodity.h - code for commodity_t annotate.h - commodity annotations pool.h - commodity pool management --- src/commodity.h | 239 -------------------------------------------------------- 1 file changed, 239 deletions(-) (limited to 'src/commodity.h') diff --git a/src/commodity.h b/src/commodity.h index c678293e..4bd5ee82 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -309,37 +309,9 @@ public: return none; } - // Methods to exchange one commodity for another, while recording the - // factored price. - - static void exchange(commodity_t& commodity, - const amount_t& per_unit_cost, - const datetime_t& moment); - - struct cost_breakdown_t { - amount_t amount; - amount_t final_cost; - amount_t basis_cost; - }; - - static cost_breakdown_t exchange(const amount_t& amount, - const amount_t& cost, - const bool is_per_unit = false, - const optional& moment = none, - const optional& tag = none); - // Methods related to parsing, reading, writing, etc., the commodity // itself. - static optional price_db; - static long download_leeway; - static bool download_quotes; - - static optional parse_commodity_price(char * line); - - optional - download_quote(const optional& commodity = none) const; - static void parse_symbol(std::istream& in, string& symbol); static void parse_symbol(char *& p, string& symbol); static string parse_symbol(std::istream& in) { @@ -360,158 +332,6 @@ inline std::ostream& operator<<(std::ostream& out, const commodity_t& comm) { return out; } -/** - * @brief Brief - * - * Long. - */ -struct annotation_t : public supports_flags<>, - public equality_comparable -{ -#define ANNOTATION_PRICE_CALCULATED 0x01 -#define ANNOTATION_PRICE_FIXATED 0x02 -#define ANNOTATION_DATE_CALCULATED 0x04 -#define ANNOTATION_TAG_CALCULATED 0x08 - - optional price; - optional date; - optional tag; - - explicit annotation_t(const optional& _price = none, - const optional& _date = none, - const optional& _tag = none) - : supports_flags<>(), price(_price), date(_date), tag(_tag) { - TRACE_CTOR(annotation_t, "const optional& + date_t + string"); - } - annotation_t(const annotation_t& other) - : supports_flags<>(other.flags()), - price(other.price), date(other.date), tag(other.tag) { - TRACE_CTOR(annotation_t, "copy"); - } - ~annotation_t() { - TRACE_DTOR(annotation_t); - } - - operator bool() const { - return price || date || tag; - } - - bool operator==(const annotation_t& rhs) const { - return (price == rhs.price && - date == rhs.date && - tag == rhs.tag); - } - - void parse(std::istream& in); - - void print(std::ostream& out, bool keep_base = false) const; - - bool valid() const { - assert(*this); - return true; - } -}; - -struct keep_details_t -{ - bool keep_price; - bool keep_date; - bool keep_tag; - bool only_actuals; - - explicit keep_details_t(bool _keep_price = false, - bool _keep_date = false, - bool _keep_tag = false, - bool _only_actuals = false) - : keep_price(_keep_price), - keep_date(_keep_date), - keep_tag(_keep_tag), - only_actuals(_only_actuals) - { - TRACE_CTOR(keep_details_t, "bool, bool, bool, bool"); - } - keep_details_t(const keep_details_t& other) - : keep_price(other.keep_price), keep_date(other.keep_date), - keep_tag(other.keep_tag), only_actuals(other.only_actuals) { - TRACE_CTOR(keep_details_t, "copy"); - } - ~keep_details_t() throw() { - TRACE_DTOR(keep_details_t); - } - - bool keep_all() const { - return keep_price && keep_date && keep_tag && ! only_actuals; - } - bool keep_all(const commodity_t& comm) const { - return (! comm.annotated || - (keep_price && keep_date && keep_tag && ! only_actuals)); - } - - bool keep_any() const { - return keep_price || keep_date || keep_tag; - } - bool keep_any(const commodity_t& comm) const { - return comm.annotated && (keep_price || keep_date || keep_tag); - } -}; - -inline std::ostream& operator<<(std::ostream& out, - const annotation_t& details) { - details.print(out); - return out; -} - -/** - * @brief Brief - * - * Long. - */ -class annotated_commodity_t - : public commodity_t, - public equality_comparable > -{ -public: - commodity_t * ptr; - annotation_t details; - - explicit annotated_commodity_t(commodity_t * _ptr, - const annotation_t& _details) - : commodity_t(_ptr->parent_, _ptr->base), ptr(_ptr), details(_details) { - TRACE_CTOR(annotated_commodity_t, ""); - annotated = true; - } - virtual ~annotated_commodity_t() { - TRACE_DTOR(annotated_commodity_t); - } - - virtual bool operator==(const commodity_t& comm) const; - virtual bool operator==(const annotated_commodity_t& comm) const { - return *this == static_cast(comm); - } - - virtual commodity_t& referent() { - return *ptr; - } - virtual const commodity_t& referent() const { - return *ptr; - } - - virtual commodity_t& strip_annotations(const keep_details_t& what_to_keep); - virtual void write_annotations(std::ostream& out) const; -}; - -inline annotated_commodity_t& -as_annotated_commodity(commodity_t& commodity) { - return downcast(commodity); -} -inline const annotated_commodity_t& -as_annotated_commodity(const commodity_t& commodity) { - return downcast(commodity); -} - - /** * @brief Brief * @@ -521,65 +341,6 @@ struct compare_amount_commodities { bool operator()(const amount_t * left, const amount_t * right) const; }; -/** - * @brief Brief - * - * Long. - */ -class commodity_pool_t : public noncopyable -{ - /** - * The commodities collection in commodity_pool_t maintains pointers to all - * the commodities which have ever been created by the user, whether - * explicitly by calling the create methods of commodity_pool_t, or - * implicitly by parsing a commoditized amount. - */ - typedef std::map commodities_map; - -public: - commodities_map commodities; - - commodity_t * null_commodity; - commodity_t * default_commodity; - - bool keep_base; - -public: - boost::function - (commodity_t& commodity, - const optional& date, - const optional& moment, - const optional& last)> get_quote; - - explicit commodity_pool_t(); - - ~commodity_pool_t() { - TRACE_DTOR(commodity_pool_t); - foreach (commodities_map::value_type pair, commodities) - checked_delete(pair.second); - } - - commodity_t * create(const string& symbol); - commodity_t * find(const string& name); - commodity_t * find_or_create(const string& symbol); - - commodity_t * create(const string& symbol, const annotation_t& details); - commodity_t * find(const string& symbol, const annotation_t& details); - commodity_t * find_or_create(const string& symbol, - const annotation_t& details); - - commodity_t * create(commodity_t& comm, - const annotation_t& details, - const string& mapping_key); - - commodity_t * find_or_create(commodity_t& comm, - const annotation_t& details); - - commodity_t * parse_commodity_prices(const std::string& str, - const bool add_prices = true, - const optional& moment = none); -}; - } // namespace ledger #endif // _COMMODITY_H -- cgit v1.2.3