proxygen
StaticPipeline.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 <type_traits>
20 
22 
23 namespace wangle {
24 
25 /*
26  * StaticPipeline allows you to create a Pipeline with minimal allocations.
27  * Specify your handlers after the input/output types of your Pipeline in order
28  * from front to back, and construct with either H&&, H*, or std::shared_ptr<H>
29  * for each handler. The pipeline will be finalized for you at the end of
30  * construction. For example:
31  *
32  * StringToStringHandler stringHandler1;
33  * auto stringHandler2 = std::make_shared<StringToStringHandler>();
34  *
35  * StaticPipeline<int, std::string,
36  * IntToStringHandler,
37  * StringToStringHandler,
38  * StringToStringHandler>(
39  * IntToStringHandler(), // H&&
40  * &stringHandler1, // H*
41  * stringHandler2) // std::shared_ptr<H>
42  * pipeline;
43  *
44  * You can then use pipeline just like any Pipeline. See Pipeline.h.
45  */
46 template <class R, class W, class... Handlers>
48 
49 template <class R, class W>
50 class StaticPipeline<R, W> : public Pipeline<R, W> {
51  protected:
52  explicit StaticPipeline(bool) : Pipeline<R, W>(true) {}
53  void initialize() {
55  }
56 };
57 
58 template <class Handler>
60  protected:
62 };
63 
64 template <class Handler>
66 };
67 
68 template <class R, class W, class Handler, class... Handlers>
69 class StaticPipeline<R, W, Handler, Handlers...>
70  : public StaticPipeline<R, W, Handlers...>
71  , public std::conditional<std::is_abstract<Handler>::value,
72  BaseWithoutOptional<Handler>,
73  BaseWithOptional<Handler>>::type {
74  public:
75  using Ptr = std::shared_ptr<StaticPipeline>;
76 
77  template <class... HandlerArgs>
78  static Ptr create(HandlerArgs&&... handlers) {
79  auto ptr = std::shared_ptr<StaticPipeline>(
80  new StaticPipeline(std::forward<HandlerArgs>(handlers)...));
81  ptr->initialize();
82  return ptr;
83  }
84 
85  ~StaticPipeline() override {
86  if (isFirst_) {
88  }
89  }
90 
91  protected:
92  template <class... HandlerArgs>
93  explicit StaticPipeline(HandlerArgs&&... handlers)
94  : StaticPipeline(true, std::forward<HandlerArgs>(handlers)...) {
95  isFirst_ = true;
96  }
97 
98  template <class HandlerArg, class... HandlerArgs>
100  bool isFirst,
101  HandlerArg&& handler,
102  HandlerArgs&&... handlers)
103  : StaticPipeline<R, W, Handlers...>(
104  false,
105  std::forward<HandlerArgs>(handlers)...) {
106  isFirst_ = isFirst;
107  setHandler(std::forward<HandlerArg>(handler));
109  }
110 
111  void initialize() {
112  CHECK(handlerPtr_);
113  ctx_.initialize(Pipeline<R, W>::shared_from_this(), handlerPtr_);
115  }
116 
117  private:
118  template <class HandlerArg>
119  typename std::enable_if<std::is_same<
121  Handler
122  >::value>::type
123  setHandler(HandlerArg&& arg) {
124  BaseWithOptional<Handler>::handler_.emplace(std::forward<HandlerArg>(arg));
125  handlerPtr_ = std::shared_ptr<Handler>(
127  [](Handler*){});
128  }
129 
130  template <class HandlerArg>
131  typename std::enable_if<std::is_same<
133  std::shared_ptr<Handler>
134  >::value>::type
135  setHandler(HandlerArg&& arg) {
136  handlerPtr_ = std::forward<HandlerArg>(arg);
137  }
138 
139  template <class HandlerArg>
140  typename std::enable_if<std::is_same<
141  typename std::decay<HandlerArg>::type,
142  Handler*
143  >::value>::type
144  setHandler(HandlerArg&& arg) {
145  handlerPtr_ = std::shared_ptr<Handler>(arg, [](Handler*){});
146  }
147 
148  bool isFirst_;
149  std::shared_ptr<Handler> handlerPtr_;
151 };
152 
153 } // namespace wangle
void * ptr
PskType type
void addContextFront(Context *ctx)
Definition: Pipeline-inl.h:150
static Ptr create(HandlerArgs &&...handlers)
STL namespace.
StaticPipeline(bool isFirst, HandlerArg &&handler, HandlerArgs &&...handlers)
std::shared_ptr< FizzServerContext > ctx_
std::enable_if< std::is_same< typename std::decay< HandlerArg >::type, Handler * >::value >::type setHandler(HandlerArg &&arg)
void handler(int, siginfo_t *, void *)
std::conditional< Handler::dir==HandlerDir::BOTH, ContextImpl< Handler >, typename std::conditional< Handler::dir==HandlerDir::IN, InboundContextImpl< Handler >, OutboundContextImpl< Handler > >::type >::type type
folly::Optional< Handler > handler_
static const char *const value
Definition: Conv.cpp:50
std::enable_if< std::is_same< typename std::decay< HandlerArg >::type, std::shared_ptr< Handler > >::value >::type setHandler(HandlerArg &&arg)
std::enable_if< std::is_same< typename std::remove_reference< HandlerArg >::type, Handler >::value >::type setHandler(HandlerArg &&arg)
void finalize() override
Definition: Pipeline-inl.h:267