GRPC C++  1.78.1
serialization_traits.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_SERIALIZATION_TRAITS_H
20 #define GRPCPP_IMPL_SERIALIZATION_TRAITS_H
21 
23 
24 #include <type_traits>
25 #include <utility>
26 
27 namespace grpc {
28 
61 template <class Message,
62  class UnusedButHereForPartialTemplateSpecialization = void>
64 
65 namespace impl {
66 
67 // Helper trait to robustly check if the allocator-aware Serialize exists.
68 template <typename Message, typename BufferPtr, typename = void>
69 struct has_allocator_serialize : std::false_type {};
70 
71 template <typename Message, typename BufferPtr>
73  Message, BufferPtr,
74  std::void_t<decltype(SerializationTraits<Message>::Serialize(
75  std::declval<grpc_event_engine::experimental::MemoryAllocator*>(),
76  std::declval<const Message&>(), std::declval<BufferPtr>(),
77  std::declval<bool*>()))>> : std::true_type {};
78 
79 template <typename Message, typename BufferPtr>
80 inline constexpr bool has_allocator_serialize_v =
82 
83 // Primary template for SerializeDispatch, now switched by a bool.
84 template <typename Message, typename BufferPtr,
85  bool = has_allocator_serialize_v<Message, BufferPtr>>
87 
88 // Specialization for when the allocator-aware Serialize *exists*.
89 template <typename Message, typename BufferPtr>
90 struct SerializeDispatch<Message, BufferPtr, true> {
91  static auto Serialize(
93  const Message& msg, BufferPtr buffer, bool* own_buffer) {
94  return SerializationTraits<Message>::Serialize(allocator, msg, buffer,
95  own_buffer);
96  }
97 };
98 
99 // Specialization for when the allocator-aware Serialize *does not* exist.
100 template <typename Message, typename BufferPtr>
101 struct SerializeDispatch<Message, BufferPtr, false> {
102  static auto Serialize(
104  const Message& msg, BufferPtr buffer, bool* own_buffer) {
105  // Fallback to the old Serialize, ignoring the allocator.
106  return SerializationTraits<Message>::Serialize(msg, buffer, own_buffer);
107  }
108 };
109 
110 } // namespace impl
111 
112 template <typename Message, typename BufferPtr>
114  const Message& msg, BufferPtr buffer, bool* own_buffer) {
116  allocator, msg, buffer, own_buffer);
117 }
118 
119 template <typename BufferPtr, typename Message>
120 auto Deserialize(BufferPtr buffer, Message* msg) {
121  return SerializationTraits<Message>::Deserialize(buffer, msg);
122 }
123 
124 } // namespace grpc
125 
126 #endif // GRPCPP_IMPL_SERIALIZATION_TRAITS_H
grpc::impl::has_allocator_serialize
Definition: serialization_traits.h:69
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::Deserialize
auto Deserialize(BufferPtr buffer, Message *msg)
Definition: serialization_traits.h:120
grpc::Serialize
auto Serialize(grpc_event_engine::experimental::MemoryAllocator *allocator, const Message &msg, BufferPtr buffer, bool *own_buffer)
Definition: serialization_traits.h:113
grpc::impl::SerializeDispatch< Message, BufferPtr, false >::Serialize
static auto Serialize(grpc_event_engine::experimental::MemoryAllocator *, const Message &msg, BufferPtr buffer, bool *own_buffer)
Definition: serialization_traits.h:102
grpc::impl::SerializeDispatch< Message, BufferPtr, true >::Serialize
static auto Serialize(grpc_event_engine::experimental::MemoryAllocator *allocator, const Message &msg, BufferPtr buffer, bool *own_buffer)
Definition: serialization_traits.h:91
grpc::impl::has_allocator_serialize_v
constexpr bool has_allocator_serialize_v
Definition: serialization_traits.h:80
grpc::impl::SerializeDispatch
Definition: serialization_traits.h:86
grpc::SerializationTraits
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:63
std
Definition: async_unary_call.h:410
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:33
memory_allocator.h
grpc::protobuf::Message
::google::protobuf::Message Message
Definition: config_protobuf.h:89