vendredi 4 décembre 2015

How to pass a non-static member function as an observer

I am using a signaling system introduced here:

http://ift.tt/1PDLBFC

It can be used like this:

Simple::Signal<char (float, int, std::string)> sig1;
sig1 += float_callback;
Foo foo;
sig1 += Simple::slot (&foo, &Foo::foo_bool);
sig1.emit (.5, 1, "12");

Simple::slot is:

template<class Class, class R, class... Args> std::function<R (Args...)>
slot (Class *object, R (Class::*method) (Args...))
{
    return [object, method] (Args... args) { return (object ->* method) (args...); };
}

That all works OK.

I am using this signaling system in a 'property' subclass to create an 'observable property'

template<typename C, typename T>
class ObservableProperty : public Property<T>
{
private:
    C* _classptr;
    Simple::Signal<void (const void* p, T v)> valueChanged;
public:
    using Property<T>::_value;
    ObservableProperty(C* classptr, T& value) : Property<T>(value), _classptr(classptr)
    {
    }
    ObservableProperty<C, T>& operator = (const T& val)
    {
        if (_value != val)
        {
            _value = val;
            valueChanged.emit(_classptr, _value);
        }
        return *this;
    };
    size_t observe_slot(param1???, param2???)
    {
        return valueChanged += Simple::slot (param1, param2);
    }
    size_t observe(void (*callback)(const void* p, T v))
    {
        return valueChanged += callback;
    }
};

I can call observe() on a property passing in a static funtion. That works OK.

But I am having difficulty working our what the parameters and arguments should be for observe_slot() so that I can pass in a non-static member function of an arbitrary class something like this:

class MyClass {
public:
ObservableProperty<MyClass, bool> m_Property;
}

void MyOtherClass::propertyChanged(const void* p, bool value)
{
}

void MyOtherClass::setupObserver(MyClass* p)
{
      p->m_Property.observer_slot(this, &MyOtherClass::propertyChanged)
}

So the questions are:

How should the parameters be declared for observer_slot() and what arguments should I use when I call observer_slot()?

Aucun commentaire:

Enregistrer un commentaire