Boost logo

Boost Users :

Subject: Re: [Boost-users] [bind/function] callback class with many function arguments?
From: Nat Goodspeed (nat_at_[hidden])
Date: 2009-02-15 17:54:29


jeti wrote:

> i try to create a class which helps me manage my
> resources usage
> it only gets function arguments and a boost::function on construction
> and this function is called with the given arguments during destruction
>
> the constructor currently looks like this
> template<typename T,typename R>
> Callback(boost::function<T (R)>,R arg1);
>
> so this would be great
> template<typename T,typename R,...>
> Callback(boost::function<T (R,...)>,R arg1,....);
>
> which i dont know how to achive or if this can easily be done using
> current c++ standard
> any alternative/creative suggestions on this would be great

? To me it looks as if you're home free. The following builds and runs
and produces the result I expect:

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>

// doesn't need to be a template
class Callback
{
     typedef boost::function<void()> function_type;

public:
     Callback(const function_type& func):
         mFunc(func)
     {}
     ~Callback()
     {
         mFunc();
     }

private:
     function_type mFunc;
};

// example cleanup function requiring args
void cleanup(const std::string& message)
{
     std::cout << message << std::endl;
}

int main(int argc, char* argv[])
{
     Callback myCallback(boost::bind(cleanup,
                                     "Going out of scope now!"));
     return 0;
}

If I've misunderstood some part of your problem, please forgive me.


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