extended_slot.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Example program for connecting an extended slot,
  2. // using a signal's connect_extended and extended_slot_type.
  3. //
  4. // Copyright Frank Mori Hess 2009.
  5. //
  6. // Use, modification and
  7. // distribution is subject to the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // For more information, see http://www.boost.org
  11. #include <boost/signals2/signal.hpp>
  12. #include <iostream>
  13. #include <string>
  14. namespace bs2 = boost::signals2;
  15. void single_shot_slot(const bs2::connection &conn, const std::string &message)
  16. {
  17. conn.disconnect();
  18. std::cout << message;
  19. }
  20. int main()
  21. {
  22. typedef bs2::signal<void (void)> sig_type;
  23. sig_type sig;
  24. {
  25. sig_type::extended_slot_type hello(&single_shot_slot, _1, "Hello");
  26. sig.connect_extended(hello);
  27. }
  28. sig(); // prints "Hello"
  29. {
  30. sig_type::extended_slot_type world(&single_shot_slot, _1, ", World!\n");
  31. sig.connect_extended(world);
  32. }
  33. sig(); // only prints ", World!\n" since hello slot has disconnected itself
  34. sig(); // prints nothing, world slot has disconnected itself
  35. return 0;
  36. }