disconnect_and_block.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Various simple examples involving disconnecting and blocking slots.
  2. // Copyright Douglas Gregor 2001-2004.
  3. // Copyright Frank Mori Hess 2009.
  4. //
  5. // Use, modification and
  6. // distribution is subject to the Boost Software License, Version
  7. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // For more information, see http://www.boost.org
  10. #include <iostream>
  11. #include <boost/signals2/signal.hpp>
  12. #include <boost/signals2/shared_connection_block.hpp>
  13. struct HelloWorld
  14. {
  15. void operator()() const
  16. {
  17. std::cout << "Hello, World!" << std::endl;
  18. }
  19. };
  20. void disconnect_example()
  21. {
  22. boost::signals2::signal<void ()> sig;
  23. //[ disconnect_code_snippet
  24. boost::signals2::connection c = sig.connect(HelloWorld());
  25. std::cout << "c is connected\n";
  26. sig(); // Prints "Hello, World!"
  27. c.disconnect(); // Disconnect the HelloWorld object
  28. std::cout << "c is disconnected\n";
  29. sig(); // Does nothing: there are no connected slots
  30. //]
  31. }
  32. void block_example()
  33. {
  34. boost::signals2::signal<void ()> sig;
  35. //[ block_code_snippet
  36. boost::signals2::connection c = sig.connect(HelloWorld());
  37. std::cout << "c is not blocked.\n";
  38. sig(); // Prints "Hello, World!"
  39. {
  40. boost::signals2::shared_connection_block block(c); // block the slot
  41. std::cout << "c is blocked.\n";
  42. sig(); // No output: the slot is blocked
  43. } // shared_connection_block going out of scope unblocks the slot
  44. std::cout << "c is not blocked.\n";
  45. sig(); // Prints "Hello, World!"}
  46. //]
  47. }
  48. struct ShortLived
  49. {
  50. void operator()() const
  51. {
  52. std::cout << "Life is short!" << std::endl;
  53. }
  54. };
  55. void scoped_connection_example()
  56. {
  57. boost::signals2::signal<void ()> sig;
  58. //[ scoped_connection_code_snippet
  59. {
  60. boost::signals2::scoped_connection c(sig.connect(ShortLived()));
  61. sig(); // will call ShortLived function object
  62. } // scoped_connection goes out of scope and disconnects
  63. sig(); // ShortLived function object no longer connected to sig
  64. //]
  65. }
  66. //[ disconnect_by_slot_def_code_snippet
  67. void foo() { std::cout << "foo"; }
  68. void bar() { std::cout << "bar\n"; }
  69. //]
  70. void disconnect_by_slot_example()
  71. {
  72. boost::signals2::signal<void()> sig;
  73. //[ disconnect_by_slot_usage_code_snippet
  74. sig.connect(&foo);
  75. sig.connect(&bar);
  76. sig();
  77. // disconnects foo, but not bar
  78. sig.disconnect(&foo);
  79. sig();
  80. //]
  81. }
  82. int main()
  83. {
  84. std::cout << "Disconnect example:\n";
  85. disconnect_example();
  86. std::cout << "\nBlock example:\n";
  87. block_example();
  88. std::cout << "\nScoped connection example:\n";
  89. scoped_connection_example();
  90. std::cout << "\nDisconnect by slot example:\n";
  91. disconnect_by_slot_example();
  92. return 0;
  93. }