blob: c03b1049ebf36d3f1dc0b96dc5f3be1ee218d5f2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_NOTIFIER_H_
12#define API_NOTIFIER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <list>
15
Steve Anton10542f22019-01-11 09:11:00 -080016#include "api/media_stream_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19namespace webrtc {
20
deadbeefb10f32f2017-02-08 01:38:21 -080021// Implements a template version of a notifier.
22// TODO(deadbeef): This is an implementation detail; move out of api/.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023template <class T>
24class Notifier : public T {
25 public:
Yves Gerey665174f2018-06-19 15:03:05 +020026 Notifier() {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28 virtual void RegisterObserver(ObserverInterface* observer) {
deadbeef8d60a942017-02-27 14:47:33 -080029 RTC_DCHECK(observer != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030 observers_.push_back(observer);
31 }
32
33 virtual void UnregisterObserver(ObserverInterface* observer) {
34 for (std::list<ObserverInterface*>::iterator it = observers_.begin();
35 it != observers_.end(); it++) {
36 if (*it == observer) {
37 observers_.erase(it);
38 break;
39 }
40 }
41 }
42
43 void FireOnChanged() {
44 // Copy the list of observers to avoid a crash if the observer object
45 // unregisters as a result of the OnChanged() call. If the same list is used
46 // UnregisterObserver will affect the list make the iterator invalid.
47 std::list<ObserverInterface*> observers = observers_;
48 for (std::list<ObserverInterface*>::iterator it = observers.begin();
49 it != observers.end(); ++it) {
50 (*it)->OnChanged();
51 }
52 }
53
54 protected:
55 std::list<ObserverInterface*> observers_;
56};
57
58} // namespace webrtc
59
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020060#endif // API_NOTIFIER_H_