jobserve.ipp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JOBSERVE_IPP
  14. #define JOBSERVE_IPP
  15. #include "jlib.hpp"
  16. class jlib_decl Notification : public INotification
  17. {
  18. public:
  19. Notification(NotifyAction _action, IObservable * _source)
  20. { action = _action; source = _source; }
  21. virtual NotifyAction getAction(void);
  22. virtual IObservable * querySource(void);
  23. private:
  24. NotifyAction action;
  25. IObservable * source;
  26. };
  27. //mixin class
  28. class jlib_decl ManyObservers
  29. {
  30. public:
  31. void addObserver(IObserver &);
  32. void removeObserver(IObserver &);
  33. bool sendBroadcast(NotifyAction, IObservable & self);
  34. bool broadcast(INotification & notify);
  35. protected:
  36. ICopyArray observers;
  37. };
  38. class jlib_decl SingleObserver
  39. {
  40. public:
  41. SingleObserver() { observer = NULL; }
  42. void addObserver(IObserver & _observer) { assertex(!observer); observer = &_observer; }
  43. void removeObserver(IObserver & _observer) { assertex(observer == &_observer); observer = NULL; }
  44. bool sendBroadcast(NotifyAction, IObservable & self);
  45. bool broadcast(INotification & notify);
  46. protected:
  47. IObserver * observer;
  48. };
  49. #define IMPLEMENT_IOBSERVABLE(PARENT, CHILD) \
  50. virtual void beforeDispose() \
  51. { \
  52. CHILD.sendBroadcast(NotifyOnDispose, *this); \
  53. PARENT::beforeDispose(); \
  54. } \
  55. virtual void addObserver(IObserver & _observer) { CHILD.addObserver(_observer); } \
  56. virtual void removeObserver(IObserver & _observer) { CHILD.removeObserver(_observer); }
  57. #endif