/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include namespace apache::thrift::conformance { // A serializer base class any thrift struct and protocol. template class BaseAnyStructSerializer : public BaseTypedAnySerializer< T, BaseAnyStructSerializer> { using Base = BaseTypedAnySerializer>; public: using Base::encode; void encode(const T& value, folly::io::QueueAppender&& appender) const; using Base::decode; void decode(folly::io::Cursor& cursor, T& value) const; }; // A standard protocol serializer for any thrift struct. template class AnyStandardSerializer : public BaseAnyStructSerializer< T, detail::protocol_reader_t, detail::protocol_writer_t> { public: const Protocol& getProtocol() const final { return getStandardProtocol(); } }; // Returns a static serializer for the given struct T and standard protocol. template FOLLY_EXPORT const AnyStandardSerializer& getAnyStandardSerializer() { static const AnyStandardSerializer kSerializer; return kSerializer; } // Implementation. template void BaseAnyStructSerializer::encode( const T& value, folly::io::QueueAppender&& appender) const { Writer writer; writer.setOutput(std::move(appender)); value.write(&writer); } template void BaseAnyStructSerializer::decode( folly::io::Cursor& cursor, T& value) const { Reader reader; reader.setInput(cursor); value.read(&reader); cursor = reader.getCursor(); } } // namespace apache::thrift::conformance