blob: 8be9ffe9dc0166c93e0707fc04a5ad3b4534934d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2011 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellanderb24317b2016-02-10 15:54:434 * 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:369 */
10
Henrik Kjellander15583c12016-02-10 09:53:1211#ifndef WEBRTC_API_NOTIFIER_H_
12#define WEBRTC_API_NOTIFIER_H_
henrike@webrtc.org28e20752013-07-10 00:45:3613
14#include <list>
15
Henrik Kjellander15583c12016-02-10 09:53:1216#include "webrtc/api/mediastreaminterface.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:0817#include "webrtc/base/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:3618
19namespace webrtc {
20
21// Implement a template version of a notifier.
22template <class T>
23class Notifier : public T {
24 public:
25 Notifier() {
26 }
27
28 virtual void RegisterObserver(ObserverInterface* observer) {
29 ASSERT(observer != NULL);
30 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
Henrik Kjellander15583c12016-02-10 09:53:1260#endif // WEBRTC_API_NOTIFIER_H_