Loading...
Searching...
No Matches
commodity.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
47#pragma once
48
49#include "expr.h"
50
51namespace ledger {
52
53struct keep_details_t;
54class commodity_pool_t;
55
57
71
73 : public flags::delegates_flags<uint_least16_t>,
74 public equality_comparable1<commodity_t, noncopyable>
75{
76protected:
77 friend class commodity_pool_t;
79
80 class base_t : public noncopyable, public flags::supports_flags<uint_least16_t>
81 {
82 public:
83#define COMMODITY_STYLE_DEFAULTS 0x000
84#define COMMODITY_STYLE_SUFFIXED 0x001
85#define COMMODITY_STYLE_SEPARATED 0x002
86#define COMMODITY_STYLE_DECIMAL_COMMA 0x004
87#define COMMODITY_STYLE_THOUSANDS 0x008
88#define COMMODITY_NOMARKET 0x010
89#define COMMODITY_BUILTIN 0x020
90#define COMMODITY_WALKED 0x040
91#define COMMODITY_KNOWN 0x080
92#define COMMODITY_PRIMARY 0x100
93#define COMMODITY_SAW_ANNOTATED 0x200
94#define COMMODITY_SAW_ANN_PRICE_FLOAT 0x400
95#define COMMODITY_SAW_ANN_PRICE_FIXATED 0x800
96#define COMMODITY_STYLE_TIME_COLON 0x1000
97#define COMMODITY_STYLE_NO_MIGRATE 0x2000
98
99 string symbol;
107
108 typedef tuple<datetime_t, datetime_t,
110 typedef std::map<memoized_price_entry,
112
113 static const std::size_t max_price_map_size = 8;
115
116 public:
125 virtual ~base_t() {
127 }
128 };
129
131
135
140 TRACE_CTOR(commodity_t, "commodity_pool_t *, shared_ptr<base_t>");
141 }
142
143public:
146
147 virtual ~commodity_t() {
149 }
150
151 operator bool() const;
152
153 virtual bool operator==(const commodity_t& comm) const {
154 if (comm.annotated)
155 return comm == *this;
156 return base.get() == comm.base.get();
157 }
158 bool operator==(const string& name) const {
159 return base_symbol() == name;
160 }
161
162 static bool symbol_needs_quotes(const string& symbol);
163
165 return *this;
166 }
167 virtual const commodity_t& referent() const {
168 return *this;
169 }
170
171 bool has_annotation() const {
172 return annotated;
173 }
174
176 return *this;
177 }
178 virtual void write_annotations(std::ostream&, bool) const {}
179
181 return *parent_;
182 }
183
184 string base_symbol() const {
185 return base->symbol;
186 }
187 string symbol() const {
189 }
190
192 return base->graph_index;
193 }
195 base->graph_index = arg;
196 }
197
199 return base->name;
200 }
202 base->name = arg;
203 }
204
206 return base->note;
207 }
209 base->note = arg;
210 }
211
213 return base->precision;
214 }
216 base->precision = arg;
217 }
218
220 return base->smaller;
221 }
223 base->smaller = arg;
224 }
225
227 return base->larger;
228 }
230 base->larger = arg;
231 }
232
233 virtual optional<expr_t> value_expr() const {
234 return base->value_expr;
235 }
237 base->value_expr = expr;
238 }
239
240 void add_price(const datetime_t& date, const amount_t& price,
241 const bool reflexive = true);
242 void remove_price(const datetime_t& date, commodity_t& commodity);
243
245 const datetime_t& moment = datetime_t(),
246 const datetime_t& _oldest = datetime_t(),
247 bool bidirectionally = false);
248
250 find_price_from_expr(expr_t& expr, const commodity_t * commodity,
251 const datetime_t& moment) const;
252
254 virtual find_price(const commodity_t * commodity = NULL,
255 const datetime_t& moment = datetime_t(),
256 const datetime_t& oldest = datetime_t()) const;
257
260 const datetime_t& moment,
261 const commodity_t * in_terms_of);
262
264
265 // Methods related to parsing, reading, writing, etc., the commodity
266 // itself.
267
268 static void parse_symbol(std::istream& in, string& symbol);
269 static void parse_symbol(char *& p, string& symbol);
270 static string parse_symbol(std::istream& in) {
271 string temp;
273 return temp;
274 }
275
276 virtual void print(std::ostream& out, bool elide_quotes = false,
277 bool print_annotations = false) const;
278 bool valid() const;
279
281 int operator()(const amount_t * left, const amount_t * right) const;
282 };
283};
284
285inline std::ostream& operator<<(std::ostream& out, const commodity_t& comm) {
286 comm.print(out, false, true);
287 return out;
288}
289
290void put_commodity(property_tree::ptree& pt, const commodity_t& comm,
291 bool commodity_details = false);
292
293//simple struct to allow std::map to compare commodities names
295 bool operator() (const commodity_t* lhs, const commodity_t* rhs) const {
296 return (lhs->symbol().compare(rhs->symbol()) < 0);
297 }
298};
299
300} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define DECLARE_EXCEPTION(name, kind)
Definition error.h:88
#define COMMODITY_STYLE_DECIMAL_COMMA
Definition commodity.h:86
#define COMMODITY_STYLE_DEFAULTS
Definition commodity.h:83
gregorian::date date
Definition utils.h:64
void put_commodity(property_tree::ptree &pt, const commodity_t &comm, bool commodity_details=false)
std::ostream & operator<<(std::ostream &out, const account_t &account)
T & downcast(U &object)
Definition utils.h:468
boost::posix_time::ptime datetime_t
Definition times.h:53
Encapsulate infinite-precision commoditized amounts.
Definition amount.h:96
optional< amount_t > price() const
uint_least16_t precision_t
The amount's decimal precision.
Definition amount.h:108
bool operator==(const price_point_t &other) const
Definition commodity.h:67
price_point_t(datetime_t _when, amount_t _price)
Definition commodity.h:64
optional< price_point_t > check_for_updated_price(const optional< price_point_t > &point, const datetime_t &moment, const commodity_t *in_terms_of)
static bool time_colon_by_default
Definition commodity.h:145
virtual optional< price_point_t > find_price(const commodity_t *commodity=NULL, const datetime_t &moment=datetime_t(), const datetime_t &oldest=datetime_t()) const
void set_note(const optional< string > &arg=none)
Definition commodity.h:208
virtual ~commodity_t()
Definition commodity.h:147
void set_value_expr(const optional< expr_t > &expr=none)
Definition commodity.h:236
amount_t::precision_t precision() const
Definition commodity.h:212
shared_ptr< base_t > base
Definition commodity.h:130
virtual void print(std::ostream &out, bool elide_quotes=false, bool print_annotations=false) const
void remove_price(const datetime_t &date, commodity_t &commodity)
void set_smaller(const optional< amount_t > &arg=none)
Definition commodity.h:222
virtual optional< expr_t > value_expr() const
Definition commodity.h:233
commodity_t & nail_down(const expr_t &expr)
optional< string > note() const
Definition commodity.h:205
virtual commodity_t & referent()
Definition commodity.h:164
void set_precision(amount_t::precision_t arg)
Definition commodity.h:215
string symbol() const
Definition commodity.h:187
optional< amount_t > smaller() const
Definition commodity.h:219
virtual const commodity_t & referent() const
Definition commodity.h:167
optional< amount_t > larger() const
Definition commodity.h:226
string base_symbol() const
Definition commodity.h:184
optional< string > qualified_symbol
Definition commodity.h:133
bool operator==(const string &name) const
Definition commodity.h:158
static string parse_symbol(std::istream &in)
Definition commodity.h:270
void set_larger(const optional< amount_t > &arg=none)
Definition commodity.h:229
static bool symbol_needs_quotes(const string &symbol)
void set_graph_index(const optional< std::size_t > &arg=none)
Definition commodity.h:194
commodity_t(commodity_pool_t *_parent, const shared_ptr< base_t > &_base)
Definition commodity.h:136
static bool decimal_comma_by_default
Definition commodity.h:144
void add_price(const datetime_t &date, const amount_t &price, const bool reflexive=true)
static void parse_symbol(char *&p, string &symbol)
void set_name(const optional< string > &arg=none)
Definition commodity.h:201
virtual void write_annotations(std::ostream &, bool) const
Definition commodity.h:178
commodity_pool_t * parent_
Definition commodity.h:132
static void parse_symbol(std::istream &in, string &symbol)
optional< std::size_t > graph_index() const
Definition commodity.h:191
optional< string > name() const
Definition commodity.h:198
optional< price_point_t > find_price_from_expr(expr_t &expr, const commodity_t *commodity, const datetime_t &moment) const
bool valid() const
commodity_pool_t & pool() const
Definition commodity.h:180
virtual bool operator==(const commodity_t &comm) const
Definition commodity.h:153
void map_prices(function< void(datetime_t, const amount_t &)> fn, const datetime_t &moment=datetime_t(), const datetime_t &_oldest=datetime_t(), bool bidirectionally=false)
bool has_annotation() const
Definition commodity.h:171
virtual commodity_t & strip_annotations(const keep_details_t &)
Definition commodity.h:175
std::map< memoized_price_entry, optional< price_point_t > > memoized_price_map
Definition commodity.h:111
optional< std::size_t > graph_index
Definition commodity.h:100
tuple< datetime_t, datetime_t, const commodity_t * > memoized_price_entry
Definition commodity.h:109
static const std::size_t max_price_map_size
Definition commodity.h:113
optional< string > name
Definition commodity.h:102
memoized_price_map price_map
Definition commodity.h:114
optional< string > note
Definition commodity.h:103
base_t(const string &_symbol)
Definition commodity.h:117
amount_t::precision_t precision
Definition commodity.h:101
optional< amount_t > larger
Definition commodity.h:105
optional< expr_t > value_expr
Definition commodity.h:106
optional< amount_t > smaller
Definition commodity.h:104
int operator()(const amount_t *left, const amount_t *right) const
bool operator()(const commodity_t *lhs, const commodity_t *rhs) const
Definition commodity.h:295