changed the way changed_value_filter is called
This commit is contained in:
parent
a2efee0a8e
commit
c5a137d9e9
3 changed files with 53 additions and 36 deletions
46
format.h
46
format.h
|
|
@ -83,9 +83,15 @@ struct format_t
|
|||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct item_formatter : public item_handler<T> {
|
||||
virtual ~item_formatter() {}
|
||||
virtual void flush() const = 0;
|
||||
};
|
||||
|
||||
#define COLLAPSED_REGISTER 1 // support collapsed registers
|
||||
|
||||
class format_transaction : public item_handler<transaction_t>
|
||||
class format_transaction : public item_formatter<transaction_t>
|
||||
{
|
||||
std::ostream& output_stream;
|
||||
const format_t& first_line_format;
|
||||
|
|
@ -127,8 +133,13 @@ class format_transaction : public item_handler<transaction_t>
|
|||
|
||||
#ifdef COLLAPSED_REGISTER
|
||||
virtual ~format_transaction() {
|
||||
flush();
|
||||
}
|
||||
|
||||
virtual void flush() const {
|
||||
if (subtotal)
|
||||
report_cumulative_subtotal();
|
||||
output_stream.flush();
|
||||
}
|
||||
|
||||
void report_cumulative_subtotal() const;
|
||||
|
|
@ -138,26 +149,34 @@ class format_transaction : public item_handler<transaction_t>
|
|||
};
|
||||
|
||||
|
||||
class changed_value_filter : public item_handler<transaction_t>
|
||||
class changed_value_filter : public item_formatter<transaction_t>
|
||||
{
|
||||
const item_handler<transaction_t>& handler;
|
||||
item_formatter<transaction_t> * handler;
|
||||
|
||||
mutable entry_t modified_entry;
|
||||
mutable transaction_t modified_xact;
|
||||
mutable transaction_t * last_xact;
|
||||
|
||||
public:
|
||||
changed_value_filter(const item_handler<transaction_t>& _handler)
|
||||
changed_value_filter(item_formatter<transaction_t> * _handler)
|
||||
: handler(_handler), modified_xact(&modified_entry, NULL),
|
||||
last_xact(NULL) {
|
||||
assert(handler);
|
||||
modified_entry.payee = "Commodities revalued";
|
||||
}
|
||||
|
||||
virtual ~changed_value_filter() {
|
||||
flush();
|
||||
handler->flush();
|
||||
assert(handler);
|
||||
delete handler;
|
||||
}
|
||||
|
||||
virtual void flush() const {
|
||||
(*this)(NULL);
|
||||
}
|
||||
|
||||
void operator()(transaction_t * xact) const {
|
||||
virtual void operator()(transaction_t * xact) const {
|
||||
if (last_xact) {
|
||||
balance_t prev_bal, cur_bal;
|
||||
|
||||
|
|
@ -178,19 +197,19 @@ class changed_value_filter : public item_handler<transaction_t>
|
|||
modified_xact.total = diff;
|
||||
modified_xact.total.negate();
|
||||
|
||||
handler(&modified_xact);
|
||||
(*handler)(&modified_xact);
|
||||
}
|
||||
}
|
||||
|
||||
if (xact)
|
||||
handler(xact);
|
||||
(*handler)(xact);
|
||||
|
||||
last_xact = xact;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class format_account : public item_handler<account_t>
|
||||
class format_account : public item_formatter<account_t>
|
||||
{
|
||||
std::ostream& output_stream;
|
||||
const format_t& format;
|
||||
|
|
@ -205,6 +224,10 @@ class format_account : public item_handler<account_t>
|
|||
disp_pred(display_predicate) {}
|
||||
virtual ~format_account() {}
|
||||
|
||||
virtual void flush() const {
|
||||
output_stream.flush();
|
||||
}
|
||||
|
||||
static bool disp_subaccounts_p(const account_t * account,
|
||||
const item_predicate<account_t>& disp_pred,
|
||||
const account_t *& to_show);
|
||||
|
|
@ -225,7 +248,7 @@ class format_account : public item_handler<account_t>
|
|||
};
|
||||
|
||||
|
||||
class format_equity : public item_handler<account_t>
|
||||
class format_equity : public item_formatter<account_t>
|
||||
{
|
||||
std::ostream& output_stream;
|
||||
const format_t& first_line_format;
|
||||
|
|
@ -251,9 +274,14 @@ class format_equity : public item_handler<account_t>
|
|||
}
|
||||
|
||||
virtual ~format_equity() {
|
||||
flush();
|
||||
}
|
||||
|
||||
virtual void flush() const {
|
||||
account_t summary(NULL, "Equity:Opening Balances");
|
||||
summary.value = - total;
|
||||
next_lines_format.format_elements(output_stream, details_t(&summary));
|
||||
output_stream.flush();
|
||||
}
|
||||
|
||||
virtual void operator()(account_t * account) const {
|
||||
|
|
|
|||
36
main.cc
36
main.cc
|
|
@ -587,8 +587,10 @@ int main(int argc, char * argv[])
|
|||
first_line_format = next_lines_format = f;
|
||||
}
|
||||
|
||||
if (command == "b") {
|
||||
format_t format(first_line_format);
|
||||
format_t nformat(next_lines_format);
|
||||
|
||||
if (command == "b") {
|
||||
format_account formatter(std::cout, format, display_predicate);
|
||||
walk_accounts(journal->master, formatter, predicate,
|
||||
xact_display_flags, show_subtotals, sort_order.get());
|
||||
|
|
@ -600,15 +602,11 @@ int main(int argc, char * argv[])
|
|||
}
|
||||
}
|
||||
else if (command == "E") {
|
||||
format_t format(first_line_format);
|
||||
format_t nformat(next_lines_format);
|
||||
format_equity formatter(std::cout, format, nformat, display_predicate);
|
||||
walk_accounts(journal->master, formatter, predicate,
|
||||
xact_display_flags, true, sort_order.get());
|
||||
}
|
||||
else if (command == "e") {
|
||||
format_t format(first_line_format);
|
||||
format_t nformat(next_lines_format);
|
||||
format_transaction formatter(std::cout, format, nformat);
|
||||
|
||||
for (transactions_list::iterator i = new_entry->transactions.begin();
|
||||
|
|
@ -617,22 +615,19 @@ int main(int argc, char * argv[])
|
|||
handle_transaction(*i, formatter, xact_display_flags);
|
||||
}
|
||||
else {
|
||||
format_t format(first_line_format);
|
||||
format_t nformat(next_lines_format);
|
||||
format_transaction formatter(std::cout, format, nformat, display_predicate,
|
||||
std::auto_ptr<item_formatter<transaction_t> >
|
||||
formatter(new format_transaction(std::cout, format, nformat,
|
||||
display_predicate,
|
||||
#ifdef COLLAPSED_REGISTER
|
||||
! show_subtotals,
|
||||
#endif
|
||||
show_inverted);
|
||||
show_inverted));
|
||||
if (show_commodities_revalued)
|
||||
formatter.reset(new changed_value_filter(formatter.release()));
|
||||
|
||||
if (! sort_order.get()) {
|
||||
if (show_commodities_revalued) {
|
||||
changed_value_filter filtered_formatter(formatter);
|
||||
walk_entries(journal->entries.begin(), journal->entries.end(),
|
||||
filtered_formatter, predicate, xact_display_flags);
|
||||
} else {
|
||||
walk_entries(journal->entries.begin(), journal->entries.end(),
|
||||
formatter, predicate, xact_display_flags);
|
||||
}
|
||||
*formatter.get(), predicate, xact_display_flags);
|
||||
} else {
|
||||
transactions_deque transactions_pool;
|
||||
walk_entries(journal->entries.begin(), journal->entries.end(),
|
||||
|
|
@ -640,15 +635,8 @@ int main(int argc, char * argv[])
|
|||
xact_display_flags);
|
||||
std::stable_sort(transactions_pool.begin(), transactions_pool.end(),
|
||||
compare_items<transaction_t>(sort_order.get()));
|
||||
|
||||
if (show_commodities_revalued) {
|
||||
changed_value_filter filtered_formatter(formatter);
|
||||
walk_transactions(transactions_pool.begin(), transactions_pool.end(),
|
||||
filtered_formatter);
|
||||
} else {
|
||||
walk_transactions(transactions_pool.begin(), transactions_pool.end(),
|
||||
formatter);
|
||||
}
|
||||
*formatter.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
1
walk.h
1
walk.h
|
|
@ -12,6 +12,7 @@ namespace ledger {
|
|||
|
||||
template <typename T>
|
||||
struct item_handler {
|
||||
virtual ~item_handler() {}
|
||||
virtual void operator()(T * item) const = 0;
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue