proxygen
Observer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <folly/ThreadLocal.h>
21 
22 namespace folly {
23 namespace observer {
24 
76 template <typename T>
77 class Observer;
78 
79 template <typename T>
80 class Snapshot {
81  public:
82  const T& operator*() const {
83  return *get();
84  }
85 
86  const T* operator->() const {
87  return get();
88  }
89 
90  const T* get() const {
91  return data_.get();
92  }
93 
94  std::shared_ptr<const T> getShared() const {
95  return data_;
96  }
97 
101  size_t getVersion() const {
102  return version_;
103  }
104 
105  private:
106  friend class Observer<T>;
107 
109  const observer_detail::Core& core,
110  std::shared_ptr<const T> data,
111  size_t version)
112  : data_(std::move(data)), version_(version), core_(&core) {
113  DCHECK(data_);
114  }
115 
116  std::shared_ptr<const T> data_;
117  size_t version_;
119 };
120 
122  public:
123  CallbackHandle();
124  template <typename T>
126  Observer<T> observer,
127  folly::Function<void(Snapshot<T>)> callback);
128  CallbackHandle(const CallbackHandle&) = delete;
129  CallbackHandle(CallbackHandle&&) = default;
130  CallbackHandle& operator=(const CallbackHandle&) = delete;
131  CallbackHandle& operator=(CallbackHandle&&) = default;
132  ~CallbackHandle();
133 
134  // If callback is currently running, waits until it completes.
135  // Callback will never be called after cancel() returns.
136  void cancel();
137 
138  private:
139  struct Context;
140  std::shared_ptr<Context> context_;
141 };
142 
143 template <typename Observable, typename Traits>
144 class ObserverCreator;
145 
146 template <typename T>
147 class Observer {
148  public:
149  explicit Observer(observer_detail::Core::Ptr core);
150 
151  Snapshot<T> getSnapshot() const;
153  return getSnapshot();
154  }
155 
160  bool needRefresh(const Snapshot<T>& snapshot) const {
161  DCHECK_EQ(core_.get(), snapshot.core_);
162  return snapshot.getVersion() < core_->getVersionLastChange();
163  }
164 
165  CallbackHandle addCallback(folly::Function<void(Snapshot<T>)> callback) const;
166 
167  private:
168  template <typename Observable, typename Traits>
169  friend class ObserverCreator;
170 
172 };
173 
185 template <typename F>
187 
188 template <typename F>
190 
191 template <typename T>
192 class TLObserver {
193  public:
194  explicit TLObserver(Observer<T> observer);
195  TLObserver(const TLObserver<T>& other);
196 
197  const Snapshot<T>& getSnapshotRef() const;
198  const Snapshot<T>& operator*() const {
199  return getSnapshotRef();
200  }
201 
202  private:
205 };
206 
210 template <typename T>
212  return TLObserver<T>(std::move(observer));
213 }
214 
215 template <typename F>
216 auto makeTLObserver(F&& creator) {
217  return makeTLObserver(makeObserver(std::forward<F>(creator)));
218 }
219 
220 template <typename T, bool CacheInThreadLocal>
221 struct ObserverTraits {};
222 
223 template <typename T>
224 struct ObserverTraits<T, false> {
225  using type = Observer<T>;
226 };
227 
228 template <typename T>
229 struct ObserverTraits<T, true> {
231 };
232 
233 template <typename T, bool CacheInThreadLocal>
235 } // namespace observer
236 } // namespace folly
237 
Observer< T > observer_
Definition: Observer.h:203
observer_detail::Core::Ptr core_
Definition: Observer.h:171
Snapshot< T > operator*() const
Definition: Observer.h:152
size_t getVersionLastChange()
Definition: Core.h:78
bool needRefresh(const Snapshot< T > &snapshot) const
Definition: Observer.h:160
size_t getVersion() const
Definition: Observer.h:101
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
const T * operator->() const
Definition: Observer.h:86
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::shared_ptr< Context > context_
Definition: Observer.h:139
std::shared_ptr< const T > data_
Definition: Observer.h:116
Observer< observer_detail::ResultOfUnwrapSharedPtr< F > > makeObserver(F &&creator)
Definition: Observer-inl.h:37
ProtocolVersion version
const T & operator*() const
Definition: Observer.h:82
std::shared_ptr< Core > Ptr
Definition: Core.h:40
TLObserver< T > makeTLObserver(Observer< T > observer)
Definition: Observer.h:211
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
std::shared_ptr< const T > getShared() const
Definition: Observer.h:94
const Snapshot< T > & operator*() const
Definition: Observer.h:198
folly::ThreadLocal< Snapshot< T > > snapshot_
Definition: Observer.h:204
typename ObserverTraits< T, CacheInThreadLocal >::type ObserverT
Definition: Observer.h:234
const observer_detail::Core * core_
Definition: Observer.h:118
Snapshot(const observer_detail::Core &core, std::shared_ptr< const T > data, size_t version)
Definition: Observer.h:108