grpcpp/impl/codegen/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 // IWYU pragma: private
23 
24 #include <type_traits>
25 
37 
40 
41 namespace grpc {
42 
43 extern CoreCodegenInterface* g_core_codegen_interface;
44 
45 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
46 template <class ProtoBufferWriter, class T>
48  bool* own_buffer) {
49  static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
51  "ProtoBufferWriter must be a subclass of "
52  "::protobuf::io::ZeroCopyOutputStream");
53  *own_buffer = true;
54  int byte_size = static_cast<int>(msg.ByteSizeLong());
55  if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
56  Slice slice(byte_size);
57  // We serialize directly into the allocated slices memory
58  GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray(
59  const_cast<uint8_t*>(slice.begin())));
60  ByteBuffer tmp(&slice, 1);
61  bb->Swap(&tmp);
62 
63  return g_core_codegen_interface->ok();
64  }
66  return msg.SerializeToZeroCopyStream(&writer)
68  : Status(StatusCode::INTERNAL, "Failed to serialize message");
69 }
70 
71 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
72 template <class ProtoBufferReader, class T>
75  static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
77  "ProtoBufferReader must be a subclass of "
78  "::protobuf::io::ZeroCopyInputStream");
79  if (buffer == nullptr) {
80  return Status(StatusCode::INTERNAL, "No payload");
81  }
83  {
85  if (!reader.status().ok()) {
86  return reader.status();
87  }
88  if (!msg->ParseFromZeroCopyStream(&reader)) {
89  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
90  }
91  }
92  buffer->Clear();
93  return result;
94 }
95 
96 // this is needed so the following class does not conflict with protobuf
97 // serializers that utilize internal-only tools.
98 #ifdef GRPC_OPEN_SOURCE_PROTO
99 // This class provides a protobuf serializer. It translates between protobuf
100 // objects and grpc_byte_buffers. More information about SerializationTraits can
101 // be found in include/grpcpp/impl/codegen/serialization_traits.h.
102 template <class T>
103 class SerializationTraits<
104  T, typename std::enable_if<
105  std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type> {
106  public:
107  static Status Serialize(const grpc::protobuf::MessageLite& msg,
108  ByteBuffer* bb, bool* own_buffer) {
109  return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
110  }
111 
112  static Status Deserialize(ByteBuffer* buffer,
114  return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
115  }
116 };
117 #endif
118 
119 } // namespace grpc
120 
121 #endif // GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
grpc
Definition: grpcpp/alarm.h:33
byte_buffer_reader.h
grpc::protobuf::io::ZeroCopyInputStream
GRPC_CUSTOM_ZEROCOPYINPUTSTREAM ZeroCopyInputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:101
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:157
serialization_traits.h
slice.h
core_codegen_interface.h
slice.h
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_types.h
grpc::GenericSerialize
Status GenericSerialize(const grpc::protobuf::MessageLite &msg, ByteBuffer *bb, bool *own_buffer)
Definition: grpcpp/impl/codegen/proto_utils.h:47
grpc::CoreCodegenInterface::ok
virtual const Status & ok()=0
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::ProtoBufferReader
Definition: impl/codegen/proto_buffer_reader.h:48
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc::protobuf::MessageLite
GRPC_CUSTOM_MESSAGELITE MessageLite
Definition: include/grpcpp/impl/codegen/config_protobuf.h:79
grpc::ByteBuffer
A sequence of bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:61
proto_buffer_writer.h
grpc::ProtoBufferWriter
Definition: impl/codegen/proto_buffer_writer.h:55
byte_buffer.h
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
grpc::GenericDeserialize
Status GenericDeserialize(ByteBuffer *buffer, grpc::protobuf::MessageLite *msg)
Definition: grpcpp/impl/codegen/proto_utils.h:73
grpc::kProtoBufferWriterMaxBufferLength
const int kProtoBufferWriterMaxBufferLength
Definition: impl/codegen/proto_buffer_writer.h:46
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
writer
void writer(void *n)
Definition: libuv/docs/code/locks/main.c:22
value
const char * value
Definition: hpack_parser_table.cc:165
status.h
config_protobuf.h
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
GRPC_SLICE_INLINED_SIZE
#define GRPC_SLICE_INLINED_SIZE
Definition: include/grpc/impl/codegen/slice.h:49
GPR_CODEGEN_ASSERT
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: grpcpp/impl/codegen/core_codegen_interface.h:151
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
grpc::Slice
Definition: include/grpcpp/impl/codegen/slice.h:36
grpc.StatusCode.INTERNAL
tuple INTERNAL
Definition: src/python/grpcio/grpc/__init__.py:277
proto_buffer_reader.h
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:47