summaryrefslogtreecommitdiff
path: root/chromium/base/scoped_observation.h
blob: 5a42193ae7471ea8de8e56f865dd19db5ccc1d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_SCOPED_OBSERVATION_H_
#define BASE_SCOPED_OBSERVATION_H_

#include <stddef.h>

#include "base/check_op.h"

namespace base {

// ScopedObservation is used to keep track of singular observation, e.g.
// where an observer observes a single source only.
//
// Use base::ScopedMultiSourceObservation for objects that observe multiple
// sources. This class and base::ScopedMultiSourceObservation replace
// ScopedObserver.
//
// When ScopedObservation is destroyed, it removes the registered observation,
// if any. Basic example (as a member variable):
//
//   class MyFooObserver : public FooObserver {
//     ...
//    private:
//     ScopedObservation<Foo, FooObserver> foo_observation_{this};
//   };
//
//   MyFooObserver::MyFooObserver(Foo* foo) {
//     foo_observation_.Observe(foo);
//   }
//
// For cases with methods not named AddObserver/RemoveObserver:
//
//   class MyFooStateObserver : public FooStateObserver {
//     ...
//    private:
//     ScopedObservation<Foo,
//                       FooStateObserver,
//                       &Foo::AddStateObserver,
//                       &Foo::RemoveStateObserver>
//       foo_observation_{this};
//   };
template <class Source,
          class Observer,
          void (Source::*AddObsFn)(Observer*) = &Source::AddObserver,
          void (Source::*RemoveObsFn)(Observer*) = &Source::RemoveObserver>
class ScopedObservation {
 public:
  explicit ScopedObservation(Observer* observer) : observer_(observer) {}
  ScopedObservation(const ScopedObservation&) = delete;
  ScopedObservation& operator=(const ScopedObservation&) = delete;
  ~ScopedObservation() {
    if (IsObserving())
      RemoveObservation();
  }

  // Adds the object passed to the constructor as an observer on |source|.
  // IsObserving() must be false.
  void Observe(Source* source) {
    DCHECK_EQ(source_, nullptr);
    source_ = source;
    (source_->*AddObsFn)(observer_);
  }

  // Remove the object passed to the constructor as an observer from |source|.
  void RemoveObservation() {
    DCHECK_NE(source_, nullptr);
    (source_->*RemoveObsFn)(observer_);
    source_ = nullptr;
  }

  // Returns true if any source is being observed.
  bool IsObserving() const { return source_ != nullptr; }

  // Returns true if |source| is being observed.
  bool IsObservingSource(Source* source) const { return source_ == source; }

 private:
  Observer* const observer_;

  // The observed source, if any.
  Source* source_ = nullptr;
};

}  // namespace base

#endif  // BASE_SCOPED_OBSERVATION_H_