22 #ifndef DCCL_NATIVE_PROTOBUF_20190218H
23 #define DCCL_NATIVE_PROTOBUF_20190218H
25 #include <google/protobuf/unknown_field_set.h>
26 #include <google/protobuf/io/coded_stream.h>
27 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
28 #include <google/protobuf/wire_format_lite.h>
30 #include <google/protobuf/stubs/common.h>
31 #if GOOGLE_PROTOBUF_VERSION < 3008000
32 #include <google/protobuf/wire_format_lite_inl.h>
35 #include "dccl/field_codec_fixed.h"
36 #include "dccl/field_codec_typed.h"
41 namespace native_protobuf
44 template<
typename WireType, google::protobuf::
internal::WireFormatLite::FieldType DeclaredType>
47 WireType decode(google::protobuf::io::CodedInputStream* input_stream)
50 google::protobuf::internal::WireFormatLite::ReadPrimitive<WireType, DeclaredType>(input_stream, &value);
56 template<
typename WireType, google::protobuf::FieldDescriptor::Type DeclaredType>
61 template<
typename WireType>
64 unsigned byte_size(
const WireType& wire_value)
65 {
return google::protobuf::internal::WireFormatLite::Int64Size(wire_value); }
66 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
67 { google::protobuf::internal::WireFormatLite::WriteInt64NoTagToArray(wire_value, &(*bytes)[0]); }
68 bool is_varint() {
return true; }
71 template<
typename WireType>
74 unsigned byte_size(
const WireType& wire_value)
75 {
return google::protobuf::internal::WireFormatLite::Int32Size(wire_value); }
76 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
77 { google::protobuf::internal::WireFormatLite::WriteInt32NoTagToArray(wire_value, &(*bytes)[0]); }
78 bool is_varint() {
return true; }
82 template<
typename WireType>
85 unsigned byte_size(
const WireType& wire_value)
86 {
return google::protobuf::internal::WireFormatLite::UInt64Size(wire_value); }
87 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
88 { google::protobuf::internal::WireFormatLite::WriteUInt64NoTagToArray(wire_value, &(*bytes)[0]); }
89 bool is_varint() {
return true; }
92 template<
typename WireType>
95 unsigned byte_size(
const WireType& wire_value)
96 {
return google::protobuf::internal::WireFormatLite::UInt32Size(wire_value); }
97 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
98 { google::protobuf::internal::WireFormatLite::WriteUInt32NoTagToArray(wire_value, &(*bytes)[0]); }
99 bool is_varint() {
return true; }
102 template<
typename WireType>
105 unsigned byte_size(
const WireType& wire_value)
106 {
return google::protobuf::internal::WireFormatLite::SInt64Size(wire_value); }
107 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
108 { google::protobuf::internal::WireFormatLite::WriteSInt64NoTagToArray(wire_value, &(*bytes)[0]); }
109 bool is_varint() {
return true; }
112 template<
typename WireType>
115 unsigned byte_size(
const WireType& wire_value)
116 {
return google::protobuf::internal::WireFormatLite::SInt32Size(wire_value); }
117 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
118 { google::protobuf::internal::WireFormatLite::WriteSInt32NoTagToArray(wire_value, &(*bytes)[0]); }
119 bool is_varint() {
return true; }
124 template<
typename WireType>
127 unsigned byte_size(
const WireType& wire_value)
128 {
return google::protobuf::internal::WireFormatLite::EnumSize(wire_value); }
129 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
130 { google::protobuf::internal::WireFormatLite::WriteEnumNoTagToArray(wire_value, &(*bytes)[0]); }
131 bool is_varint() {
return true; }
135 template<
typename WireType>
138 unsigned byte_size(
const WireType& wire_value)
139 {
return google::protobuf::internal::WireFormatLite::kDoubleSize; }
140 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
141 { google::protobuf::internal::WireFormatLite::WriteDoubleNoTagToArray(wire_value, &(*bytes)[0]); }
142 bool is_varint() {
return false; }
145 template<
typename WireType>
148 unsigned byte_size(
const WireType& wire_value)
149 {
return google::protobuf::internal::WireFormatLite::kFloatSize; }
150 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
151 { google::protobuf::internal::WireFormatLite::WriteFloatNoTagToArray(wire_value, &(*bytes)[0]); }
152 bool is_varint() {
return false; }
155 template<
typename WireType>
158 unsigned byte_size(
const WireType& wire_value)
159 {
return google::protobuf::internal::WireFormatLite::kBoolSize; }
160 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
161 { google::protobuf::internal::WireFormatLite::WriteBoolNoTagToArray(wire_value, &(*bytes)[0]); }
162 bool is_varint() {
return false; }
166 template<
typename WireType>
169 unsigned byte_size(
const WireType& wire_value)
170 {
return google::protobuf::internal::WireFormatLite::kFixed64Size; }
171 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
172 { google::protobuf::internal::WireFormatLite::WriteFixed64NoTagToArray(wire_value, &(*bytes)[0]); }
173 bool is_varint() {
return false; }
176 template<
typename WireType>
179 unsigned byte_size(
const WireType& wire_value)
180 {
return google::protobuf::internal::WireFormatLite::kFixed32Size; }
181 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
182 { google::protobuf::internal::WireFormatLite::WriteFixed32NoTagToArray(wire_value, &(*bytes)[0]); }
183 bool is_varint() {
return false; }
186 template<
typename WireType>
189 unsigned byte_size(
const WireType& wire_value)
190 {
return google::protobuf::internal::WireFormatLite::kSFixed64Size; }
191 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
192 { google::protobuf::internal::WireFormatLite::WriteSFixed64NoTagToArray(wire_value, &(*bytes)[0]); }
193 bool is_varint() {
return false; }
196 template<
typename WireType>
199 unsigned byte_size(
const WireType& wire_value)
200 {
return google::protobuf::internal::WireFormatLite::kSFixed32Size; }
201 void encode(WireType wire_value, std::vector<google::protobuf::uint8>* bytes)
202 { google::protobuf::internal::WireFormatLite::WriteSFixed32NoTagToArray(wire_value, &(*bytes)[0]); }
203 bool is_varint() {
return false; }
206 template<
typename WireType, google::protobuf::FieldDescriptor::Type DeclaredType,
typename FieldType = WireType>
210 unsigned presence_bit_size()
220 if(helper_.is_varint())
223 return BITS_IN_BYTE*helper_.byte_size(WireType());
228 return presence_bit_size();
236 return std::max<unsigned>(size(std::numeric_limits<WireType>::min()),
237 size(std::numeric_limits<WireType>::max()));
245 unsigned size(
const WireType& wire_value)
247 unsigned data_bytes = helper_.byte_size(wire_value);
248 unsigned size = presence_bit_size() + BITS_IN_BYTE*data_bytes;
255 return Bitset(min_size(), 0);
258 Bitset encode(
const WireType& wire_value)
260 std::vector<google::protobuf::uint8> bytes(size(wire_value) / BITS_IN_BYTE, 0);
262 helper_.encode(wire_value, &bytes);
268 data_bits.resize(data_bits.size() + presence_bit_size());
270 data_bits.
set(0,
true);
276 WireType decode(
Bitset* bits)
280 dccl::uint64 uint_value = (bits->template to<dccl::uint64>)();
284 if(helper_.is_varint())
287 bits->
get_more_bits(BITS_IN_BYTE*helper_.byte_size(WireType()));
290 if(helper_.is_varint())
293 while(bits->
test(bits->size()-1))
298 google::protobuf::io::CodedInputStream input_stream(
reinterpret_cast<const google::protobuf::uint8*
>(bytes.data()), bytes.size());
300 return helper_.decode(&input_stream);
311 int pre_encode(
const google::protobuf::EnumValueDescriptor*
const& field_value);
312 const google::protobuf::EnumValueDescriptor*
post_decode(
const int& wire_value);