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 :
19 : //- https://protobuf.dev/programming-guides/encoding/
20 : enum class wire_type : uint8_t
21 : {
22 : //- int32, int64, uint32, uint64, sint32, sint64, bool, enum
23 : varint = 0,
24 : //- fixed64, sfixed64, double
25 : fixed64 = 1,
26 : //- string, bytes, embedded messages, packed repeated fields
27 : length_delimited = 2,
28 : //- not used
29 : StartGroup = 3,
30 : //- not used
31 : EndGroup = 4,
32 : //- fixed32, sfixed32, float
33 : fixed32 = 5
34 : };
35 :
36 : //- type1, type2 and packed flag
37 : enum class scalar_encoder : uint8_t
38 : {
39 : //- int32, int64, uint32, uint64, bool
40 : varint = 0x01,
41 : //- zigzag int32 or int64
42 : svarint = 0x02,
43 : //- 4 bytes
44 : i32 = 0x03,
45 : //- 8 bytes
46 : i64 = 0x04,
47 : //- packed array
48 : packed = 0x08
49 : };
50 :
51 : static constexpr scalar_encoder operator&( scalar_encoder lhs, scalar_encoder rhs ) noexcept
52 : {
53 : return scalar_encoder( std::underlying_type_t< scalar_encoder >( lhs ) &
54 : std::underlying_type_t< scalar_encoder >( rhs ) );
55 : }
56 :
57 : static constexpr scalar_encoder operator|( scalar_encoder lhs, scalar_encoder rhs ) noexcept
58 : {
59 : return scalar_encoder( std::underlying_type_t< scalar_encoder >( lhs ) |
60 : std::underlying_type_t< scalar_encoder >( rhs ) );
61 : }
62 :
63 : static constexpr auto scalar_encoder_combine( scalar_encoder type1, scalar_encoder type2 ) noexcept
64 : -> scalar_encoder
65 : {
66 : return scalar_encoder( ( std::underlying_type_t< scalar_encoder >( type1 ) & 0x0f ) |
67 : ( ( std::underlying_type_t< scalar_encoder >( type2 ) & 0x0f ) << 4 ) );
68 : }
69 :
70 : static constexpr auto scalar_encoder_is_packed( scalar_encoder a ) noexcept -> bool
71 : {
72 : return ( a & scalar_encoder::packed ) == scalar_encoder::packed;
73 : }
74 :
75 1257 : static constexpr auto scalar_encoder_type1( scalar_encoder a ) noexcept -> scalar_encoder
76 : {
77 1257 : return scalar_encoder( static_cast< std::underlying_type_t< scalar_encoder > >( a ) & 0x07 );
78 : }
79 :
80 : static constexpr auto scalar_encoder_type2( scalar_encoder a ) noexcept -> scalar_encoder
81 : {
82 : return scalar_encoder( ( static_cast< std::underlying_type_t< scalar_encoder > >( a ) >> 4 ) &
83 : 0x07 );
84 : }
85 :
86 1257 : static constexpr auto wire_type_from_scalar_encoder( scalar_encoder a ) noexcept -> wire_type
87 : {
88 1257 : switch( scalar_encoder_type1( a ) )
89 : {
90 396 : case scalar_encoder::i32:
91 396 : return wire_type::fixed32;
92 321 : case scalar_encoder::i64:
93 321 : return wire_type::fixed64;
94 540 : default:
95 540 : return wire_type::varint;
96 : }
97 : }
98 :
99 : }// namespace spb::pb::detail
|