aboutsummaryrefslogtreecommitdiff
path: root/third_party/upb/upb/mini_descriptor/internal/encode.hpp
blob: cfa3bed6cc00b297da4a8f732dd3dd178828e2e0 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#ifndef UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_
#define UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_

#include <string>

#include "upb/base/internal/log2.h"
#include "upb/mini_descriptor/internal/encode.h"

namespace upb {

class MtDataEncoder {
 public:
  MtDataEncoder() : appender_(&encoder_) {}

  bool StartMessage(uint64_t msg_mod) {
    return appender_([this, msg_mod](char* buf) {
      return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod);
    });
  }

  bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) {
    return appender_([this, type, field_num, field_mod](char* buf) {
      return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num,
                                        field_mod);
    });
  }

  bool StartOneof() {
    return appender_([this](char* buf) {
      return upb_MtDataEncoder_StartOneof(&encoder_, buf);
    });
  }

  bool PutOneofField(uint32_t field_num) {
    return appender_([this, field_num](char* buf) {
      return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num);
    });
  }

  bool StartEnum() {
    return appender_([this](char* buf) {
      return upb_MtDataEncoder_StartEnum(&encoder_, buf);
    });
  }

  bool PutEnumValue(uint32_t enum_value) {
    return appender_([this, enum_value](char* buf) {
      return upb_MtDataEncoder_PutEnumValue(&encoder_, buf, enum_value);
    });
  }

  bool EndEnum() {
    return appender_([this](char* buf) {
      return upb_MtDataEncoder_EndEnum(&encoder_, buf);
    });
  }

  bool EncodeExtension(upb_FieldType type, uint32_t field_num,
                       uint64_t field_mod) {
    return appender_([this, type, field_num, field_mod](char* buf) {
      return upb_MtDataEncoder_EncodeExtension(&encoder_, buf, type, field_num,
                                               field_mod);
    });
  }

  bool EncodeMap(upb_FieldType key_type, upb_FieldType val_type,
                 uint64_t key_mod, uint64_t val_mod) {
    return appender_([this, key_type, val_type, key_mod, val_mod](char* buf) {
      return upb_MtDataEncoder_EncodeMap(&encoder_, buf, key_type, val_type,
                                         key_mod, val_mod);
    });
  }

  bool EncodeMessageSet() {
    return appender_([this](char* buf) {
      return upb_MtDataEncoder_EncodeMessageSet(&encoder_, buf);
    });
  }

  const std::string& data() const { return appender_.data(); }

 private:
  class StringAppender {
   public:
    StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); }

    template <class T>
    bool operator()(T&& func) {
      char* end = func(buf_);
      if (!end) return false;
      // C++ does not guarantee that string has doubling growth behavior, but
      // we need it to avoid O(n^2).
      str_.reserve(upb_Log2CeilingSize(str_.size() + (end - buf_)));
      str_.append(buf_, end - buf_);
      return true;
    }

    const std::string& data() const { return str_; }

   private:
    char buf_[kUpb_MtDataEncoder_MinSize];
    std::string str_;
  };

  upb_MtDataEncoder encoder_;
  StringAppender appender_;
};

}  // namespace upb

#endif /* UPB_MINI_TABLE_ENCODE_INTERNAL_HPP_ */