fork download
  1. #include <ostd/io.hh>
  2. #include <ostd/string.hh>
  3. #include <ostd/event.hh>
  4.  
  5. using namespace ostd;
  6.  
  7. struct SignalTest {
  8. /* const on the class means that a const reference to the event
  9.   * can actually emit (in that case, the reference passed to each
  10.   * callback will always be const to make sure nothing changes
  11.   */
  12. Signal<const SignalTest, int, const char *> on_simple = this;
  13. Signal< SignalTest, float > on_param = this;
  14.  
  15. SignalTest(): p_param(3.14f) {
  16. /* we can connect methods */
  17. on_simple.connect(&SignalTest::simple_method);
  18. writeln("constructed signal test");
  19. }
  20.  
  21. float get_param() const { return p_param; }
  22. void set_param(float nv) {
  23. float oldval = p_param;
  24. p_param = nv;
  25. /* types passed to emit must match the types of the event */
  26. on_param.emit(oldval);
  27. }
  28.  
  29. void foo() const {
  30. /* because on_simple is const, we can emit form const method */
  31. on_simple.emit(150, "hello world");
  32. }
  33.  
  34. void simple_method(int v, const char *str) const {
  35. writefln("simple method handler: %d, %s", v, str);
  36. }
  37.  
  38. private:
  39. float p_param;
  40. };
  41.  
  42. int main() {
  43. writeln("=== program start ===");
  44. SignalTest st;
  45.  
  46. int test = 42;
  47.  
  48. /* we can connect lambdas, including closures
  49.   * this callback can access "test" easily and it will still work
  50.   */
  51. auto idx = st.on_simple.connect([&](const SignalTest &, int v,
  52. const char *str) {
  53. writefln("and lambda test: %d, %s (%d)", v, str, test);
  54. });
  55.  
  56. writeln("--- test emit ---");
  57. st.foo();
  58.  
  59. /* we can disconnect callbacks too */
  60. st.on_simple.disconnect(idx);
  61.  
  62. /* this should not print from the lambda above */
  63. writeln("--- test emit after disconnect ---");
  64. st.foo();
  65.  
  66. writeln("--- set value ---");
  67. st.set_param(6.28f);
  68.  
  69. /* the reference to SignalTest here is mutable */
  70. st.on_param.connect([](SignalTest &self, float oldval) {
  71. writeln("value changed...");
  72. writefln(" old value: %f, new value: %f", oldval,
  73. self.get_param());
  74.  
  75. /* when we have a mutable reference we can change the original
  76.   * object, for example re-emit its own signal once again
  77.   */
  78. if (self.get_param() > 140.0f) return;
  79. self.set_param(self.get_param() + 1.0f);
  80. });
  81.  
  82. /* trigger the callback above */
  83. writeln("--- test set value ---");
  84. st.set_param(134.28f);
  85.  
  86. writeln("=== program end ===");
  87. return 0;
  88. }
  89.  
  90. /*
  91. === program start ===
  92. constructed signal test
  93. --- test emit ---
  94. simple method handler: 150, hello world
  95. and lambda test: 150, hello world (42)
  96. --- test emit after disconnect ---
  97. simple method handler: 150, hello world
  98. --- set value ---
  99. --- test set value ---
  100. value changed...
  101.   old value: 6.280000, new value: 134.279999
  102. value changed...
  103.   old value: 134.279999, new value: 135.279999
  104. value changed...
  105.   old value: 135.279999, new value: 136.279999
  106. value changed...
  107.   old value: 136.279999, new value: 137.279999
  108. value changed...
  109.   old value: 137.279999, new value: 138.279999
  110. value changed...
  111.   old value: 138.279999, new value: 139.279999
  112. value changed...
  113.   old value: 139.279999, new value: 140.279999
  114. === program end ===
  115. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:22: fatal error: ostd/io.hh: No such file or directory
compilation terminated.
stdout
Standard output is empty