From a2efee0a8e895fc94e6eb4e048d32aa248b92080 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Tue, 10 Aug 2004 18:19:36 -0400 Subject: use polymorphism, instead of templates, for walking items --- walk.h | 126 +++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 60 insertions(+), 66 deletions(-) (limited to 'walk.h') diff --git a/walk.h b/walk.h index f54138df..1c91d699 100644 --- a/walk.h +++ b/walk.h @@ -10,6 +10,11 @@ namespace ledger { +template +struct item_handler { + virtual void operator()(T * item) const = 0; +}; + template struct compare_items { const node_t * sort_order; @@ -32,15 +37,14 @@ struct compare_items { typedef std::deque transactions_deque; -class collect_transactions -{ +class collect_transactions : public item_handler { transactions_deque& transactions; public: collect_transactions(transactions_deque& _transactions) : transactions(_transactions) {} - void operator()(transaction_t * xact) const { + virtual void operator()(transaction_t * xact) const { transactions.push_back(xact); } }; @@ -52,19 +56,16 @@ inline void sort_transactions(transactions_deque& transactions, compare_items(sort_order)); } -class ignore_transaction -{ - public: - void operator()(transaction_t * xact) const {} +struct ignore_transaction : public item_handler { + virtual void operator()(transaction_t * xact) const {} }; #define MATCHING_TRANSACTIONS 0x01 #define OTHER_TRANSACTIONS 0x02 -template -void handle_transaction(transaction_t * xact, - const Function& functor, - unsigned int flags) +inline void handle_transaction(transaction_t * xact, + const item_handler& handler, + unsigned int flags) { for (transactions_list::iterator i = xact->entry->transactions.begin(); i != xact->entry->transactions.end(); @@ -75,42 +76,39 @@ void handle_transaction(transaction_t * xact, (*i == xact ? (flags & MATCHING_TRANSACTIONS) : (flags & OTHER_TRANSACTIONS))) { (*i)->dflags |= TRANSACTION_HANDLED; - functor(*i); + handler(*i); } } -template -void walk_entries(entries_list::iterator begin, - entries_list::iterator end, - const Function& functor, - const std::string& predicate, - unsigned int flags) +inline void walk_entries(entries_list::iterator begin, + entries_list::iterator end, + const item_handler& handler, + const std::string& predicate, + unsigned int flags) { - item_predicate pred_functor(predicate); + item_predicate pred(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, flags); + if (pred(*j)) + handle_transaction(*j, handler, flags); } -template -void walk_entries(entries_list::iterator begin, - entries_list::iterator end, const Function& functor) +inline void walk_entries(entries_list::iterator begin, + entries_list::iterator end, + const item_handler& handler) { for (entries_list::iterator i = begin; i != end; i++) for (transactions_list::iterator j = (*i)->transactions.begin(); j != (*i)->transactions.end(); j++) - functor(*j); + handler(*j); } -class clear_flags -{ - public: - void operator()(transaction_t * xact) const { +struct clear_flags : public item_handler { + virtual void operator()(transaction_t * xact) const { xact->dflags = 0; } }; @@ -118,25 +116,23 @@ class clear_flags inline void clear_transaction_display_flags(entries_list::iterator begin, entries_list::iterator end) { - walk_entries(begin, end, clear_flags()); + walk_entries(begin, end, clear_flags()); } -template -void walk_transactions(transactions_list::iterator begin, - transactions_list::iterator end, - const Function& functor) +inline void walk_transactions(transactions_list::iterator begin, + transactions_list::iterator end, + const item_handler& handler) { for (transactions_list::iterator i = begin; i != end; i++) - functor(*i); + handler(*i); } -template -void walk_transactions(transactions_deque::iterator begin, - transactions_deque::iterator end, - const Function& functor) +inline void walk_transactions(transactions_deque::iterator begin, + transactions_deque::iterator end, + const item_handler& handler) { for (transactions_deque::iterator i = begin; i != end; i++) - functor(*i); + handler(*i); } typedef std::deque accounts_deque; @@ -154,23 +150,22 @@ inline void sort_accounts(account_t * account, compare_items(sort_order)); } -template -void walk__accounts(account_t * account, const Function& functor) +inline void walk__accounts(account_t * account, + const item_handler& handler) { - functor(account); + handler(account); for (accounts_map::const_iterator i = account->accounts.begin(); i != account->accounts.end(); i++) - walk__accounts((*i).second, functor); + walk__accounts((*i).second, handler); } -template -void walk__accounts_sorted(account_t * account, - const Function& functor, - const node_t * sort_order) +inline void walk__accounts_sorted(account_t * account, + const item_handler& handler, + const node_t * sort_order) { - functor(account); + handler(account); accounts_deque accounts; @@ -185,22 +180,22 @@ void walk__accounts_sorted(account_t * account, for (accounts_deque::const_iterator i = accounts.begin(); i != accounts.end(); i++) - walk__accounts_sorted(*i, functor, sort_order); + walk__accounts_sorted(*i, handler, sort_order); } -template -void for_each_account(account_t * account, const Function& functor) +inline void for_each_account(account_t * account, + const item_handler& handler) { - functor(account); + handler(account); for (accounts_map::iterator i = account->accounts.begin(); i != account->accounts.end(); i++) - walk__accounts((*i).second, functor); + walk__accounts((*i).second, handler); } void calc__accounts(account_t * account, - const item_predicate& pred_functor, + const item_predicate& pred, unsigned int flags); inline void sum__accounts(account_t * account) @@ -214,24 +209,23 @@ inline void sum__accounts(account_t * account) account->total += account->value; } -template -void walk_accounts(account_t * account, - const Function& functor, - const std::string& predicate, - unsigned int flags, - const bool calc_subtotals, - const node_t * sort_order = NULL) +inline void walk_accounts(account_t * account, + const item_handler& handler, + const std::string& predicate, + unsigned int flags, + const bool calc_subtotals, + const node_t * sort_order = NULL) { - item_predicate pred_functor(predicate); + item_predicate pred(predicate); - calc__accounts(account, pred_functor, flags); + calc__accounts(account, pred, flags); if (calc_subtotals) sum__accounts(account); if (sort_order) - walk__accounts_sorted(account, functor, sort_order); + walk__accounts_sorted(account, handler, sort_order); else - walk__accounts(account, functor); + walk__accounts(account, handler); } } // namespace ledger -- cgit v1.2.3