proxygen
StringCodec.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/channel/Handler.h>
20 
21 namespace wangle {
22 
23 /*
24  * StringCodec converts a pipeline from IOBufs to std::strings.
25  */
26 class StringCodec : public Handler<std::unique_ptr<folly::IOBuf>, std::string,
27  std::string, std::unique_ptr<folly::IOBuf>> {
28  public:
29  typedef typename Handler<
30  std::unique_ptr<folly::IOBuf>, std::string,
31  std::string, std::unique_ptr<folly::IOBuf>>::Context Context;
32 
33  void read(Context* ctx, std::unique_ptr<folly::IOBuf> buf) override {
34  if (buf) {
35  buf->coalesce();
36  std::string data((const char*)buf->data(), buf->length());
37  ctx->fireRead(data);
38  }
39  }
40 
41  folly::Future<folly::Unit> write(Context* ctx, std::string msg) override {
42  auto buf = folly::IOBuf::copyBuffer(msg.data(), msg.length());
43  return ctx->fireWrite(std::move(buf));
44  }
45 };
46 
47 } // namespace wangle
folly::Future< folly::Unit > write(Context *ctx, std::string msg) override
Definition: StringCodec.h:41
ByteRange coalesce()
Definition: IOBuf.h:1095
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
const uint8_t * data() const
Definition: IOBuf.h:499
Handler< std::unique_ptr< folly::IOBuf >, std::string, std::string, std::unique_ptr< folly::IOBuf > >::Context Context
Definition: StringCodec.h:31
void read(Context *ctx, std::unique_ptr< folly::IOBuf > buf) override
Definition: StringCodec.h:33
std::size_t length() const
Definition: IOBuf.h:533
const char * string
Definition: Conv.cpp:212
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43