24 #ifndef DCCL_NATIVE_PROTOBUF_20190218H
25 #define DCCL_NATIVE_PROTOBUF_20190218H
27 #include <google/protobuf/io/coded_stream.h>
28 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
29 #include <google/protobuf/unknown_field_set.h>
30 #include <google/protobuf/wire_format_lite.h>
32 #include <google/protobuf/stubs/common.h>
33 #if GOOGLE_PROTOBUF_VERSION < 3008000
34 #include <google/protobuf/wire_format_lite_inl.h>
37 #include "../field_codec_fixed.h"
38 #include "../field_codec_typed.h"
43 namespace native_protobuf
45 template <
typename WireType, google::protobuf::
internal::WireFormatLite::FieldType DeclaredType>
48 WireType decode(google::protobuf::io::CodedInputStream* input_stream)
51 google::protobuf::internal::WireFormatLite::ReadPrimitive<WireType, DeclaredType>(
52 input_stream, &value);
57 template <
typename WireType, google::protobuf::FieldDescriptor::Type DeclaredType>
62 template <
typename WireType>
65 google::protobuf::internal::WireFormatLite::TYPE_INT64>
67 unsigned byte_size(
const WireType& wire_value)
69 return google::protobuf::internal::WireFormatLite::Int64Size(wire_value);
71 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
73 google::protobuf::internal::WireFormatLite::WriteInt64NoTagToArray(wire_value,
76 bool is_varint() {
return true; }
79 template <
typename WireType>
82 google::protobuf::internal::WireFormatLite::TYPE_INT32>
84 unsigned byte_size(
const WireType& wire_value)
86 return google::protobuf::internal::WireFormatLite::Int32Size(wire_value);
88 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
90 google::protobuf::internal::WireFormatLite::WriteInt32NoTagToArray(wire_value,
93 bool is_varint() {
return true; }
96 template <
typename WireType>
99 google::protobuf::internal::WireFormatLite::TYPE_UINT64>
101 unsigned byte_size(
const WireType& wire_value)
103 return google::protobuf::internal::WireFormatLite::UInt64Size(wire_value);
105 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
107 google::protobuf::internal::WireFormatLite::WriteUInt64NoTagToArray(wire_value,
110 bool is_varint() {
return true; }
113 template <
typename WireType>
116 google::protobuf::internal::WireFormatLite::TYPE_UINT32>
118 unsigned byte_size(
const WireType& wire_value)
120 return google::protobuf::internal::WireFormatLite::UInt32Size(wire_value);
122 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
124 google::protobuf::internal::WireFormatLite::WriteUInt32NoTagToArray(wire_value,
127 bool is_varint() {
return true; }
130 template <
typename WireType>
133 google::protobuf::internal::WireFormatLite::TYPE_SINT64>
135 unsigned byte_size(
const WireType& wire_value)
137 return google::protobuf::internal::WireFormatLite::SInt64Size(wire_value);
139 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
141 google::protobuf::internal::WireFormatLite::WriteSInt64NoTagToArray(wire_value,
144 bool is_varint() {
return true; }
147 template <
typename WireType>
150 google::protobuf::internal::WireFormatLite::TYPE_SINT32>
152 unsigned byte_size(
const WireType& wire_value)
154 return google::protobuf::internal::WireFormatLite::SInt32Size(wire_value);
156 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
158 google::protobuf::internal::WireFormatLite::WriteSInt32NoTagToArray(wire_value,
161 bool is_varint() {
return true; }
164 template <
typename WireType>
167 google::protobuf::internal::WireFormatLite::TYPE_ENUM>
169 unsigned byte_size(
const WireType& wire_value)
171 return google::protobuf::internal::WireFormatLite::EnumSize(wire_value);
173 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
175 google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(wire_value, &(*bytes)[0]);
177 bool is_varint() {
return true; }
180 template <
typename WireType>
183 google::protobuf::internal::WireFormatLite::TYPE_DOUBLE>
185 unsigned byte_size(
const WireType& )
187 return google::protobuf::internal::WireFormatLite::kDoubleSize;
189 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
191 google::protobuf::internal::WireFormatLite::WriteDoubleNoTagToArray(wire_value,
194 bool is_varint() {
return false; }
197 template <
typename WireType>
200 google::protobuf::internal::WireFormatLite::TYPE_FLOAT>
202 unsigned byte_size(
const WireType& )
204 return google::protobuf::internal::WireFormatLite::kFloatSize;
206 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
208 google::protobuf::internal::WireFormatLite::WriteFloatNoTagToArray(wire_value,
211 bool is_varint() {
return false; }
214 template <
typename WireType>
217 google::protobuf::internal::WireFormatLite::TYPE_BOOL>
219 unsigned byte_size(
const WireType& )
221 return google::protobuf::internal::WireFormatLite::kBoolSize;
223 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
225 google::protobuf::internal::WireFormatLite::WriteBoolNoTagToArray(wire_value, &(*bytes)[0]);
227 bool is_varint() {
return false; }
230 template <
typename WireType>
233 google::protobuf::internal::WireFormatLite::TYPE_FIXED64>
235 unsigned byte_size(
const WireType& )
237 return google::protobuf::internal::WireFormatLite::kFixed64Size;
239 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
241 google::protobuf::internal::WireFormatLite::WriteFixed64NoTagToArray(wire_value,
244 bool is_varint() {
return false; }
247 template <
typename WireType>
250 google::protobuf::internal::WireFormatLite::TYPE_FIXED32>
252 unsigned byte_size(
const WireType& )
254 return google::protobuf::internal::WireFormatLite::kFixed32Size;
256 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
258 google::protobuf::internal::WireFormatLite::WriteFixed32NoTagToArray(wire_value,
261 bool is_varint() {
return false; }
264 template <
typename WireType>
267 google::protobuf::internal::WireFormatLite::TYPE_SFIXED64>
269 unsigned byte_size(
const WireType& )
271 return google::protobuf::internal::WireFormatLite::kSFixed64Size;
273 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
275 google::protobuf::internal::WireFormatLite::WriteSFixed64NoTagToArray(wire_value,
278 bool is_varint() {
return false; }
281 template <
typename WireType>
284 google::protobuf::internal::WireFormatLite::TYPE_SFIXED32>
286 unsigned byte_size(
const WireType& )
288 return google::protobuf::internal::WireFormatLite::kSFixed32Size;
290 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
292 google::protobuf::internal::WireFormatLite::WriteSFixed32NoTagToArray(wire_value,
295 bool is_varint() {
return false; }
298 template <
typename WireType, google::protobuf::FieldDescriptor::Type DeclaredType,
299 typename FieldType = WireType>
303 unsigned presence_bit_size() {
return this->
use_required() ? 0 : 1; }
305 unsigned min_size()
override
310 if (helper_.is_varint())
313 return BITS_IN_BYTE * helper_.byte_size(WireType());
318 return presence_bit_size();
322 unsigned max_size()
override
325 return std::max<unsigned>(size(std::numeric_limits<WireType>::min()),
326 size(std::numeric_limits<WireType>::max()));
329 unsigned size()
override {
return min_size(); }
331 unsigned size(
const WireType& wire_value)
override
333 unsigned data_bytes = helper_.byte_size(wire_value);
334 unsigned size = presence_bit_size() + BITS_IN_BYTE * data_bytes;
341 return Bitset(min_size(), 0);
344 Bitset encode(
const WireType& wire_value)
override
346 std::vector<google::protobuf::uint8> bytes(size(wire_value) / BITS_IN_BYTE, 0);
348 helper_.encode(wire_value, &bytes);
354 data_bits.resize(data_bits.size() + presence_bit_size());
356 data_bits.
set(0,
true);
361 WireType decode(
Bitset* bits)
override
365 dccl::uint64 uint_value = (bits->template to<dccl::uint64>)();
370 if (helper_.is_varint())
373 bits->
get_more_bits(BITS_IN_BYTE * helper_.byte_size(WireType()));
376 if (helper_.is_varint())
383 google::protobuf::io::CodedInputStream input_stream(
384 reinterpret_cast<const google::protobuf::uint8*
>(bytes.data()), bytes.size());
386 return helper_.decode(&input_stream);
395 const google::protobuf::EnumValueDescriptor*>
398 int pre_encode(
const google::protobuf::EnumValueDescriptor*
const& field_value)
override;
399 const google::protobuf::EnumValueDescriptor*
post_decode(
const int& wire_value)
override;