Boost logo

Boost :

From: Christian Holmquist (c.holmquist_at_[hidden])
Date: 2007-08-20 12:05:37


Hello,

>
> I haven't often found myself in need of something like
> ScopeGuard/ScopeExit, but I am convinced that some people find it
> indispensible. I encourage those people to give the macros a chance,
> because what Alexander says about writing lambda expressions
> definitely rings true.
>

I really like the way of writing the guard body using the scheme of
ScopeExit, it seems to make it much easier to maintain compared to writing
horrible lambda expressions or specialized function objects.

What I'm missing is a way of nesting guards, making it possible to build
complex transactions out of many small ones. The examples have very simple
transactions that fit easily into a couple of lines, but this is not the
case with what I am used to work with.

Let's take the example from the docs:

void World::addPerson(Person const& person) {
    bool commit = false;

    m_persons.push_back(person);
    BOOST_SCOPE_EXIT( (commit)(m_persons) )
    {
        if(!commit)
            m_persons.pop_back();
    } BOOST_SCOPE_EXIT_END

    // ... other operations

    commit = true;
}

Let's say I have two Worlds, and would like to add one person into each of
these.I would prefer to write that like:

boost::scope_guard guard;
World world1, world2;

world1.addPerson(guard, Person(...));
world2.addPersion(guard, Person(...));

guard.commit();

With the current proposal I don't see how I this could be done, since there
is no visible notion of a context keeping the guards.

For my own work i've written a simple scope_guard that holds a
std::vector<boost::function>, where I add all lambda expressions to be
invoked unless I call scope_guard::commit(). This works but the lambda mess
remains.

Would it be possible to maybe add such functionality on-top of the ScopeExit
as it looks today?

Best regards,

Christian


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk