1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
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 
17 #pragma once
18 
19 #include <folly/experimental/observer/Observer.h>
20 #include <folly/synchronization/Baton.h>
21 
22 namespace folly {
23 namespace observer {
24 
25 namespace detail {
26 template <typename Observable, typename Traits>
27 class ObserverCreatorContext;
28 }
29 
30 template <typename Observable>
31 struct ObservableTraits {
32   using element_type =
33       typename std::remove_reference<Observable>::type::element_type;
34 
getObservableTraits35   static std::shared_ptr<const element_type> get(Observable& observable) {
36     return observable.get();
37   }
38 
39   template <typename F>
subscribeObservableTraits40   static void subscribe(Observable& observable, F&& callback) {
41     observable.subscribe(std::forward<F>(callback));
42   }
43 
unsubscribeObservableTraits44   static void unsubscribe(Observable& observable) { observable.unsubscribe(); }
45 };
46 
47 template <typename Observable, typename Traits = ObservableTraits<Observable>>
48 class ObserverCreator {
49  public:
50   using T = typename Traits::element_type;
51 
52   template <typename... Args>
53   explicit ObserverCreator(Args&&... args);
54 
55   Observer<T> getObserver() &&;
56 
57  private:
58   using Context = detail::ObserverCreatorContext<Observable, Traits>;
59   class ContextPrimaryPtr;
60 
61   std::shared_ptr<Context> context_;
62 };
63 } // namespace observer
64 } // namespace folly
65 
66 #include <folly/experimental/observer/Observable-inl.h>
67