From 0cf1d8fe695ce570b9171c9e0eae6701a09b610b Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Wed, 11 Aug 2004 23:16:00 -0400 Subject: more reorg --- walk.h | 160 ++++++++++++++++------------------------------------------------- 1 file changed, 38 insertions(+), 122 deletions(-) (limited to 'walk.h') diff --git a/walk.h b/walk.h index c29e5f6d..33c3f5d4 100644 --- a/walk.h +++ b/walk.h @@ -14,6 +14,7 @@ namespace ledger { template struct item_handler { virtual ~item_handler() {} + virtual item_handler * copy() { return NULL; } virtual void flush() {} virtual void operator()(T * item) = 0; }; @@ -46,18 +47,28 @@ struct compare_items { typedef std::deque transactions_deque; typedef std::deque entries_deque; -struct ignore_transactions : public item_handler +class ignore_transactions : public item_handler { + public: virtual void operator()(transaction_t * xact) {} }; -struct clear_display_flags : public item_handler +class clear_display_flags : public item_handler { + public: virtual void operator()(transaction_t * xact) { xact->dflags = 0; } }; +class add_to_account_value : public item_handler +{ + public: + virtual void operator()(transaction_t * xact) { + xact->account->value += *xact; + } +}; + class sort_transactions : public item_handler { transactions_deque transactions; @@ -191,10 +202,11 @@ class collapse_transactions : public item_handler } }; -// This filter requires that calc_transactions be used. - class changed_value_transactions : public item_handler { + // This filter requires that calc_transactions be used at some point + // later in the chain. + transaction_t * last_xact; item_handler * handler; @@ -229,11 +241,11 @@ class changed_value_transactions : public item_handler virtual void operator()(transaction_t * xact); }; -typedef std::map balances_map; -typedef std::pair balances_pair; - class subtotal_transactions : public item_handler { + typedef std::map balances_map; + typedef std::pair balances_pair; + protected: std::time_t start; std::time_t finish; @@ -364,7 +376,6 @@ inline void walk_transactions(transactions_deque& deque, inline void walk_entries(entries_list::iterator begin, entries_list::iterator end, item_handler& handler) { - // jww (2004-08-11): do transaction dflags need to be cleared first? for (entries_list::iterator i = begin; i != end; i++) walk_transactions((*i)->transactions, handler); } @@ -377,103 +388,9 @@ inline void walk_entries(entries_list& list, ////////////////////////////////////////////////////////////////////// // -// Account handlers +// Account walking functions // -typedef std::deque accounts_deque; - -struct add_to_account_value : public item_handler -{ - virtual void operator()(transaction_t * xact) { - xact->account->value += *xact; - } -}; - -#if 0 - -class format_accounts : public item_handler -{ -}; - -class filter_accounts : public item_handler -{ - item_handler * handler; - - public: - filter_accounts(item_handler * _handler) - : handler(_handler) {} - - virtual ~filter_accounts() { - handler->flush(); - delete handler; - } - - virtual void flush() {} - - virtual void operator()(account_t * account) { - } -}; - -class sort_accounts : public item_handler -{ - value_expr_t * sort_order; - - item_handler * handler; - - public: - sort_accounts(item_handler * _handler, - value_expr_t * _sort_order) - : sort_order(_sort_order), handler(_handler) {} - - virtual ~sort_accounts() { - handler->flush(); - delete handler; - } - - virtual void flush() {} - - virtual void operator()(account_t * account) { - accounts_deque accounts; - - for (accounts_map::iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - accounts.push_back((*i).second); - - std::stable_sort(accounts.begin(), accounts.end(), - compare_items(sort_order)); - } -}; - -class balance_accounts : public item_handler -{ - item_handler * handler; - - public: - balance_accounts(item_handler * _handler) - : handler(_handler) {} - - virtual ~balance_accounts() { - handler->flush(); - delete handler; - } - - virtual void flush() { - if (format_account::disp_subaccounts_p(top)) { - std::string end_format = "--------------------\n"; - format.reset(end_format + f); - format.format_elements(std::cout, details_t(top)); - } - } - - virtual void operator()(account_t * account) { - } -}; - -#endif - -////////////////////////////////////////////////////////////////////// - inline void sum_accounts(account_t * account) { for (accounts_map::iterator i = account->accounts.begin(); i != account->accounts.end(); @@ -484,9 +401,11 @@ inline void sum_accounts(account_t * account) { account->total += account->value; } +typedef std::deque accounts_deque; + inline void sort_accounts(account_t * account, - accounts_deque& accounts, - const value_expr_t * sort_order) { + const value_expr_t * sort_order, + accounts_deque& accounts) { for (accounts_map::iterator i = account->accounts.begin(); i != account->accounts.end(); i++) @@ -498,25 +417,22 @@ inline void sort_accounts(account_t * account, inline void walk_accounts(account_t * account, item_handler& handler, - const value_expr_t * sort_order) { + const value_expr_t * sort_order = NULL) { handler(account); - accounts_deque accounts; - sort_accounts(account, accounts, sort_order); - for (accounts_deque::const_iterator i = accounts.begin(); - i != accounts.end(); - i++) - walk_accounts(*i, handler, sort_order); -} - -inline void walk_accounts(account_t * account, - item_handler& handler) { - handler(account); - - for (accounts_map::const_iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - walk_accounts((*i).second, handler); + if (sort_order) { + accounts_deque accounts; + sort_accounts(account, sort_order, accounts); + for (accounts_deque::const_iterator i = accounts.begin(); + i != accounts.end(); + i++) + walk_accounts(*i, handler, sort_order); + } else { + for (accounts_map::const_iterator i = account->accounts.begin(); + i != account->accounts.end(); + i++) + walk_accounts((*i).second, handler); + } } } // namespace ledger -- cgit v1.2.3