jobserve.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "jobserve.hpp"
  14. #include "jlib.hpp"
  15. #include "jobserve.ipp"
  16. //-----------------------------------------------------------------------
  17. //-- Classes for helping with notification
  18. //-----------------------------------------------------------------------
  19. NotifyAction Notification::getAction(void)
  20. {
  21. return action;
  22. }
  23. IObservable * Notification::querySource(void)
  24. {
  25. return source;
  26. }
  27. //-----------------------------------------------------------------------
  28. bool IObserver::onNotify(INotification &)
  29. {
  30. return true;
  31. }
  32. //-----------------------------------------------------------------------
  33. void IObservable::addObserver(IObserver & observer)
  34. {
  35. assertex(!"cannot add observer");
  36. }
  37. void IObservable::removeObserver(IObserver & observer)
  38. {
  39. assertex(!"cannot remove observer");
  40. }
  41. //-----------------------------------------------------------------------
  42. void ManyObservers::addObserver(IObserver & observer)
  43. {
  44. observers.append(observer);
  45. }
  46. void ManyObservers::removeObserver(IObserver & observer)
  47. {
  48. observers.zap(observer);
  49. }
  50. bool ManyObservers::sendBroadcast(NotifyAction action, IObservable & self)
  51. {
  52. Notification notify(action, &self);
  53. return broadcast(notify);
  54. }
  55. bool ManyObservers::broadcast(INotification & notify)
  56. {
  57. ForEachItemInRev(idx,observers)
  58. {
  59. if (!((IObserver &)observers.item(idx)).onNotify(notify))
  60. return false;
  61. }
  62. return true;
  63. }
  64. //-----------------------------------------------------------------------
  65. bool SingleObserver::sendBroadcast(NotifyAction action, IObservable & self)
  66. {
  67. Notification notify(action, &self);
  68. return broadcast(notify);
  69. }
  70. bool SingleObserver::broadcast(INotification & notify)
  71. {
  72. if (observer)
  73. return observer->onNotify(notify);
  74. return true;
  75. }