blob: ceeda4de557dcdddeec4419fc92e2b333845cb76 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/mediastreaminterface.h"
17#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:
26 Notifier() {
27 }
28
29 virtual void RegisterObserver(ObserverInterface* observer) {
deadbeef8d60a942017-02-27 14:47:33 -080030 RTC_DCHECK(observer != nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031 observers_.push_back(observer);
32 }
33
34 virtual void UnregisterObserver(ObserverInterface* observer) {
35 for (std::list<ObserverInterface*>::iterator it = observers_.begin();
36 it != observers_.end(); it++) {
37 if (*it == observer) {
38 observers_.erase(it);
39 break;
40 }
41 }
42 }
43
44 void FireOnChanged() {
45 // Copy the list of observers to avoid a crash if the observer object
46 // unregisters as a result of the OnChanged() call. If the same list is used
47 // UnregisterObserver will affect the list make the iterator invalid.
48 std::list<ObserverInterface*> observers = observers_;
49 for (std::list<ObserverInterface*>::iterator it = observers.begin();
50 it != observers.end(); ++it) {
51 (*it)->OnChanged();
52 }
53 }
54
55 protected:
56 std::list<ObserverInterface*> observers_;
57};
58
59} // namespace webrtc
60
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020061#endif // API_NOTIFIER_H_