2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <folly/experimental/observer/detail/ObserverManager.h>
24 Snapshot<T> Observer<T>::getSnapshot() const {
25 auto data = core_->getData();
28 std::static_pointer_cast<const T>(std::move(data.data)),
33 Observer<T>::Observer(observer_detail::Core::Ptr core)
34 : core_(std::move(core)) {}
37 Observer<observer_detail::ResultOfUnwrapSharedPtr<F>> makeObserver(
39 auto core = observer_detail::Core::
40 create([creator = std::forward<F>(creator)]() mutable {
41 return std::static_pointer_cast<const void>(creator());
44 observer_detail::ObserverManager::initCore(core);
46 return Observer<observer_detail::ResultOfUnwrapSharedPtr<F>>(core);
50 Observer<observer_detail::ResultOf<F>> makeObserver(F&& creator) {
51 return makeObserver([creator = std::forward<F>(creator)]() mutable {
52 return std::make_shared<observer_detail::ResultOf<F>>(creator());
57 TLObserver<T>::TLObserver(Observer<T> observer)
58 : observer_(observer),
59 snapshot_([&] { return new Snapshot<T>(observer_.getSnapshot()); }) {}
62 TLObserver<T>::TLObserver(const TLObserver<T>& other)
63 : TLObserver(other.observer_) {}
66 const Snapshot<T>& TLObserver<T>::getSnapshotRef() const {
67 auto& snapshot = *snapshot_;
68 if (observer_.needRefresh(snapshot) ||
69 observer_detail::ObserverManager::inManagerThread()) {
70 snapshot = observer_.getSnapshot();