henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_NOTIFIER_H_ |
| 12 | #define API_NOTIFIER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <list> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "api/mediastreaminterface.h" |
| 17 | #include "rtc_base/checks.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
deadbeef | b10f32f | 2017-02-08 01:38:21 -0800 | [diff] [blame] | 21 | // Implements a template version of a notifier. |
| 22 | // TODO(deadbeef): This is an implementation detail; move out of api/. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | template <class T> |
| 24 | class Notifier : public T { |
| 25 | public: |
| 26 | Notifier() { |
| 27 | } |
| 28 | |
| 29 | virtual void RegisterObserver(ObserverInterface* observer) { |
deadbeef | 8d60a94 | 2017-02-27 14:47:33 -0800 | [diff] [blame] | 30 | RTC_DCHECK(observer != nullptr); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 61 | #endif // API_NOTIFIER_H_ |