proxygen
Observer.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-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 
17 #pragma once
18 
19 #include <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 wangle {
26 
27 template <class T> struct FunctionObserver;
28 
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 
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 std::make_unique<FunctionObserver<T>>(
52  std::forward<N>(onNextFn),
53  std::forward<E>(onErrorFn),
54  std::forward<C>(onCompletedFn));
55  }
56 
59  template <class N, class E>
60  static std::unique_ptr<Observer> create(N&& onNextFn, E&& onErrorFn) {
61  return std::make_unique<FunctionObserver<T>>(
62  std::forward<N>(onNextFn),
63  std::forward<E>(onErrorFn),
64  nullptr);
65  }
66 
69  template <class N>
70  static std::unique_ptr<Observer> create(N&& onNextFn) {
71  return std::make_unique<FunctionObserver<T>>(
72  std::forward<N>(onNextFn),
73  nullptr,
74  nullptr);
75  }
76 };
77 
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 
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 } // namespace wangle
void onError(Error e) override
Definition: Observer.h:99
#define T(v)
Definition: http_parser.c:233
std::function< void(const T &)> OnNext
Definition: Observer.h:82
virtual void onError(Error)=0
static std::unique_ptr< Observer > create(N &&onNextFn)
Definition: Observer.h:70
STL namespace.
double val
Definition: String.cpp:273
OnCompleted onCompleted_
Definition: Observer.h:110
virtual void onCompleted()=0
void onCompleted() override
Definition: Observer.h:103
virtual ~Observer()=default
#define C(name, bit)
Definition: CpuId.h:204
static std::unique_ptr< Observer > create(N &&onNextFn, E &&onErrorFn)
Definition: Observer.h:60
FunctionObserver(N &&n, E &&e, C &&c)
Definition: Observer.h:89
std::function< void(Error)> OnError
Definition: Observer.h:83
char c
static std::unique_ptr< Observer > create(N &&onNextFn, E &&onErrorFn, C &&onCompletedFn)
Definition: Observer.h:48
void onNext(const T &val) override
Definition: Observer.h:95
std::function< void()> OnCompleted
Definition: Observer.h:84
virtual void onNext(const T &)=0