Did away with the "finalizer" abstraction
This was from an earlier time, when it was intended to be used by Python. But it's not needed anymore.
This commit is contained in:
parent
31f85cc803
commit
c22b8457ef
8 changed files with 11 additions and 246 deletions
81
src/hooks.h
81
src/hooks.h
|
|
@ -1,81 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2003-2009, John Wiegley. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* - Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* - Neither the name of New Artisans LLC nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup util
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file hooks.h
|
||||
* @author John Wiegley
|
||||
*
|
||||
* @ingroup util
|
||||
*/
|
||||
#ifndef _HOOKS_H
|
||||
#define _HOOKS_H
|
||||
|
||||
template <typename T, typename Data>
|
||||
class hooks_t : public boost::noncopyable
|
||||
{
|
||||
public:
|
||||
typedef boost::function<bool (Data&, bool)> function_t;
|
||||
|
||||
protected:
|
||||
std::list<T *> list;
|
||||
|
||||
public:
|
||||
hooks_t() {
|
||||
TRACE_CTOR(hooks_t, "");
|
||||
}
|
||||
~hooks_t() throw() {
|
||||
TRACE_DTOR(hooks_t);
|
||||
}
|
||||
|
||||
void add_hook(T * func, const bool prepend = false) {
|
||||
if (prepend)
|
||||
list.push_front(func);
|
||||
else
|
||||
list.push_back(func);
|
||||
}
|
||||
|
||||
void remove_hook(T * func) {
|
||||
list.remove(func);
|
||||
}
|
||||
|
||||
bool run_hooks(Data& item) {
|
||||
foreach (T * func, list)
|
||||
if (! (*func)(item))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // _HOOKS_H
|
||||
|
|
@ -126,16 +126,23 @@ bool journal_t::add_xact(xact_t * xact)
|
|||
{
|
||||
xact->journal = this;
|
||||
|
||||
if (! xact->finalize() || ! xact_finalize_hooks.run_hooks(*xact)) {
|
||||
if (! xact->finalize()) {
|
||||
xact->journal = NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
extend_xact(xact);
|
||||
xacts.push_back(xact);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void journal_t::extend_xact(xact_base_t * xact)
|
||||
{
|
||||
foreach (auto_xact_t * auto_xact, auto_xacts)
|
||||
auto_xact->extend_xact(*xact);
|
||||
}
|
||||
|
||||
bool journal_t::remove_xact(xact_t * xact)
|
||||
{
|
||||
bool found = false;
|
||||
|
|
|
|||
|
|
@ -43,15 +43,14 @@
|
|||
#define _JOURNAL_H
|
||||
|
||||
#include "utils.h"
|
||||
#include "hooks.h"
|
||||
#include "times.h"
|
||||
|
||||
namespace ledger {
|
||||
|
||||
class commodity_pool_t;
|
||||
class xact_base_t;
|
||||
class xact_t;
|
||||
class auto_xact_t;
|
||||
class xact_finalizer_t;
|
||||
class period_xact_t;
|
||||
class account_t;
|
||||
class scope_t;
|
||||
|
|
@ -114,7 +113,6 @@ public:
|
|||
bool was_loaded;
|
||||
|
||||
shared_ptr<commodity_pool_t> commodity_pool;
|
||||
hooks_t<xact_finalizer_t, xact_t> xact_finalize_hooks;
|
||||
|
||||
journal_t();
|
||||
journal_t(const path& pathname);
|
||||
|
|
@ -138,6 +136,7 @@ public:
|
|||
account_t * find_account_re(const string& regexp);
|
||||
|
||||
bool add_xact(xact_t * xact);
|
||||
void extend_xact(xact_base_t * xact);
|
||||
bool remove_xact(xact_t * xact);
|
||||
|
||||
xacts_list::iterator xacts_begin() {
|
||||
|
|
@ -159,13 +158,6 @@ public:
|
|||
return period_xacts.end();
|
||||
}
|
||||
|
||||
void add_xact_finalizer(xact_finalizer_t * finalizer) {
|
||||
xact_finalize_hooks.add_hook(finalizer);
|
||||
}
|
||||
void remove_xact_finalizer(xact_finalizer_t * finalizer) {
|
||||
xact_finalize_hooks.remove_hook(finalizer);
|
||||
}
|
||||
|
||||
std::size_t read(std::istream& in,
|
||||
const path& pathname,
|
||||
account_t * master = NULL,
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@
|
|||
|
||||
#include "pyinterp.h"
|
||||
#include "pyutils.h"
|
||||
#include "hooks.h"
|
||||
#include "journal.h"
|
||||
#include "xact.h"
|
||||
|
||||
|
|
@ -130,42 +129,6 @@ namespace {
|
|||
return journal.find_account(name, auto_create);
|
||||
}
|
||||
|
||||
struct py_xact_finalizer_t : public xact_finalizer_t {
|
||||
object pyobj;
|
||||
py_xact_finalizer_t() {}
|
||||
py_xact_finalizer_t(object obj) : pyobj(obj) {}
|
||||
py_xact_finalizer_t(const py_xact_finalizer_t& other)
|
||||
: pyobj(other.pyobj) {}
|
||||
virtual bool operator()(xact_t& xact) {
|
||||
return call<bool>(pyobj.ptr(), xact);
|
||||
}
|
||||
};
|
||||
|
||||
std::list<py_xact_finalizer_t> py_finalizers;
|
||||
|
||||
void py_add_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
py_finalizers.push_back(py_xact_finalizer_t(x));
|
||||
journal.add_xact_finalizer(&py_finalizers.back());
|
||||
}
|
||||
|
||||
void py_remove_xact_finalizer(journal_t& journal, object x)
|
||||
{
|
||||
for (std::list<py_xact_finalizer_t>::iterator i = py_finalizers.begin();
|
||||
i != py_finalizers.end();
|
||||
i++)
|
||||
if ((*i).pyobj == x) {
|
||||
journal.remove_xact_finalizer(&(*i));
|
||||
py_finalizers.erase(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void py_run_xact_finalizers(journal_t& journal, xact_t& xact)
|
||||
{
|
||||
journal.xact_finalize_hooks.run_hooks(xact);
|
||||
}
|
||||
|
||||
std::size_t py_read(journal_t& journal, const string& pathname)
|
||||
{
|
||||
return journal.read(pathname);
|
||||
|
|
@ -206,11 +169,6 @@ void export_journal()
|
|||
.add_property("commodity_pool",
|
||||
make_getter(&journal_t::commodity_pool,
|
||||
return_internal_reference<>()))
|
||||
#if 0
|
||||
.add_property("xact_finalize_hooks",
|
||||
make_getter(&journal_t::xact_finalize_hooks),
|
||||
make_setter(&journal_t::xact_finalize_hooks))
|
||||
#endif
|
||||
|
||||
.def("add_account", &journal_t::add_account)
|
||||
.def("remove_account", &journal_t::remove_account)
|
||||
|
|
@ -223,10 +181,6 @@ void export_journal()
|
|||
.def("add_xact", &journal_t::add_xact)
|
||||
.def("remove_xact", &journal_t::remove_xact)
|
||||
|
||||
.def("add_xact_finalizer", py_add_xact_finalizer)
|
||||
.def("remove_xact_finalizer", py_remove_xact_finalizer)
|
||||
.def("run_xact_finalizers", py_run_xact_finalizers)
|
||||
|
||||
.def("__len__", xacts_len)
|
||||
.def("__getitem__", xacts_getitem, return_internal_reference<1>())
|
||||
|
||||
|
|
|
|||
|
|
@ -127,11 +127,6 @@ void export_xact()
|
|||
.def("valid", &xact_t::valid)
|
||||
;
|
||||
|
||||
class_< xact_finalizer_t, boost::noncopyable >
|
||||
("TransactionFinalizer", no_init)
|
||||
.def("__call__", &xact_finalizer_t::operator())
|
||||
;
|
||||
|
||||
class_< auto_xact_t, bases<xact_base_t> > ("AutomatedTransaction")
|
||||
.def(init<predicate_t>())
|
||||
|
||||
|
|
@ -142,16 +137,6 @@ void export_xact()
|
|||
.def("extend_xact", &auto_xact_t::extend_xact)
|
||||
;
|
||||
|
||||
class_< auto_xact_finalizer_t, bases<xact_finalizer_t> >
|
||||
("AutomatedTransactionFinalizer")
|
||||
.add_property("journal",
|
||||
make_getter(&auto_xact_finalizer_t::journal,
|
||||
return_internal_reference<>()),
|
||||
make_setter(&auto_xact_finalizer_t::journal,
|
||||
with_custodian_and_ward<1, 2>()))
|
||||
.def("__call__", &auto_xact_finalizer_t::operator())
|
||||
;
|
||||
|
||||
class_< period_xact_t, bases<xact_base_t> > ("PeriodicTransaction")
|
||||
.def(init<string>())
|
||||
|
||||
|
|
@ -162,16 +147,6 @@ void export_xact()
|
|||
make_getter(&period_xact_t::period_string),
|
||||
make_setter(&period_xact_t::period_string))
|
||||
;
|
||||
|
||||
class_< func_finalizer_t, bases<xact_finalizer_t> >
|
||||
("FunctionalFinalizer", init<func_finalizer_t::func_t>())
|
||||
.add_property("func",
|
||||
make_getter(&func_finalizer_t::func),
|
||||
make_setter(&func_finalizer_t::func))
|
||||
.def("__call__", &func_finalizer_t::operator())
|
||||
;
|
||||
|
||||
scope().attr("extend_xact_base") = &extend_xact_base;
|
||||
}
|
||||
|
||||
} // namespace ledger
|
||||
|
|
|
|||
|
|
@ -79,8 +79,6 @@ namespace {
|
|||
|
||||
optional<date_t::year_type> current_year;
|
||||
|
||||
scoped_ptr<auto_xact_finalizer_t> auto_xact_finalizer;
|
||||
|
||||
instance_t(std::list<account_t *>& _account_stack,
|
||||
std::list<string>& _tag_stack,
|
||||
#if defined(TIMELOG_SUPPORT)
|
||||
|
|
@ -227,9 +225,6 @@ instance_t::~instance_t()
|
|||
TRACE_DTOR(instance_t);
|
||||
|
||||
account_stack.pop_front();
|
||||
|
||||
if (auto_xact_finalizer.get())
|
||||
journal.remove_xact_finalizer(auto_xact_finalizer.get());
|
||||
}
|
||||
|
||||
void instance_t::parse()
|
||||
|
|
@ -546,11 +541,6 @@ void instance_t::automated_xact_directive(char * line)
|
|||
|
||||
try {
|
||||
|
||||
if (! auto_xact_finalizer.get()) {
|
||||
auto_xact_finalizer.reset(new auto_xact_finalizer_t(&journal));
|
||||
journal.add_xact_finalizer(auto_xact_finalizer.get());
|
||||
}
|
||||
|
||||
std::auto_ptr<auto_xact_t> ae
|
||||
(new auto_xact_t(query_t(string(skip_ws(line + 1)),
|
||||
keep_details_t(true, true, true))));
|
||||
|
|
@ -601,8 +591,7 @@ void instance_t::period_xact_directive(char * line)
|
|||
pe->journal = &journal;
|
||||
|
||||
if (pe->finalize()) {
|
||||
extend_xact_base(&journal, *pe.get());
|
||||
|
||||
journal.extend_xact(pe.get());
|
||||
journal.period_xacts.push_back(pe.get());
|
||||
|
||||
pe->pos = position_t();
|
||||
|
|
|
|||
70
src/xact.h
70
src/xact.h
|
|
@ -140,11 +140,6 @@ private:
|
|||
#endif // HAVE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
struct xact_finalizer_t {
|
||||
virtual ~xact_finalizer_t() {}
|
||||
virtual bool operator()(xact_t& xact) = 0;
|
||||
};
|
||||
|
||||
class auto_xact_t : public xact_base_t
|
||||
{
|
||||
public:
|
||||
|
|
@ -183,39 +178,6 @@ private:
|
|||
#endif // HAVE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
struct auto_xact_finalizer_t : public xact_finalizer_t
|
||||
{
|
||||
journal_t * journal;
|
||||
|
||||
auto_xact_finalizer_t() : journal(NULL) {
|
||||
TRACE_CTOR(auto_xact_finalizer_t, "");
|
||||
}
|
||||
auto_xact_finalizer_t(const auto_xact_finalizer_t& other)
|
||||
: xact_finalizer_t(), journal(other.journal) {
|
||||
TRACE_CTOR(auto_xact_finalizer_t, "copy");
|
||||
}
|
||||
auto_xact_finalizer_t(journal_t * _journal) : journal(_journal) {
|
||||
TRACE_CTOR(auto_xact_finalizer_t, "journal_t *");
|
||||
}
|
||||
~auto_xact_finalizer_t() throw() {
|
||||
TRACE_DTOR(auto_xact_finalizer_t);
|
||||
}
|
||||
|
||||
virtual bool operator()(xact_t& xact);
|
||||
|
||||
#if defined(HAVE_BOOST_SERIALIZATION)
|
||||
private:
|
||||
/** Serialization. */
|
||||
|
||||
friend class boost::serialization::access;
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive& ar, const unsigned int /* version */) {
|
||||
ar & journal;
|
||||
}
|
||||
#endif // HAVE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
class period_xact_t : public xact_base_t
|
||||
{
|
||||
public:
|
||||
|
|
@ -253,38 +215,6 @@ private:
|
|||
#endif // HAVE_BOOST_SERIALIZATION
|
||||
};
|
||||
|
||||
class func_finalizer_t : public xact_finalizer_t
|
||||
{
|
||||
func_finalizer_t();
|
||||
|
||||
public:
|
||||
typedef function<bool (xact_t& xact)> func_t;
|
||||
|
||||
func_t func;
|
||||
|
||||
func_finalizer_t(func_t _func) : func(_func) {
|
||||
TRACE_CTOR(func_finalizer_t, "func_t");
|
||||
}
|
||||
func_finalizer_t(const func_finalizer_t& other) :
|
||||
xact_finalizer_t(), func(other.func) {
|
||||
TRACE_CTOR(func_finalizer_t, "copy");
|
||||
}
|
||||
~func_finalizer_t() throw() {
|
||||
TRACE_DTOR(func_finalizer_t);
|
||||
}
|
||||
|
||||
virtual bool operator()(xact_t& xact) {
|
||||
return func(xact);
|
||||
}
|
||||
};
|
||||
|
||||
void extend_xact_base(journal_t * journal, xact_base_t& xact);
|
||||
|
||||
inline bool auto_xact_finalizer_t::operator()(xact_t& xact) {
|
||||
extend_xact_base(journal, xact);
|
||||
return true;
|
||||
}
|
||||
|
||||
typedef std::list<xact_t *> xacts_list;
|
||||
typedef std::list<auto_xact_t *> auto_xacts_list;
|
||||
typedef std::list<period_xact_t *> period_xacts_list;
|
||||
|
|
|
|||
|
|
@ -89,7 +89,6 @@ libledger_report_la_LDFLAGS = -release $(VERSION)
|
|||
pkginclude_HEADERS = \
|
||||
src/utils.h \
|
||||
src/flags.h \
|
||||
src/hooks.h \
|
||||
src/error.h \
|
||||
src/times.h \
|
||||
src/mask.h \
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue