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 4.3 kB

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