GRPC C++  1.30.0
proto_utils.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
20 #define GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
21 
22 #include <type_traits>
23 
35 
38 
39 namespace grpc {
40 
41 extern CoreCodegenInterface* g_core_codegen_interface;
42 
43 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
44 template <class ProtoBufferWriter, class T>
46  bool* own_buffer) {
47  static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
48  ProtoBufferWriter>::value,
49  "ProtoBufferWriter must be a subclass of "
50  "::protobuf::io::ZeroCopyOutputStream");
51  *own_buffer = true;
52  int byte_size = msg.ByteSizeLong();
53  if ((size_t)byte_size <= GRPC_SLICE_INLINED_SIZE) {
54  Slice slice(byte_size);
55  // We serialize directly into the allocated slices memory
56  GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray(
57  const_cast<uint8_t*>(slice.begin())));
58  ByteBuffer tmp(&slice, 1);
59  bb->Swap(&tmp);
60 
61  return g_core_codegen_interface->ok();
62  }
64  return msg.SerializeToZeroCopyStream(&writer)
66  : Status(StatusCode::INTERNAL, "Failed to serialize message");
67 }
68 
69 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
70 template <class ProtoBufferReader, class T>
73  static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
74  ProtoBufferReader>::value,
75  "ProtoBufferReader must be a subclass of "
76  "::protobuf::io::ZeroCopyInputStream");
77  if (buffer == nullptr) {
78  return Status(StatusCode::INTERNAL, "No payload");
79  }
81  {
82  ProtoBufferReader reader(buffer);
83  if (!reader.status().ok()) {
84  return reader.status();
85  }
86  if (!msg->ParseFromZeroCopyStream(&reader)) {
87  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
88  }
89  }
90  buffer->Clear();
91  return result;
92 }
93 
94 // this is needed so the following class does not conflict with protobuf
95 // serializers that utilize internal-only tools.
96 #ifdef GRPC_OPEN_SOURCE_PROTO
97 // This class provides a protobuf serializer. It translates between protobuf
98 // objects and grpc_byte_buffers. More information about SerializationTraits can
99 // be found in include/grpcpp/impl/codegen/serialization_traits.h.
100 template <class T>
101 class SerializationTraits<
102  T, typename std::enable_if<
103  std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type> {
104  public:
105  static Status Serialize(const grpc::protobuf::MessageLite& msg,
106  ByteBuffer* bb, bool* own_buffer) {
107  return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
108  }
109 
110  static Status Deserialize(ByteBuffer* buffer,
112  return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
113  }
114 };
115 #endif
116 
117 } // namespace grpc
118 
119 #endif // GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:53
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
status.h
byte_buffer_reader.h
grpc::protobuf::io::ZeroCopyOutputStream
::google::protobuf::io::ZeroCopyOutputStream ZeroCopyOutputStream
Definition: config_protobuf.h:96
grpc::Slice::begin
const uint8_t * begin() const
Raw pointer to the beginning (first element) of the slice.
Definition: slice.h:104
serialization_traits.h
grpc::protobuf::io::ZeroCopyInputStream
::google::protobuf::io::ZeroCopyInputStream ZeroCopyInputStream
Definition: config_protobuf.h:97
grpc::CoreCodegenInterface::ok
virtual const Status & ok()=0
core_codegen_interface.h
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: status.h:118
byte_buffer.h
grpc::Slice::end
const uint8_t * end() const
Raw pointer to the end (one byte past the last element) of the slice.
Definition: slice.h:107
grpc_types.h
grpc::GenericSerialize
Status GenericSerialize(const grpc::protobuf::MessageLite &msg, ByteBuffer *bb, bool *own_buffer)
Definition: proto_utils.h:45
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: byte_buffer.h:128
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:67
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:156
grpc::ProtoBufferReader
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
proto_buffer_writer.h
grpc::GenericDeserialize
Status GenericDeserialize(ByteBuffer *buffer, grpc::protobuf::MessageLite *msg)
Definition: proto_utils.h:71
grpc::kProtoBufferWriterMaxBufferLength
const int kProtoBufferWriterMaxBufferLength
Definition: proto_buffer_writer.h:44
grpc::protobuf::util::Status
::google::protobuf::util::Status Status
Definition: config_protobuf.h:90
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:35
grpc::ProtoBufferReader::status
Status status() const
Returns the status of the buffer reader.
Definition: proto_buffer_reader.h:96
config_protobuf.h
std
Definition: async_unary_call_impl.h:301
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:93
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:146
slice.h
GRPC_SLICE_INLINED_SIZE
#define GRPC_SLICE_INLINED_SIZE
Definition: slice.h:47
proto_buffer_reader.h
grpc::protobuf::MessageLite
::google::protobuf::MessageLite MessageLite
Definition: config_protobuf.h:76
slice.h
grpc::INTERNAL
Internal errors.
Definition: status_code_enum.h:119