File: structured_proto.h

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (107 lines) | stat: -rw-r--r-- 3,729 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2024 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: log/internal/structured_proto.h
// -----------------------------------------------------------------------------

#ifndef ABSL_LOG_INTERNAL_STRUCTURED_PROTO_H_
#define ABSL_LOG_INTERNAL_STRUCTURED_PROTO_H_

#include <cstddef>
#include <cstdint>

#include "absl/base/config.h"
#include "absl/log/internal/proto.h"
#include "absl/types/span.h"
#include "absl/types/variant.h"

namespace absl {
ABSL_NAMESPACE_BEGIN
namespace log_internal {

// Sum type holding a single valid protobuf field suitable for encoding.
struct StructuredProtoField final {
  // Numeric type encoded with varint encoding:
  // https://protobuf.dev/programming-guides/encoding/#varints
  using Varint = absl::variant<uint64_t, int64_t, uint32_t, int32_t, bool>;

  // Fixed-length 64-bit integer encoding:
  // https://protobuf.dev/programming-guides/encoding/#non-varints
  using I64 = absl::variant<uint64_t, int64_t, double>;

  // Length-delimited record type (string, sub-message):
  // https://protobuf.dev/programming-guides/encoding/#length-types
  using LengthDelimited = absl::Span<const char>;

  // Fixed-length 32-bit integer encoding:
  // https://protobuf.dev/programming-guides/encoding/#non-varints
  using I32 = absl::variant<uint32_t, int32_t, float>;

  // Valid record type:
  // https://protobuf.dev/programming-guides/encoding/#structure
  using Value = absl::variant<Varint, I64, LengthDelimited, I32>;

  // Field number for the protobuf value.
  uint64_t field_number;

  // Value to encode.
  Value value;
};

// Estimates the number of bytes needed to encode `field` using
// protobuf encoding.
//
// The returned value might be larger than the actual number of bytes needed.
inline size_t BufferSizeForStructuredProtoField(StructuredProtoField field) {
  // Visitor to estimate the number of bytes of one of the types contained
  // inside `StructuredProtoField`.
  struct BufferSizeVisitor final {
    size_t operator()(StructuredProtoField::Varint /*unused*/) {
      return BufferSizeFor(field_number, WireType::kVarint);
    }

    size_t operator()(StructuredProtoField::I64 /*unused*/) {
      return BufferSizeFor(field_number, WireType::k64Bit);
    }

    size_t operator()(StructuredProtoField::LengthDelimited length_delimited) {
      return BufferSizeFor(field_number, WireType::kLengthDelimited) +
             length_delimited.size();
    }

    size_t operator()(StructuredProtoField::I32 /*unused*/) {
      return BufferSizeFor(field_number, WireType::k32Bit);
    }

    uint64_t field_number;
  };

  return absl::visit(BufferSizeVisitor{field.field_number}, field.value);
}

// Encodes `field` into `buf` using protobuf encoding.
//
// On success, returns `true` and advances `buf` to the end of
// the bytes consumed.
//
// On failure (if `buf` was too small), returns `false`.
bool EncodeStructuredProtoField(StructuredProtoField field,
                                absl::Span<char>& buf);

}  // namespace log_internal
ABSL_NAMESPACE_END
}  // namespace absl

#endif  // ABSL_LOG_INTERNAL_STRUCTURED_PROTO_H_