sewenew / redis-plus-plus

Redis client written in C++
Apache License 2.0
1.64k stars 351 forks source link

[FEATURE] Subscriber on_message() callback interface with optional void* param for user context/data #425

Closed sknagappan closed 1 year ago

sknagappan commented 1 year ago

Currenlty Subscriber::on_message() has callback interface "void (std::string channel, std::string msg)" it will be useful if it has an optional void param to hold some user context/data like "void (std::string channel, std::string msg, void user_data)"

sewenew commented 1 year ago

Since you can use lambda or function object (class which overloads call operator) as callback, there's no need to have an extra parameter for the callback. Because you can set user context/data in lambda or function object.

UserData data;

sub.on_message([data](std::string channel, std::string msg) {
            // Process message with user data: data
        });

or

class Callback {
public:
    Callback(UserData data) : _data(data) {}

    void operator(std::string channel, std::string msg) {
         // process message with user data: _data
    }
private:
    UserData _data;
};

sub.on_message(Callback(user_data));

Regards