简体   繁体   中英

How do you disconnect from a boost signal2 using a functor to a method of a class?

I am not getting the behavior I am expecting when disconnecting a slot (that is a class method) from from a boost::signals2. My terminology is probably off, so I will provide a minimal working example (MWE) below demonstrating what I see and what I expect. The short version is that I am disconnecting from the signal, but it is staying there. Everything works great if I do this with a stand-alone function, it is when I use a class method that I run into this behavior.

Any help would be greatly appreciated!

>> tree

.
├── main.cpp
└── SConstruct

0 directories, 2 files

>> cat SConstruct

Program('main.cpp')

>> cat main.cpp

#include <boost/signals2.hpp>
#include <iostream>
struct foo {
    void bar(int n) {
        std::cout << "Called foo::bar with " << n << std::endl;
    }
};
typedef boost::function<void(int)> Signal_f;
int main() {

    foo f;
    boost::signals2::signal< void(int) > my_signal;
    Signal_f functor = boost::bind(&foo::bar, f, _1);
    std::cout << "Created signal, and it has "
              << my_signal.num_slots() << " subscribers." << std::endl;
    my_signal.connect(functor);
    std::cout << "Subscribed to signal, and it has "
              << my_signal.num_slots() << " subsciber." << std::endl;
    my_signal(1);
    my_signal.disconnect(&functor);
    std::cout << "Un-Subscribed to signal, but it still has "
              << my_signal.num_slots()
              << " subsciber, and it should not have any now." << std::endl;
    my_signal(2);
    return 0;
}

>> scons

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main.o -c main.cpp
g++ -o main main.o
scons: done building targets.

>> ./main

Created signal, and it has 0 subscribers.
Subscribed to signal, and it has 1 subsciber.
Called foo::bar with 1
Un-Subscribed to signal, but it still has 1 subsciber, and it should not have any now.
Called foo::bar with 2

re-implementing using scoped_connection :

#include <boost/signals2.hpp>
#include <iostream>
struct foo {
    void bar(int n) {
        std::cout << "Called foo::bar with " << n << std::endl;
    }
};



typedef boost::function<void(int)> Signal_f;
int main() {

    using boost::signals2::scoped_connection;

    foo f;
    boost::signals2::signal< void(int) > my_signal;
    Signal_f functor = boost::bind(&foo::bar, f, _1);
    std::cout << "Created signal, and it has "
              << my_signal.num_slots() << " subscribers." << std::endl;

    // the scoped_connection object is RAII
    auto con = scoped_connection(my_signal.connect(functor));

    std::cout << "Subscribed to signal, and it has "
              << my_signal.num_slots() << " subsciber." << std::endl;
    my_signal(1);

    // disconnect the connection object, not the signal
    con.disconnect();
    std::cout << "Un-Subscribed to signal, and it now has "
              << my_signal.num_slots()
              << " subscibers." << std::endl;
    my_signal(2);
    return 0;
}

expected output:

Created signal, and it has 0 subscribers.
Subscribed to signal, and it has 1 subsciber.
Called foo::bar with 1
Un-Subscribed to signal, and it still has 0 subscibers.

我确实使用connect方法返回的boost :: signals2 :: connection对象与boost :: signal2 :: signal断开连接。

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