fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. class Notify
  7. {
  8. using HandlerType = std::function<void ( Notify& )>;
  9.  
  10. unsigned int Value_;
  11. HandlerType Handler_;
  12.  
  13. public:
  14. Notify() : Value_( 0 )
  15. {
  16. }
  17.  
  18. unsigned int value() const
  19. {
  20. return Value_;
  21. }
  22.  
  23. void set_value( unsigned int Val )
  24. {
  25. if( Val != value() )
  26. {
  27. Value_ = Val;
  28. if( Handler_ )
  29. {
  30. Handler_( *this );
  31. }
  32. }
  33. }
  34.  
  35. void set_handler( HandlerType Handler )
  36. {
  37. Handler_ = Handler;
  38. }
  39. };
  40.  
  41. Notify n;
  42.  
  43. class Sink
  44. {
  45. public:
  46. Sink()
  47. {
  48. n.set_handler( std::bind( &Sink::handler, this, std::placeholders::_1 ) );
  49. }
  50.  
  51. void handler( Notify& obj )
  52. {
  53. std::cout << "Handler called" << endl;
  54. }
  55. };
  56.  
  57. int main()
  58. {
  59. Sink s;
  60. n.set_value( 123 );
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Handler called