#ifndef ANDROID_PDX_RPC_ARGUMENT_ENCODER_H_ #define ANDROID_PDX_RPC_ARGUMENT_ENCODER_H_ #include #include #include #include #include namespace android { namespace pdx { namespace rpc { // Provides automatic serialization of argument lists and return // values by analyzing the supplied function signature types. // Examples: // ArgumentEncoder encoder(writer); // encoder.EncodeArguments(1, 1.0); template class ArgumentEncoder; // Specialization of ArgumentEncoder for void return types. template class ArgumentEncoder { public: explicit ArgumentEncoder(MessageWriter* writer) : writer_{writer} {} // Serializes the arguments as a tuple. void EncodeArguments(Args... args) { Serialize(std::forward_as_tuple(args...), writer_); } private: MessageWriter* writer_; }; // Specialization of ArgumentEncoder for non-void return types. template class ArgumentEncoder { public: // Simplified types with reference and cv removed. using ReturnType = typename std::decay::type; explicit ArgumentEncoder(MessageWriter* writer) : writer_{writer} {} // Serializes the arguments as a tuple. void EncodeArguments(Args... args) { Serialize(std::forward_as_tuple(args...), writer_); } // Serializes the return value for rvalue references. void EncodeReturn(const ReturnType& return_value) { Serialize(return_value, writer_); } private: MessageWriter* writer_; }; // Utility to build an ArgumentEncoder from a function pointer and a message // writer. template inline ArgumentEncoder MakeArgumentEncoder( Return (*)(Args...), MessageWriter* writer) { return ArgumentEncoder(writer); } // Utility to build an ArgumentEncoder from a method pointer and a message // writer. template inline ArgumentEncoder MakeArgumentEncoder( Return (Class::*)(Args...), MessageWriter* writer) { return ArgumentEncoder(writer); } // Utility to build an ArgumentEncoder from a const method pointer and a // message writer. template inline ArgumentEncoder MakeArgumentEncoder( Return (Class::*)(Args...) const, MessageWriter* writer) { return ArgumentEncoder(writer); } // Utility to build an ArgumentEncoder from a function type and a message // writer. template inline ArgumentEncoder MakeArgumentEncoder(MessageWriter* writer) { return ArgumentEncoder(writer); } ////////////////////////////////////////////////////////////////////////////// // Provides automatic deserialization of argument lists and return // values by analyzing the supplied function signature types. // Examples: // auto decoder = MakeArgumentDecoder(reader); // ErrorType error = decoder.DecodeReturn(&return_value); template class ArgumentDecoder; // Specialization of ArgumentDecoder for void return types. template class ArgumentDecoder { public: // Simplified types with reference and cv removed. using ArgsTupleType = std::tuple::type...>; explicit ArgumentDecoder(MessageReader* reader) : reader_{reader} {} // Deserializes arguments into a tuple. ArgsTupleType DecodeArguments(ErrorType* error) { ArgsTupleType value; *error = Deserialize(&value, reader_); return value; } private: MessageReader* reader_; }; // Specialization of ArgumentDecoder for non-void return types. template class ArgumentDecoder { public: // Simplified types with reference and cv removed. using ArgsTupleType = std::tuple::type...>; using ReturnType = typename std::decay::type; explicit ArgumentDecoder(MessageReader* reader) : reader_{reader} {} // Deserializes arguments into a tuple. ArgsTupleType DecodeArguments(ErrorType* error) { ArgsTupleType value; *error = Deserialize(&value, reader_); return value; } // Deserializes the return value. ErrorType DecodeReturn(ReturnType* value) { return Deserialize(value, reader_); } private: MessageReader* reader_; }; // Utility to build an ArgumentDecoder from a function pointer and a message // reader. template inline ArgumentDecoder MakeArgumentDecoder( Return (*)(Args...), MessageReader* reader) { return ArgumentDecoder(reader); } // Utility to build an ArgumentDecoder from a method pointer and a message // reader. template inline ArgumentDecoder MakeArgumentDecoder( Return (Class::*)(Args...), MessageReader* reader) { return ArgumentDecoder(reader); } // Utility to build an ArgumentDecoder from a const method pointer and a // message reader. template inline ArgumentDecoder MakeArgumentDecoder( Return (Class::*)(Args...) const, MessageReader* reader) { return ArgumentDecoder(reader); } // Utility to build an ArgumentDecoder from a function type and a message // reader. template inline ArgumentDecoder MakeArgumentDecoder(MessageReader* reader) { return ArgumentDecoder(reader); } } // namespace rpc } // namespace pdx } // namespace android #endif // ANDROID_PDX_RPC_ARGUMENT_ENCODER_H_