Re: sigc::mem_fun and Glib::RefPtr



Am Thu, 04 Oct 2012 18:31:17 +0200
schrieb Horváth Imre <blemidon gmail com>:

> Hi!
> 
> IS there a solution to use Glib::RefPtr with sigc::mem_fun?
> Example:
> 
> Glib::RefPtr<MyClass> c;
> ...
> button.signal_clicked().connect(sigc::mem_fun(c,
> &MyClass::on_button));

yes, there is, by using sigc lambdas. a little complicated but it works.

short example program (replace std::shared_ptr by Glib::RefPtr):

<code>
#include <memory>
#include <sigc++/sigc++.h>


namespace sigc
{

// the partially specialized struct dereference_trait is essential,
// otherwise sigc can't deduce the return type from expression *sigc::_1

template <class T>
struct dereference_trait<std::shared_ptr<T> >
  { typedef T type; };

template <class T>
struct dereference_trait<std::shared_ptr<T>& >
  { typedef T type; };

}


struct MyClass
{
    void on_button()
    {
    }
};


int main()
{
    std::shared_ptr<MyClass> c(new MyClass);

    sigc::slot<void> f =
      sigc::bind(sigc::group(sigc::mem_fun(&MyClass::on_button),
                             *sigc::_1),
                 c);
    f();


    return 0;
}
</code>

if you have variadic templates available you could pack the
sigc::bind(sigc::group()) part into a factory function, in order to
make the code more readable.



greetings,
klaus


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]