folly/wangle -> wangle cutover
[folly.git] / folly / wangle / deprecated / rx / Observer.h
1 /*
2  * Copyright 2015 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
17 #pragma once
18
19 #include <folly/wangle/deprecated/rx/types.h> // must come first
20 #include <functional>
21 #include <memory>
22 #include <stdexcept>
23 #include <folly/Memory.h>
24
25 namespace folly { namespace wangle {
26
27 template <class T> struct FunctionObserver;
28
29 /// Observer interface. You can subclass it, or you can just use create()
30 /// to use std::functions.
31 template <class T>
32 struct Observer {
33   // These are what it means to be an Observer.
34   virtual void onNext(const T&) = 0;
35   virtual void onError(Error) = 0;
36   virtual void onCompleted() = 0;
37
38   virtual ~Observer() = default;
39
40   /// Create an Observer with std::function callbacks. Handy to make ad-hoc
41   /// Observers with lambdas.
42   ///
43   /// Templated for maximum perfect forwarding flexibility, but ultimately
44   /// whatever you pass in has to implicitly become a std::function for the
45   /// same signature as onNext(), onError(), and onCompleted() respectively.
46   /// (see the FunctionObserver typedefs)
47   template <class N, class E, class C>
48   static std::unique_ptr<Observer> create(
49     N&& onNextFn, E&& onErrorFn, C&& onCompletedFn)
50   {
51     return folly::make_unique<FunctionObserver<T>>(
52       std::forward<N>(onNextFn),
53       std::forward<E>(onErrorFn),
54       std::forward<C>(onCompletedFn));
55   }
56
57   /// Create an Observer with only onNext and onError callbacks.
58   /// onCompleted will just be a no-op.
59   template <class N, class E>
60   static std::unique_ptr<Observer> create(N&& onNextFn, E&& onErrorFn) {
61     return folly::make_unique<FunctionObserver<T>>(
62       std::forward<N>(onNextFn),
63       std::forward<E>(onErrorFn),
64       nullptr);
65   }
66
67   /// Create an Observer with only an onNext callback.
68   /// onError and onCompleted will just be no-ops.
69   template <class N>
70   static std::unique_ptr<Observer> create(N&& onNextFn) {
71     return folly::make_unique<FunctionObserver<T>>(
72       std::forward<N>(onNextFn),
73       nullptr,
74       nullptr);
75   }
76 };
77
78 /// An observer that uses std::function callbacks. You don't really want to
79 /// make one of these directly - instead use the Observer::create() methods.
80 template <class T>
81 struct FunctionObserver : public Observer<T> {
82   typedef std::function<void(const T&)> OnNext;
83   typedef std::function<void(Error)> OnError;
84   typedef std::function<void()> OnCompleted;
85
86   /// We don't need any fancy overloads of this constructor because that's
87   /// what Observer::create() is for.
88   template <class N = OnNext, class E = OnError, class C = OnCompleted>
89   FunctionObserver(N&& n, E&& e, C&& c)
90     : onNext_(std::forward<N>(n)),
91       onError_(std::forward<E>(e)),
92       onCompleted_(std::forward<C>(c))
93   {}
94
95   void onNext(const T& val) override {
96     if (onNext_) onNext_(val);
97   }
98
99   void onError(Error e) override {
100     if (onError_) onError_(e);
101   }
102
103   void onCompleted() override {
104     if (onCompleted_) onCompleted_();
105   }
106
107  protected:
108   OnNext onNext_;
109   OnError onError_;
110   OnCompleted onCompleted_;
111 };
112
113 }}