mini_table.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of Google LLC nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_MINI_TABLE_HPP_
29 #define UPB_MINI_TABLE_HPP_
30 
31 #include <assert.h>
32 
33 #include <string>
34 
35 #include "upb/mini_table.h"
36 
37 namespace upb {
38 
40  public:
42 
43  bool StartMessage(uint64_t msg_mod) {
44  return appender_([=](char* buf) {
45  return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod);
46  });
47  }
48 
49  bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) {
50  return appender_([=](char* buf) {
51  return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num,
52  field_mod);
53  });
54  }
55 
56  bool StartOneof() {
57  return appender_([=](char* buf) {
59  });
60  }
61 
62  bool PutOneofField(uint32_t field_num) {
63  return appender_([=](char* buf) {
64  return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num);
65  });
66  }
67 
68  const std::string& data() const { return appender_.data(); }
69 
70  private:
72  public:
73  StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); }
74 
75  template <class T>
76  bool operator()(T&& func) {
77  char* end = func(buf_);
78  if (!end) return false;
79  // C++ does not guarantee that string has doubling growth behavior, but
80  // we need it to avoid O(n^2).
81  str_.reserve(_upb_Log2CeilingSize(str_.size() + (end - buf_)));
82  str_.append(buf_, end - buf_);
83  return true;
84  }
85 
86  const std::string& data() const { return str_; }
87 
88  private:
91  };
92 
95 };
96 
97 } // namespace upb
98 
99 #endif /* UPB_MINI_TABLE_HPP_ */
upb::MtDataEncoder
Definition: mini_table.hpp:39
upb_MtDataEncoder_StartMessage
char * upb_MtDataEncoder_StartMessage(upb_MtDataEncoder *e, char *ptr, uint64_t msg_mod)
Definition: mini_table.c:176
upb_FieldType
upb_FieldType
Definition: upb/upb/upb.h:308
kUpb_MtDataEncoder_MinSize
#define kUpb_MtDataEncoder_MinSize
Definition: mini_table.h:70
upb_MtDataEncoder_PutOneofField
char * upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder *e, char *ptr, uint32_t field_num)
Definition: mini_table.c:268
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
upb::MtDataEncoder::encoder_
upb_MtDataEncoder encoder_
Definition: mini_table.hpp:93
upb::MtDataEncoder::PutOneofField
bool PutOneofField(uint32_t field_num)
Definition: mini_table.hpp:62
_upb_Log2CeilingSize
UPB_INLINE int _upb_Log2CeilingSize(int x)
Definition: upb/upb/upb.h:365
upb_MtDataEncoder_PutField
char * upb_MtDataEncoder_PutField(upb_MtDataEncoder *e, char *ptr, upb_FieldType type, uint32_t field_num, uint64_t field_mod)
Definition: mini_table.c:185
upb::MtDataEncoder::StringAppender
Definition: mini_table.hpp:71
T
#define T(upbtypeconst, upbtype, ctype, default_value)
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
upb::MtDataEncoder::StartMessage
bool StartMessage(uint64_t msg_mod)
Definition: mini_table.hpp:43
upb::MtDataEncoder::data
const std::string & data() const
Definition: mini_table.hpp:68
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
upb_MtDataEncoder
Definition: mini_table.h:62
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
upb::MtDataEncoder::StringAppender::data
const std::string & data() const
Definition: mini_table.hpp:86
upb
Definition: def.hpp:38
upb::MtDataEncoder::MtDataEncoder
MtDataEncoder()
Definition: mini_table.hpp:41
upb::MtDataEncoder::StringAppender::buf_
char buf_[kUpb_MtDataEncoder_MinSize]
Definition: mini_table.hpp:89
func
const EVP_CIPHER *(* func)(void)
Definition: cipher_extra.c:73
upb::MtDataEncoder::PutField
bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod)
Definition: mini_table.hpp:49
upb::MtDataEncoder::appender_
StringAppender appender_
Definition: mini_table.hpp:94
upb::MtDataEncoder::StartOneof
bool StartOneof()
Definition: mini_table.hpp:56
upb_MtDataEncoder_StartOneof
char * upb_MtDataEncoder_StartOneof(upb_MtDataEncoder *e, char *ptr)
Definition: mini_table.c:256
upb::MtDataEncoder::StringAppender::operator()
bool operator()(T &&func)
Definition: mini_table.hpp:76
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
mini_table.h
upb::MtDataEncoder::StringAppender::str_
std::string str_
Definition: mini_table.hpp:90
upb::MtDataEncoder::StringAppender::StringAppender
StringAppender(upb_MtDataEncoder *e)
Definition: mini_table.hpp:73


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:40