rearrange Pipeline to have more functionality in PipelineBase
[folly.git] / folly / wangle / channel / Pipeline.cpp
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 #include <folly/wangle/channel/Pipeline.h>
18
19 namespace folly { namespace wangle {
20
21 void PipelineBase::setWriteFlags(WriteFlags flags) {
22   writeFlags_ = flags;
23 }
24
25 WriteFlags PipelineBase::getWriteFlags() {
26   return writeFlags_;
27 }
28
29 void PipelineBase::setReadBufferSettings(
30     uint64_t minAvailable,
31     uint64_t allocationSize) {
32   readBufferSettings_ = std::make_pair(minAvailable, allocationSize);
33 }
34
35 std::pair<uint64_t, uint64_t> PipelineBase::getReadBufferSettings() {
36   return readBufferSettings_;
37 }
38
39 typename PipelineBase::ContextIterator PipelineBase::removeAt(
40     const typename PipelineBase::ContextIterator& it) {
41   (*it)->detachPipeline();
42
43   const auto dir = (*it)->getDirection();
44   if (dir == HandlerDir::BOTH || dir == HandlerDir::IN) {
45     auto it2 = std::find(inCtxs_.begin(), inCtxs_.end(), it->get());
46     CHECK(it2 != inCtxs_.end());
47     inCtxs_.erase(it2);
48   }
49
50   if (dir == HandlerDir::BOTH || dir == HandlerDir::OUT) {
51     auto it2 = std::find(outCtxs_.begin(), outCtxs_.end(), it->get());
52     CHECK(it2 != outCtxs_.end());
53     outCtxs_.erase(it2);
54   }
55
56   return ctxs_.erase(it);
57 }
58
59 PipelineBase& PipelineBase::removeFront() {
60   if (ctxs_.empty()) {
61     throw std::invalid_argument("No handlers in pipeline");
62   }
63   removeAt(ctxs_.begin());
64   return *this;
65 }
66
67 PipelineBase& PipelineBase::removeBack() {
68   if (ctxs_.empty()) {
69     throw std::invalid_argument("No handlers in pipeline");
70   }
71   removeAt(--ctxs_.end());
72   return *this;
73 }
74
75 void PipelineBase::detachHandlers() {
76   for (auto& ctx : ctxs_) {
77     if (ctx != owner_) {
78       ctx->detachPipeline();
79     }
80   }
81 }
82
83 }} // folly::wangle