代码之家  ›  专栏  ›  技术社区  ›  Kenneth E. Bellock

如何使用类方法的函子断开boost signal2的连接?

  •  2
  • Kenneth E. Bellock  · 技术社区  · 7 年前

    任何帮助都将不胜感激!

    >> 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
    
    2 回复  |  直到 7 年前
        1
  •  3
  •   sehe    7 年前

    使用重新实现 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;
    }
    

    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.
    
        2
  •  1
  •   Dobby    7 年前

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