// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef BASE_OBSERVER_LIST_H_ #define BASE_OBSERVER_LIST_H_ #include <stddef.h> #include <algorithm> #include <iterator> #include <limits> #include <ostream> #include <string> #include <utility> #include <vector> #include "base/check.h" #include "base/check_op.h" #include "base/dcheck_is_on.h" #include "base/debug/dump_without_crashing.h" #include "base/notreached.h" #include "base/observer_list_internal.h" #include "base/ranges/algorithm.h" #include "base/sequence_checker.h" #include "build/build_config.h" /////////////////////////////////////////////////////////////////////////////// // // OVERVIEW: // // A list of observers. Unlike a standard vector or list, this container can // be modified during iteration without invalidating the iterator. So, it // safely handles the case of an observer removing itself or other observers // from the list while observers are being notified. // // // WARNING: // // ObserverList is not thread-compatible. Iterating on the same ObserverList // simultaneously in different threads is not safe, even when the ObserverList // itself is not modified. // // For a thread-safe observer list, see ObserverListThreadSafe. // // // TYPICAL USAGE: // // class MyWidget { // public: // ... // // class Observer : public base::CheckedObserver { // public: // virtual void OnFoo(MyWidget* w) = 0; // virtual void OnBar(MyWidget* w, int x, int y) = 0; // }; // // void AddObserver(Observer* obs) { // observers_.AddObserver(obs); // } // // void RemoveObserver(Observer* obs) { // observers_.RemoveObserver(obs); // } // // void NotifyFoo() { // for (Observer& obs : observers_) // obs.OnFoo(this); // } // // void NotifyBar(int x, int y) { // for (Observer& obs : observers_) // obs.OnBar(this, x, y); // } // // private: // base::ObserverList<Observer> observers_; // }; // // /////////////////////////////////////////////////////////////////////////////// namespace base { // Enumeration of which observers are notified by ObserverList. enum class ObserverListPolicy { … }; // When check_empty is true, assert that the list is empty on destruction. // When allow_reentrancy is false, iterating throught the list while already in // the iteration loop will result in DCHECK failure. // TODO(oshima): Change the default to non reentrant. https://crbug.com/812109 template <class ObserverType, bool check_empty = false, bool allow_reentrancy = true, class ObserverStorageType = internal::CheckedObserverAdapter> class ObserverList { … }; ReentrantObserverList; } // namespace base #endif // BASE_OBSERVER_LIST_H_