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:
MessageImplHdc::SerialStruct::SerialDetail::MessageImpl40             MessageImpl(Fields &&... fields)
41                 : _fields(std::move(fields)...)
42             {
43             }
44 
VisitHdc::SerialStruct::SerialDetail::MessageImpl45             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 
VisitImplHdc::SerialStruct::SerialDetail::MessageImpl53             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 
getHdc::SerialStruct::SerialDetail::FieldImpl66             static decltype(auto) get(const type &value)
67             {
68                 return value.*MemPtr;
69             }
70 
getHdc::SerialStruct::SerialDetail::FieldImpl71             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 
getHdc::SerialStruct::SerialDetail::OneofFieldImpl85             static decltype(auto) get(const type &value)
86             {
87                 return value.*MemPtr;
88             }
89 
getHdc::SerialStruct::SerialDetail::OneofFieldImpl90             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 
getHdc::SerialStruct::SerialDetail::MapFieldImpl106             static decltype(auto) get(const type &value)
107             {
108                 return value.*MemPtr;
109             }
110 
getHdc::SerialStruct::SerialDetail::MapFieldImpl111             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");
typeHdc::SerialStruct::Descriptor132         static void type()
133         {
134         }
135     };
136 
Message(Fields &&.... fields)137     template<class... Fields> constexpr auto Message(Fields &&... fields)
138     {
139         return SerialDetail::MessageImpl<Fields...>(std::forward<Fields>(fields)...);
140     }
141 
Field(const std::string &fieldName)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>
OneofField(const std::string &fieldName)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>
MapField(const std::string &fieldName)154     constexpr auto MapField(const std::string &fieldName)
155     {
156         return SerialDetail::MapFieldImpl<Tag, decltype(MemPtr), MemPtr, KeyFlags, ValueFlags> { fieldName };
157     }
158 
MessageType()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 
MakeTagWireType(uint32_t tag, WireType wireType)202         static uint32_t MakeTagWireType(uint32_t tag, WireType wireType)
203         {
204             return (tag << 3) | static_cast<uint32_t>(wireType);  // 3:tag type offset
205         }
206 
ReadTagWireType(uint32_t tagKey, uint32_t &tag, WireType &wireType)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;  // 3:tag type offset
211         }
212 
MakeZigzagValue(int32_t value)213         static uint32_t MakeZigzagValue(int32_t value)
214         {
215             return (static_cast<uint32_t>(value) << 1) ^ static_cast<uint32_t>(value >> 31);  //31:uint32_t val offset
216         }
217 
MakeZigzagValue(int64_t value)218         static uint64_t MakeZigzagValue(int64_t value)
219         {
220             return (static_cast<uint64_t>(value) << 1) ^ static_cast<uint64_t>(value >> 63);  //63:uint64_t val offset
221         }
222 
ReadZigzagValue(uint32_t value)223         static int32_t ReadZigzagValue(uint32_t value)
224         {
225             return static_cast<int32_t>((value >> 1) ^ (~(value & 1) + 1));
226         }
227 
ReadZigzagValue(uint64_t value)228         static int64_t ReadZigzagValue(uint64_t value)
229         {
230             return static_cast<int64_t>((value >> 1) ^ (~(value & 1) + 1));
231         }
232 
BitCast(From from)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 {
LimitedReaderHdc::SerialStruct::SerialDetail::LimitedReader252             LimitedReader(reader &parent, size_t sizeLimit)
253                 : _parent(parent), _size_limit(sizeLimit)
254             {
255             }
256 
ReadHdc::SerialStruct::SerialDetail::LimitedReader257             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 
AvailableBytesHdc::SerialStruct::SerialDetail::LimitedReader265             size_t AvailableBytes() const
266             {
267                 return _size_limit;
268             }
269 
270         private:
271             reader &_parent;
272             size_t _size_limit;
273         };
274 
ReadByte(uint8_t &value, reader &in)275         static bool ReadByte(uint8_t &value, reader &in)
276         {
277             return in.Read(&value, 1) == 1;
278         }
279 
WriteVarint(uint32_t value, Writer &out)280         static void WriteVarint(uint32_t value, Writer &out)
281         {
282             constexpr uint8_t bytesSize = 5;
283             constexpr uint8_t bytesOffset = 7;
284             uint8_t b[bytesSize] {};
285             for (size_t i = 0; i < bytesSize; ++i) {
286                 b[i] = value & 0b0111'1111;
287                 value >>= bytesOffset;
288                 if (value) {
289                     b[i] |= 0b1000'0000;
290                 } else {
291                     out.Write(b, i + 1);
292                     break;
293                 }
294             }
295         }
296 
297         static void WriteVarint(uint64_t value, Writer &out)
298         {
299             constexpr uint8_t bytesSize = 10;
300             constexpr uint8_t bytesOffset = 7;
301             uint8_t b[bytesSize] {};
302             for (size_t i = 0; i < bytesSize; ++i) {
303                 b[i] = value & 0b0111'1111;
304                 value >>= bytesOffset;
305                 if (value) {
306                     b[i] |= 0b1000'0000;
307                 } else {
308                     out.Write(b, i + 1);
309                     break;
310                 }
311             }
312         }
313 
314 #if defined(HOST_MAC)
315         static void WriteVarint(unsigned long value, Writer &out)
316         {
317             WriteVarint(static_cast<uint64_t>(value), out);
318         }
319 #endif
320 
321         static bool ReadVarint(uint32_t &value, reader &in)
322         {
323             constexpr uint8_t bytesSize = 5;
324             constexpr uint8_t bytesOffset = 7;
325             value = 0;
326             for (size_t c = 0; c < bytesSize; ++c) {
327                 uint8_t x;
328                 if (!ReadByte(x, in)) {
329                     return false;
330                 }
331                 value |= static_cast<uint32_t>(x & 0b0111'1111) << bytesOffset * c;
332                 if (!(x & 0b1000'0000)) {
333                     return true;
334                 }
335             }
336 
337             return false;
338         }
339 
ReadVarint(uint64_t &value, reader &in)340         static bool ReadVarint(uint64_t &value, reader &in)
341         {
342             constexpr uint8_t bytesSize = 10;
343             constexpr uint8_t bytesOffset = 7;
344             value = 0;
345             for (size_t c = 0; c < bytesSize; ++c) {
346                 uint8_t x;
347                 if (!ReadByte(x, in)) {
348                     return false;
349                 }
350                 value |= static_cast<uint64_t>(x & 0b0111'1111) << bytesOffset * c;
351                 if (!(x & 0b1000'0000)) {
352                     return true;
353                 }
354             }
355             return false;
356         }
357 
358 #if defined(HOST_MAC)
359         static bool ReadVarint(unsigned long &value, reader &in)
360         {
361             uint64_t intermediateValue;
362             if (ReadVarint(intermediateValue, in)) {
363                 value = static_cast<unsigned long>(intermediateValue);
364                 return true;
365             }
366             return false;
367         }
368 #endif
369 
370         static void WriteFixed(uint32_t value, Writer &out)
371         {
372 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
373             out.Write(&value, sizeof(value));
374 #else
375             static_assert(false, "Not a little-endian");
376 #endif
377         }
378 
379         static void WriteFixed(uint64_t value, Writer &out)
380         {
381 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
382             out.Write(&value, sizeof(value));
383 #else
384             static_assert(false, "Not a little-endian");
385 #endif
386         }
387 
388         static void WriteFixed(double value, Writer &out)
389         {
390             WriteFixed(BitCast<uint64_t>(value), out);
391         }
392 
393         static void WriteFixed(float value, Writer &out)
394         {
395             WriteFixed(BitCast<uint32_t>(value), out);
396         }
397 
398         static void WriteVarint(int32_t value, Writer &out)
399         {
400             WriteVarint(BitCast<uint32_t>(value), out);
401         }
402 
403         static void WriteVarint(int64_t value, Writer &out)
404         {
405             WriteVarint(BitCast<uint64_t>(value), out);
406         }
407 
408         static void WriteSignedVarint(int32_t value, Writer &out)
409         {
410             WriteVarint(MakeZigzagValue(value), out);
411         }
412 
413         static void WriteSignedVarint(int64_t value, Writer &out)
414         {
415             WriteVarint(MakeZigzagValue(value), out);
416         }
417 
418         static void WriteSignedFixed(int32_t value, Writer &out)
419         {
420             WriteFixed(static_cast<uint32_t>(value), out);
421         }
422 
423         static void WriteSignedFixed(int64_t value, Writer &out)
424         {
425             WriteFixed(static_cast<uint64_t>(value), out);
426         }
427 
428         static void WriteTagWriteType(uint32_t tag, WireType wireType, Writer &out)
429         {
430             WriteVarint(MakeTagWireType(tag, wireType), out);
431         }
432 
433         static bool ReadFixed(uint32_t &value, reader &in)
434         {
435 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
436             return in.Read(&value, sizeof(value)) == sizeof(value);
437 #else
438             static_assert(false, "Not a little-endian");
439 #endif
440         }
441 
442         static bool ReadFixed(uint64_t &value, reader &in)
443         {
444 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
445             return in.Read(&value, sizeof(value)) == sizeof(value);
446 #else
447             static_assert(false, "Not a little-endian");
448 #endif
449         }
450 
451         static bool ReadFixed(double &value, reader &in)
452         {
453             uint64_t intermediateValue;
454             if (ReadFixed(intermediateValue, in)) {
455                 value = BitCast<double>(intermediateValue);
456                 return true;
457             }
458             return false;
459         }
460 
461         static bool ReadFixed(float &value, reader &in)
462         {
463             uint32_t intermediateValue;
464             if (ReadFixed(intermediateValue, in)) {
465                 value = BitCast<float>(intermediateValue);
466                 return true;
467             }
468             return false;
469         }
470 
471         static bool ReadVarint(int32_t &value, reader &in)
472         {
473             uint32_t intermediateValue;
474             if (ReadVarint(intermediateValue, in)) {
475                 value = BitCast<int32_t>(intermediateValue);
476                 return true;
477             }
478             return false;
479         }
480 
481         static bool ReadVarint(int64_t &value, reader &in)
482         {
483             uint64_t intermediateValue;
484             if (ReadVarint(intermediateValue, in)) {
485                 value = BitCast<int64_t>(intermediateValue);
486                 return true;
487             }
488             return false;
489         }
490 
491         static bool ReadSignedVarint(int32_t &value, reader &in)
492         {
493             uint32_t intermediateValue;
494             if (ReadVarint(intermediateValue, in)) {
495                 value = ReadZigzagValue(intermediateValue);
496                 return true;
497             }
498             return false;
499         }
500 
501         static bool ReadSignedVarint(int64_t &value, reader &in)
502         {
503             uint64_t intermediateValue;
504             if (ReadVarint(intermediateValue, in)) {
505                 value = ReadZigzagValue(intermediateValue);
506                 return true;
507             }
508             return false;
509         }
510 
511         static bool ReadSignedFixed(int32_t &value, reader &in)
512         {
513             uint32_t intermediateValue;
514             if (ReadFixed(intermediateValue, in)) {
515                 value = static_cast<int64_t>(intermediateValue);
516                 return true;
517             }
518             return false;
519         }
520 
521         static bool ReadSignedFixed(int64_t &value, reader &in)
522         {
523             uint64_t intermediateValue;
524             if (ReadFixed(intermediateValue, in)) {
525                 value = static_cast<int64_t>(intermediateValue);
526                 return true;
527             }
528             return false;
529         }
530 
531         template<class T, uint32_t Tag, size_t Index, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
532         void WriteField(const T &value,
533             const SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags> &, Writer &out)
534         {
535             using OneOf = SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags>;
536             Serializer<typename OneOf::MemberType>::template SerializeOneof<OneOf::index>(
537                 OneOf::TAG, OneOf::get(value), FlagsType<OneOf::FLAGS>(), out);
538         }
539 
540         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t KeyFlags, uint32_t ValueFlags>
541         void WriteField(const T &value,
542             const SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags> &, Writer &out)
543         {
544             using Map = SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags>;
545             Serializer<typename Map::MemberType>::SerializeMap(
546                 Map::TAG, Map::get(value), FlagsType<Map::KEY_FLAGS>(), FlagsType<Map::VALUE_FLAGS>(), out);
547         }
548 
549         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
550         void WriteField(const T &value, const SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags> &, Writer &out)
551         {
552             using Field = SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags>;
553             Serializer<typename Field::MemberType>::Serialize(
554                 Field::TAG, Field::get(value), FlagsType<Field::FLAGS>(), out);
555         }
556 
557         template<class T, class... Field>
558         void WriteMessage(const T &value, const SerialDetail::MessageImpl<Field...> &message, Writer &out)
559         {
560             message.Visit([&](const auto &field) { WriteField(value, field, out); });
561         }
562 
563         template<uint32_t Flags, class ValueType, class It>
564         void WriteRepeated(uint32_t tag, It begin, It end, Writer &out)
565         {
566             if (begin == end) {
567                 return;
568             }
569             if constexpr (SerialDetail::HAS_SERIALIZE_PACKED_V<Serializer<ValueType>, ValueType, FlagsType<Flags>,
570                 Writer>) {
571                 WriteVarint(MakeTagWireType(tag, WireType::LENGTH_DELIMETED), out);
572                 WriterSizeCollector sizeCollector;
573                 for (auto it = begin; it != end; ++it) {
574                     Serializer<ValueType>::SerializePacked(*it, FlagsType<Flags> {}, sizeCollector);
575                 }
576                 WriteVarint(sizeCollector.byte_size, out);
577                 for (auto it = begin; it != end; ++it) {
578                     Serializer<ValueType>::SerializePacked(*it, FlagsType<Flags> {}, out);
579                 }
580             } else {
581                 for (auto it = begin; it != end; ++it) {
582                     Serializer<ValueType>::Serialize(tag, *it, FlagsType<Flags>(), out);
583                 }
584             }
585         }
586 
587         template<uint32_t KeyFlags, uint32_t ValueFlags, class Key, class Value>
588         void WriteMapKeyValue(const std::pair<const Key, Value> &value, Writer &out)
589         {
590             Serializer<Key>::Serialize(1, value.first, FlagsType<KeyFlags> {}, out, true);
591             Serializer<Value>::Serialize(2, value.second, FlagsType<ValueFlags> {}, out, true);  // 2:tag value
592         }
593 
594         template<uint32_t KeyFlags, uint32_t ValueFlags, class T>
595         void WriteMap(uint32_t tag, const T &value, Writer &out)
596         {
597             auto begin = std::begin(value);
598             auto end = std::end(value);
599 
600             for (auto it = begin; it != end; ++it) {
601                 WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
602                 WriterSizeCollector sizeCollector;
603                 WriteMapKeyValue<KeyFlags, ValueFlags>(*it, sizeCollector);
604                 WriteVarint(sizeCollector.byte_size, out);
605                 WriteMapKeyValue<KeyFlags, ValueFlags>(*it, out);
606             }
607         }
608 
609         template<uint32_t KeyFlags, uint32_t ValueFlags, class Key, class Value>
610         bool ReadMapKeyValue(std::pair<Key, Value> &value, reader &in)
611         {
612             static const auto pairAsMessage = Message(Field<1, &std::pair<Key, Value>::first, KeyFlags>("key"),
613                 Field<2, &std::pair<Key, Value>::second, ValueFlags>("value"));
614             return ReadMessage(value, pairAsMessage, in);
615         }
616 
617         template<uint32_t KeyFlags, uint32_t ValueFlags, class T>
618         bool ReadMap(WireType wireType, T &value, reader &in)
619         {
620             if (wireType != WireType::LENGTH_DELIMETED) {
621                 return false;
622             }
623             size_t size;
624             if (ReadVarint(size, in)) {
625                 LimitedReader limitedIn(in, size);
626                 while (limitedIn.AvailableBytes() > 0) {
627                     std::pair<typename T::key_type, typename T::mapped_type> item;
628                     if (!ReadMapKeyValue<KeyFlags, ValueFlags>(item, limitedIn)) {
629                         return false;
630                     }
631                     value.insert(std::move(item));
632                 }
633                 return true;
634             }
635             return false;
636         }
637 
638         template<uint32_t Flags, class ValueType, class OutputIt>
639         bool ReadRepeated(WireType wireType, OutputIt output_it, reader &in)
640         {
641             if constexpr (SerialDetail::HAS_PARSE_PACKED_V<Serializer<ValueType>, ValueType, FlagsType<Flags>,
642                 reader>) {
643                 if (wireType != WireType::LENGTH_DELIMETED) {
644                     return false;
645                 }
646 
647                 size_t size;
648                 if (ReadVarint(size, in)) {
649                     LimitedReader limitedIn(in, size);
650 
651                     while (limitedIn.AvailableBytes() > 0) {
652                         ValueType value;
653                         if (!Serializer<ValueType>::ParsePacked(value, FlagsType<Flags>(), limitedIn)) {
654                             return false;
655                         }
656                         output_it = value;
657                         ++output_it;
658                     }
659                     return true;
660                 }
661                 return false;
662             } else {
663                 ValueType value;
664                 if (Serializer<ValueType>::Parse(wireType, value, FlagsType<Flags>(), in)) {
665                     output_it = value;
666                     ++output_it;
667                     return true;
668                 }
669                 return false;
670             }
671         }
672 
673         template<class T, uint32_t Tag, size_t Index, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
674         void ReadField(T &value, uint32_t tag, WireType wireType,
675             const SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags> &, reader &in)
676         {
677             if (Tag != tag) {
678                 return;
679             }
680             using OneOf = SerialDetail::OneofFieldImpl<Tag, Index, MemPtrT, MemPtr, Flags>;
681             Serializer<typename OneOf::MemberType>::template ParseOneof<OneOf::index>(
682                 wireType, OneOf::get(value), FlagsType<OneOf::FLAGS>(), in);
683         }
684 
685         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t KeyFlags, uint32_t ValueFlags>
686         void ReadField(T &value, uint32_t tag, WireType wireType,
687             const SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags> &, reader &in)
688         {
689             if (Tag != tag) {
690                 return;
691             }
692             using Map = SerialDetail::MapFieldImpl<Tag, MemPtrT, MemPtr, KeyFlags, ValueFlags>;
693             Serializer<typename Map::MemberType>::ParseMap(
694                 wireType, Map::get(value), FlagsType<Map::KEY_FLAGS>(), FlagsType<Map::VALUE_FLAGS>(), in);
695         }
696 
697         template<class T, uint32_t Tag, class MemPtrT, MemPtrT MemPtr, uint32_t Flags>
698         void ReadField(T &value, uint32_t tag, WireType wireType,
699             const SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags> &, reader &in)
700         {
701             if (Tag != tag) {
702                 return;
703             }
704             using Field = SerialDetail::FieldImpl<Tag, MemPtrT, MemPtr, Flags>;
705             Serializer<typename Field::MemberType>::Parse(wireType, Field::get(value), FlagsType<Field::FLAGS>(), in);
706         }
707 
708         template<class T, class... Field> bool ReadMessage(T &value, const MessageImpl<Field...> &message, reader &in)
709         {
710             uint32_t tagKey;
711             while (ReadVarint(tagKey, in)) {
712                 uint32_t tag;
713                 WireType wireType;
714                 ReadTagWireType(tagKey, tag, wireType);
715                 message.Visit([&](const auto &field) { ReadField(value, tag, wireType, field, in); });
716             }
717             return true;
718         }
719     }
720 
721     template<class T, class Enable> struct Serializer {
722         // Commion Serializer threat type as Message
723         static void Serialize(uint32_t tag, const T &value, FlagsType<>, Writer &out, bool force = false)
724         {
725             SerialDetail::WriterSizeCollector sizeCollector;
726             SerialDetail::WriteMessage(value, MessageType<T>(), sizeCollector);
727             if (!force && sizeCollector.byte_size == 0) {
728                 return;
729             }
730             SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
731             SerialDetail::WriteVarint(sizeCollector.byte_size, out);
732             SerialDetail::WriteMessage(value, MessageType<T>(), out);
733         }
734 
735         static bool Parse(WireType wireType, T &value, FlagsType<>, reader &in)
736         {
737             if (wireType != WireType::LENGTH_DELIMETED) {
738                 return false;
739             }
740             size_t size;
741             if (SerialDetail::ReadVarint(size, in)) {
742                 SerialDetail::LimitedReader limitedIn(in, size);
743                 return SerialDetail::ReadMessage(value, MessageType<T>(), limitedIn);
744             }
745             return false;
746         }
747     };
748 
749     template<> struct Serializer<int32_t> {
750         static void Serialize(uint32_t tag, int32_t value, FlagsType<>, Writer &out, bool force = false)
751         {
752             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
753             SerialDetail::WriteVarint(value, out);
754         }
755 
756         static void Serialize(uint32_t tag, int32_t value, FlagsType<flags::s>, Writer &out, bool force = false)
757         {
758             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
759             SerialDetail::WriteSignedVarint(value, out);
760         }
761 
762         static void Serialize(
763             uint32_t tag, int32_t value, FlagsType<flags::s | flags::f>, Writer &out, bool force = false)
764         {
765             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
766             SerialDetail::WriteSignedFixed(value, out);
767         }
768 
769         static void SerializePacked(int32_t value, FlagsType<>, Writer &out)
770         {
771             SerialDetail::WriteVarint(value, out);
772         }
773 
774         static void SerializePacked(int32_t value, FlagsType<flags::s>, Writer &out)
775         {
776             SerialDetail::WriteSignedVarint(value, out);
777         }
778 
779         static void SerializePacked(int32_t value, FlagsType<flags::s | flags::f>, Writer &out)
780         {
781             SerialDetail::WriteSignedFixed(value, out);
782         }
783 
784         static bool Parse(WireType wire_type, int32_t &value, FlagsType<>, reader &in)
785         {
786             if (wire_type != WireType::VARINT)
787                 return false;
788             return SerialDetail::ReadVarint(value, in);
789         }
790 
791         static bool Parse(WireType wire_type, int32_t &value, FlagsType<flags::s>, reader &in)
792         {
793             if (wire_type != WireType::VARINT)
794                 return false;
795             return SerialDetail::ReadSignedVarint(value, in);
796         }
797 
798         static bool Parse(WireType wire_type, int32_t &value, FlagsType<flags::s | flags::f>, reader &in)
799         {
800             if (wire_type != WireType::FIXED32)
801                 return false;
802             return SerialDetail::ReadSignedFixed(value, in);
803         }
804 
805         static bool ParsePacked(int32_t &value, FlagsType<>, reader &in)
806         {
807             return SerialDetail::ReadVarint(value, in);
808         }
809 
810         static bool ParsePacked(int32_t &value, FlagsType<flags::s>, reader &in)
811         {
812             return SerialDetail::ReadSignedVarint(value, in);
813         }
814 
815         static bool ParsePacked(int32_t &value, FlagsType<flags::s | flags::f>, reader &in)
816         {
817             return SerialDetail::ReadSignedFixed(value, in);
818         }
819     };
820 
821     template<> struct Serializer<uint32_t> {
822         static void Serialize(uint32_t tag, uint32_t value, FlagsType<>, Writer &out, bool force = false)
823         {
824             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
825             SerialDetail::WriteVarint(value, out);
826         }
827 
828         static void Serialize(uint32_t tag, uint32_t value, FlagsType<flags::f>, Writer &out, bool force = false)
829         {
830             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
831             SerialDetail::WriteFixed(value, out);
832         }
833 
834         static void SerializePacked(uint32_t value, FlagsType<>, Writer &out)
835         {
836             SerialDetail::WriteVarint(value, out);
837         }
838 
839         static void SerializePacked(uint32_t value, FlagsType<flags::f>, Writer &out)
840         {
841             SerialDetail::WriteFixed(value, out);
842         }
843 
844         static bool Parse(WireType wire_type, uint32_t &value, FlagsType<>, reader &in)
845         {
846             if (wire_type != WireType::VARINT)
847                 return false;
848             return SerialDetail::ReadVarint(value, in);
849         }
850 
851         static bool Parse(WireType wire_type, uint32_t &value, FlagsType<flags::f>, reader &in)
852         {
853             if (wire_type != WireType::FIXED32)
854                 return false;
855             return SerialDetail::ReadFixed(value, in);
856         }
857 
858         static bool ParsePacked(uint32_t &value, FlagsType<>, reader &in)
859         {
860             return SerialDetail::ReadVarint(value, in);
861         }
862 
863         static bool ParsePacked(uint32_t &value, FlagsType<flags::f>, reader &in)
864         {
865             return SerialDetail::ReadFixed(value, in);
866         }
867     };
868 
869     template<> struct Serializer<int64_t> {
870         static void Serialize(uint32_t tag, int64_t value, FlagsType<>, Writer &out, bool force = false)
871         {
872             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
873             SerialDetail::WriteVarint(value, out);
874         }
875 
876         static void Serialize(uint32_t tag, int64_t value, FlagsType<flags::s>, Writer &out, bool force = false)
877         {
878             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
879             SerialDetail::WriteSignedVarint(value, out);
880         }
881 
882         static void Serialize(
883             uint32_t tag, int64_t value, FlagsType<flags::s | flags::f>, Writer &out, bool force = false)
884         {
885             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
886             SerialDetail::WriteSignedFixed(value, out);
887         }
888 
889         static void SerializePacked(int64_t value, FlagsType<>, Writer &out)
890         {
891             SerialDetail::WriteVarint(value, out);
892         }
893 
894         static void SerializePacked(int64_t value, FlagsType<flags::s>, Writer &out)
895         {
896             SerialDetail::WriteSignedVarint(value, out);
897         }
898 
899         static void SerializePacked(int64_t value, FlagsType<flags::s | flags::f>, Writer &out)
900         {
901             SerialDetail::WriteSignedFixed(value, out);
902         }
903 
904         static bool Parse(WireType wire_type, int64_t &value, FlagsType<>, reader &in)
905         {
906             if (wire_type != WireType::VARINT)
907                 return false;
908             return SerialDetail::ReadVarint(value, in);
909         }
910 
911         static bool Parse(WireType wire_type, int64_t &value, FlagsType<flags::s>, reader &in)
912         {
913             if (wire_type != WireType::VARINT)
914                 return false;
915             return SerialDetail::ReadSignedVarint(value, in);
916         }
917 
918         static bool Parse(WireType wire_type, int64_t &value, FlagsType<flags::s | flags::f>, reader &in)
919         {
920             if (wire_type != WireType::FIXED64)
921                 return false;
922             return SerialDetail::ReadSignedFixed(value, in);
923         }
924 
925         static bool ParsePacked(int64_t &value, FlagsType<>, reader &in)
926         {
927             return SerialDetail::ReadVarint(value, in);
928         }
929 
930         static bool ParsePacked(int64_t &value, FlagsType<flags::s>, reader &in)
931         {
932             return SerialDetail::ReadSignedVarint(value, in);
933         }
934 
935         static bool ParsePacked(int64_t &value, FlagsType<flags::s | flags::f>, reader &in)
936         {
937             return SerialDetail::ReadSignedFixed(value, in);
938         }
939     };
940 
941     template<> struct Serializer<uint64_t> {
942         static void Serialize(uint32_t tag, uint64_t value, FlagsType<>, Writer &out, bool force = false)
943         {
944             SerialDetail::WriteTagWriteType(tag, WireType::VARINT, out);
945             SerialDetail::WriteVarint(value, out);
946         }
947 
948         static void Serialize(uint32_t tag, uint64_t value, FlagsType<flags::f>, Writer &out, bool force = false)
949         {
950             if (!force && value == UINT64_C(0))
951                 return;
952 
953             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
954             SerialDetail::WriteFixed(value, out);
955         }
956 
957         static void SerializePacked(uint64_t value, FlagsType<>, Writer &out)
958         {
959             SerialDetail::WriteVarint(value, out);
960         }
961 
962         static void SerializePacked(uint64_t value, FlagsType<flags::f>, Writer &out)
963         {
964             SerialDetail::WriteFixed(value, out);
965         }
966 
967         static bool Parse(WireType wire_type, uint64_t &value, FlagsType<>, reader &in)
968         {
969             if (wire_type != WireType::VARINT)
970                 return false;
971             return SerialDetail::ReadVarint(value, in);
972         }
973 
974         static bool Parse(WireType wire_type, uint64_t &value, FlagsType<flags::f>, reader &in)
975         {
976             if (wire_type != WireType::FIXED64)
977                 return false;
978             return SerialDetail::ReadFixed(value, in);
979         }
980 
981         static bool ParsePacked(uint64_t &value, FlagsType<>, reader &in)
982         {
983             return SerialDetail::ReadVarint(value, in);
984         }
985 
986         static bool ParsePacked(uint64_t &value, FlagsType<flags::f>, reader &in)
987         {
988             return SerialDetail::ReadFixed(value, in);
989         }
990     };
991 
992     template<> struct Serializer<double> {
993         static void Serialize(uint32_t tag, double value, FlagsType<>, Writer &out, bool force = false)
994         {
995             if (!force && std::fpclassify(value) == FP_ZERO) {
996                 return;
997             }
998             SerialDetail::WriteTagWriteType(tag, WireType::FIXED64, out);
999             SerialDetail::WriteFixed(value, out);
1000         }
1001 
1002         static void SerializePacked(double value, FlagsType<>, Writer &out)
1003         {
1004             SerialDetail::WriteFixed(value, out);
1005         }
1006 
1007         static bool Parse(WireType wire_type, double &value, FlagsType<>, reader &in)
1008         {
1009             if (wire_type != WireType::FIXED64) {
1010                 return false;
1011             }
1012             return SerialDetail::ReadFixed(value, in);
1013         }
1014 
1015         static bool ParsePacked(double &value, FlagsType<>, reader &in)
1016         {
1017             return SerialDetail::ReadFixed(value, in);
1018         }
1019     };
1020 
1021     template<> struct Serializer<float> {
1022         static void Serialize(uint32_t tag, float value, FlagsType<>, Writer &out, bool force = false)
1023         {
1024             if (!force && std::fpclassify(value) == FP_ZERO) {
1025                 return;
1026             }
1027             SerialDetail::WriteTagWriteType(tag, WireType::FIXED32, out);
1028             SerialDetail::WriteFixed(value, out);
1029         }
1030 
1031         static void SerializePacked(float value, FlagsType<>, Writer &out)
1032         {
1033             SerialDetail::WriteFixed(value, out);
1034         }
1035 
1036         static bool Parse(WireType wire_type, float &value, FlagsType<>, reader &in)
1037         {
1038             if (wire_type != WireType::FIXED32) {
1039                 return false;
1040             }
1041             return SerialDetail::ReadFixed(value, in);
1042         }
1043 
1044         static bool ParsePacked(float &value, FlagsType<>, reader &in)
1045         {
1046             return SerialDetail::ReadFixed(value, in);
1047         }
1048     };
1049 
1050     template<> struct Serializer<bool> {
1051         static void Serialize(uint32_t tag, bool value, FlagsType<>, Writer &out, bool force = false)
1052         {
1053             Serializer<uint32_t>::Serialize(tag, value ? 1 : 0, FlagsType(), out, force);
1054         }
1055 
1056         static void SerializePacked(bool value, FlagsType<>, Writer &out)
1057         {
1058             Serializer<uint32_t>::SerializePacked(value ? 1 : 0, FlagsType(), out);
1059         }
1060 
1061         static bool Parse(WireType wire_type, bool &value, FlagsType<>, reader &in)
1062         {
1063             uint32_t intermedaite_value;
1064             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1065                 value = static_cast<bool>(intermedaite_value);
1066                 return true;
1067             }
1068             return false;
1069         }
1070 
1071         static bool ParsePacked(bool &value, FlagsType<>, reader &in)
1072         {
1073             uint32_t intermedaite_value;
1074             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1075                 value = static_cast<bool>(intermedaite_value);
1076                 return true;
1077             }
1078             return false;
1079         }
1080     };
1081 
1082     template<class T> struct Serializer<T, std::enable_if_t<std::is_enum_v<T>>> {
1083         using U = std::underlying_type_t<T>;
1084 
1085         static void Serialize(uint32_t tag, T value, FlagsType<>, Writer &out, bool force = false)
1086         {
1087             Serializer<U>::Serialize(tag, static_cast<U>(value), FlagsType<>(), out, force);
1088         }
1089 
1090         static void SerializePacked(T value, FlagsType<>, Writer &out)
1091         {
1092             Serializer<U>::SerializePacked(static_cast<U>(value), FlagsType<>(), out);
1093         }
1094 
1095         static bool Parse(WireType wire_type, T &value, FlagsType<>, reader &in)
1096         {
1097             U intermedaite_value;
1098             if (Serializer<U>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1099                 value = static_cast<T>(intermedaite_value);
1100                 return true;
1101             }
1102             return false;
1103         }
1104 
1105         static bool ParsePacked(T &value, FlagsType<>, reader &in)
1106         {
1107             U intermedaite_value;
1108             if (Serializer<U>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1109                 value = static_cast<T>(intermedaite_value);
1110                 return true;
1111             }
1112             return false;
1113         }
1114     };
1115 
1116     template<> struct Serializer<std::string> {
1117         static void Serialize(uint32_t tag, const std::string &value, FlagsType<>, Writer &out, bool force = false)
1118         {
1119             SerialDetail::WriteTagWriteType(tag, WireType::LENGTH_DELIMETED, out);
1120             SerialDetail::WriteVarint(value.size(), out);
1121             out.Write(value.data(), value.size());
1122         }
1123 
1124         static bool Parse(WireType wire_type, std::string &value, FlagsType<>, reader &in)
1125         {
1126             if (wire_type != WireType::LENGTH_DELIMETED) {
1127                 return false;
1128             }
1129             size_t size;
1130             if (SerialDetail::ReadVarint(size, in)) {
1131                 value.resize(size);
1132                 if (in.Read(value.data(), size) == size) {
1133                     return true;
1134                 }
1135             }
1136             return false;
1137         }
1138     };
1139 
1140     template<class T> struct Serializer<std::vector<T>> {
1141         template<uint32_t Flags>
1142         static void Serialize(uint32_t tag, const std::vector<T> &value, FlagsType<Flags>, Writer &out)
1143         {
1144             SerialDetail::WriteRepeated<Flags, T>(tag, value.begin(), value.end(), out);
1145         }
1146 
1147         template<uint32_t Flags>
1148         static bool Parse(WireType wire_type, std::vector<T> &value, FlagsType<Flags>, reader &in)
1149         {
1150             return SerialDetail::ReadRepeated<Flags, T>(wire_type, std::back_inserter(value), in);
1151         }
1152     };
1153 
1154     template<class T> struct Serializer<std::optional<T>> {
1155         template<uint32_t Flags>
1156         static void Serialize(uint32_t tag, const std::optional<T> &value, FlagsType<Flags>, Writer &out)
1157         {
1158             if (!value.has_value()) {
1159                 return;
1160             }
1161             Serializer<T>::Serialize(tag, *value, FlagsType<Flags>(), out);
1162         }
1163 
1164         template<uint32_t Flags>
1165         static bool Parse(WireType wire_type, std::optional<T> &value, FlagsType<Flags>, reader &in)
1166         {
1167             return Serializer<T>::Parse(wire_type, value.emplace(), FlagsType<Flags>(), in);
1168         }
1169     };
1170 
1171     template<class... T> struct Serializer<std::variant<T...>> {
1172         template<size_t Index, uint32_t Flags>
1173         static void SerializeOneof(uint32_t tag, const std::variant<T...> &value, FlagsType<Flags>, Writer &out)
1174         {
1175             if (value.index() != Index)
1176                 return;
1177 
1178             Serializer<std::variant_alternative_t<Index, std::variant<T...>>>::Serialize(
1179                 tag, std::get<Index>(value), FlagsType<Flags>(), out);
1180         }
1181 
1182         template<size_t Index, uint32_t Flags>
1183         static bool ParseOneof(WireType wire_type, std::variant<T...> &value, FlagsType<Flags>, reader &in)
1184         {
1185             return Serializer<std::variant_alternative_t<Index, std::variant<T...>>>::Parse(
1186                 wire_type, value.template emplace<Index>(), FlagsType<Flags>(), in);
1187         }
1188     };
1189 
1190     template<class Key, class Value> struct Serializer<std::map<Key, Value>> {
1191         template<uint32_t KeyFlags, uint32_t ValueFlags>
1192         static void SerializeMap(
1193             uint32_t tag, const std::map<Key, Value> &value, FlagsType<KeyFlags>, FlagsType<ValueFlags>, Writer &out)
1194         {
1195             SerialDetail::WriteMap<KeyFlags, ValueFlags>(tag, value, out);
1196         }
1197 
1198         template<uint32_t KeyFlags, uint32_t ValueFlags>
1199         static bool ParseMap(
1200             WireType wire_type, std::map<Key, Value> &value, FlagsType<KeyFlags>, FlagsType<ValueFlags>, reader &in)
1201         {
1202             return SerialDetail::ReadMap<KeyFlags, ValueFlags>(wire_type, value, in);
1203         }
1204     };
1205 
1206     struct StringWriter : public Writer {
1207         StringWriter(std::string &out)
1208             : _out(out)
1209         {
1210         }
1211 
1212         void Write(const void *bytes, size_t size) override
1213         {
1214             _out.append(reinterpret_cast<const char *>(bytes), size);
1215         }
1216 
1217     private:
1218         std::string &_out;
1219     };
1220 
1221     struct StringReader : public reader {
1222         StringReader(const std::string &in)
1223             : _in(in), _pos(0)
1224         {
1225         }
1226 
1227         size_t Read(void *bytes, size_t size) override
1228         {
1229             if (_in.size() < _pos) {
1230                 return 0;
1231             }
1232             size_t readSize = std::min(size, _in.size() - _pos);
1233             if (memcpy_s(bytes, size, _in.data() + _pos, readSize) != EOK) {
1234                 return readSize;
1235             }
1236             _pos += readSize;
1237             return readSize;
1238         }
1239 
1240     private:
1241         const std::string &_in;
1242         size_t _pos;
1243     };
1244     // mytype begin, just support base type, but really use protobuf raw type(uint32)
1245     template<> struct Serializer<uint8_t> {
1246         static void Serialize(uint32_t tag, uint8_t value, FlagsType<>, Writer &out, bool force = false)
1247         {
1248             Serializer<uint32_t>::Serialize(tag, value, FlagsType(), out, force);
1249         }
1250 
1251         static void SerializePacked(uint8_t value, FlagsType<>, Writer &out)
1252         {
1253             Serializer<uint32_t>::SerializePacked(value, FlagsType(), out);
1254         }
1255 
1256         static bool Parse(WireType wire_type, uint8_t &value, FlagsType<>, reader &in)
1257         {
1258             uint32_t intermedaite_value;
1259             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1260                 value = static_cast<uint8_t>(intermedaite_value);
1261                 return true;
1262             }
1263             return false;
1264         }
1265 
1266         static bool ParsePacked(uint8_t &value, FlagsType<>, reader &in)
1267         {
1268             uint32_t intermedaite_value;
1269             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1270                 value = static_cast<uint8_t>(intermedaite_value);
1271                 return true;
1272             }
1273             return false;
1274         }
1275     };
1276     template<> struct Serializer<uint16_t> {
1277         static void Serialize(uint32_t tag, uint16_t value, FlagsType<>, Writer &out, bool force = false)
1278         {
1279             Serializer<uint32_t>::Serialize(tag, value, FlagsType(), out, force);
1280         }
1281 
1282         static void SerializePacked(uint16_t value, FlagsType<>, Writer &out)
1283         {
1284             Serializer<uint32_t>::SerializePacked(value, FlagsType(), out);
1285         }
1286 
1287         static bool Parse(WireType wire_type, uint16_t &value, FlagsType<>, reader &in)
1288         {
1289             uint32_t intermedaite_value;
1290             if (Serializer<uint32_t>::Parse(wire_type, intermedaite_value, FlagsType<>(), in)) {
1291                 value = static_cast<uint16_t>(intermedaite_value);
1292                 return true;
1293             }
1294             return false;
1295         }
1296 
1297         static bool ParsePacked(uint16_t &value, FlagsType<>, reader &in)
1298         {
1299             uint32_t intermedaite_value;
1300             if (Serializer<uint32_t>::ParsePacked(intermedaite_value, FlagsType<>(), in)) {
1301                 value = static_cast<uint16_t>(intermedaite_value);
1302                 return true;
1303             }
1304             return false;
1305         }
1306     };
1307     // mytype finish
1308 
1309     template<class T> std::string SerializeToString(const T &value)
1310     {
1311         std::string out;
1312         StringWriter stringOut(out);
1313         SerialDetail::WriteMessage(value, MessageType<T>(), stringOut);
1314         return out;
1315     }
1316 
1317     template<class T> bool ParseFromString(T &value, const std::string &in)
1318     {
1319         StringReader stringIn(in);
1320         return SerialDetail::ReadMessage(value, MessageType<T>(), stringIn);
1321     }
1322 }
1323 // clang-format on
1324 }  // Hdc
1325 #endif  // HDC_SERIAL_STRUCT_DEFINE_H
1326