// ********************************************************************** // // Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved. // // This copy of Ice Protobuf is licensed to you under the terms // described in the ICE_PROTOBUF_LICENSE file included in this // distribution. // // ********************************************************************** #ifndef STREAM_PROTOBUF_H #define STREAM_PROTOBUF_H #include #include #ifdef ICE_CPP11 #include #endif // // Tell Ice how to marshal and unmarshal a protobuf message to/from a sequence // namespace Ice { #ifdef ICE_CPP11 // // We'll use std::enable_if to give protobuf its own helper category (see below) // const StreamHelperCategory StreamHelperCategoryProtobuf = 100; #else // // We just assume a single "custom" category: Unknown // const StreamHelperCategory StreamHelperCategoryProtobuf = StreamHelperCategoryUnknown; #endif #ifdef ICE_CPP11 template struct StreamableTraits::value >::type> { static const StreamHelperCategory helper = StreamHelperCategoryProtobuf; static const int minWireSize = 1; static const bool fixedLength = false; }; #else // // We use the default 'Unknown' StreamHelperCategory // #endif template struct StreamHelper { template static inline void write(S* stream, const ::google::protobuf::MessageLite& v) { std::vector data(v.ByteSize()); if(!v.IsInitialized()) { throw MarshalException(__FILE__, __LINE__, "message not fully initialized: " + v.InitializationErrorString()); } Byte* r = v.SerializeWithCachedSizesToArray(&data[0]); if(r != &data[0] + data.size()) { throw MarshalException(__FILE__, __LINE__, "message modified during marshaling"); } stream->write(&data[0], &data[0] + data.size()); } template static inline void read(S* stream, ::google::protobuf::MessageLite& v) { std::pair data; stream->read(data); if(!v.ParseFromArray(data.first, static_cast(data.second - data.first))) { throw MarshalException(__FILE__, __LINE__, "ParseFromArray failed"); } } }; // // Use default marshaling/unmarshaling, with optionalFormat = OptionalFormatVSize // template<> struct GetOptionalFormat { static const OptionalFormat value = OptionalFormatVSize; }; } #endif