summaryrefslogtreecommitdiff
path: root/walk.h
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-08-06 21:38:27 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-08-06 21:38:27 -0400
commit5db1e1165b05ae56e0348a4634144072dfcace1f (patch)
tree18e6f9981d99c8042aa8d93208a8a85ad6382d58 /walk.h
parent88df7968809717123d09c45be5709b673a4b7172 (diff)
downloadfork-ledger-5db1e1165b05ae56e0348a4634144072dfcace1f.tar.gz
fork-ledger-5db1e1165b05ae56e0348a4634144072dfcace1f.tar.bz2
fork-ledger-5db1e1165b05ae56e0348a4634144072dfcace1f.zip
improvements to transaction formatting
Diffstat (limited to 'walk.h')
-rw-r--r--walk.h91
1 files changed, 29 insertions, 62 deletions
diff --git a/walk.h b/walk.h
index b3c39f88..850eb228 100644
--- a/walk.h
+++ b/walk.h
@@ -11,25 +11,6 @@
namespace ledger {
template <typename T>
-class item_predicate
-{
- const node_t * predicate;
-
- public:
- item_predicate(const node_t * _predicate) : predicate(_predicate) {}
-
- bool operator()(const T * item) const {
- if (predicate) {
- balance_t result;
- predicate->compute(result, details_t(item));
- return result;
- } else {
- return true;
- }
- }
-};
-
-template <typename T>
struct compare_items {
const node_t * sort_order;
@@ -59,7 +40,7 @@ class collect_transactions
collect_transactions(transactions_deque& _transactions)
: transactions(_transactions) {}
- void operator()(transaction_t * xact) {
+ void operator()(transaction_t * xact) const {
transactions.push_back(xact);
}
};
@@ -81,17 +62,14 @@ class ignore_transaction
#define OTHER_TRANSACTIONS 0x02
template <typename Function>
-void handle_transaction(transaction_t * xact, Function functor,
- item_predicate<transaction_t>& pred_functor,
- unsigned int flags)
+void handle_transaction(transaction_t * xact,
+ const Function& functor,
+ unsigned int flags)
{
if ((flags & MATCHING_TRANSACTIONS) &&
! (xact->flags & TRANSACTION_HANDLED)) {
xact->flags |= TRANSACTION_HANDLED;
- if (pred_functor(xact)) {
- xact->flags |= TRANSACTION_DISPLAYED;
- functor(xact);
- }
+ functor(xact);
}
if (flags & OTHER_TRANSACTIONS)
@@ -103,35 +81,30 @@ void handle_transaction(transaction_t * xact, Function functor,
continue;
(*i)->flags |= TRANSACTION_HANDLED;
- if (pred_functor(xact)) {
- xact->flags |= TRANSACTION_DISPLAYED;
- functor(*i);
- }
+ functor(*i);
}
}
template <typename Function>
void walk_entries(entries_list::iterator begin,
entries_list::iterator end,
- Function functor,
- const node_t * predicate,
- unsigned int flags,
- const node_t * display_predicate = NULL)
+ const Function& functor,
+ const node_t * predicate,
+ unsigned int flags)
{
item_predicate<transaction_t> pred_functor(predicate);
- item_predicate<transaction_t> disp_pred_functor(display_predicate);
for (entries_list::iterator i = begin; i != end; i++)
for (transactions_list::iterator j = (*i)->transactions.begin();
j != (*i)->transactions.end();
j++)
if (pred_functor(*j))
- handle_transaction(*j, functor, disp_pred_functor, flags);
+ handle_transaction(*j, functor, flags);
}
template <typename Function>
void walk_entries(entries_list::iterator begin,
- entries_list::iterator end, Function functor)
+ entries_list::iterator end, const Function& functor)
{
for (entries_list::iterator i = begin; i != end; i++)
for (transactions_list::iterator j = (*i)->transactions.begin();
@@ -144,7 +117,7 @@ class clear_flags
{
public:
void operator()(transaction_t * xact) const {
- xact->flags &= ~(TRANSACTION_HANDLED | TRANSACTION_DISPLAYED);
+ xact->flags &= ~TRANSACTION_TRANSIENT;
}
};
@@ -156,7 +129,8 @@ inline void clear_transaction_display_flags(entries_list::iterator begin,
template <typename Function>
void walk_transactions(transactions_list::iterator begin,
- transactions_list::iterator end, Function functor)
+ transactions_list::iterator end,
+ const Function& functor)
{
for (transactions_list::iterator i = begin; i != end; i++)
functor(*i);
@@ -164,7 +138,8 @@ void walk_transactions(transactions_list::iterator begin,
template <typename Function>
void walk_transactions(transactions_deque::iterator begin,
- transactions_deque::iterator end, Function functor)
+ transactions_deque::iterator end,
+ const Function& functor)
{
for (transactions_deque::iterator i = begin; i != end; i++)
functor(*i);
@@ -187,28 +162,24 @@ inline void sort_accounts(account_t * account,
template <typename Function>
void walk__accounts(const account_t * account,
- Function functor,
- const unsigned int max_depth,
- item_predicate<account_t>& disp_pred_functor)
+ const Function& functor,
+ const unsigned int max_depth)
{
- if (disp_pred_functor(account))
- functor(account, max_depth);
+ functor(account, max_depth);
for (accounts_map::const_iterator i = account->accounts.begin();
i != account->accounts.end();
i++)
- walk__accounts((*i).second, functor, max_depth, disp_pred_functor);
+ walk__accounts((*i).second, functor, max_depth);
}
template <typename Function>
void walk__accounts_sorted(const account_t * account,
- Function functor,
+ const Function& functor,
const unsigned int max_depth,
- const node_t * sort_order,
- item_predicate<account_t>& disp_pred_functor)
+ const node_t * sort_order)
{
- if (disp_pred_functor(account))
- functor(account, max_depth);
+ functor(account, max_depth);
accounts_deque accounts;
@@ -223,12 +194,11 @@ void walk__accounts_sorted(const account_t * account,
for (accounts_deque::const_iterator i = accounts.begin();
i != accounts.end();
i++)
- walk__accounts_sorted(*i, functor, max_depth, sort_order,
- disp_pred_functor);
+ walk__accounts_sorted(*i, functor, max_depth, sort_order);
}
template <typename Function>
-void for_each_account(account_t * account, Function functor)
+void for_each_account(account_t * account, const Function& functor)
{
functor(account);
@@ -255,26 +225,23 @@ inline void sum__accounts(account_t * account)
template <typename Function>
void walk_accounts(account_t * account,
- Function functor,
+ const Function& functor,
const node_t * predicate,
unsigned int flags,
const bool calc_subtotals,
const unsigned int max_depth,
- const node_t * display_predicate = NULL,
- const node_t * sort_order = NULL)
+ const node_t * sort_order = NULL)
{
item_predicate<transaction_t> pred_functor(predicate);
- item_predicate<account_t> disp_pred_functor(display_predicate);
calc__accounts(account, pred_functor, flags);
if (calc_subtotals)
sum__accounts(account);
if (sort_order)
- walk__accounts_sorted<Function>(account, functor, max_depth, sort_order,
- disp_pred_functor);
+ walk__accounts_sorted<Function>(account, functor, max_depth, sort_order);
else
- walk__accounts<Function>(account, functor, max_depth, disp_pred_functor);
+ walk__accounts<Function>(account, functor, max_depth);
}
} // namespace ledger