notifier.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef API_NOTIFIER_H_
  11. #define API_NOTIFIER_H_
  12. #include <list>
  13. #include "api/media_stream_interface.h"
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. // Implements a template version of a notifier.
  17. // TODO(deadbeef): This is an implementation detail; move out of api/.
  18. template <class T>
  19. class Notifier : public T {
  20. public:
  21. Notifier() {}
  22. virtual void RegisterObserver(ObserverInterface* observer) {
  23. RTC_DCHECK(observer != nullptr);
  24. observers_.push_back(observer);
  25. }
  26. virtual void UnregisterObserver(ObserverInterface* observer) {
  27. for (std::list<ObserverInterface*>::iterator it = observers_.begin();
  28. it != observers_.end(); it++) {
  29. if (*it == observer) {
  30. observers_.erase(it);
  31. break;
  32. }
  33. }
  34. }
  35. void FireOnChanged() {
  36. // Copy the list of observers to avoid a crash if the observer object
  37. // unregisters as a result of the OnChanged() call. If the same list is used
  38. // UnregisterObserver will affect the list make the iterator invalid.
  39. std::list<ObserverInterface*> observers = observers_;
  40. for (std::list<ObserverInterface*>::iterator it = observers.begin();
  41. it != observers.end(); ++it) {
  42. (*it)->OnChanged();
  43. }
  44. }
  45. protected:
  46. std::list<ObserverInterface*> observers_;
  47. };
  48. } // namespace webrtc
  49. #endif // API_NOTIFIER_H_