2 * Copyright 2015 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <folly/wangle/channel/Pipeline.h>
21 namespace folly { namespace wangle {
24 * StaticPipeline allows you to create a Pipeline with minimal allocations.
25 * Specify your handlers after the input/output types of your Pipeline in order
26 * from front to back, and construct with either H&&, H*, or std::shared_ptr<H>
27 * for each handler. The pipeline will be finalized for you at the end of
28 * construction. For example:
30 * StringToStringHandler stringHandler1;
31 * auto stringHandler2 = std::make_shared<StringToStringHandler>();
33 * StaticPipeline<int, std::string,
35 * StringToStringHandler,
36 * StringToStringHandler>(
37 * IntToStringHandler(), // H&&
38 * &stringHandler1, // H*
39 * stringHandler2) // std::shared_ptr<H>
42 * You can then use pipeline just like any Pipeline. See Pipeline.h.
44 template <class R, class W, class... Handlers>
47 template <class R, class W>
48 class StaticPipeline<R, W> : public Pipeline<R, W> {
50 explicit StaticPipeline(bool) : Pipeline<R, W>(true) {}
53 template <class R, class W, class Handler, class... Handlers>
54 class StaticPipeline<R, W, Handler, Handlers...>
55 : public StaticPipeline<R, W, Handlers...> {
57 template <class... HandlerArgs>
58 explicit StaticPipeline(HandlerArgs&&... handlers)
59 : StaticPipeline(true, std::forward<HandlerArgs>(handlers)...) {
65 Pipeline<R, W>::detachHandlers();
70 template <class HandlerArg, class... HandlerArgs>
74 HandlerArgs&&... handlers)
75 : StaticPipeline<R, W, Handlers...>(
77 std::forward<HandlerArgs>(handlers)...) {
79 setHandler(std::forward<HandlerArg>(handler));
81 ctx_.initialize(this, handlerPtr_);
82 Pipeline<R, W>::addContextFront(&ctx_);
84 Pipeline<R, W>::finalize();
89 template <class HandlerArg>
90 typename std::enable_if<std::is_same<
91 typename std::remove_reference<HandlerArg>::type,
94 setHandler(HandlerArg&& arg) {
95 handler_.emplace(std::forward<HandlerArg>(arg));
96 handlerPtr_ = std::shared_ptr<Handler>(&(*handler_), [](Handler*){});
99 template <class HandlerArg>
100 typename std::enable_if<std::is_same<
101 typename std::decay<HandlerArg>::type,
102 std::shared_ptr<Handler>
104 setHandler(HandlerArg&& arg) {
105 handlerPtr_ = std::forward<HandlerArg>(arg);
108 template <class HandlerArg>
109 typename std::enable_if<std::is_same<
110 typename std::decay<HandlerArg>::type,
113 setHandler(HandlerArg&& arg) {
114 handlerPtr_ = std::shared_ptr<Handler>(arg, [](Handler*){});
118 folly::Optional<Handler> handler_;
119 std::shared_ptr<Handler> handlerPtr_;
120 typename ContextType<Handler, Pipeline<R, W>>::type ctx_;