07e1d81846b2574f36554f0f58bfaf8536065094
[folly.git] / folly / experimental / observer / SimpleObservable-inl.h
1 /*
2  * Copyright 2017 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/experimental/observer/Observable.h>
19
20 namespace folly {
21 namespace observer {
22
23 template <typename T>
24 SimpleObservable<T>::SimpleObservable(T value)
25     : context_(std::make_shared<Context>()) {
26   setValue(std::move(value));
27 }
28
29 template <typename T>
30 SimpleObservable<T>::SimpleObservable(std::shared_ptr<const T> value)
31     : context_(std::make_shared<Context>()) {
32   setValue(std::move(value));
33 }
34
35 template <typename T>
36 void SimpleObservable<T>::setValue(T value) {
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>
52 struct SimpleObservable<T>::Wrapper {
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() {
66     folly::Function<void()> empty;
67     context->callback_.swap(empty);
68   }
69 };
70
71 template <typename T>
72 Observer<T> SimpleObservable<T>::getObserver() {
73   std::call_once(observerInit_, [&]() {
74     SimpleObservable<T>::Wrapper wrapper;
75     wrapper.context = context_;
76     ObserverCreator<SimpleObservable<T>::Wrapper> creator(std::move(wrapper));
77     observer_ = std::move(creator).getObserver();
78   });
79   return *observer_;
80 }
81 }
82 }