1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#ifndef _AMOUNT_H
#define _AMOUNT_H
#include <map>
#include <string>
#include <ctime>
#include <iostream>
#include "debug.h"
namespace ledger {
class commodity_t;
class amount_t
{
void _init();
void _copy(const amount_t& amt);
void _release();
void _dup();
void _resize(unsigned int prec);
void _clear() {
if (quantity)
_release();
quantity = NULL;
commodity = NULL;
}
public:
class bigint_t;
bigint_t * quantity;
commodity_t * commodity;
bool valid() const {
if (quantity)
return commodity != NULL;
else
return commodity == NULL;
}
// constructors
amount_t(commodity_t * _commodity = NULL)
: quantity(NULL), commodity(_commodity) {
DEBUG_PRINT("ledger.memory.ctors", "ctor amount_t");
}
amount_t(const amount_t& amt) : quantity(NULL) {
DEBUG_PRINT("ledger.memory.ctors", "ctor amount_t");
if (amt.quantity)
_copy(amt);
else
commodity = NULL;
}
amount_t(const std::string& value) : quantity(NULL) {
DEBUG_PRINT("ledger.memory.ctors", "ctor amount_t");
parse(value);
}
amount_t(const char * value) : quantity(NULL) {
DEBUG_PRINT("ledger.memory.ctors", "ctor amount_t");
parse(value);
}
amount_t(const bool value);
amount_t(const int value);
amount_t(const unsigned int value);
amount_t(const double value);
// destructor
~amount_t() {
DEBUG_PRINT("ledger.memory.dtors", "dtor amount_t");
if (quantity)
_release();
}
// assignment operator
amount_t& operator=(const amount_t& amt);
amount_t& operator=(const std::string& value);
amount_t& operator=(const char * value);
amount_t& operator=(const bool value);
amount_t& operator=(const int value);
amount_t& operator=(const unsigned int value);
amount_t& operator=(const double value);
// general methods
amount_t round(unsigned int prec) const;
// in-place arithmetic
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);
// simple arithmetic
amount_t operator*(const amount_t& amt) const {
amount_t temp = *this;
temp *= amt;
return temp;
}
amount_t operator/(const amount_t& amt) const {
amount_t temp = *this;
temp /= amt;
return temp;
}
amount_t operator+(const amount_t& amt) const {
amount_t temp = *this;
temp += amt;
return temp;
}
amount_t operator-(const amount_t& amt) const {
amount_t temp = *this;
temp -= amt;
return temp;
}
// unary negation
amount_t& negate();
amount_t negated() const {
amount_t temp = *this;
temp.negate();
return temp;
}
amount_t operator-() const {
return negated();
}
// test for non-zero (use ! for zero)
operator bool() const;
// integer comparisons
bool operator<(const int num) const;
bool operator<=(const int num) const;
bool operator>(const int num) const;
bool operator>=(const int num) const;
bool operator<(const unsigned int num) const;
bool operator<=(const unsigned int num) const;
bool operator>(const unsigned int num) const;
bool operator>=(const unsigned int num) const;
bool operator==(const unsigned int num) const;
bool operator!=(const unsigned int num) const {
return ! (*this == num);
}
// comparisons between amounts
bool operator<(const amount_t& amt) const;
bool operator<=(const amount_t& amt) const;
bool operator>(const amount_t& amt) const;
bool operator>=(const amount_t& amt) const;
bool operator==(const amount_t& amt) const;
bool operator!=(const amount_t& amt) const {
if (commodity != amt.commodity)
return true;
return ! (*this == amt);
}
amount_t value(const std::time_t moment) const;
void abs() {
if (*this < 0)
negate();
}
void parse(std::istream& in);
void parse(const std::string& str);
void write_quantity(std::ostream& out) const;
void read_quantity(std::istream& in);
friend std::istream& operator>>(std::istream& in, amount_t& amt);
};
void parse_quantity(std::istream& in, std::string& value);
void parse_commodity(std::istream& in, std::string& symbol);
inline amount_t abs(const amount_t& amt) {
return amt < 0 ? amt.negated() : amt;
}
inline std::istream& operator>>(std::istream& in, amount_t& amt) {
amt.parse(in);
return in;
}
std::ostream& operator<<(std::ostream& out, const amount_t& amt);
#define COMMODITY_STYLE_DEFAULTS 0x0000
#define COMMODITY_STYLE_SUFFIXED 0x0001
#define COMMODITY_STYLE_SEPARATED 0x0002
#define COMMODITY_STYLE_EUROPEAN 0x0004
#define COMMODITY_STYLE_THOUSANDS 0x0008
#define COMMODITY_STYLE_NOMARKET 0x0010
typedef std::map<const std::time_t, amount_t> history_map;
typedef std::pair<const std::time_t, amount_t> history_pair;
typedef std::map<const std::string, commodity_t *> commodities_map;
typedef std::pair<const std::string, commodity_t *> commodities_pair;
class commodity_t
{
public:
class updater_t {
public:
virtual ~updater_t() {}
virtual void operator()(commodity_t * commodity,
const std::time_t moment,
const std::time_t date,
const std::time_t last,
amount_t& price) = 0;
};
typedef unsigned long ident_t;
std::string symbol;
bool quote;
std::string name;
std::string note;
unsigned short precision;
unsigned short flags;
history_map history;
std::time_t last_lookup;
amount_t conversion;
ident_t ident;
// If set, this global function pointer is called to determine
// whether prices have been updated in the meanwhile.
static updater_t * updater;
// This map remembers all commodities that have been
// defined thus far.
static commodities_map commodities;
static commodity_t * null_commodity;
static void add_commodity(commodity_t * commodity,
const std::string symbol = "") {
commodities.insert(commodities_pair((symbol.empty() ?
commodity->symbol : symbol),
commodity));
}
static bool remove_commodity(commodity_t * commodity) {
commodities_map::size_type n = commodities.erase(commodity->symbol);
return n > 0;
}
static commodity_t * find_commodity(const std::string& symbol,
bool auto_create = false);
// Now the per-object constructor and methods
commodity_t(const std::string& _symbol = "",
unsigned int _precision = 0,
unsigned int _flags = COMMODITY_STYLE_DEFAULTS)
: symbol(_symbol), quote(false), precision(_precision),
flags(_flags), last_lookup(0) {
DEBUG_PRINT("ledger.memory.ctors", "ctor commodity_t");
check_symbol();
}
#ifdef DEBUG_ENABLED
~commodity_t() {
DEBUG_PRINT("ledger.memory.dtors", "dtor commodity_t");
}
#endif
void check_symbol() {
for (const char * p = symbol.c_str(); *p; p++)
if (std::isspace(*p) || std::isdigit(*p) || *p == '-' || *p == '.') {
quote = true;
return;
}
}
void add_price(const std::time_t date, const amount_t& price) {
history.insert(history_pair(date, price));
}
bool remove_price(const std::time_t date) {
history_map::size_type n = history.erase(date);
return n > 0;
}
void set_conversion(const amount_t& price) {
conversion = price;
}
amount_t value(const std::time_t moment = std::time(NULL));
};
} // namespace ledger
#endif // _AMOUNT_H
|