|
Boost Users : |
Subject: Re: [Boost-users] Scoped Pairs of functions?
From: Nat Linden (nat_at_[hidden])
Date: 2011-08-18 13:04:16
On Wed, Aug 17, 2011 at 10:49 AM, Brian Allison
<brian.w.allison_at_[hidden]> wrote:
> I'm on a project which uses a C++ compiler, but with a lot of C-isms. One of those is that there are objects with pairs of functions which allocate/deallocate resources. I don't have time to fix all of the design issues with the classes that do this (yet), but within one compilation unit I'd like to remove the chance for leaks.
>
> So - if I wanted to use boost types to wrap around 2 functions f() and g(), where each function
>
> has a void or primitive return type
> takes zero or 1 argument
> where f() is called at some point in a scope, and then g() is called on exiting the scope from which f() is called and only if f() doesn't throw..
> has its return value ignored. [Gahh....]
>
> Is there a boost::bind (or lambda) expression that I can wrap in a type?
>
> How do I generalize to include the possibility of a single argument?
The following works for me with gcc 4.0.1 (yeah, I know, old compiler):
#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
void acquire_fixed()
{
std::cout << "Acquiring fixed resource\n";
}
void release_fixed()
{
std::cout << "Releasing fixed resource\n";
}
void acquire(const std::string& resource)
{
std::cout << "Acquiring " << resource << '\n';
}
void release(const std::string& resource)
{
std::cout << "Releasing " << resource << '\n';
}
class ScopedResource
{
public:
typedef boost::function<void()> ResourceFunc;
ScopedResource(const ResourceFunc& acq, const ResourceFunc& rel):
mRelease(rel)
{
acq();
}
virtual ~ScopedResource()
{
mRelease();
}
private:
ResourceFunc mRelease;
};
int main(int argc, char *argv[])
{
{
ScopedResource fixed(acquire_fixed, release_fixed);
std::cout << "during lifespan of 'fixed'\n";
}
{
ScopedResource something(boost::bind(acquire, "something"),
boost::bind(release, "something"));
std::cout << "during lifespan of 'something'\n";
}
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net