Loading...
Searching...
No Matches
views.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
42#pragma once
43
44#include "utils.h"
45
46#if DOCUMENT_MODEL
47
48#include "scope.h"
49#include "item.h"
50#include "report.h"
51#include "post.h"
52#include "predicate.h"
53
54namespace ledger {
55
56class journal_t;
57class xact_t;
58class post_t;
59class account_t;
60class report_t;
61
62class r_base_t : public supports_flags<uint_least16_t>,
63 public scope_t
64{
65public:
66 r_base_t() : refc(0) {
68 }
69 r_base_t(const r_base_t& other) : refc(0) {
70 TRACE_CTOR(r_base_t, "copy");
71 }
72 virtual ~r_base_t() {
74 }
75
76protected:
80 mutable int refc;
81
86 void acquire() const {
87 VERIFY(refc >= 0);
88 refc++;
89 }
90 void release() const {
91 VERIFY(refc > 0);
92 if (--refc == 0)
93 checked_delete(this);
94 }
95
96 friend inline void intrusive_ptr_add_ref(r_base_t * r_ptr) {
97 r_ptr->acquire();
98 }
99 friend inline void intrusive_ptr_release(r_base_t * r_ptr) {
100 r_ptr->release();
101 }
102};
103
104class r_journal_t;
105class r_item_t;
106class r_xact_t;
107class r_post_t;
108class r_account_t;
109
115
116typedef std::list<r_xact_ptr> r_xacts_list;
117typedef std::list<r_post_ptr> r_posts_list;
118
119class r_journal_t : public r_base_t
120{
121 journal_t * base_journal;
122
123 journal_t * ptr() {
124 return base_journal;
125 }
126 const journal_t * ptr() const {
127 return base_journal;
128 }
129
131 r_xacts_list xacts;
132 r_posts_list posts;
133
134 void set_master(r_account_ptr ptr) {
135 master_ptr = ptr;
136 }
137
138public:
141 TRACE_CTOR(r_journal_t, "journal_t *, account_t *");
142 }
144 : r_base_t(other),
147 xacts(other.xacts),
148 posts(other.posts) {
149 TRACE_CTOR(r_journal_t, "copy");
150 }
151 virtual ~r_journal_t() {
153 }
154
155 r_xact_ptr create_xact(xact_t * xact = NULL);
156
157 void add_xact(r_xact_ptr xact);
158
159 r_xacts_list::iterator xacts_begin() {
160 return xacts.begin();
161 }
162 r_xacts_list::iterator xacts_end() {
163 return xacts.end();
164 }
165
166 r_post_ptr add_post(post_t * post);
167 void add_post(r_post_ptr post);
168
169 r_post_ptr create_post(post_t * post = NULL, r_xact_ptr xact = NULL,
170 r_account_ptr account = NULL);
171 r_post_ptr create_post(r_post_ptr post, r_xact_ptr xact = NULL,
172 r_account_ptr account = NULL);
173
174 r_posts_list::iterator posts_begin() {
175 return posts.begin();
176 }
177 r_posts_list::iterator posts_end() {
178 return posts.end();
179 }
180
181 r_account_ptr create_account(account_t * account = NULL);
182 r_account_ptr create_account(const std::string& name);
183
184 friend void put_journal(property_tree::ptree& pt, r_journal_ptr journal);
185};
186
187class r_item_t : public r_base_t
188{
189protected:
190 item_t * base_item;
191
192 item_t * ptr() {
193 return base_item;
194 }
195 const item_t * ptr() const {
196 return base_item;
197 }
198
200
201public:
204 TRACE_CTOR(r_item_t, "r_journal_ptr, item_t *");
205 }
206 r_item_t(const r_item_t& other)
207 : r_base_t(other),
210 TRACE_CTOR(r_item_t, "copy");
211 }
212 virtual ~r_item_t() {
214 }
215
216 const optional<position_t> position() const;
217
218 string id() const {
219 return ptr()->id();
220 }
221 std::size_t seq() const {
222 return ptr()->seq();
223 }
224
225 date_t date() const;
226 void set_date(const date_t& when);
227
228 item_t::state_t state() const;
229 void set_state(item_t::state_t val);
230
231 string payee() const;
232 void set_payee(const string& name);
233
234 optional<string> note() const {
235 return ptr()->note;
236 }
237
238 bool has_tag(const string& tag) const {
239 return ptr()->has_tag(tag);
240 }
241 bool has_tag(const mask_t& tag_mask,
242 const optional<mask_t>& value_mask = none) const {
243 return ptr()->has_tag(tag_mask, value_mask);
244 }
245
246 optional<value_t> get_tag(const string& tag) const {
247 return ptr()->get_tag(tag);
248 }
249 optional<value_t> get_tag(const mask_t& tag_mask,
250 const optional<mask_t>& value_mask = none) const {
251 return ptr()->get_tag(tag_mask, value_mask);
252 }
253
254 void set_tag(const string& tag,
255 const optional<value_t>& value = none,
256 const bool overwrite_existing = true) {
257 ptr()->set_tag(tag, value, overwrite_existing);
258 }
259
263 virtual void define(const symbol_t::kind_t, const string&,
265 virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
266 const string& name);
267
268 friend class r_journal_t;
269 friend void put_item(property_tree::ptree& pt, r_item_ptr journal);
270};
271
272class r_xact_t : public r_item_t
273{
274 xact_t * ptr() {
275 return reinterpret_cast<xact_t *>(base_item);
276 }
277 const xact_t * ptr() const {
278 return reinterpret_cast<const xact_t *>(base_item);
279 }
280
281 r_posts_list posts;
282
283public:
286 TRACE_CTOR(r_xact_t, "r_journal_ptr, xact_t *");
287 }
288 r_xact_t(const r_xact_t& other)
289 : r_item_t(other),
290 posts(other.posts) {
291 TRACE_CTOR(r_xact_t, "copy");
292 }
293 virtual ~r_xact_t() {
295 }
296
297 virtual string description();
298
299 void add_post(r_post_ptr post);
300
301#if 0
302 r_post_ptr create_post(post_t * post = NULL, r_account_ptr account = NULL);
303 r_post_ptr create_post(r_post_ptr post, r_account_ptr account = NULL);
304#endif
305
306 r_posts_list::iterator posts_begin() {
307 return posts.begin();
308 }
309 r_posts_list::iterator posts_end() {
310 return posts.end();
311 }
312
313 string code() const;
314 string payee() const;
315
316 friend class r_journal_t;
317 friend void put_xact(property_tree::ptree& pt, r_xact_ptr journal);
318};
319
320class r_post_t : public r_item_t
321{
322 post_t * ptr() {
323 return reinterpret_cast<post_t *>(base_item);
324 }
325 const post_t * ptr() const {
326 return reinterpret_cast<const post_t *>(base_item);
327 }
328
331
332 void set_xact(r_xact_ptr ptr) {
333 xact_ptr = ptr;
334 }
335 void set_account(r_account_ptr ptr) {
336 account_ptr = ptr;
337 }
338
339public:
344 TRACE_CTOR(r_post_t, "r_journal_ptr, post_t *, r_xact_ptr, r_account_ptr");
345 }
346 r_post_t(const r_post_t& other)
347 : r_item_t(other),
350 TRACE_CTOR(r_post_t, "copy");
351 }
352 virtual ~r_post_t() {
354 }
355
356 virtual string description();
357
358 string payee() const;
359
360 r_xact_ptr xact();
361 r_account_ptr account();
362
363 value_t amount() const;
364 value_t cost() const;
365
366 std::size_t count() const;
367 value_t running_total() const;
368
369 optional<datetime_t> checkin() const;
370 optional<datetime_t> checkout() const;
371
372 friend class r_journal_t;
373 friend void put_post(property_tree::ptree& pt, r_post_ptr journal);
374};
375
376typedef std::map<string, r_account_ptr> r_accounts_map;
377
378class r_account_t : public r_base_t
379{
382 r_accounts_map accounts;
383 r_posts_list posts;
384
385 string name;
386
387 mutable string _fullname;
388
389public:
391 string _name)
393 name(_name) {
394 TRACE_CTOR(r_account_t, "r_journal_ptr, r_account_ptr, string");
395 }
397 : r_base_t(other),
400 accounts(other.accounts),
401 posts(other.posts),
402 name(other.name),
403 _fullname(other._fullname) {
404 TRACE_CTOR(r_account_t, "copy");
405 }
406 virtual ~r_account_t() {
408 }
409
410 virtual string description();
411
412 void add_post(r_post_ptr post);
413
414 r_posts_list::iterator posts_begin() {
415 return posts.begin();
416 }
417 r_posts_list::iterator posts_end() {
418 return posts.end();
419 }
420
421 r_account_ptr create_account(const std::string& name);
422
423 string fullname() const;
424
428 virtual void define(const symbol_t::kind_t, const string&,
430 virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
431 const string& fname) {
432 return NULL;
433 }
434
435 friend class r_journal_t;
436 friend void put_account(property_tree::ptree& pt, r_account_ptr journal);
437};
438
439template <typename PostsIterator>
440void populate_journal(r_journal_ptr journal, report_t& report,
441 PostsIterator iter, predicate_t& pred)
442{
443 while (post_t * post = *iter) {
444 bind_scope_t bound_scope(report, *post);
445 if (pred.calc(bound_scope))
446 journal->add_post(post);
447
448 iter.increment();
449 }
450}
451
452} // namespace ledger
453
454#endif /* DOCUMENT_MODEL */
General utility facilities used by Ledger.
#define VERIFY(x)
Definition utils.h:141
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
void put_post(property_tree::ptree &pt, const post_t &post)
gregorian::date date
Definition utils.h:64
void put_account(property_tree::ptree &pt, const account_t &acct, function< bool(const account_t &)> pred)
boost::gregorian::date date_t
Definition times.h:60
void put_xact(property_tree::ptree &pt, const xact_t &xact)
T & downcast(U &object)
Definition utils.h:468
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57