summaryrefslogtreecommitdiff
path: root/autoxact.h
blob: c85890c60a005c1db6fceab77cc3e4e60dbcea7e (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
#ifndef _AUTOXACT_H
#define _AUTOXACT_H

#include "journal.h"
#include "valexpr.h"

#include <deque>

namespace ledger {

typedef std::deque<transaction_t *> transactions_deque;

class automated_transaction_t
{
public:
  item_predicate<transaction_t> predicate;
  transactions_deque		transactions;

  automated_transaction_t(const std::string&  _predicate,
			  transactions_deque& _transactions)
    : predicate(_predicate) {
    DEBUG_PRINT("ledger.memory.ctors", "ctor automated_transaction_t");
    transactions.insert(transactions.begin(),
			_transactions.begin(), _transactions.end());
    // Take over ownership of the pointers
    _transactions.clear();
  }

  ~automated_transaction_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor automated_transaction_t");
    for (transactions_deque::iterator i = transactions.begin();
	 i != transactions.end();
	 i++)
      delete *i;
  }

  void extend_entry(entry_t& entry);
};


typedef std::deque<automated_transaction_t *> automated_transactions_deque;

class automated_transactions_t
{
#ifdef DEBUG_ENABLED
  automated_transactions_t(const automated_transactions_t&);
#endif

public:
  automated_transactions_deque automated_transactions;

#ifdef DEBUG_ENABLED
  automated_transactions_t() {
    DEBUG_PRINT("ledger.memory.ctors", "ctor automated_transactions_t");
  }
#endif
  ~automated_transactions_t() {
    DEBUG_PRINT("ledger.memory.dtors", "dtor automated_transactions_t");
    for (automated_transactions_deque::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++)
      delete *i;
  }

  void extend_entry(entry_t& entry) {
    for (automated_transactions_deque::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++)
      (*i)->extend_entry(entry);
  }

  void add_automated_transaction(automated_transaction_t * auto_xact) {
    automated_transactions.push_back(auto_xact);
  }
  bool remove_automated_transaction(automated_transaction_t * auto_xact) {
    for (automated_transactions_deque::iterator i
	   = automated_transactions.begin();
	 i != automated_transactions.end();
	 i++) {
      if (*i == auto_xact) {
	automated_transactions.erase(i);
	return true;
      }
    }
    return false;
  }
};

struct autoxact_finalizer_t : public entry_finalizer_t {
  automated_transactions_t auto_xacts;
  virtual bool operator()(entry_t& entry) {
    if (! auto_xacts.automated_transactions.empty())
      auto_xacts.extend_entry(entry);
    return true;
  }
};

} // namespace ledger

#endif // _AUTOXACT_H