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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
|
#ifndef _WALK_H
#define _WALK_H
#include "journal.h"
namespace ledger {
template <typename T>
struct item_handler : public noncopyable
{
shared_ptr<item_handler> handler;
public:
item_handler() {
TRACE_CTOR(item_handler, "");
}
item_handler(shared_ptr<item_handler> _handler) : handler(_handler) {
TRACE_CTOR(item_handler, "shared_ptr<item_handler>");
}
virtual ~item_handler() {
TRACE_DTOR(item_handler);
}
virtual void flush() {
if (handler.get())
handler->flush();
}
virtual void operator()(T& item) {
if (handler.get())
(*handler.get())(item);
}
};
typedef shared_ptr<item_handler<transaction_t> > xact_handler_ptr;
template <typename T>
class compare_items
{
value_expr sort_order;
compare_items();
public:
compare_items(const compare_items& other) : sort_order(other.sort_order) {
TRACE_CTOR(compare_items, "copy");
}
compare_items(const value_expr& _sort_order) : sort_order(_sort_order) {
TRACE_CTOR(compare_items, "const value_expr&");
}
~compare_items() throw() {
TRACE_DTOR(compare_items);
}
bool operator()(const T * left, const T * right);
};
template <typename T>
bool compare_items<T>::operator()(const T * left, const T * right)
{
assert(left);
assert(right);
value_t left_result;
value_t right_result;
sort_order.compute(left_result, details_t(*left));
sort_order.compute(right_result, details_t(*right));
return left_result < right_result;
}
template <>
bool compare_items<transaction_t>::operator()(const transaction_t * left,
const transaction_t * right);
template <>
bool compare_items<account_t>::operator()(const account_t * left,
const account_t * right);
//////////////////////////////////////////////////////////////////////
//
// Transaction handlers
//
#define TRANSACTION_RECEIVED 0x0001
#define TRANSACTION_HANDLED 0x0002
#define TRANSACTION_TO_DISPLAY 0x0004
#define TRANSACTION_DISPLAYED 0x0008
#define TRANSACTION_NO_TOTAL 0x0010
#define TRANSACTION_SORT_CALC 0x0020
#define TRANSACTION_COMPOUND 0x0040
#define TRANSACTION_MATCHES 0x0080
struct transaction_xdata_t : public noncopyable
{
value_t total;
value_t sort_value;
value_t value;
unsigned int index;
unsigned short dflags;
datetime_t date;
account_t * account;
void * ptr;
transactions_list * component_xacts;
transaction_xdata_t()
: index(0), dflags(0),
account(NULL), ptr(NULL), component_xacts(NULL) {
TRACE_CTOR(transaction_xdata_t, "");
}
~transaction_xdata_t() {
TRACE_DTOR(transaction_xdata_t);
if (component_xacts)
checked_delete(component_xacts);
}
void remember_xact(transaction_t& xact) {
if (! component_xacts)
component_xacts = new transactions_list;
component_xacts->push_back(&xact);
}
bool have_component_xacts() const {
return component_xacts != NULL && ! component_xacts->empty();
}
void copy_component_xacts(transactions_list& xacts) {
for (transactions_list::const_iterator i = xacts.begin();
i != xacts.end();
i++)
remember_xact(**i);
}
void walk_component_xacts(item_handler<transaction_t>& handler) const {
for (transactions_list::const_iterator i = component_xacts->begin();
i != component_xacts->end();
i++)
handler(**i);
}
};
inline bool transaction_has_xdata(const transaction_t& xact) {
return xact.data != NULL;
}
inline transaction_xdata_t& transaction_xdata_(const transaction_t& xact) {
return *((transaction_xdata_t *) xact.data);
}
transaction_xdata_t& transaction_xdata(const transaction_t& xact);
void add_transaction_to(const transaction_t& xact, value_t& value);
inline account_t * xact_account(transaction_t& xact) {
if (xact.data) {
account_t * account = transaction_xdata(xact).account;
if (account)
return account;
}
return xact.account;
}
inline const account_t * xact_account(const transaction_t& xact) {
return xact_account(const_cast<transaction_t&>(xact));
}
//////////////////////////////////////////////////////////////////////
class entries_iterator : public noncopyable
{
ptr_list<journal_t>::iterator journals_i;
ptr_list<journal_t>::iterator journals_end;
bool journals_uninitialized;
entries_list::iterator entries_i;
entries_list::iterator entries_end;
bool entries_uninitialized;
public:
entries_iterator()
: journals_uninitialized(true), entries_uninitialized(true) {
TRACE_CTOR(entries_iterator, "");
}
entries_iterator(session_t& session)
: journals_uninitialized(true), entries_uninitialized(true) {
TRACE_CTOR(entries_iterator, "session_t&");
reset(session);
}
~entries_iterator() throw() {
TRACE_DTOR(entries_iterator);
}
void reset(session_t& session);
entry_t * operator()();
};
class transactions_iterator : public noncopyable
{
public:
virtual transaction_t * operator()() = 0;
};
class entry_transactions_iterator : public transactions_iterator
{
transactions_list::iterator xacts_i;
transactions_list::iterator xacts_end;
bool xacts_uninitialized;
public:
entry_transactions_iterator() : xacts_uninitialized(true) {
TRACE_CTOR(entry_transactions_iterator, "");
}
entry_transactions_iterator(entry_t& entry)
: xacts_uninitialized(true) {
TRACE_CTOR(entry_transactions_iterator, "entry_t&");
reset(entry);
}
virtual ~entry_transactions_iterator() throw() {
TRACE_DTOR(entry_transactions_iterator);
}
void reset(entry_t& entry) {
xacts_i = entry.transactions.begin();
xacts_end = entry.transactions.end();
xacts_uninitialized = false;
}
virtual transaction_t * operator()() {
if (xacts_i == xacts_end || xacts_uninitialized)
return NULL;
return *xacts_i++;
}
};
class session_transactions_iterator : public transactions_iterator
{
entries_iterator entries;
entry_transactions_iterator xacts;
public:
session_transactions_iterator() {
TRACE_CTOR(session_transactions_iterator, "");
}
session_transactions_iterator(session_t& session) {
TRACE_CTOR(session_transactions_iterator, "session_t&");
reset(session);
}
virtual ~session_transactions_iterator() throw() {
TRACE_DTOR(session_transactions_iterator);
}
void reset(session_t& session);
virtual transaction_t * operator()();
};
//////////////////////////////////////////////////////////////////////
class ignore_transactions : public item_handler<transaction_t>
{
public:
virtual void operator()(transaction_t& xact) {}
};
class clear_transaction_xdata : public item_handler<transaction_t>
{
public:
virtual void operator()(transaction_t& xact) {
if (xact.data) {
delete (transaction_xdata_t *) xact.data;
xact.data = NULL;
}
}
};
class pass_down_transactions : public item_handler<transaction_t>
{
pass_down_transactions();
public:
pass_down_transactions(xact_handler_ptr handler,
transactions_iterator& iter)
: item_handler<transaction_t>(handler) {
TRACE_CTOR(pass_down_transactions,
"xact_handler_ptr, transactions_iterator");
for (transaction_t * xact = iter(); xact; xact = iter())
item_handler<transaction_t>::operator()(*xact);
}
virtual ~pass_down_transactions() {
TRACE_DTOR(pass_down_transactions);
}
};
class truncate_entries : public item_handler<transaction_t>
{
int head_count;
int tail_count;
transactions_list xacts;
truncate_entries();
public:
truncate_entries(xact_handler_ptr handler,
int _head_count, int _tail_count)
: item_handler<transaction_t>(handler),
head_count(_head_count), tail_count(_tail_count) {
TRACE_CTOR(truncate_entries, "xact_handler_ptr, int, int");
}
virtual ~truncate_entries() {
TRACE_DTOR(truncate_entries);
}
virtual void flush();
virtual void operator()(transaction_t& xact) {
xacts.push_back(&xact);
}
};
class set_account_value : public item_handler<transaction_t>
{
public:
set_account_value(xact_handler_ptr handler = xact_handler_ptr())
: item_handler<transaction_t>(handler) {}
virtual void operator()(transaction_t& xact);
};
class push_to_transactions_list : public item_handler<transaction_t>
{
push_to_transactions_list();
public:
transactions_list& xact_list;
push_to_transactions_list(transactions_list& _xact_list)
: xact_list(_xact_list) {
TRACE_CTOR(push_to_transactions_list, "transactions_list&");
}
virtual ~push_to_transactions_list() {
TRACE_DTOR(push_to_transactions_list);
}
virtual void operator()(transaction_t& xact) {
xact_list.push_back(&xact);
}
};
class sort_transactions : public item_handler<transaction_t>
{
typedef std::deque<transaction_t *> transactions_deque;
transactions_deque transactions;
const value_expr sort_order;
sort_transactions();
public:
sort_transactions(xact_handler_ptr handler,
const value_expr& _sort_order)
: item_handler<transaction_t>(handler),
sort_order(_sort_order) {
TRACE_CTOR(sort_transactions,
"xact_handler_ptr, const value_expr&");
}
sort_transactions(xact_handler_ptr handler,
const string& _sort_order)
: item_handler<transaction_t>(handler),
sort_order(_sort_order) {
TRACE_CTOR(sort_transactions,
"xact_handler_ptr, const string&");
}
virtual ~sort_transactions() {
TRACE_DTOR(sort_transactions);
}
virtual void post_accumulated_xacts();
virtual void flush() {
post_accumulated_xacts();
item_handler<transaction_t>::flush();
}
virtual void operator()(transaction_t& xact) {
transactions.push_back(&xact);
}
};
class sort_entries : public item_handler<transaction_t>
{
sort_transactions sorter;
entry_t * last_entry;
sort_entries();
public:
sort_entries(xact_handler_ptr handler,
const value_expr& _sort_order)
: sorter(handler, _sort_order) {
TRACE_CTOR(sort_entries,
"xact_handler_ptr, const value_expr&");
}
sort_entries(xact_handler_ptr handler,
const string& _sort_order)
: sorter(handler, _sort_order) {
TRACE_CTOR(sort_entries,
"xact_handler_ptr, const string&");
}
virtual ~sort_entries() {
TRACE_DTOR(sort_entries);
}
virtual void flush() {
sorter.flush();
item_handler<transaction_t>::flush();
}
virtual void operator()(transaction_t& xact) {
if (last_entry && xact.entry != last_entry)
sorter.post_accumulated_xacts();
sorter(xact);
last_entry = xact.entry;
}
};
class filter_transactions : public item_handler<transaction_t>
{
item_predicate<transaction_t> pred;
filter_transactions();
public:
filter_transactions(xact_handler_ptr handler,
const value_expr& predicate)
: item_handler<transaction_t>(handler), pred(predicate) {
TRACE_CTOR(filter_transactions,
"xact_handler_ptr, const value_expr&");
}
filter_transactions(xact_handler_ptr handler,
const string& predicate)
: item_handler<transaction_t>(handler), pred(predicate) {
TRACE_CTOR(filter_transactions,
"xact_handler_ptr, const string&");
}
virtual ~filter_transactions() {
TRACE_DTOR(filter_transactions);
}
virtual void operator()(transaction_t& xact) {
if (pred(xact)) {
transaction_xdata(xact).dflags |= TRANSACTION_MATCHES;
(*handler)(xact);
}
}
};
class calc_transactions : public item_handler<transaction_t>
{
transaction_t * last_xact;
calc_transactions();
public:
calc_transactions(xact_handler_ptr handler)
: item_handler<transaction_t>(handler), last_xact(NULL) {
TRACE_CTOR(calc_transactions, "xact_handler_ptr");
}
virtual ~calc_transactions() {
TRACE_DTOR(calc_transactions);
}
virtual void operator()(transaction_t& xact);
};
class invert_transactions : public item_handler<transaction_t>
{
invert_transactions();
public:
invert_transactions(xact_handler_ptr handler)
: item_handler<transaction_t>(handler) {}
virtual void operator()(transaction_t& xact);
};
inline void clear_entries_transactions(std::list<entry_t>& entries_list) {
for (std::list<entry_t>::iterator i = entries_list.begin();
i != entries_list.end();
i++)
(*i).transactions.clear();
}
class collapse_transactions : public item_handler<transaction_t>
{
value_t subtotal;
unsigned int count;
entry_t * last_entry;
transaction_t * last_xact;
account_t totals_account;
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
collapse_transactions();
public:
collapse_transactions(xact_handler_ptr handler)
: item_handler<transaction_t>(handler), count(0),
last_entry(NULL), last_xact(NULL),
totals_account(NULL, "<Total>") {
TRACE_CTOR(collapse_transactions, "xact_handler_ptr");
}
virtual ~collapse_transactions() {
TRACE_DTOR(collapse_transactions);
clear_entries_transactions(entry_temps);
}
virtual void flush() {
if (subtotal)
report_subtotal();
item_handler<transaction_t>::flush();
}
void report_subtotal();
virtual void operator()(transaction_t& xact);
};
class component_transactions : public item_handler<transaction_t>
{
item_predicate<transaction_t> pred;
component_transactions();
public:
component_transactions(xact_handler_ptr handler,
const value_expr& predicate)
: item_handler<transaction_t>(handler), pred(predicate) {
TRACE_CTOR(component_transactions,
"xact_handler_ptr, const value_expr&");
}
component_transactions(xact_handler_ptr handler,
const string& predicate)
: item_handler<transaction_t>(handler), pred(predicate) {
TRACE_CTOR(component_transactions,
"xact_handler_ptr, const string&");
}
virtual ~component_transactions() throw() {
TRACE_DTOR(component_transactions);
}
virtual void operator()(transaction_t& xact);
};
class related_transactions : public item_handler<transaction_t>
{
transactions_list transactions;
bool also_matching;
related_transactions();
public:
related_transactions(xact_handler_ptr handler,
const bool _also_matching = false)
: item_handler<transaction_t>(handler),
also_matching(_also_matching) {
TRACE_CTOR(related_transactions,
"xact_handler_ptr, const bool");
}
virtual ~related_transactions() throw() {
TRACE_DTOR(related_transactions);
}
virtual void flush();
virtual void operator()(transaction_t& xact) {
transaction_xdata(xact).dflags |= TRANSACTION_RECEIVED;
transactions.push_back(&xact);
}
};
class changed_value_transactions : public item_handler<transaction_t>
{
// This filter requires that calc_transactions be used at some point
// later in the chain.
bool changed_values_only;
transaction_t * last_xact;
value_t last_balance;
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
changed_value_transactions();
public:
changed_value_transactions(xact_handler_ptr handler,
bool _changed_values_only)
: item_handler<transaction_t>(handler),
changed_values_only(_changed_values_only), last_xact(NULL) {
TRACE_CTOR(changed_value_transactions,
"xact_handler_ptr, bool");
}
virtual ~changed_value_transactions() {
TRACE_DTOR(changed_value_transactions);
clear_entries_transactions(entry_temps);
}
virtual void flush() {
if (last_xact) {
output_diff(current_moment);
last_xact = NULL;
}
item_handler<transaction_t>::flush();
}
void output_diff(const datetime_t& current);
virtual void operator()(transaction_t& xact);
};
class subtotal_transactions : public item_handler<transaction_t>
{
class acct_value_t
{
acct_value_t();
public:
account_t * account;
value_t value;
transactions_list components;
acct_value_t(account_t * a) : account(a) {
TRACE_CTOR(acct_value_t, "acount_t *");
}
acct_value_t(account_t * a, value_t& v) : account(a), value(v) {
TRACE_CTOR(acct_value_t, "acount_t *, value_t&");
}
acct_value_t(const acct_value_t& av)
: account(av.account), value(av.value),
components(av.components) {
TRACE_CTOR(acct_value_t, "copy");
}
~acct_value_t() throw() {
TRACE_DTOR(acct_value_t);
}
};
typedef std::map<string, acct_value_t> values_map;
typedef std::pair<string, acct_value_t> values_pair;
subtotal_transactions();
protected:
values_map values;
bool remember_components;
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
public:
datetime_t start;
datetime_t finish;
subtotal_transactions(xact_handler_ptr handler,
bool _remember_components = false)
: item_handler<transaction_t>(handler),
remember_components(_remember_components) {
TRACE_CTOR(subtotal_transactions,
"xact_handler_ptr, bool");
}
virtual ~subtotal_transactions() {
TRACE_DTOR(subtotal_transactions);
clear_entries_transactions(entry_temps);
}
void report_subtotal(const char * spec_fmt = NULL);
virtual void flush() {
if (values.size() > 0)
report_subtotal();
item_handler<transaction_t>::flush();
}
virtual void operator()(transaction_t& xact);
};
class interval_expr_error : public error {
public:
interval_expr_error(const string& reason,
error_context * ctxt = NULL) throw()
: error(reason, ctxt) {}
virtual ~interval_expr_error() throw() {}
};
class interval_transactions : public subtotal_transactions
{
interval_t interval;
transaction_t * last_xact;
bool started;
interval_transactions();
public:
interval_transactions(xact_handler_ptr _handler,
const interval_t& _interval,
bool remember_components = false)
: subtotal_transactions(_handler, remember_components),
interval(_interval), last_xact(NULL), started(false) {
TRACE_CTOR(interval_transactions,
"xact_handler_ptr, const interval_t&, bool");
}
interval_transactions(xact_handler_ptr _handler,
const string& _interval,
bool remember_components = false)
: subtotal_transactions(_handler, remember_components),
interval(_interval), last_xact(NULL), started(false) {
TRACE_CTOR(interval_transactions,
"xact_handler_ptr, const string&, bool");
}
virtual ~interval_transactions() throw() {
TRACE_DTOR(interval_transactions);
}
void report_subtotal(const datetime_t& moment = datetime_t());
virtual void flush() {
if (last_xact)
report_subtotal();
subtotal_transactions::flush();
}
virtual void operator()(transaction_t& xact);
};
class by_payee_transactions : public item_handler<transaction_t>
{
typedef std::map<string, subtotal_transactions *> payee_subtotals_map;
typedef std::pair<string, subtotal_transactions *> payee_subtotals_pair;
payee_subtotals_map payee_subtotals;
bool remember_components;
by_payee_transactions();
public:
by_payee_transactions(xact_handler_ptr handler,
bool _remember_components = false)
: item_handler<transaction_t>(handler),
remember_components(_remember_components) {
TRACE_CTOR(by_payee_transactions,
"xact_handler_ptr, bool");
}
virtual ~by_payee_transactions();
virtual void flush();
virtual void operator()(transaction_t& xact);
};
class set_comm_as_payee : public item_handler<transaction_t>
{
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
set_comm_as_payee();
public:
set_comm_as_payee(xact_handler_ptr handler)
: item_handler<transaction_t>(handler) {
TRACE_CTOR(set_comm_as_payee, "xact_handler_ptr");
}
virtual ~set_comm_as_payee() {
TRACE_DTOR(set_comm_as_payee);
clear_entries_transactions(entry_temps);
}
virtual void operator()(transaction_t& xact);
};
class set_code_as_payee : public item_handler<transaction_t>
{
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
set_code_as_payee();
public:
set_code_as_payee(xact_handler_ptr handler)
: item_handler<transaction_t>(handler) {
TRACE_CTOR(set_code_as_payee, "xact_handler_ptr");
}
virtual ~set_code_as_payee() {
TRACE_DTOR(set_code_as_payee);
clear_entries_transactions(entry_temps);
}
virtual void operator()(transaction_t& xact);
};
class dow_transactions : public subtotal_transactions
{
transactions_list days_of_the_week[7];
dow_transactions();
public:
dow_transactions(xact_handler_ptr handler,
bool remember_components = false)
: subtotal_transactions(handler, remember_components) {
TRACE_CTOR(dow_transactions, "xact_handler_ptr, bool");
}
virtual ~dow_transactions() throw() {
TRACE_DTOR(dow_transactions);
}
virtual void flush();
virtual void operator()(transaction_t& xact) {
days_of_the_week[xact.date().date().day_of_week()].push_back(&xact);
}
};
class generate_transactions : public item_handler<transaction_t>
{
generate_transactions();
protected:
typedef std::pair<interval_t, transaction_t *> pending_xacts_pair;
typedef std::list<pending_xacts_pair> pending_xacts_list;
pending_xacts_list pending_xacts;
std::list<entry_t> entry_temps;
std::list<transaction_t> xact_temps;
public:
generate_transactions(xact_handler_ptr handler)
: item_handler<transaction_t>(handler) {
TRACE_CTOR(dow_transactions, "xact_handler_ptr");
}
virtual ~generate_transactions() {
TRACE_DTOR(generate_transactions);
clear_entries_transactions(entry_temps);
}
void add_period_entries(period_entries_list& period_entries);
virtual void add_transaction(const interval_t& period, transaction_t& xact);
};
#define BUDGET_NO_BUDGET 0x00
#define BUDGET_BUDGETED 0x01
#define BUDGET_UNBUDGETED 0x02
class budget_transactions : public generate_transactions
{
unsigned short flags;
budget_transactions();
public:
budget_transactions(xact_handler_ptr handler,
unsigned long _flags = BUDGET_BUDGETED)
: generate_transactions(handler), flags(_flags) {
TRACE_CTOR(budget_transactions,
"xact_handler_ptr, unsigned long");
}
virtual ~budget_transactions() throw() {
TRACE_DTOR(budget_transactions);
}
void report_budget_items(const datetime_t& moment);
virtual void operator()(transaction_t& xact);
};
class forecast_transactions : public generate_transactions
{
item_predicate<transaction_t> pred;
public:
forecast_transactions(xact_handler_ptr handler,
const value_expr& predicate)
: generate_transactions(handler), pred(predicate) {
TRACE_CTOR(forecast_transactions, "xact_handler_ptr, const value_expr&");
}
forecast_transactions(xact_handler_ptr handler,
const string& predicate)
: generate_transactions(handler), pred(predicate) {
TRACE_CTOR(forecast_transactions, "xact_handler_ptr, const string&");
}
virtual ~forecast_transactions() throw() {
TRACE_DTOR(forecast_transactions);
}
virtual void add_transaction(const interval_t& period,
transaction_t& xact);
virtual void flush();
};
//////////////////////////////////////////////////////////////////////
//
// Account walking functions
//
#define ACCOUNT_TO_DISPLAY 0x0001
#define ACCOUNT_DISPLAYED 0x0002
#define ACCOUNT_SORT_CALC 0x0004
#define ACCOUNT_HAS_NON_VIRTUALS 0x0008
#define ACCOUNT_HAS_UNB_VIRTUALS 0x0010
struct account_xdata_t : public noncopyable
{
value_t value;
value_t total;
value_t sort_value;
unsigned int count; // transactions counted toward amount
unsigned int total_count; // transactions counted toward total
unsigned int virtuals;
unsigned short dflags;
account_xdata_t() : count(0), total_count(0), virtuals(0), dflags(0) {
TRACE_CTOR(account_xdata_t, "");
}
~account_xdata_t() throw() {
TRACE_DTOR(account_xdata_t);
}
};
inline bool account_has_xdata(const account_t& account) {
return account.data != NULL;
}
inline account_xdata_t& account_xdata_(const account_t& account) {
return *((account_xdata_t *) account.data);
}
account_xdata_t& account_xdata(const account_t& account);
void sum_accounts(account_t& account);
//////////////////////////////////////////////////////////////////////
class accounts_iterator : public noncopyable
{
std::list<accounts_map::const_iterator> accounts_i;
std::list<accounts_map::const_iterator> accounts_end;
public:
accounts_iterator() {
TRACE_CTOR(accounts_iterator, "");
}
accounts_iterator(account_t& account) {
TRACE_CTOR(accounts_iterator, "account_t&");
push_back(account);
}
virtual ~accounts_iterator() throw() {
TRACE_DTOR(accounts_iterator);
}
void push_back(account_t& account) {
accounts_i.push_back(account.accounts.begin());
accounts_end.push_back(account.accounts.end());
}
virtual account_t * operator()();
};
class sorted_accounts_iterator : public noncopyable
{
value_expr sort_cmp;
typedef std::deque<account_t *> accounts_deque_t;
std::list<accounts_deque_t> accounts_list;
std::list<accounts_deque_t::const_iterator> sorted_accounts_i;
std::list<accounts_deque_t::const_iterator> sorted_accounts_end;
public:
sorted_accounts_iterator(const string& sort_order) {
TRACE_CTOR(sorted_accounts_iterator, "const string&");
sort_cmp = value_expr(sort_order);
}
sorted_accounts_iterator(account_t& account, const string& sort_order) {
TRACE_CTOR(sorted_accounts_iterator, "account_t&, const string&");
sort_cmp = value_expr(sort_order);
push_back(account);
}
virtual ~sorted_accounts_iterator() throw() {
TRACE_DTOR(sorted_accounts_iterator);
}
void sort_accounts(account_t& account, accounts_deque_t& deque);
void push_back(account_t& account) {
accounts_list.push_back(accounts_deque_t());
sort_accounts(account, accounts_list.back());
sorted_accounts_i.push_back(accounts_list.back().begin());
sorted_accounts_end.push_back(accounts_list.back().end());
}
virtual account_t * operator()();
};
//////////////////////////////////////////////////////////////////////
typedef shared_ptr<item_handler<account_t> > acct_handler_ptr;
class clear_account_xdata : public item_handler<account_t>
{
public:
virtual void operator()(account_t& acct) {
if (acct.data) {
delete (account_xdata_t *) acct.data;
acct.data = NULL;
}
}
};
template <typename Iterator>
class pass_down_accounts : public item_handler<account_t>
{
pass_down_accounts();
public:
pass_down_accounts(acct_handler_ptr handler, Iterator& iter)
: item_handler<account_t>(handler) {
TRACE_CTOR(pass_down_accounts,
"acct_handler_ptr, accounts_iterator");
for (account_t * account = iter(); account; account = iter())
item_handler<account_t>::operator()(*account);
}
virtual ~pass_down_accounts() {
TRACE_DTOR(pass_down_accounts);
}
};
//////////////////////////////////////////////////////////////////////
#if 0
inline void clear_journal_xdata(journal_t& journal) {
clear_transaction_xdata xact_cleaner;
walk_entries(journal.entries, xact_cleaner);
clear_account_xdata acct_cleaner;
walk_accounts(*journal.master, acct_cleaner);
}
#endif
//////////////////////////////////////////////////////////////////////
class journals_iterator : public noncopyable
{
ptr_list<journal_t>::iterator journals_i;
ptr_list<journal_t>::iterator journals_end;
public:
journals_iterator() {
TRACE_CTOR(journals_iterator, "");
}
journals_iterator(session_t& session) {
TRACE_CTOR(journals_iterator, "session_t&");
reset(session);
}
virtual ~journals_iterator() throw() {
TRACE_DTOR(journals_iterator);
}
void reset(session_t& session);
virtual journal_t * operator()();
};
} // namespace ledger
#endif // _WALK_H
|