Line data Source code
1 : /***************************************************************************\
2 : * Name : serialize library for protobuf *
3 : * Description : encoding in protobuf *
4 : * Author : antonin.kriz@gmail.com *
5 : * ------------------------------------------------------------------------- *
6 : * This is free software; you can redistribute it and/or modify it under the *
7 : * terms of the MIT license. A copy of the license can be found in the file *
8 : * "LICENSE" at the root of this distribution. *
9 : \***************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include <cstdint>
14 : #include <type_traits>
15 :
16 : namespace spb::pb::detail
17 : {
18 : enum class tag_type : uint32_t
19 : {
20 : invalid = 0
21 : };
22 :
23 : //- https://protobuf.dev/programming-guides/encoding/
24 : enum class wire_type : uint8_t
25 : {
26 : //- int32, int64, uint32, uint64, sint32, sint64, bool, enum
27 : varint = 0,
28 : //- fixed64, sfixed64, double
29 : fixed64 = 1,
30 : //- string, bytes, embedded messages, packed repeated fields
31 : length_delimited = 2,
32 : //- not used
33 : StartGroup = 3,
34 : //- not used
35 : EndGroup = 4,
36 : //- fixed32, sfixed32, float
37 : fixed32 = 5
38 : };
39 :
40 : //- type1, type2 and packed flag
41 : enum class scalar_encoder : uint8_t
42 : {
43 : //- int32, int64, uint32, uint64, bool
44 : varint = 0x01,
45 : //- zigzag int32 or int64
46 : svarint = 0x02,
47 : //- 4 bytes
48 : i32 = 0x03,
49 : //- 8 bytes
50 : i64 = 0x04,
51 : //- packed array
52 : packed = 0x08
53 : };
54 :
55 : struct field_attributes
56 : {
57 : union
58 : {
59 : uint32_t number;
60 : wire_type type;
61 : };
62 : size_t max_count = 0;
63 : size_t max_size = 0;
64 : };
65 :
66 : static constexpr scalar_encoder operator&(scalar_encoder lhs, scalar_encoder rhs) noexcept
67 : {
68 : return scalar_encoder(std::underlying_type_t<scalar_encoder>(lhs) &
69 : std::underlying_type_t<scalar_encoder>(rhs));
70 : }
71 :
72 : static constexpr scalar_encoder operator|(scalar_encoder lhs, scalar_encoder rhs) noexcept
73 : {
74 : return scalar_encoder(std::underlying_type_t<scalar_encoder>(lhs) |
75 : std::underlying_type_t<scalar_encoder>(rhs));
76 : }
77 :
78 : static constexpr auto scalar_encoder_combine(scalar_encoder type1, scalar_encoder type2) noexcept
79 : -> scalar_encoder
80 : {
81 : return scalar_encoder((std::underlying_type_t<scalar_encoder>(type1) & 0x0f) |
82 : ((std::underlying_type_t<scalar_encoder>(type2) & 0x0f) << 4));
83 : }
84 :
85 : static constexpr auto scalar_encoder_is_packed(scalar_encoder a) noexcept -> bool
86 : {
87 : return (a & scalar_encoder::packed) == scalar_encoder::packed;
88 : }
89 :
90 1237 : static constexpr auto scalar_encoder_type1(scalar_encoder a) noexcept -> scalar_encoder
91 : {
92 1237 : return scalar_encoder(static_cast<std::underlying_type_t<scalar_encoder>>(a) & 0x07);
93 : }
94 :
95 : static constexpr auto scalar_encoder_type2(scalar_encoder a) noexcept -> scalar_encoder
96 : {
97 : return scalar_encoder((static_cast<std::underlying_type_t<scalar_encoder>>(a) >> 4) & 0x07);
98 : }
99 :
100 1237 : static constexpr auto wire_type_from_scalar_encoder(scalar_encoder a) noexcept -> wire_type
101 : {
102 1237 : switch (scalar_encoder_type1(a))
103 : {
104 370 : case scalar_encoder::i32:
105 370 : return wire_type::fixed32;
106 305 : case scalar_encoder::i64:
107 305 : return wire_type::fixed64;
108 562 : default:
109 562 : return wire_type::varint;
110 : }
111 : }
112 :
113 : } // namespace spb::pb::detail
|