diff options
author | John Wiegley <johnw@newartisans.com> | 2004-08-10 18:19:36 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2004-08-10 18:19:36 -0400 |
commit | a2efee0a8e895fc94e6eb4e048d32aa248b92080 (patch) | |
tree | a24ecdcae48add2ce5bafa3a3532aceb0e375159 /walk.h | |
parent | 8792945e76277cea96c13444fa5e4cad7f3ce6ee (diff) | |
download | fork-ledger-a2efee0a8e895fc94e6eb4e048d32aa248b92080.tar.gz fork-ledger-a2efee0a8e895fc94e6eb4e048d32aa248b92080.tar.bz2 fork-ledger-a2efee0a8e895fc94e6eb4e048d32aa248b92080.zip |
use polymorphism, instead of templates, for walking items
Diffstat (limited to 'walk.h')
-rw-r--r-- | walk.h | 126 |
1 files changed, 60 insertions, 66 deletions
@@ -11,6 +11,11 @@ namespace ledger { template <typename T> +struct item_handler { + virtual void operator()(T * item) const = 0; +}; + +template <typename T> struct compare_items { const node_t * sort_order; @@ -32,15 +37,14 @@ struct compare_items { typedef std::deque<transaction_t *> transactions_deque; -class collect_transactions -{ +class collect_transactions : public item_handler<transaction_t> { 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<transaction_t>(sort_order)); } -class ignore_transaction -{ - public: - void operator()(transaction_t * xact) const {} +struct ignore_transaction : public item_handler<transaction_t> { + virtual void operator()(transaction_t * xact) const {} }; #define MATCHING_TRANSACTIONS 0x01 #define OTHER_TRANSACTIONS 0x02 -template <typename Function> -void handle_transaction(transaction_t * xact, - const Function& functor, - unsigned int flags) +inline void handle_transaction(transaction_t * xact, + const item_handler<transaction_t>& 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 <typename Function> -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<transaction_t>& handler, + const std::string& predicate, + unsigned int flags) { - item_predicate<transaction_t> pred_functor(predicate); + item_predicate<transaction_t> 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 <typename Function> -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<transaction_t>& 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<transaction_t> { + 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<clear_flags>(begin, end, clear_flags()); + walk_entries(begin, end, clear_flags()); } -template <typename Function> -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<transaction_t>& handler) { for (transactions_list::iterator i = begin; i != end; i++) - functor(*i); + handler(*i); } -template <typename Function> -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<transaction_t>& handler) { for (transactions_deque::iterator i = begin; i != end; i++) - functor(*i); + handler(*i); } typedef std::deque<account_t *> accounts_deque; @@ -154,23 +150,22 @@ inline void sort_accounts(account_t * account, compare_items<account_t>(sort_order)); } -template <typename Function> -void walk__accounts(account_t * account, const Function& functor) +inline void walk__accounts(account_t * account, + const item_handler<account_t>& 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 <typename Function> -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<account_t>& 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 <typename Function> -void for_each_account(account_t * account, const Function& functor) +inline void for_each_account(account_t * account, + const item_handler<account_t>& 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<transaction_t>& pred_functor, + const item_predicate<transaction_t>& 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 <typename Function> -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<account_t>& handler, + const std::string& predicate, + unsigned int flags, + const bool calc_subtotals, + const node_t * sort_order = NULL) { - item_predicate<transaction_t> pred_functor(predicate); + item_predicate<transaction_t> 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<Function>(account, functor, sort_order); + walk__accounts_sorted(account, handler, sort_order); else - walk__accounts<Function>(account, functor); + walk__accounts(account, handler); } } // namespace ledger |