Advertisement
Guest User

On value changed event.

a guest
Mar 2nd, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <functional>
  2.  
  3. class TempWrapper
  4. {
  5. public:
  6.     TempWrapper() { /*...*/ }
  7.     TempWrapper(int val) : Value(val) { /*...*/ }
  8.  
  9.     ~TempWrapper() { /*...*/ }
  10.  
  11.     TempWrapper & operator = (int val)
  12.     {
  13.         Value = val;
  14.         if (Changed) Changed(Value);
  15.         return *this;
  16.     }
  17.  
  18.     typedef std::function<void(int)> EvDef;
  19.     EvDef Changed;
  20.  
  21.     int Value;
  22. };
  23.  
  24. class UsingWrapper
  25. {
  26. public:
  27.  
  28.     TempWrapper MyValue;
  29.  
  30.     UsingWrapper() { /*...*/ }
  31.     UsingWrapper(int val) : MyValue(val) { /*...*/ }
  32.  
  33.     ~UsingWrapper() { /*...*/ }
  34. };
  35.  
  36. void onChanged(int value)
  37. {
  38.     printf("value changed %d\n", value);
  39. }
  40.  
  41. int main()
  42. {
  43.     UsingWrapper some_value(32);
  44.  
  45.     some_value.MyValue.Changed = TempWrapper::EvDef(onChanged);
  46.  
  47.     some_value.MyValue = 77; // With event
  48.     some_value.MyValue.Value = 27; // Without event
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement