proxygen
Observer-inl.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 
19 
20 namespace folly {
21 namespace observer {
22 
23 template <typename T>
25  auto data = core_->getData();
26  return Snapshot<T>(
27  *core_,
28  std::static_pointer_cast<const T>(std::move(data.data)),
29  data.version);
30 }
31 
32 template <typename T>
34  : core_(std::move(core)) {}
35 
36 template <typename F>
38  F&& creator) {
40  [creator = std::forward<F>(creator)]() mutable {
41  return std::static_pointer_cast<const void>(creator());
42  });
43 
45 
47 }
48 
49 template <typename F>
51  return makeObserver([creator = std::forward<F>(creator)]() mutable {
52  return std::make_shared<observer_detail::ResultOf<F>>(creator());
53  });
54 }
55 
56 template <typename T>
58  : observer_(observer),
59  snapshot_([&] { return new Snapshot<T>(observer_.getSnapshot()); }) {}
60 
61 template <typename T>
63  : TLObserver(other.observer_) {}
64 
65 template <typename T>
67  auto& snapshot = *snapshot_;
68  if (observer_.needRefresh(snapshot) ||
70  snapshot = observer_.getSnapshot();
71  }
72 
73  return snapshot;
74 }
75 
78  Synchronized<bool> canceled{false};
79 };
80 
82 
83 template <typename T>
85  Observer<T> observer,
86  folly::Function<void(Snapshot<T>)> callback) {
87  context_ = std::make_shared<Context>();
88  context_->observer = makeObserver([observer = std::move(observer),
89  callback = std::move(callback),
90  context = context_]() mutable {
91  auto rCanceled = context->canceled.rlock();
92  if (*rCanceled) {
93  return folly::unit;
94  }
95  callback(*observer);
96  return folly::unit;
97  });
98 }
99 
101  cancel();
102 }
103 
104 inline void CallbackHandle::cancel() {
105  if (!context_) {
106  return;
107  }
108  context_->observer.reset();
109  context_->canceled = true;
110  context_.reset();
111 }
112 
113 template <typename T>
115  folly::Function<void(Snapshot<T>)> callback) const {
116  return CallbackHandle(*this, std::move(callback));
117 }
118 } // namespace observer
119 } // namespace folly
Observer< T > observer_
Definition: Observer.h:203
TLObserver(Observer< T > observer)
Definition: Observer-inl.h:57
CallbackHandle addCallback(folly::Function< void(Snapshot< T >)> callback) const
Definition: Observer-inl.h:114
context
Definition: CMakeCache.txt:563
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Observer< observer_detail::ResultOfUnwrapSharedPtr< F > > makeObserver(F &&creator)
Definition: Observer-inl.h:37
std::shared_ptr< Core > Ptr
Definition: Core.h:40
static void initCore(Core::Ptr core)
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
constexpr Unit unit
Definition: Unit.h:45
Snapshot< T > getSnapshot() const
Definition: Observer-inl.h:24
const Snapshot< T > & getSnapshotRef() const
Definition: Observer-inl.h:66
folly::ThreadLocal< Snapshot< T > > snapshot_
Definition: Observer.h:204
Observer(observer_detail::Core::Ptr core)
Definition: Observer-inl.h:33
static Ptr create(folly::Function< std::shared_ptr< const void >()> creator)
Definition: Core.cpp:158
Optional< Observer< folly::Unit > > observer
Definition: Observer-inl.h:77