• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PROTO_UTILS_H
20 #define GRPCPP_IMPL_PROTO_UTILS_H
21 
22 #include <grpc/byte_buffer_reader.h>
23 #include <grpc/impl/grpc_types.h>
24 #include <grpc/slice.h>
25 #include <grpcpp/impl/codegen/config_protobuf.h>
26 #include <grpcpp/impl/generic_serialize.h>
27 #include <grpcpp/impl/serialization_traits.h>
28 #include <grpcpp/support/byte_buffer.h>
29 #include <grpcpp/support/proto_buffer_reader.h>
30 #include <grpcpp/support/proto_buffer_writer.h>
31 #include <grpcpp/support/slice.h>
32 #include <grpcpp/support/status.h>
33 
34 #include <type_traits>
35 
36 /// This header provides serialization and deserialization between gRPC
37 /// messages serialized using protobuf and the C++ objects they represent.
38 
39 namespace grpc {
40 
41 // This class provides a protobuf serializer. It translates between protobuf
42 // objects and grpc_byte_buffers. More information about SerializationTraits can
43 // be found in include/grpcpp/impl/codegen/serialization_traits.h.
44 template <class T>
45 class SerializationTraits<
46     T, typename std::enable_if<
47            std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type> {
48  public:
Serialize(const grpc::protobuf::MessageLite & msg,ByteBuffer * bb,bool * own_buffer)49   static Status Serialize(const grpc::protobuf::MessageLite& msg,
50                           ByteBuffer* bb, bool* own_buffer) {
51     return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
52   }
53 
Deserialize(ByteBuffer * buffer,grpc::protobuf::MessageLite * msg)54   static Status Deserialize(ByteBuffer* buffer,
55                             grpc::protobuf::MessageLite* msg) {
56     return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
57   }
58 };
59 
60 }  // namespace grpc
61 
62 #endif  // GRPCPP_IMPL_PROTO_UTILS_H
63