summaryrefslogtreecommitdiff
path: root/src/views.h
blob: 0d745f528074e142e8a09a5d452770fab996243b (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * Copyright (c) 2003-2023, John Wiegley.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @defgroup views Views
 */

/**
 * @file   views.h
 * @author John Wiegley
 *
 * @ingroup views
 */
#pragma once

#include "utils.h"

#if DOCUMENT_MODEL

#include "scope.h"
#include "item.h"
#include "report.h"
#include "post.h"
#include "predicate.h"

namespace ledger {

class journal_t;
class xact_t;
class post_t;
class account_t;
class report_t;

class r_base_t : public supports_flags<uint_least16_t>,
                 public scope_t
{
public:
  r_base_t() : refc(0) {
    TRACE_CTOR(r_base_t, "");
  }
  r_base_t(const r_base_t& other) : refc(0) {
    TRACE_CTOR(r_base_t, "copy");
  }
  virtual ~r_base_t() {
    TRACE_DTOR(r_base_t);
  }

protected:
  /**
   * `refc' holds the current reference count for each object.
   */
  mutable int refc;

  /**
   * Reference counting methods.  The intrusive_ptr_* methods are used
   * by boost::intrusive_ptr to manage the calls to acquire and release.
   */
  void acquire() const {
    VERIFY(refc >= 0);
    refc++;
  }
  void release() const {
    VERIFY(refc > 0);
    if (--refc == 0)
      checked_delete(this);
  }

  friend inline void intrusive_ptr_add_ref(r_base_t * r_ptr) {
    r_ptr->acquire();
  }
  friend inline void intrusive_ptr_release(r_base_t * r_ptr) {
    r_ptr->release();
  }
};

class r_journal_t;
class r_item_t;
class r_xact_t;
class r_post_t;
class r_account_t;

typedef intrusive_ptr<r_journal_t> r_journal_ptr;
typedef intrusive_ptr<r_item_t>    r_item_ptr;
typedef intrusive_ptr<r_xact_t>    r_xact_ptr;
typedef intrusive_ptr<r_post_t>    r_post_ptr;
typedef intrusive_ptr<r_account_t> r_account_ptr;

typedef std::list<r_xact_ptr> r_xacts_list;
typedef std::list<r_post_ptr> r_posts_list;

class r_journal_t : public r_base_t
{
  journal_t * base_journal;

  journal_t * ptr() {
    return base_journal;
  }
  const journal_t * ptr() const {
    return base_journal;
  }

  r_account_ptr master_ptr;
  r_xacts_list  xacts;
  r_posts_list  posts;

  void set_master(r_account_ptr ptr) {
    master_ptr = ptr;
  }

public:
  r_journal_t(journal_t * _journal, r_account_ptr _master)
    : r_base_t(), base_journal(_journal), master_ptr(_master) {
    TRACE_CTOR(r_journal_t, "journal_t *, account_t *");
  }
  r_journal_t(const r_journal_t& other)
    : r_base_t(other),
      base_journal(other.base_journal),
      master_ptr(other.master_ptr),
      xacts(other.xacts),
      posts(other.posts) {
    TRACE_CTOR(r_journal_t, "copy");
  }
  virtual ~r_journal_t() {
    TRACE_DTOR(r_journal_t);
  }

  r_xact_ptr create_xact(xact_t * xact = NULL);

  void add_xact(r_xact_ptr xact);

  r_xacts_list::iterator xacts_begin() {
    return xacts.begin();
  }
  r_xacts_list::iterator xacts_end() {
    return xacts.end();
  }

  r_post_ptr add_post(post_t * post);
  void add_post(r_post_ptr post);

  r_post_ptr create_post(post_t * post = NULL, r_xact_ptr xact = NULL,
                         r_account_ptr account = NULL);
  r_post_ptr create_post(r_post_ptr post, r_xact_ptr xact = NULL,
                         r_account_ptr account = NULL);

  r_posts_list::iterator posts_begin() {
    return posts.begin();
  }
  r_posts_list::iterator posts_end() {
    return posts.end();
  }

  r_account_ptr create_account(account_t * account = NULL);
  r_account_ptr create_account(const std::string& name);

  friend void put_journal(property_tree::ptree& pt, r_journal_ptr journal);
};

class r_item_t : public r_base_t
{
protected:
  item_t * base_item;

  item_t * ptr() {
    return base_item;
  }
  const item_t * ptr() const {
    return base_item;
  }

  r_journal_ptr journal_ptr;

public:
  r_item_t(r_journal_ptr _journal_ptr, item_t * _item)
    : r_base_t(), base_item(_item), journal_ptr(_journal_ptr) {
    TRACE_CTOR(r_item_t, "r_journal_ptr, item_t *");
  }
  r_item_t(const r_item_t& other)
    : r_base_t(other),
      base_item(other.base_item),
      journal_ptr(other.journal_ptr) {
    TRACE_CTOR(r_item_t, "copy");
  }
  virtual ~r_item_t() {
    TRACE_DTOR(r_item_t);
  }

  const optional<position_t> position() const;

  string id() const {
    return ptr()->id();
  }
  std::size_t seq() const {
    return ptr()->seq();
  }

  date_t date() const;
  void set_date(const date_t& when);

  item_t::state_t state() const;
  void set_state(item_t::state_t val);

  string payee() const;
  void set_payee(const string& name);

  optional<string> note() const {
    return ptr()->note;
  }

  bool has_tag(const string& tag) const {
    return ptr()->has_tag(tag);
  }
  bool has_tag(const mask_t&           tag_mask,
               const optional<mask_t>& value_mask = none) const {
    return ptr()->has_tag(tag_mask, value_mask);
  }

  optional<value_t> get_tag(const string& tag) const {
    return ptr()->get_tag(tag);
  }
  optional<value_t> get_tag(const mask_t&           tag_mask,
                            const optional<mask_t>& value_mask = none) const {
    return ptr()->get_tag(tag_mask, value_mask);
  }

  void set_tag(const string&            tag,
               const optional<value_t>& value              = none,
               const bool               overwrite_existing = true) {
    ptr()->set_tag(tag, value, overwrite_existing);
  }

  /**
   * Symbol scope methods.
   */
  virtual void define(const symbol_t::kind_t, const string&,
                      expr_t::ptr_op_t);
  virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
                                  const string& name);

  friend class r_journal_t;
  friend void put_item(property_tree::ptree& pt, r_item_ptr journal);
};

class r_xact_t : public r_item_t
{
  xact_t * ptr() {
    return reinterpret_cast<xact_t *>(base_item);
  }
  const xact_t * ptr() const {
    return reinterpret_cast<const xact_t *>(base_item);
  }

  r_posts_list posts;

public:
  r_xact_t(r_journal_ptr journal_ptr, xact_t * _xact)
    : r_item_t(journal_ptr, reinterpret_cast<item_t *>(_xact)) {
    TRACE_CTOR(r_xact_t, "r_journal_ptr, xact_t *");
  }
  r_xact_t(const r_xact_t& other)
    : r_item_t(other),
      posts(other.posts) {
    TRACE_CTOR(r_xact_t, "copy");
  }
  virtual ~r_xact_t() {
    TRACE_DTOR(r_xact_t);
  }

  virtual string description();

  void add_post(r_post_ptr post);

#if 0
  r_post_ptr create_post(post_t * post = NULL, r_account_ptr account = NULL);
  r_post_ptr create_post(r_post_ptr post, r_account_ptr account = NULL);
#endif

  r_posts_list::iterator posts_begin() {
    return posts.begin();
  }
  r_posts_list::iterator posts_end() {
    return posts.end();
  }

  string code() const;
  string payee() const;

  friend class r_journal_t;
  friend void put_xact(property_tree::ptree& pt, r_xact_ptr journal);
};

class r_post_t : public r_item_t
{
  post_t * ptr() {
    return reinterpret_cast<post_t *>(base_item);
  }
  const post_t * ptr() const {
    return reinterpret_cast<const post_t *>(base_item);
  }

  r_xact_ptr    xact_ptr;
  r_account_ptr account_ptr;

  void set_xact(r_xact_ptr ptr) {
    xact_ptr = ptr;
  }
  void set_account(r_account_ptr ptr) {
    account_ptr = ptr;
  }

public:
  r_post_t(r_journal_ptr journal_ptr, post_t * _post,
           r_xact_ptr _xact_ptr, r_account_ptr _account_ptr)
    : r_item_t(journal_ptr, reinterpret_cast<item_t *>(_post)),
      xact_ptr(_xact_ptr), account_ptr(_account_ptr) {
    TRACE_CTOR(r_post_t, "r_journal_ptr, post_t *, r_xact_ptr, r_account_ptr");
  }
  r_post_t(const r_post_t& other)
    : r_item_t(other),
      xact_ptr(other.xact_ptr),
      account_ptr(other.account_ptr) {
    TRACE_CTOR(r_post_t, "copy");
  }
  virtual ~r_post_t() {
    TRACE_DTOR(r_post_t);
  }

  virtual string description();

  string payee() const;

  r_xact_ptr    xact();
  r_account_ptr account();

  value_t amount() const;
  value_t cost() const;

  std::size_t count() const;
  value_t     running_total() const;

  optional<datetime_t> checkin() const;
  optional<datetime_t> checkout() const;

  friend class r_journal_t;
  friend void put_post(property_tree::ptree& pt, r_post_ptr journal);
};

typedef std::map<string, r_account_ptr> r_accounts_map;

class r_account_t : public r_base_t
{
  r_journal_ptr  journal_ptr;
  r_account_ptr  parent_ptr;
  r_accounts_map accounts;
  r_posts_list   posts;

  string name;

  mutable string _fullname;

public:
  r_account_t(r_journal_ptr _journal_ptr, r_account_ptr _parent_ptr,
              string _name)
    : r_base_t(), journal_ptr(_journal_ptr), parent_ptr(_parent_ptr),
      name(_name) {
    TRACE_CTOR(r_account_t, "r_journal_ptr, r_account_ptr, string");
  }
  r_account_t(const r_account_t& other)
    : r_base_t(other),
      journal_ptr(other.journal_ptr),
      parent_ptr(other.parent_ptr),
      accounts(other.accounts),
      posts(other.posts),
      name(other.name),
      _fullname(other._fullname) {
    TRACE_CTOR(r_account_t, "copy");
  }
  virtual ~r_account_t() {
    TRACE_DTOR(r_account_t);
  }

  virtual string description();

  void add_post(r_post_ptr post);

  r_posts_list::iterator posts_begin() {
    return posts.begin();
  }
  r_posts_list::iterator posts_end() {
    return posts.end();
  }

  r_account_ptr create_account(const std::string& name);

  string fullname() const;

  /**
   * Symbol scope methods.
   */
  virtual void define(const symbol_t::kind_t, const string&,
                      expr_t::ptr_op_t) {}
  virtual expr_t::ptr_op_t lookup(const symbol_t::kind_t kind,
                                  const string& fname) {
    return NULL;
  }

  friend class r_journal_t;
  friend void put_account(property_tree::ptree& pt, r_account_ptr journal);
};

template <typename PostsIterator>
void populate_journal(r_journal_ptr journal, report_t& report,
                      PostsIterator iter, predicate_t& pred)
{
  while (post_t * post = *iter) {
    bind_scope_t bound_scope(report, *post);
    if (pred.calc(bound_scope))
      journal->add_post(post);

    iter.increment();
  }
}

} // namespace ledger

#endif /* DOCUMENT_MODEL */