You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mini_table.hpp 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef UPB_MINI_TABLE_HPP_
  28. #define UPB_MINI_TABLE_HPP_
  29. #include <assert.h>
  30. #include <string>
  31. #include "upb/mini_table.h"
  32. namespace upb {
  33. class MtDataEncoder {
  34. public:
  35. MtDataEncoder() : appender_(&encoder_) {}
  36. bool StartMessage(uint64_t msg_mod) {
  37. return appender_([=](char* buf) {
  38. return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod);
  39. });
  40. }
  41. bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) {
  42. return appender_([=](char* buf) {
  43. return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num,
  44. field_mod);
  45. });
  46. }
  47. bool StartOneof() {
  48. return appender_([=](char* buf) {
  49. return upb_MtDataEncoder_StartOneof(&encoder_, buf);
  50. });
  51. }
  52. bool PutOneofField(uint32_t field_num) {
  53. return appender_([=](char* buf) {
  54. return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num);
  55. });
  56. }
  57. void StartEnum() { upb_MtDataEncoder_StartEnum(&encoder_); }
  58. bool PutEnumValue(uint32_t enum_value) {
  59. return appender_([=](char* buf) {
  60. return upb_MtDataEncoder_PutEnumValue(&encoder_, buf, enum_value);
  61. });
  62. }
  63. bool EndEnum() {
  64. return appender_(
  65. [=](char* buf) { return upb_MtDataEncoder_EndEnum(&encoder_, buf); });
  66. }
  67. const std::string& data() const { return appender_.data(); }
  68. private:
  69. class StringAppender {
  70. public:
  71. StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); }
  72. template <class T>
  73. bool operator()(T&& func) {
  74. char* end = func(buf_);
  75. if (!end) return false;
  76. // C++ does not guarantee that string has doubling growth behavior, but
  77. // we need it to avoid O(n^2).
  78. str_.reserve(_upb_Log2CeilingSize(str_.size() + (end - buf_)));
  79. str_.append(buf_, end - buf_);
  80. return true;
  81. }
  82. const std::string& data() const { return str_; }
  83. private:
  84. char buf_[kUpb_MtDataEncoder_MinSize];
  85. std::string str_;
  86. };
  87. upb_MtDataEncoder encoder_;
  88. StringAppender appender_;
  89. };
  90. } // namespace upb
  91. #endif /* UPB_MINI_TABLE_HPP_ */