• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HDC_SERIAL_STRUCT_DEFINE_H
16 #define HDC_SERIAL_STRUCT_DEFINE_H
17 #include <cmath>
18 #include <cstdint>
19 #include <cstring>
20 #include <map>
21 #include <optional>
22 #include <string>
23 #include <tuple>
24 #include <variant>
25 #include <vector>
26 
27 // static file define. No need not modify. by zako
28 namespace Hdc {
29 // clang-format off
30 namespace SerialStruct {
31     namespace SerialDetail {
32         template<class MemPtrT> struct MemPtr {
33         };
34         template<class T, class U> struct MemPtr<U T::*> {
35             using type = T;
36             using MemberType = U;
37         };
38         template<class... Fields> struct MessageImpl {
39         public:
40             MessageImpl(Fields &&... fields)
41                 : _fields(std::move(fields)...)
42             {
43             }
44 
45             template<class Handler> void Visit(Handler &&handler) const
46             {
47                 VisitImpl(std::forward<Handler>(handler), std::make_index_sequence<sizeof...(Fields)>());
48             }
49 
50         private:
51             std::tuple<Fields...> _fields;
52 
53             template<class Handler, size_t... I> void VisitImpl(Handler &&handler, std::index_sequence<I...>) const
54             {
55                 (handler(std::get<I>(_fields)), ...);
56             }
57         };
58 
59         template<uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t Flags> struct FieldImpl {
60             using type = typename SerialDetail::MemPtr<MemPtrT>::type;
61             using MemberType = typename SerialDetail::MemPtr<MemPtrT>::MemberType;
62             constexpr static const uint32_t tag = Tag;
63             constexpr static const uint32_t flags = Flags;
64             const std::string field_name;
65 
66             static decltype(auto) get(const type &value)
67             {
68                 return value.*MemPtr;
69             }
70 
71             static decltype(auto) get(type &value)
72             {
73                 return value.*MemPtr;
74             }
75         };
76 
77         template<uint32_t Tag, size_t Index, class MemPtrT, MemPtrT MemPtr, uint32_t Flags> struct OneofFieldImpl {
78             using type = typename SerialDetail::MemPtr<MemPtrT>::type;
79             using MemberType = typename SerialDetail::MemPtr<MemPtrT>::MemberType;
80             constexpr static const uint32_t tag = Tag;
81             constexpr static const size_t index = Index;
82             constexpr static const uint32_t flags = Flags;
83             const std::string field_name;
84 
85             static decltype(auto) get(const type &value)
86             {
87                 return value.*MemPtr;
88             }
89 
90             static decltype(auto) get(type &value)
91             {
92                 return value.*MemPtr;
93             }
94         };
95 
96         template<uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t KeyFlags, uint32_t ValueFlags>
97         struct MapFieldImpl {
98             using type = typename SerialDetail::MemPtr<MemPtrT>::type;
99             using MemberType = typename SerialDetail::MemPtr<MemPtrT>::MemberType;
100             constexpr static const uint32_t tag = Tag;
101             constexpr static const uint32_t KEY_FLAGS = KeyFlags;
102             constexpr static const uint32_t VALUE_FLAGS = ValueFlags;
103 
104             const std::string field_name;
105 
106             static decltype(auto) get(const type &value)
107             {
108                 return value.*MemPtr;
109             }
110 
111             static decltype(auto) get(type &value)
112             {
113                 return value.*MemPtr;
114             }
115         };
116     }
117 
118     enum class WireType : uint32_t {
119         VARINT = 0,
120         FIXED64 = 1,
121         LENGTH_DELIMETED = 2,
122         START_GROUP = 3,
123         END_GROUP = 4,
124         FIXED32 = 5,
125     };
126     enum flags { no = 0, s = 1, f = 2 };
127     template<uint32_t flags = flags::no> struct FlagsType {
128     };
129 
130     template<class T> struct Descriptor {
131         static_assert(sizeof(T) == 0, "You need to implement descriptor for your own types");
132         static void type()
133         {
134         }
135     };
136 
137     template<class... Fields> constexpr auto Message(Fields &&... fields)
138     {
139         return SerialDetail::MessageImpl<Fields...>(std::forward<Fields>(fields)...);
140     }
141 
142     template<uint32_t Tag, auto MemPtr, uint32_t Flags = flags::no> constexpr auto Field(const std::string &fieldName)
143     {
144         return SerialDetail::FieldImpl<Tag, decltype(MemPtr), MemPtr, Flags> { fieldName };
145     }
146 
147     template<uint32_t Tag, size_t Index, auto MemPtr, uint32_t Flags = flags::no>
148     constexpr auto OneofField(const std::string &fieldName)
149     {
150         return SerialDetail::OneofFieldImpl<Tag, Index, decltype(MemPtr), MemPtr, Flags> { fieldName };
151     }
152 
153     template<uint32_t Tag, auto MemPtr, uint32_t KeyFlags = flags::no, uint32_t ValueFlags = flags::no>
154     constexpr auto MapField(const std::string &fieldName)
155     {
156         return SerialDetail::MapFieldImpl<Tag, decltype(MemPtr), MemPtr, KeyFlags, ValueFlags> { fieldName };
157     }
158 
159     template<class T> const auto &MessageType()
160     {
161         static const auto message = Descriptor<T>::type();
162         return message;
163     }
164 
165     template<class T, class Enable = void> struct Serializer;
166 
167     struct Writer {
168         virtual void Write(const void *bytes, size_t size) = 0;
169     };
170 
171     struct reader {
172         virtual size_t Read(void *bytes, size_t size) = 0;
173     };
174 
175     namespace SerialDetail {
176         template<class T, class V, class F, class W, class Enable = void>
177         struct HasSerializePacked : public std::false_type {
178         };
179 
180         template<class T, class V, class F, class W>
181         struct HasSerializePacked<T, V, F, W,
182             std::void_t<decltype(std::declval<T>().SerializePacked(
183                 std::declval<V>(), std::declval<F>(), std::declval<W &>()))>> : public std::true_type {
184         };
185 
186         template<class T, class V, class F, class W>
187         constexpr bool HAS_SERIALIZE_PACKED_V = HasSerializePacked<T, V, F, W>::value;
188 
189         template<class T, class V, class F, class R, class Enable = void>
190         struct HasParsePacked : public std::false_type {
191         };
192 
193         template<class T, class V, class F, class R>
194         struct HasParsePacked<T, V, F, R,
195             std::void_t<decltype(std::declval<T>().ParsePacked(
196                 std::declval<V &>(), std::declval<F>(), std::declval<R &>()))>> : public std::true_type {
197         };
198 
199         template<class T, class V, class F, class R>
200         constexpr bool HAS_PARSE_PACKED_V = HasParsePacked<T, V, F, R>::value;
201 
202         static uint32_t MakeTagWireType(uint32_t tag, WireType wireType)
203         {
204             return (tag << 3) | static_cast<uint32_t>(wireType);
205         }
206 
207         static inline void ReadTagWireType(uint32_t tagKey, uint32_t &tag, WireType &wireType)
208         {
209             wireType = static_cast<WireType>(tagKey & 0b0111);
210             tag = tagKey >> 3;
211         }
212 
213         static uint32_t MakeZigzagValue(int32_t value)
214         {
215             return (static_cast<uint32_t>(value) << 1) ^ static_cast<uint32_t>(value >> 31);
216         }
217 
218         static uint64_t MakeZigzagValue(int64_t value)
219         {
220             return (static_cast<uint64_t>(value) << 1) ^ static_cast<uint64_t>(value >> 63);
221         }
222 
223         static int32_t ReadZigzagValue(uint32_t value)
224         {
225             return static_cast<int32_t>((value >> 1) ^ (~(value & 1) + 1));
226         }
227 
228         static int64_t ReadZigzagValue(uint64_t value)
229         {
230             return static_cast<int64_t>((value >> 1) ^ (~(value & 1) + 1));
231         }
232 
233         template<class To, class From> To BitCast(From from)
234         {
235             static_assert(sizeof(To) == sizeof(From), "");
236             static_assert(std::is_trivially_copyable_v<To>, "");
237             static_assert(std::is_trivially_copyable_v<From>, "");
238             To to;
239             memcpy_s(&to, sizeof(To), &from, sizeof(from));
240             return to;
241         }
242 
243         struct WriterSizeCollector : public Writer {
244             void Write(const void *, size_t size) override
245             {
246                 byte_size += size;
247             }
248             size_t byte_size = 0;
249         };
250 
251         struct LimitedReader : public reader {
252             LimitedReader(reader &parent, size_t sizeLimit)
253                 : _parent(parent), _size_limit(sizeLimit)
254             {
255             }
256 
257             size_t Read(void *bytes, size_t size)
258             {
259                 auto sizeToRead = std::min(size, _size_limit);
260                 auto readSize = _parent.Read(bytes, sizeToRead);
261                 _size_limit -= readSize;
262                 return readSize;
263             }
264 
265             size_t AvailableBytes() const
266             {
267                 return _size_limit;
268             }
269 
270         private:
271             reader &_parent;
272             size_t _size_limit;
273         };
274 
275         static bool ReadByte(uint8_t &value, reader &in)
276         {
277             return in.Read(&value, 1) == 1;
278         }
279 
280         static void WriteVarint(uint32_t value, Writer &out)
281         {
282             uint8_t b[5] {};
283             for (size_t i = 0; i < 5; ++i) {
284                 b[i] = value & 0b0111'1111;
285                 value >>= 7;
286                 if (value) {
287                     b[i] |= 0b1000'0000;
288                 } else {
289                     out.Write(b, i + 1);
290                     break;
291                 }
292             }
293         }
294 
295         static void WriteVarint(uint64_t value, Writer &out)
296         {
297             uint8_t b[10] {};
298             for (size_t i = 0; i < 10; ++i) {
299                 b[i] = value & 0b0111'1111;
300                 value >>= 7;
301                 if (value) {
302                     b[i] |= 0b1000'0000;
303                 } else {
304                     out.Write(b, i + 1);
305                     break;
306                 }
307             }
308         }
309 
310 #if defined(HOST_MAC)
311         static void WriteVarint(unsigned long value, Writer &out)
312         {
313             WriteVarint(static_cast<uint64_t>(value), out);
314         }
315 #endif
316 
317         static bool ReadVarint(uint32_t &value, reader &in)
318         {
319             value = 0;
320             for (size_t c = 0; c < 5; ++c) {
321                 uint8_t x;
322                 if (!ReadByte(x, in)) {
323                     return false;
324                 }
325                 value |= static_cast<uint32_t>(x & 0b0111'1111) << 7 * c;
326                 if (!(x & 0b1000'0000)) {
327                     return true;
328                 }
329             }
330 
331             return false;
332         }
333 
334         static bool ReadVarint(uint64_t &value, reader &in)
335         {
336             value = 0;
337             for (size_t c = 0; c < 10; ++c) {
338                 uint8_t x;
339                 if (!ReadByte(x, in)) {
340                     return false;
341                 }
342                 value |= static_cast<uint64_t>(x & 0b0111'1111) << 7 * c;
343                 if (!(x & 0b1000'0000)) {
344                     return true;
345                 }
346             }
347             return false;
348         }
349 
350 #if defined(HOST_MAC)
351         static bool ReadVarint(unsigned long &value, reader &in)
352         {
353             uint64_t intermediateValue;
354             if (ReadVarint(intermediateValue, in)) {
355                 value = static_cast<unsigned long>(intermediateValue);
356                 return true;
357             }
358             return false;
359         }
360 #endif
361 
362         static void WriteFixed(uint32_t value, Writer &out)
363         {
364 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
365             out.Write(&value, sizeof(value));
366 #else
367             static_assert(false, "Not a little-endian");
368 #endif
369         }
370 
371         static void WriteFixed(uint64_t value, Writer &out)
372         {
373 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
374             out.Write(&value, sizeof(value));
375 #else
376             static_assert(false, "Not a little-endian");
377 #endif
378         }
379 
380         static void WriteFixed(double value, Writer &out)
381         {
382             WriteFixed(BitCast<uint64_t>(value), out);
383         }
384 
385         static void WriteFixed(float value, Writer &out)
386         {
387             WriteFixed(BitCast<uint32_t>(value), out);
388         }
389 
390         static void WriteVarint(int32_t value, Writer &out)
391         {
392             WriteVarint(BitCast<uint32_t>(value), out);
393         }
394 
395         static void WriteVarint(int64_t value, Writer &out)
396         {
397             WriteVarint(BitCast<uint64_t>(value), out);
398         }
399 
400         static void WriteSignedVarint(int32_t value, Writer &out)
401         {
402             WriteVarint(MakeZigzagValue(value), out);
403         }
404 
405         static void WriteSignedVarint(int64_t value, Writer &out)
406         {
407             WriteVarint(MakeZigzagValue(value), out);
408         }
409 
410         static void WriteSignedFixed(int32_t value, Writer &out)
411         {
412             WriteFixed(static_cast<uint32_t>(value), out);
413         }
414 
415         static void WriteSignedFixed(int64_t value, Writer &out)
416         {
417             WriteFixed(static_cast<uint64_t>(value), out);
418         }
419 
420         static void WriteTagWriteType(uint32_t tag, WireType wireType, Writer &out)
421         {
422             WriteVarint(MakeTagWireType(tag, wireType), out);
423         }
424 
425         static bool ReadFixed(uint32_t &value, reader &in)
426         {
427 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
428             return in.Read(&value, sizeof(value)) == sizeof(value);
429 #else
430             static_assert(false, "Not a little-endian");
431 #endif
432         }
433 
434         static bool ReadFixed(uint64_t &value, reader &in)
435         {
436 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
437             return in.Read(&value, sizeof(value)) == sizeof(value);
438 #else
439             static_assert(false, "Not a little-endian");
440 #endif
441         }
442 
443         static bool ReadFixed(double &value, reader &in)
444         {
445             uint64_t intermediateValue;
446             if (ReadFixed(intermediateValue, in)) {
447                 value = BitCast<double>(intermediateValue);
448                 return true;
449             }
450             return false;
451         }
452 
453         static bool ReadFixed(float &value, reader &in)
454         {
455             uint32_t intermediateValue;
456             if (ReadFixed(intermediateValue, in)) {
457                 value = BitCast<float>(intermediateValue);
458                 return true;
459             }
460             return false;
461         }
462 
463         static bool ReadVarint(int32_t &value, reader &in)
464         {
465             uint32_t intermediateValue;
466             if (ReadVarint(intermediateValue, in)) {
467                 value = BitCast<int32_t>(intermediateValue);
468                 return true;
469             }
470             return false;
471         }
472 
473         static bool ReadVarint(int64_t &value, reader &in)
474         {
475             uint64_t intermediateValue;
476             if (ReadVarint(intermediateValue, in)) {
477                 value = BitCast<int64_t>(intermediateValue);
478                 return true;
479             }
480             return false;
481         }
482 
483         static bool ReadSignedVarint(int32_t &value, reader &in)
484         {
485             uint32_t intermediateValue;
486             if (ReadVarint(intermediateValue, in)) {
487                 value = ReadZigzagValue(intermediateValue);
488                 return true;
489             }
490             return false;
491         }
492 
493         static bool ReadSignedVarint(int64_t &value, reader &in)
494         {
495             uint64_t intermediateValue;
496             if (ReadVarint(intermediateValue, in)) {
497                 value = ReadZigzagValue(intermediateValue);
498                 return true;
499             }
500             return false;
501         }
502 
503         static bool ReadSignedFixed(int32_t &value, reader &in)
504         {
505             uint32_t intermediateValue;
506             if (ReadFixed(intermediateValue, in)) {
507                 value = static_cast<int64_t>(intermediateValue);
508                 return true;
509             }
510             return false;
511         }
512 
513         static bool ReadSignedFixed(int64_t &value, reader &in)
514         {
515             uint64_t intermediateValue;
516             if (ReadFixed(intermediateValue, in)) {
517                 value = static_cast<int64_t>(intermediateValue);
518                 return true;
519             }
520             return false;
521         }
522 
523         template<class T, uint32_t Tag, size_t Index, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
524         void WriteField(const T &value,
525             const SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags> &, Writer &out)
526         {
527             using OneOf = SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags>;
528             Serializer<typename OneOf::MemberType>::template SerializeOneof<OneOf::index>(
529                 OneOf::tag, OneOf::get(value), FlagsType<OneOf::flags>(), out);
530         }
531 
532         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t KeyFlags, uint32_t ValueFlags>
533         void WriteField(const T &value,
534             const SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags> &, Writer &out)
535         {
536             using Map = SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags>;
537             Serializer<typename Map::MemberType>::SerializeMap(
538                 Map::tag, Map::get(value), FlagsType<Map::KEY_FLAGS>(), FlagsType<Map::VALUE_FLAGS>(), out);
539         }
540 
541         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
542         void WriteField(const T &value, const SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags> &, Writer &out)
543         {
544             using Field = SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags>;
545             Serializer<typename Field::MemberType>::Serialize(
546                 Field::tag, Field::get(value), FlagsType<Field::flags>(), out);
547         }
548 
549         template<class T, class... Field>
550         void WriteMessage(const T &value, const SerialDetail::MessageImpl<Field...> &message, Writer &out)
551         {
552             message.Visit([&](const auto &field) { WriteField(value, field, out); });
553         }
554 
555         template<uint32_t Flags, class ValueType, class It>
556         void WriteRepeated(uint32_t tag, It begin, It end, Writer &out)
557         {
558             if (begin == end) {
559                 return;
560             }
561             if constexpr (SerialDetail::HAS_SERIALIZE_PACKED_V<Serializer<ValueType>, ValueType, FlagsType<Flags>,
562                 Writer>) {
563                 WriteVarint(MakeTagWireType(tag, WireType::LENGTH_DELIMETED), out);
564                 WriterSizeCollector sizeCollector;
565                 for (auto it = begin; it != end; ++it) {
566                     Serializer<ValueType>::SerializePacked(*it, FlagsType<Flags> {}, sizeCollector);
567                 }
568                 WriteVarint(sizeCollector.byte_size, out);
569                 for (auto it = begin; it != end; ++it) {
570                     Serializer<ValueType>::SerializePacked(*it, FlagsType<Flags> {}, out);
571                 }
572             } else {
573                 for (auto it = begin; it != end; ++it) {
574                     Serializer<ValueType>::Serialize(tag, *it, FlagsType<Flags>(), out);
575                 }
576             }
577         }
578 
579         template<uint32_t KeyFlags, uint32_t ValueFlags, class Key, class Value>
580         void WriteMapKeyValue(const std::pair<const Key, Value> &value, Writer &out)
581         {
582             Serializer<Key>::Serialize(1, value.first, FlagsType<KeyFlags> {}, out, true);
583             Serializer<Value>::Serialize(2, value.second, FlagsType<ValueFlags> {}, out, true);
584         }
585 
586         template<uint32_t KeyFlags, uint32_t ValueFlags, class T>
587         void WriteMap(uint32_t tag, const T &value, Writer &out)
588         {
589             auto begin = std::begin(value);
590             auto end = std::end(value);
591 
592             for (auto it = begin; it != end; ++it) {
593                 WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
594                 WriterSizeCollector sizeCollector;
595                 WriteMapKeyValue<KeyFlags, ValueFlags>(*it, sizeCollector);
596                 WriteVarint(sizeCollector.byte_size, out);
597                 WriteMapKeyValue<KeyFlags, ValueFlags>(*it, out);
598             }
599         }
600 
601         template<uint32_t KeyFlags, uint32_t ValueFlags, class Key, class Value>
602         bool ReadMapKeyValue(std::pair<Key, Value> &value, reader &in)
603         {
604             static const auto pairAsMessage = Message(Field<1, &std::pair<Key, Value>::first, KeyFlags>("key"),
605                 Field<2, &std::pair<Key, Value>::second, ValueFlags>("value"));
606             return ReadMessage(value, pairAsMessage, in);
607         }
608 
609         template<uint32_t KeyFlags, uint32_t ValueFlags, class T>
610         bool ReadMap(WireType wireType, T &value, reader &in)
611         {
612             if (wireType != WireType::LENGTH_DELIMETED) {
613                 return false;
614             }
615             size_t size;
616             if (ReadVarint(size, in)) {
617                 LimitedReader limitedIn(in, size);
618                 while (limitedIn.AvailableBytes() > 0) {
619                     std::pair<typename T::key_type, typename T::mapped_type> item;
620                     if (!ReadMapKeyValue<KeyFlags, ValueFlags>(item, limitedIn)) {
621                         return false;
622                     }
623                     value.insert(std::move(item));
624                 }
625                 return true;
626             }
627             return false;
628         }
629 
630         template<uint32_t Flags, class ValueType, class OutputIt>
631         bool ReadRepeated(WireType wireType, OutputIt output_it, reader &in)
632         {
633             if constexpr (SerialDetail::HAS_PARSE_PACKED_V<Serializer<ValueType>, ValueType, FlagsType<Flags>,
634                 reader>) {
635                 if (wireType != WireType::LENGTH_DELIMETED) {
636                     return false;
637                 }
638 
639                 size_t size;
640                 if (ReadVarint(size, in)) {
641                     LimitedReader limitedIn(in, size);
642 
643                     while (limitedIn.AvailableBytes() > 0) {
644                         ValueType value;
645                         if (!Serializer<ValueType>::ParsePacked(value, FlagsType<Flags>(), limitedIn)) {
646                             return false;
647                         }
648                         output_it = value;
649                         ++output_it;
650                     }
651                     return true;
652                 }
653                 return false;
654             } else {
655                 ValueType value;
656                 if (Serializer<ValueType>::Parse(wireType, value, FlagsType<Flags>(), in)) {
657                     output_it = value;
658                     ++output_it;
659                     return true;
660                 }
661                 return false;
662             }
663         }
664 
665         template<class T, uint32_t Tag, size_t Index, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
666         void ReadField(T &value, uint32_t tag, WireType wireType,
667             const SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags> &, reader &in)
668         {
669             if (Tag != tag) {
670                 return;
671             }
672             using OneOf = SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags>;
673             Serializer<typename OneOf::MemberType>::template ParseOneof<OneOf::index>(
674                 wireType, OneOf::get(value), FlagsType<OneOf::flags>(), in);
675         }
676 
677         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t KeyFlags, uint32_t ValueFlags>
678         void ReadField(T &value, uint32_t tag, WireType wireType,
679             const SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags> &, reader &in)
680         {
681             if (Tag != tag) {
682                 return;
683             }
684             using Map = SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags>;
685             Serializer<typename Map::MemberType>::ParseMap(
686                 wireType, Map::get(value), FlagsType<Map::KEY_FLAGS>(), FlagsType<Map::VALUE_FLAGS>(), in);
687         }
688 
689         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
690         void ReadField(T &value, uint32_t tag, WireType wireType,
691             const SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags> &, reader &in)
692         {
693             if (Tag != tag) {
694                 return;
695             }
696             using Field = SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags>;
697             Serializer<typename Field::MemberType>::Parse(wireType, Field::get(value), FlagsType<Field::flags>(), in);
698         }
699 
700         template<class T, class... Field> bool ReadMessage(T &value, const MessageImpl<Field...> &message, reader &in)
701         {
702             uint32_t tagKey;
703             while (ReadVarint(tagKey, in)) {
704                 uint32_t tag;
705                 WireType wireType;
706                 ReadTagWireType(tagKey, tag, wireType);
707                 message.Visit([&](const auto &field) { ReadField(value, tag, wireType, field, in); });
708             }
709             return true;
710         }
711     }
712 
713     template<class T, class Enable> struct Serializer {
714         // Commion Serializer threat type as Message
715         static void Serialize(uint32_t tag, const T &value, FlagsType<>, Writer &out, bool force = false)
716         {
717             SerialDetail::WriterSizeCollector sizeCollector;
718             SerialDetail::WriteMessage(value, MessageType<T>(), sizeCollector);
719             if (!force && sizeCollector.byte_size == 0) {
720                 return;
721             }
722             SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
723             SerialDetail::WriteVarint(sizeCollector.byte_size, out);
724             SerialDetail::WriteMessage(value, MessageType<T>(), out);
725         }
726 
727         static bool Parse(WireType wireType, T &value, FlagsType<>, reader &in)
728         {
729             if (wireType != WireType::LENGTH_DELIMETED) {
730                 return false;
731             }
732             size_t size;
733             if (SerialDetail::ReadVarint(size, in)) {
734                 SerialDetail::LimitedReader limitedIn(in, size);
735                 return SerialDetail::ReadMessage(value, MessageType<T>(), limitedIn);
736             }
737             return false;
738         }
739     };
740 
741     template<> struct Serializer<int32_t> {
742         static void Serialize(uint32_t tag, int32_t value, FlagsType<>, Writer &out, bool force = false)
743         {
744             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
745             SerialDetail::WriteVarint(value, out);
746         }
747 
748         static void Serialize(uint32_t tag, int32_t value, FlagsType<flags::s>, Writer &out, bool force = false)
749         {
750             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
751             SerialDetail::WriteSignedVarint(value, out);
752         }
753 
754         static void Serialize(
755             uint32_t tag, int32_t value, FlagsType<flags::s | flags::f>, Writer &out, bool force = false)
756         {
757             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
758             SerialDetail::WriteSignedFixed(value, out);
759         }
760 
761         static void SerializePacked(int32_t value, FlagsType<>, Writer &out)
762         {
763             SerialDetail::WriteVarint(value, out);
764         }
765 
766         static void SerializePacked(int32_t value, FlagsType<flags::s>, Writer &out)
767         {
768             SerialDetail::WriteSignedVarint(value, out);
769         }
770 
771         static void SerializePacked(int32_t value, FlagsType<flags::s | flags::f>, Writer &out)
772         {
773             SerialDetail::WriteSignedFixed(value, out);
774         }
775 
776         static bool Parse(WireType wire_type, int32_t &value, FlagsType<>, reader &in)
777         {
778             if (wire_type != WireType::VARINT)
779                 return false;
780             return SerialDetail::ReadVarint(value, in);
781         }
782 
783         static bool Parse(WireType wire_type, int32_t &value, FlagsType<flags::s>, reader &in)
784         {
785             if (wire_type != WireType::VARINT)
786                 return false;
787             return SerialDetail::ReadSignedVarint(value, in);
788         }
789 
790         static bool Parse(WireType wire_type, int32_t &value, FlagsType<flags::s | flags::f>, reader &in)
791         {
792             if (wire_type != WireType::FIXED32)
793                 return false;
794             return SerialDetail::ReadSignedFixed(value, in);
795         }
796 
797         static bool ParsePacked(int32_t &value, FlagsType<>, reader &in)
798         {
799             return SerialDetail::ReadVarint(value, in);
800         }
801 
802         static bool ParsePacked(int32_t &value, FlagsType<flags::s>, reader &in)
803         {
804             return SerialDetail::ReadSignedVarint(value, in);
805         }
806 
807         static bool ParsePacked(int32_t &value, FlagsType<flags::s | flags::f>, reader &in)
808         {
809             return SerialDetail::ReadSignedFixed(value, in);
810         }
811     };
812 
813     template<> struct Serializer<uint32_t> {
814         static void Serialize(uint32_t tag, uint32_t value, FlagsType<>, Writer &out, bool force = false)
815         {
816             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
817             SerialDetail::WriteVarint(value, out);
818         }
819 
820         static void Serialize(uint32_t tag, uint32_t value, FlagsType<flags::f>, Writer &out, bool force = false)
821         {
822             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
823             SerialDetail::WriteFixed(value, out);
824         }
825 
826         static void SerializePacked(uint32_t value, FlagsType<>, Writer &out)
827         {
828             SerialDetail::WriteVarint(value, out);
829         }
830 
831         static void SerializePacked(uint32_t value, FlagsType<flags::f>, Writer &out)
832         {
833             SerialDetail::WriteFixed(value, out);
834         }
835 
836         static bool Parse(WireType wire_type, uint32_t &value, FlagsType<>, reader &in)
837         {
838             if (wire_type != WireType::VARINT)
839                 return false;
840             return SerialDetail::ReadVarint(value, in);
841         }
842 
843         static bool Parse(WireType wire_type, uint32_t &value, FlagsType<flags::f>, reader &in)
844         {
845             if (wire_type != WireType::FIXED32)
846                 return false;
847             return SerialDetail::ReadFixed(value, in);
848         }
849 
850         static bool ParsePacked(uint32_t &value, FlagsType<>, reader &in)
851         {
852             return SerialDetail::ReadVarint(value, in);
853         }
854 
855         static bool ParsePacked(uint32_t &value, FlagsType<flags::f>, reader &in)
856         {
857             return SerialDetail::ReadFixed(value, in);
858         }
859     };
860 
861     template<> struct Serializer<int64_t> {
862         static void Serialize(uint32_t tag, int64_t value, FlagsType<>, Writer &out, bool force = false)
863         {
864             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
865             SerialDetail::WriteVarint(value, out);
866         }
867 
868         static void Serialize(uint32_t tag, int64_t value, FlagsType<flags::s>, Writer &out, bool force = false)
869         {
870             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
871             SerialDetail::WriteSignedVarint(value, out);
872         }
873 
874         static void Serialize(
875             uint32_t tag, int64_t value, FlagsType<flags::s | flags::f>, Writer &out, bool force = false)
876         {
877             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
878             SerialDetail::WriteSignedFixed(value, out);
879         }
880 
881         static void SerializePacked(int64_t value, FlagsType<>, Writer &out)
882         {
883             SerialDetail::WriteVarint(value, out);
884         }
885 
886         static void SerializePacked(int64_t value, FlagsType<flags::s>, Writer &out)
887         {
888             SerialDetail::WriteSignedVarint(value, out);
889         }
890 
891         static void SerializePacked(int64_t value, FlagsType<flags::s | flags::f>, Writer &out)
892         {
893             SerialDetail::WriteSignedFixed(value, out);
894         }
895 
896         static bool Parse(WireType wire_type, int64_t &value, FlagsType<>, reader &in)
897         {
898             if (wire_type != WireType::VARINT)
899                 return false;
900             return SerialDetail::ReadVarint(value, in);
901         }
902 
903         static bool Parse(WireType wire_type, int64_t &value, FlagsType<flags::s>, reader &in)
904         {
905             if (wire_type != WireType::VARINT)
906                 return false;
907             return SerialDetail::ReadSignedVarint(value, in);
908         }
909 
910         static bool Parse(WireType wire_type, int64_t &value, FlagsType<flags::s | flags::f>, reader &in)
911         {
912             if (wire_type != WireType::FIXED64)
913                 return false;
914             return SerialDetail::ReadSignedFixed(value, in);
915         }
916 
917         static bool ParsePacked(int64_t &value, FlagsType<>, reader &in)
918         {
919             return SerialDetail::ReadVarint(value, in);
920         }
921 
922         static bool ParsePacked(int64_t &value, FlagsType<flags::s>, reader &in)
923         {
924             return SerialDetail::ReadSignedVarint(value, in);
925         }
926 
927         static bool ParsePacked(int64_t &value, FlagsType<flags::s | flags::f>, reader &in)
928         {
929             return SerialDetail::ReadSignedFixed(value, in);
930         }
931     };
932 
933     template<> struct Serializer<uint64_t> {
934         static void Serialize(uint32_t tag, uint64_t value, FlagsType<>, Writer &out, bool force = false)
935         {
936             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
937             SerialDetail::WriteVarint(value, out);
938         }
939 
940         static void Serialize(uint32_t tag, uint64_t value, FlagsType<flags::f>, Writer &out, bool force = false)
941         {
942             if (!force && value == UINT64_C(0))
943                 return;
944 
945             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
946             SerialDetail::WriteFixed(value, out);
947         }
948 
949         static void SerializePacked(uint64_t value, FlagsType<>, Writer &out)
950         {
951             SerialDetail::WriteVarint(value, out);
952         }
953 
954         static void SerializePacked(uint64_t value, FlagsType<flags::f>, Writer &out)
955         {
956             SerialDetail::WriteFixed(value, out);
957         }
958 
959         static bool Parse(WireType wire_type, uint64_t &value, FlagsType<>, reader &in)
960         {
961             if (wire_type != WireType::VARINT)
962                 return false;
963             return SerialDetail::ReadVarint(value, in);
964         }
965 
966         static bool Parse(WireType wire_type, uint64_t &value, FlagsType<flags::f>, reader &in)
967         {
968             if (wire_type != WireType::FIXED64)
969                 return false;
970             return SerialDetail::ReadFixed(value, in);
971         }
972 
973         static bool ParsePacked(uint64_t &value, FlagsType<>, reader &in)
974         {
975             return SerialDetail::ReadVarint(value, in);
976         }
977 
978         static bool ParsePacked(uint64_t &value, FlagsType<flags::f>, reader &in)
979         {
980             return SerialDetail::ReadFixed(value, in);
981         }
982     };
983 
984     template<> struct Serializer<double> {
985         static void Serialize(uint32_t tag, double value, FlagsType<>, Writer &out, bool force = false)
986         {
987             if (!force && std::fpclassify(value) == FP_ZERO) {
988                 return;
989             }
990             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
991             SerialDetail::WriteFixed(value, out);
992         }
993 
994         static void SerializePacked(double value, FlagsType<>, Writer &out)
995         {
996             SerialDetail::WriteFixed(value, out);
997         }
998 
999         static bool Parse(WireType wire_type, double &value, FlagsType<>, reader &in)
1000         {
1001             if (wire_type != WireType::FIXED64) {
1002                 return false;
1003             }
1004             return SerialDetail::ReadFixed(value, in);
1005         }
1006 
1007         static bool ParsePacked(double &value, FlagsType<>, reader &in)
1008         {
1009             return SerialDetail::ReadFixed(value, in);
1010         }
1011     };
1012 
1013     template<> struct Serializer<float> {
1014         static void Serialize(uint32_t tag, float value, FlagsType<>, Writer &out, bool force = false)
1015         {
1016             if (!force && std::fpclassify(value) == FP_ZERO) {
1017                 return;
1018             }
1019             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
1020             SerialDetail::WriteFixed(value, out);
1021         }
1022 
1023         static void SerializePacked(float value, FlagsType<>, Writer &out)
1024         {
1025             SerialDetail::WriteFixed(value, out);
1026         }
1027 
1028         static bool Parse(WireType wire_type, float &value, FlagsType<>, reader &in)
1029         {
1030             if (wire_type != WireType::FIXED32) {
1031                 return false;
1032             }
1033             return SerialDetail::ReadFixed(value, in);
1034         }
1035 
1036         static bool ParsePacked(float &value, FlagsType<>, reader &in)
1037         {
1038             return SerialDetail::ReadFixed(value, in);
1039         }
1040     };
1041 
1042     template<> struct Serializer<bool> {
1043         static void Serialize(uint32_t tag, bool value, FlagsType<>, Writer &out, bool force = false)
1044         {
1045             Serializer<uint32_t>::Serialize(tag, value ? 1 : 0, FlagsType(), out, force);
1046         }
1047 
1048         static void SerializePacked(bool value, FlagsType<>, Writer &out)
1049         {
1050             Serializer<uint32_t>::SerializePacked(value ? 1 : 0, FlagsType(), out);
1051         }
1052 
1053         static bool Parse(WireType wire_type, bool &value, FlagsType<>, reader &in)
1054         {
1055             uint32_t intermedaite_value;
1056             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1057                 value = static_cast<bool>(intermedaite_value);
1058                 return true;
1059             }
1060             return false;
1061         }
1062 
1063         static bool ParsePacked(bool &value, FlagsType<>, reader &in)
1064         {
1065             uint32_t intermedaite_value;
1066             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1067                 value = static_cast<bool>(intermedaite_value);
1068                 return true;
1069             }
1070             return false;
1071         }
1072     };
1073 
1074     template<class T> struct Serializer<T, std::enable_if_t<std::is_enum_v<T>>> {
1075         using U = std::underlying_type_t<T>;
1076 
1077         static void Serialize(uint32_t tag, T value, FlagsType<>, Writer &out, bool force = false)
1078         {
1079             Serializer<U>::Serialize(tag, static_cast<U>(value), FlagsType<>(), out, force);
1080         }
1081 
1082         static void SerializePacked(T value, FlagsType<>, Writer &out)
1083         {
1084             Serializer<U>::SerializePacked(static_cast<U>(value), FlagsType<>(), out);
1085         }
1086 
1087         static bool Parse(WireType wire_type, T &value, FlagsType<>, reader &in)
1088         {
1089             U intermedaite_value;
1090             if (Serializer<U>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1091                 value = static_cast<T>(intermedaite_value);
1092                 return true;
1093             }
1094             return false;
1095         }
1096 
1097         static bool ParsePacked(T &value, FlagsType<>, reader &in)
1098         {
1099             U intermedaite_value;
1100             if (Serializer<U>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1101                 value = static_cast<T>(intermedaite_value);
1102                 return true;
1103             }
1104             return false;
1105         }
1106     };
1107 
1108     template<> struct Serializer<std::string> {
1109         static void Serialize(uint32_t tag, const std::string &value, FlagsType<>, Writer &out, bool force = false)
1110         {
1111             SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
1112             SerialDetail::WriteVarint(value.size(), out);
1113             out.Write(value.data(), value.size());
1114         }
1115 
1116         static bool Parse(WireType wire_type, std::string &value, FlagsType<>, reader &in)
1117         {
1118             if (wire_type != WireType::LENGTH_DELIMETED) {
1119                 return false;
1120             }
1121             size_t size;
1122             if (SerialDetail::ReadVarint(size, in)) {
1123                 value.resize(size);
1124                 if (in.Read(value.data(), size) == size) {
1125                     return true;
1126                 }
1127             }
1128             return false;
1129         }
1130     };
1131 
1132     template<class T> struct Serializer<std::vector<T>> {
1133         template<uint32_t Flags>
1134         static void Serialize(uint32_t tag, const std::vector<T> &value, FlagsType<Flags>, Writer &out)
1135         {
1136             SerialDetail::WriteRepeated<Flags, T>(tag, value.begin(), value.end(), out);
1137         }
1138 
1139         template<uint32_t Flags>
1140         static bool Parse(WireType wire_type, std::vector<T> &value, FlagsType<Flags>, reader &in)
1141         {
1142             return SerialDetail::ReadRepeated<Flags, T>(wire_type, std::back_inserter(value), in);
1143         }
1144     };
1145 
1146     template<class T> struct Serializer<std::optional<T>> {
1147         template<uint32_t Flags>
1148         static void Serialize(uint32_t tag, const std::optional<T> &value, FlagsType<Flags>, Writer &out)
1149         {
1150             if (!value.has_value()) {
1151                 return;
1152             }
1153             Serializer<T>::Serialize(tag, *value, FlagsType<Flags>(), out);
1154         }
1155 
1156         template<uint32_t Flags>
1157         static bool Parse(WireType wire_type, std::optional<T> &value, FlagsType<Flags>, reader &in)
1158         {
1159             return Serializer<T>::Parse(wire_type, value.emplace(), FlagsType<Flags>(), in);
1160         }
1161     };
1162 
1163     template<class... T> struct Serializer<std::variant<T...>> {
1164         template<size_t Index, uint32_t Flags>
1165         static void SerializeOneof(uint32_t tag, const std::variant<T...> &value, FlagsType<Flags>, Writer &out)
1166         {
1167             if (value.index() != Index)
1168                 return;
1169 
1170             Serializer<std::variant_alternative_t<Index, std::variant<T...>>>::Serialize(
1171                 tag, std::get<Index>(value), FlagsType<Flags>(), out);
1172         }
1173 
1174         template<size_t Index, uint32_t Flags>
1175         static bool ParseOneof(WireType wire_type, std::variant<T...> &value, FlagsType<Flags>, reader &in)
1176         {
1177             return Serializer<std::variant_alternative_t<Index, std::variant<T...>>>::Parse(
1178                 wire_type, value.template emplace<Index>(), FlagsType<Flags>(), in);
1179         }
1180     };
1181 
1182     template<class Key, class Value> struct Serializer<std::map<Key, Value>> {
1183         template<uint32_t KeyFlags, uint32_t ValueFlags>
1184         static void SerializeMap(
1185             uint32_t tag, const std::map<Key, Value> &value, FlagsType<KeyFlags>, FlagsType<ValueFlags>, Writer &out)
1186         {
1187             SerialDetail::WriteMap<KeyFlags, ValueFlags>(tag, value, out);
1188         }
1189 
1190         template<uint32_t KeyFlags, uint32_t ValueFlags>
1191         static bool ParseMap(
1192             WireType wire_type, std::map<Key, Value> &value, FlagsType<KeyFlags>, FlagsType<ValueFlags>, reader &in)
1193         {
1194             return SerialDetail::ReadMap<KeyFlags, ValueFlags>(wire_type, value, in);
1195         }
1196     };
1197 
1198     struct StringWriter : public Writer {
1199         StringWriter(std::string &out)
1200             : _out(out)
1201         {
1202         }
1203 
1204         void Write(const void *bytes, size_t size) override
1205         {
1206             _out.append(reinterpret_cast<const char *>(bytes), size);
1207         }
1208 
1209     private:
1210         std::string &_out;
1211     };
1212 
1213     struct StringReader : public reader {
1214         StringReader(const std::string &in)
1215             : _in(in), _pos(0)
1216         {
1217         }
1218 
1219         size_t Read(void *bytes, size_t size) override
1220         {
1221             size_t readSize = std::min(size, _in.size() - _pos);
1222             if (memcpy_s(bytes, size, _in.data() + _pos, readSize) != EOK) {
1223                 return readSize;
1224             }
1225             _pos += readSize;
1226             return readSize;
1227         }
1228 
1229     private:
1230         const std::string &_in;
1231         size_t _pos;
1232     };
1233     // mytype begin, just support base type, but really use protobuf raw type(uint32)
1234     template<> struct Serializer<uint8_t> {
1235         static void Serialize(uint32_t tag, uint8_t value, FlagsType<>, Writer &out, bool force = false)
1236         {
1237             Serializer<uint32_t>::Serialize(tag, value, FlagsType(), out, force);
1238         }
1239 
1240         static void SerializePacked(uint8_t value, FlagsType<>, Writer &out)
1241         {
1242             Serializer<uint32_t>::SerializePacked(value, FlagsType(), out);
1243         }
1244 
1245         static bool Parse(WireType wire_type, uint8_t &value, FlagsType<>, reader &in)
1246         {
1247             uint32_t intermedaite_value;
1248             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1249                 value = static_cast<uint8_t>(intermedaite_value);
1250                 return true;
1251             }
1252             return false;
1253         }
1254 
1255         static bool ParsePacked(uint8_t &value, FlagsType<>, reader &in)
1256         {
1257             uint32_t intermedaite_value;
1258             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1259                 value = static_cast<uint8_t>(intermedaite_value);
1260                 return true;
1261             }
1262             return false;
1263         }
1264     };
1265     template<> struct Serializer<uint16_t> {
1266         static void Serialize(uint32_t tag, uint16_t value, FlagsType<>, Writer &out, bool force = false)
1267         {
1268             Serializer<uint32_t>::Serialize(tag, value, FlagsType(), out, force);
1269         }
1270 
1271         static void SerializePacked(uint16_t value, FlagsType<>, Writer &out)
1272         {
1273             Serializer<uint32_t>::SerializePacked(value, FlagsType(), out);
1274         }
1275 
1276         static bool Parse(WireType wire_type, uint16_t &value, FlagsType<>, reader &in)
1277         {
1278             uint32_t intermedaite_value;
1279             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1280                 value = static_cast<uint16_t>(intermedaite_value);
1281                 return true;
1282             }
1283             return false;
1284         }
1285 
1286         static bool ParsePacked(uint16_t &value, FlagsType<>, reader &in)
1287         {
1288             uint32_t intermedaite_value;
1289             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1290                 value = static_cast<uint16_t>(intermedaite_value);
1291                 return true;
1292             }
1293             return false;
1294         }
1295     };
1296     // mytype finish
1297 
1298     template<class T> std::string SerializeToString(const T &value)
1299     {
1300         std::string out;
1301         StringWriter stringOut(out);
1302         SerialDetail::WriteMessage(value, MessageType<T>(), stringOut);
1303         return out;
1304     }
1305 
1306     template<class T> bool ParseFromString(T &value, const std::string &in)
1307     {
1308         StringReader stringIn(in);
1309         return SerialDetail::ReadMessage(value, MessageType<T>(), stringIn);
1310     }
1311 }
1312 // clang-format on
1313 }  // Hdc
1314 #endif  // HDC_SERIAL_STRUCT_DEFINE_H
1315