Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::signals2 slot as a non-static function member?

I have been playing around with boost::signals2 for learning purposes lately, and I was wondering whether I could connect signals to a non-static slot located within a class (like I could in Qt). Consider the following:

class Worker {
    typedef boost::signals2::signal<void (const std::string &)> SendMessage;
public:
    typedef SendMessage::slot_type SendMessageSlotType;
    boost::signals2::connection connect(const SendMessageSlotType &slot) {
        return send_message.connect(slot);
    }
private:
    SendMessage send_message;
};

class Controller {
public:
    Controller() {
        worker.connect(&Controller::print);
    }
private:
    static void print(const std::string &message) {
        std::cout << message << std::endl;
    }

    Worker worker;
};

Now I would want to make Controller::print a non-static member. With boost::thread for example, this can be achieved using boost::bind; is there any way to do this with boost::signals2?

like image 291
nijansen Avatar asked Apr 03 '12 22:04

nijansen


1 Answers

Just:

class Controller {
public:
    Controller() {
        worker.connect(boost::bind(&Controller::print, this, _1));
    }
private:
    void print(const std::string &message) {
        std::cout << message << std::endl;
    }

    Worker worker;
};
like image 170
Guy Sirton Avatar answered Nov 06 '22 09:11

Guy Sirton