diff options
author | Vijay Pai <vpai@google.com> | 2017-09-15 12:32:20 -0700 |
---|---|---|
committer | Vijay Pai <vpai@google.com> | 2017-09-21 13:12:52 -0700 |
commit | a44cffa11ffd66b0f9ff6d2de0e90d782af88841 (patch) | |
tree | f83e11f60614994e7a2a2c026df5569df88da5c4 /include/grpc++/impl/codegen | |
parent | efce6e1e5017e8a6c6a81f348a4adcc4825b757d (diff) |
Base the new SerializationTraits API on implicit type conversion rather than SFINAE
Diffstat (limited to 'include/grpc++/impl/codegen')
-rw-r--r-- | include/grpc++/impl/codegen/byte_buffer.h | 45 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/call.h | 76 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/method_handler_impl.h | 8 | ||||
-rw-r--r-- | include/grpc++/impl/codegen/serialization_traits.h | 10 |
4 files changed, 46 insertions, 93 deletions
diff --git a/include/grpc++/impl/codegen/byte_buffer.h b/include/grpc++/impl/codegen/byte_buffer.h index 87d390c688..57d731be18 100644 --- a/include/grpc++/impl/codegen/byte_buffer.h +++ b/include/grpc++/impl/codegen/byte_buffer.h @@ -34,10 +34,14 @@ namespace grpc { template <class R> class CallOpRecvMessage; class MethodHandler; -namespace internal { -template <class M, class T> -class MessageDeserializer; -} +template <class ServiceType, class RequestType, class ResponseType> +class RpcMethodHandler; +template <class ServiceType, class RequestType, class ResponseType> +class ServerStreamingHandler; +namespace CallOpGenericRecvMessageHelper { +template <class R> +class DeserializeFuncType; +} // namespace CallOpGenericRecvMessageHelper /// A sequence of bytes. class ByteBuffer final { @@ -98,8 +102,14 @@ class ByteBuffer final { friend class CallOpRecvMessage; friend class CallOpGenericRecvMessage; friend class MethodHandler; - template <class M, class T> - friend class internal::MessageDeserializer; + template <class ServiceType, class RequestType, class ResponseType> + friend class RpcMethodHandler; + template <class ServiceType, class RequestType, class ResponseType> + friend class ServerStreamingHandler; + template <class R> + friend class CallOpGenericRecvMessageHelper::DeserializeFuncType; + + grpc_byte_buffer* buffer_; // takes ownership void set_buffer(grpc_byte_buffer* buf) { @@ -112,20 +122,25 @@ class ByteBuffer final { grpc_byte_buffer* c_buffer() { return buffer_; } grpc_byte_buffer** c_buffer_ptr() { return &buffer_; } - // DEPRECATED: Implicit conversion to transparently - // support deprecated SerializationTraits API - // No need to inline since deprecated - operator grpc_byte_buffer*(); - operator const grpc_byte_buffer*() const; - - grpc_byte_buffer* buffer_; + class ByteBufferPointer { + public: + ByteBufferPointer(const ByteBuffer* b) + : bbuf_(const_cast<ByteBuffer*>(b)) {} + operator ByteBuffer*() { return bbuf_; } + operator grpc_byte_buffer*() { return bbuf_->buffer_; } + operator grpc_byte_buffer**() { return &bbuf_->buffer_; } + + private: + ByteBuffer* bbuf_; + }; + ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); } }; template <> class SerializationTraits<ByteBuffer, void> { public: - static Status Deserialize(const ByteBuffer& byte_buffer, ByteBuffer* dest) { - dest->set_buffer(byte_buffer.buffer_); + static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) { + dest->set_buffer(byte_buffer->buffer_); return Status::OK; } static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer, diff --git a/include/grpc++/impl/codegen/call.h b/include/grpc++/impl/codegen/call.h index 3c418c7ae2..d9988e51fc 100644 --- a/include/grpc++/impl/codegen/call.h +++ b/include/grpc++/impl/codegen/call.h @@ -305,9 +305,6 @@ class CallOpSendMessage { void FinishOp(bool* status) { send_buf_.Clear(); } private: - template <class M, class T = void> - class MessageSerializer; - ByteBuffer send_buf_; WriteOptions write_options_; }; @@ -318,40 +315,11 @@ T Example(); } // namespace internal template <class M> -class CallOpSendMessage::MessageSerializer< - M, typename std::enable_if<std::is_same< - ::grpc::Status, decltype(SerializationTraits<M>::Serialize( - internal::Example<const M&>(), - internal::Example<grpc_byte_buffer**>(), - internal::Example<bool*>()))>::value>::type> { - public: - static Status SendMessageInternal(const M& message, ByteBuffer* bbuf, - bool* own_buf) { - return SerializationTraits<M>::Serialize(message, bbuf->c_buffer_ptr(), - own_buf); - } -}; - -template <class M> -class CallOpSendMessage::MessageSerializer< - M, typename std::enable_if<std::is_same< - ::grpc::Status, decltype(SerializationTraits<M>::Serialize( - internal::Example<const M&>(), - internal::Example<::grpc::ByteBuffer*>(), - internal::Example<bool*>()))>::value>::type> { - public: - static Status SendMessageInternal(const M& message, ByteBuffer* bbuf, - bool* own_buf) { - return SerializationTraits<M>::Serialize(message, bbuf, own_buf); - } -}; - -template <class M> Status CallOpSendMessage::SendMessage(const M& message, WriteOptions options) { write_options_ = options; bool own_buf; - Status result = - MessageSerializer<M>::SendMessageInternal(message, &send_buf_, &own_buf); + Status result = SerializationTraits<M>::Serialize( + message, send_buf_.bbuf_ptr(), &own_buf); if (!own_buf) { send_buf_.Duplicate(); } @@ -363,36 +331,6 @@ Status CallOpSendMessage::SendMessage(const M& message) { return SendMessage(message, WriteOptions()); } -namespace internal { -template <class M, class T = void> -class MessageDeserializer; - -template <class M> -class MessageDeserializer< - M, typename std::enable_if<std::is_same< - ::grpc::Status, decltype(SerializationTraits<M>::Deserialize( - internal::Example<const ::grpc::ByteBuffer&>(), - internal::Example<M*>()))>::value>::type> { - public: - static Status Deserialize(const ByteBuffer& bbuf, M* message) { - return SerializationTraits<M>::Deserialize(bbuf, message); - } -}; - -template <class M> -class MessageDeserializer< - M, typename std::enable_if<std::is_same< - ::grpc::Status, decltype(SerializationTraits<M>::Deserialize( - internal::Example<grpc_byte_buffer*>(), - internal::Example<M*>()))>::value>::type> { - public: - static Status Deserialize(const ByteBuffer& bbuf, M* message) { - return SerializationTraits<M>::Deserialize( - const_cast<ByteBuffer&>(bbuf).c_buffer(), message); - } -}; -} // namespace internal - template <class R> class CallOpRecvMessage { public: @@ -423,7 +361,7 @@ class CallOpRecvMessage { if (recv_buf_.Valid()) { if (*status) { got_message = *status = - internal::MessageDeserializer<R>::Deserialize(recv_buf_, message_) + SerializationTraits<R>::Deserialize(recv_buf_.bbuf_ptr(), message_) .ok(); recv_buf_.Release(); } else { @@ -448,7 +386,7 @@ class CallOpRecvMessage { namespace CallOpGenericRecvMessageHelper { class DeserializeFunc { public: - virtual Status Deserialize(const ByteBuffer& buf) = 0; + virtual Status Deserialize(ByteBuffer* buf) = 0; virtual ~DeserializeFunc() {} }; @@ -456,8 +394,8 @@ template <class R> class DeserializeFuncType final : public DeserializeFunc { public: DeserializeFuncType(R* message) : message_(message) {} - Status Deserialize(const ByteBuffer& buf) override { - return grpc::internal::MessageDeserializer<R>::Deserialize(buf, message_); + Status Deserialize(ByteBuffer* buf) override { + return SerializationTraits<R>::Deserialize(buf->bbuf_ptr(), message_); } ~DeserializeFuncType() override {} @@ -501,7 +439,7 @@ class CallOpGenericRecvMessage { if (recv_buf_.Valid()) { if (*status) { got_message = true; - *status = deserialize_->Deserialize(recv_buf_).ok(); + *status = deserialize_->Deserialize(&recv_buf_).ok(); recv_buf_.Release(); } else { got_message = false; diff --git a/include/grpc++/impl/codegen/method_handler_impl.h b/include/grpc++/impl/codegen/method_handler_impl.h index 8125e0a651..e14cb0e926 100644 --- a/include/grpc++/impl/codegen/method_handler_impl.h +++ b/include/grpc++/impl/codegen/method_handler_impl.h @@ -38,8 +38,8 @@ class RpcMethodHandler : public MethodHandler { void RunHandler(const HandlerParameter& param) final { RequestType req; - Status status = internal::MessageDeserializer<RequestType>::Deserialize( - param.request, &req); + Status status = SerializationTraits<RequestType>::Deserialize( + param.request.bbuf_ptr(), &req); ResponseType rsp; if (status.ok()) { status = func_(service_, param.server_context, &req, &rsp); @@ -124,8 +124,8 @@ class ServerStreamingHandler : public MethodHandler { void RunHandler(const HandlerParameter& param) final { RequestType req; - Status status = internal::MessageDeserializer<RequestType>::Deserialize( - param.request, &req); + Status status = SerializationTraits<RequestType>::Deserialize( + param.request.bbuf_ptr(), &req); if (status.ok()) { ServerWriter<ResponseType> writer(param.call, param.server_context); diff --git a/include/grpc++/impl/codegen/serialization_traits.h b/include/grpc++/impl/codegen/serialization_traits.h index 7fc4e24172..4d91739cc4 100644 --- a/include/grpc++/impl/codegen/serialization_traits.h +++ b/include/grpc++/impl/codegen/serialization_traits.h @@ -24,20 +24,20 @@ namespace grpc { /// Defines how to serialize and deserialize some type. /// /// Used for hooking different message serialization API's into GRPC. -/// Each SerializationTraits implementation must provide the following -/// functions: +/// Each SerializationTraits<Message> implementation must provide the +/// following functions: /// 1. static Status Serialize(const Message& msg, /// ByteBuffer* buffer, /// bool* own_buffer); -/// AND/OR +/// OR /// static Status Serialize(const Message& msg, /// grpc_byte_buffer** buffer, /// bool* own_buffer); /// The former is preferred; the latter is deprecated /// -/// 2. static Status Deserialize(const ByteBuffer& buffer, +/// 2. static Status Deserialize(ByteBuffer* buffer, /// Message* msg); -/// AND/OR +/// OR /// static Status Deserialize(grpc_byte_buffer* buffer, /// Message* msg); /// The former is preferred; the latter is deprecated |