From 4f56735dfa83b4415be52793bcc99fa4623b69a5 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 24 Sep 2004 04:10:39 -0400 Subject: changed entry_finalizer scheme to use objects, not just function pointers --- journal.cc | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'journal.cc') diff --git a/journal.cc b/journal.cc index af3f4832..4166d45e 100644 --- a/journal.cc +++ b/journal.cc @@ -360,6 +360,7 @@ journal_t::~journal_t() { DEBUG_PRINT("ledger.memory.dtors", "dtor journal_t"); + delete default_finalizer; delete master; // Don't bother unhooking each entry's transactions from the @@ -574,27 +575,42 @@ account_t * py_find_account_2(journal_t& journal, const std::string& name, return journal.find_account(name, auto_create); } -#if 0 +struct py_entry_finalizer_t : public entry_finalizer_t { + object pyobj; + py_entry_finalizer_t() {} + py_entry_finalizer_t(object obj) : pyobj(obj) {} + py_entry_finalizer_t(const py_entry_finalizer_t& other) + : pyobj(other.pyobj) {} + virtual bool operator()(entry_t& entry) { + return call(pyobj.ptr(), entry); + } +}; + +std::list py_finalizers; -void py_add_entry_finalize_hook(journal_t& journal, object x) +void py_add_entry_finalizer(journal_t& journal, object x) { - add_hook(journal.entry_finalize_hooks, - extract(x)); + py_finalizers.push_back(py_entry_finalizer_t(x)); + add_hook(journal.entry_finalize_hooks, + &py_finalizers.back()); } -void py_remove_entry_finalize_hook(journal_t& journal, object x) +void py_remove_entry_finalizer(journal_t& journal, object x) { - remove_hook(journal.entry_finalize_hooks, - extract(x)); + for (std::list::iterator i = py_finalizers.begin(); + i != py_finalizers.end(); + i++) + if ((*i).pyobj == x) { + remove_hook(journal.entry_finalize_hooks, &(*i)); + return; + } } -void py_run_entry_finalize_hooks(journal_t& journal, entry_t& entry) +void py_run_entry_finalizers(journal_t& journal, entry_t& entry) { run_hooks(journal.entry_finalize_hooks, entry); } -#endif - #define EXC_TRANSLATOR(type) \ void exc_translate_ ## type(const type& err) { \ PyErr_SetString(PyExc_RuntimeError, err.what()); \ @@ -682,11 +698,9 @@ void export_journal() .def("add_entry", &journal_t::add_entry) .def("remove_entry", &journal_t::remove_entry) -#if 0 - .def("add_entry_finalize_hook", py_add_entry_finalize_hook) - .def("remove_entry_finalize_hook", py_remove_entry_finalize_hook) - .def("run_entry_finalize_hooks", py_run_entry_finalize_hooks) -#endif + .def("add_entry_finalizer", py_add_entry_finalizer) + .def("remove_entry_finalizer", py_remove_entry_finalizer) + .def("run_entry_finalizers", py_run_entry_finalizers) .def("valid", &journal_t::valid) ; -- cgit v1.2.3