简体   繁体   中英

Passing both member functions and lambdas to std::function

I'm trying to create control/window events similar to how they are done in C#'s Windows Forms/WPF. Each event class contains an argument type and passes that as well as a pointer to the sending object. This is all fine and dandy when using a lambda function, but when I try to add a member function to the event I get a compile error of C2276 '&': illegal operation on bound member function expression and C2660 'Event<MouseButtonEventArgs>::add': function does not take 1 arguments .

How would I create an event class that can take lambda functions and member functions from any class (whether or not that class is the "sender")?

Event.h

template<class Args> struct EventListener {

    /* The function type to be called by the event. */
    typedef function<void(ControlBaseSPtr, Args&)> ArgsFunction;
    string name;
    ArgsFunction function;
};

template<class Args> class Event {

    /* The function type to be called by the event. */
    typedef function<void(ControlBaseSPtr, Args&)> ArgsFunction;
    deque<EventListener<Args>> listeners;

    Event(EventDirections direction) {
        this->direction = direction;
    }

    void add(const string& name, ArgsFunction function) {
        EventListener<Args> listener = EventListener<Args>(name, function);
        for (auto it = listeners.begin(); it != listeners.end(); it++) {
            if (it->name == name) {
                throw exception("Event::operator+= listener already exists in the event");
            }
        }
        listeners.push_back(listener);
    }

    //...
};

Control.h

class ControlBase {
    Event<MouseEventArgs> _eventMouseMovedGlobal;
    Event<MouseButtonEventArgs> _eventMouseButtonGlobal;

    ControlBase();

    void onMouseMovedGlobal(ControlBaseSPtr sender, MouseEventArgs e);
};

Control.cpp

ControlBase::ControlBase() {
    // Works
    _eventMouseButtonGlobal.add("some name", [](ControlSPtr control, MouseButtonEventArgs e){
        //...
    };

    // Doesn't work
    _eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this, placeholders::_2))
    // Doesn't work
    _eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this))
}

void ControlBase::onMouseWheelGlobal(ControlBaseSPtr sender, MouseWheelEventArgs e) {
    //...
}

Well, since onMouseMovedGlobal has 2 input arguments, you need placeholders or values for both of them when creating a binder. So, instead of

_eventMouseMovedGlobal.add("some name", bind(&onMouseMovedGlobal, this, placeholders::_2))

you need

_eventMouseMovedGlobal.add("some name", bind(&ControlBase::onMouseMovedGlobal, this, placeholders::_1, placeholders::_2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM