proxygen
SimpleObservable-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  : context_(std::make_shared<Context>()) {
26  setValue(std::move(value));
27 }
28 
29 template <typename T>
31  : context_(std::make_shared<Context>()) {
32  setValue(std::move(value));
33 }
34 
35 template <typename T>
37  setValue(std::make_shared<const T>(std::move(value)));
38 }
39 
40 template <typename T>
41 void SimpleObservable<T>::setValue(std::shared_ptr<const T> value) {
42  context_->value_.swap(value);
43 
44  context_->callback_.withWLock([](folly::Function<void()>& callback) {
45  if (callback) {
46  callback();
47  }
48  });
49 }
50 
51 template <typename T>
53  using element_type = T;
54 
55  std::shared_ptr<Context> context;
56 
57  std::shared_ptr<const T> get() {
58  return context->value_.copy();
59  }
60 
61  void subscribe(folly::Function<void()> callback) {
62  context->callback_.swap(callback);
63  }
64 
65  void unsubscribe() {
67  context->callback_.swap(empty);
68  }
69 };
70 
71 template <typename T>
75  wrapper.context = context_;
77  observer_ = std::move(creator).getObserver();
78  });
79  return *observer_;
80 }
81 } // namespace observer
82 } // namespace folly
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::Optional< Observer< T > > observer_
FOLLY_ALWAYS_INLINE void call_once(basic_once_flag< Mutex, Atom > &flag, F &&f, Args &&...args)
Definition: CallOnce.h:56
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
std::shared_ptr< Context > context_
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
void subscribe(folly::Function< void()> callback)