slight changed semantics of item_handler<T>, to play better with Boost.Python

This commit is contained in:
John Wiegley 2004-09-10 04:38:38 -04:00
parent 359b3ba326
commit 6261da4660
4 changed files with 66 additions and 43 deletions

67
main.cc
View file

@ -141,47 +141,50 @@ void parse_ledger_data(journal_t * journal,
item_handler<transaction_t> * item_handler<transaction_t> *
chain_formatters(const std::string& command, chain_formatters(const std::string& command,
item_handler<transaction_t> * base_formatter) item_handler<transaction_t> * base_formatter,
std::list<item_handler<transaction_t> *>& ptrs)
{ {
std::auto_ptr<item_handler<transaction_t> > formatter; item_handler<transaction_t> * formatter = NULL;
// format_transactions write each transaction received to the // format_transactions write each transaction received to the
// output stream. // output stream.
if (command == "b" || command == "E") { if (command == "b" || command == "E") {
formatter.reset(base_formatter); ptrs.push_back(formatter = base_formatter);
} else { } else {
formatter.reset(base_formatter); ptrs.push_back(formatter = base_formatter);
// filter_transactions will only pass through transactions // filter_transactions will only pass through transactions
// matching the `display_predicate'. // matching the `display_predicate'.
if (! config.display_predicate.empty()) if (! config.display_predicate.empty())
formatter.reset(new filter_transactions(formatter.release(), ptrs.push_back(formatter =
config.display_predicate)); new filter_transactions(formatter,
config.display_predicate));
// calc_transactions computes the running total. When this // calc_transactions computes the running total. When this
// appears will determine, for example, whether filtered // appears will determine, for example, whether filtered
// transactions are included or excluded from the running total. // transactions are included or excluded from the running total.
formatter.reset(new calc_transactions(formatter.release(), ptrs.push_back(formatter =
config.show_inverted)); new calc_transactions(formatter, config.show_inverted));
// sort_transactions will sort all the transactions it sees, based // sort_transactions will sort all the transactions it sees, based
// on the `sort_order' value expression. // on the `sort_order' value expression.
if (config.sort_order.get()) if (config.sort_order.get())
formatter.reset(new sort_transactions(formatter.release(), ptrs.push_back(formatter =
config.sort_order.get())); new sort_transactions(formatter, config.sort_order.get()));
// changed_value_transactions adds virtual transactions to the // changed_value_transactions adds virtual transactions to the
// list to account for changes in market value of commodities, // list to account for changes in market value of commodities,
// which otherwise would affect the running total unpredictably. // which otherwise would affect the running total unpredictably.
if (config.show_revalued) if (config.show_revalued)
formatter.reset(new changed_value_transactions(formatter.release(), ptrs.push_back(formatter =
config.show_revalued_only)); new changed_value_transactions(formatter,
config.show_revalued_only));
// collapse_transactions causes entries with multiple transactions // collapse_transactions causes entries with multiple transactions
// to appear as entries with a subtotaled transaction for each // to appear as entries with a subtotaled transaction for each
// commodity used. // commodity used.
if (config.show_collapsed) if (config.show_collapsed)
formatter.reset(new collapse_transactions(formatter.release())); ptrs.push_back(formatter = new collapse_transactions(formatter));
// subtotal_transactions combines all the transactions it receives // subtotal_transactions combines all the transactions it receives
// into one subtotal entry, which has one transaction for each // into one subtotal entry, which has one transaction for each
@ -195,13 +198,14 @@ chain_formatters(const std::string& command,
// reports all the transactions that fall on each subsequent day // reports all the transactions that fall on each subsequent day
// of the week. // of the week.
if (config.show_subtotal) if (config.show_subtotal)
formatter.reset(new subtotal_transactions(formatter.release())); ptrs.push_back(formatter = new subtotal_transactions(formatter));
else if (config.report_interval) else if (config.report_interval)
formatter.reset(new interval_transactions(formatter.release(), ptrs.push_back(formatter =
config.report_interval, new interval_transactions(formatter,
config.interval_begin)); config.report_interval,
config.interval_begin));
else if (config.days_of_the_week) else if (config.days_of_the_week)
formatter.reset(new dow_transactions(formatter.release())); ptrs.push_back(formatter = new dow_transactions(formatter));
} }
// related_transactions will pass along all transactions related // related_transactions will pass along all transactions related
@ -210,16 +214,17 @@ chain_formatters(const std::string& command,
// one transaction of an entry is to be printed, all the // one transaction of an entry is to be printed, all the
// transaction for that entry will be printed. // transaction for that entry will be printed.
if (config.show_related) if (config.show_related)
formatter.reset(new related_transactions(formatter.release(), ptrs.push_back(formatter =
config.show_all_related)); new related_transactions(formatter,
config.show_all_related));
// This filter_transactions will only pass through transactions // This filter_transactions will only pass through transactions
// matching the `predicate'. // matching the `predicate'.
if (! config.predicate.empty()) if (! config.predicate.empty())
formatter.reset(new filter_transactions(formatter.release(), ptrs.push_back(formatter = new filter_transactions(formatter,
config.predicate)); config.predicate));
return formatter.release(); return formatter;
} }
int parse_and_report(int argc, char * argv[], char * envp[]) int parse_and_report(int argc, char * argv[], char * envp[])
@ -306,15 +311,17 @@ int parse_and_report(int argc, char * argv[], char * envp[])
TIMER_START(report_gen); TIMER_START(report_gen);
std::auto_ptr<item_handler<transaction_t> > formatter; item_handler<transaction_t> * formatter;
std::list<item_handler<transaction_t> *> formatter_ptrs;
if (command == "b" || command == "E") { if (command == "b" || command == "E") {
formatter.reset(chain_formatters(command, new set_account_value)); formatter = new set_account_value;
formatter = chain_formatters(command, formatter, formatter_ptrs);
} else { } else {
std::ostream& out(config.output_stream.get() ? std::ostream& out(config.output_stream.get() ?
*config.output_stream : std::cout); *config.output_stream : std::cout);
formatter.reset(chain_formatters(command, formatter = new format_transactions(out, config.format, config.nformat);
new format_transactions(out, config.format, config.nformat))); formatter = chain_formatters(command, formatter, formatter_ptrs);
} }
if (command == "e") if (command == "e")
@ -354,6 +361,12 @@ int parse_and_report(int argc, char * argv[], char * envp[])
} }
#if DEBUG_LEVEL >= BETA #if DEBUG_LEVEL >= BETA
for (std::list<item_handler<transaction_t> *>::iterator i
= formatter_ptrs.begin();
i != formatter_ptrs.end();
i++)
delete *i;
// Cleanup the data handlers that might be present on some objects. // Cleanup the data handlers that might be present on some objects.
clear_transaction_data xact_cleanup; clear_transaction_data xact_cleanup;

View file

@ -24,8 +24,8 @@ class OutputTransaction (TransactionHandler):
print xact.entry.payee print xact.entry.payee
handler = OutputTransaction() handler = OutputTransaction()
chain = FilterTransactions (handler, "/Checking/") handler = FilterTransactions (handler, "/Checking/")
for entry in journal: for entry in journal:
for xact in entry: for xact in entry:
chain (xact) handler (xact)

36
walk.cc
View file

@ -312,18 +312,19 @@ template <typename T>
struct item_handler_wrap : public item_handler<T> struct item_handler_wrap : public item_handler<T>
{ {
PyObject* self; PyObject* self;
item_handler_wrap(PyObject * self_) : self(self_) {} item_handler_wrap(PyObject * self_) : self(self_) {}
item_handler_wrap(PyObject * self_, const item_handler<T>& handler) item_handler_wrap(PyObject * self_, const item_handler<T>& handler)
: item_handler<T>(const_cast<item_handler<T> *>(&handler)), self(self_) {} : item_handler<T>(const_cast<item_handler<T> *>(&handler)), self(self_) {}
void flush() { virtual void flush() {
call_method<void>(self, "flush"); call_method<void>(self, "flush");
} }
void default_flush() { void default_flush() {
item_handler<T>::flush(); item_handler<T>::flush();
} }
void operator()(T& item) { virtual void operator()(T& item) {
call_method<void>(self, "__call__", item); call_method<void>(self, "__call__", item);
} }
void default_call(T& item) { void default_call(T& item) {
@ -357,65 +358,76 @@ void export_walk()
; ;
class_< set_account_value > class_< set_account_value >
("SetAccountValue", init<item_handler<transaction_t> *>()) ("SetAccountValue", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &item_handler<transaction_t>::flush) .def("flush", &item_handler<transaction_t>::flush)
.def("__call__", &set_account_value::operator()); .def("__call__", &set_account_value::operator());
; ;
#if 0 #if 0
class_< sort_transactions > class_< sort_transactions >
("SortTransactions", init<item_handler<transaction_t> *>()) ("SortTransactions", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &sort_transactions::flush) .def("flush", &sort_transactions::flush)
.def("__call__", &sort_transactions::operator()); .def("__call__", &sort_transactions::operator());
; ;
#endif #endif
class_< filter_transactions > class_< filter_transactions >
("FilterTransactions", init<item_handler<transaction_t> *, std::string>()) ("FilterTransactions", init<item_handler<transaction_t> *, std::string>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &item_handler<transaction_t>::flush) .def("flush", &item_handler<transaction_t>::flush)
.def("__call__", &filter_transactions::operator()); .def("__call__", &filter_transactions::operator());
; ;
class_< calc_transactions > class_< calc_transactions >
("CalcTransactions", init<item_handler<transaction_t> *, optional<bool> >()) ("CalcTransactions", init<item_handler<transaction_t> *, optional<bool> >()
[with_custodian_and_ward<1, 2>()])
.def("flush", &item_handler<transaction_t>::flush) .def("flush", &item_handler<transaction_t>::flush)
.def("__call__", &calc_transactions::operator()); .def("__call__", &calc_transactions::operator());
; ;
class_< collapse_transactions > class_< collapse_transactions >
("CollapseTransactions", init<item_handler<transaction_t> *>()) ("CollapseTransactions", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &collapse_transactions::flush) .def("flush", &collapse_transactions::flush)
.def("__call__", &collapse_transactions::operator()); .def("__call__", &collapse_transactions::operator());
; ;
class_< changed_value_transactions > class_< changed_value_transactions >
("ChangeValueTransactions", init<item_handler<transaction_t> *, bool>()) ("ChangeValueTransactions", init<item_handler<transaction_t> *, bool>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &changed_value_transactions::flush) .def("flush", &changed_value_transactions::flush)
.def("__call__", &changed_value_transactions::operator()); .def("__call__", &changed_value_transactions::operator());
; ;
class_< subtotal_transactions > class_< subtotal_transactions >
("SubtotalTransactions", init<item_handler<transaction_t> *>()) ("SubtotalTransactions", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", subtotal_transactions_flush) .def("flush", subtotal_transactions_flush)
.def("__call__", &subtotal_transactions::operator()); .def("__call__", &subtotal_transactions::operator());
; ;
#if 0 #if 0
class_< interval_transactions > class_< interval_transactions >
("IntervalTransactions", init<item_handler<transaction_t> *>()) ("IntervalTransactions", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &item_handler<transaction_t>::flush) .def("flush", &item_handler<transaction_t>::flush)
.def("__call__", &interval_transactions::operator()); .def("__call__", &interval_transactions::operator());
; ;
#endif #endif
class_< dow_transactions > class_< dow_transactions >
("DowTransactions", init<item_handler<transaction_t> *>()) ("DowTransactions", init<item_handler<transaction_t> *>()
[with_custodian_and_ward<1, 2>()])
.def("flush", &dow_transactions::flush) .def("flush", &dow_transactions::flush)
.def("__call__", &dow_transactions::operator()); .def("__call__", &dow_transactions::operator());
; ;
class_< related_transactions > class_< related_transactions >
("RelatedTransactions", init<item_handler<transaction_t> *, optional<bool> >()) ("RelatedTransactions",
init<item_handler<transaction_t> *, optional<bool> >()
[with_custodian_and_ward<1, 2>()])
.def("flush", &item_handler<transaction_t>::flush) .def("flush", &item_handler<transaction_t>::flush)
.def("__call__", &related_transactions::operator()); .def("__call__", &related_transactions::operator());
; ;

2
walk.h
View file

@ -25,8 +25,6 @@ struct item_handler {
virtual ~item_handler() { virtual ~item_handler() {
DEBUG_PRINT("ledger.memory.dtors", "dtor item_handler<T>"); DEBUG_PRINT("ledger.memory.dtors", "dtor item_handler<T>");
if (handler)
delete handler;
} }
virtual void flush() { virtual void flush() {
if (handler) if (handler)