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.

upb.hpp 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2009-2021, Google LLC
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above copyright
  9. // notice, this list of conditions and the following disclaimer in the
  10. // documentation and/or other materials provided with the distribution.
  11. // * Neither the name of Google LLC nor the
  12. // names of its contributors may be used to endorse or promote products
  13. // derived from this software without specific prior written permission.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  19. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. #ifndef UPB_HPP_
  26. #define UPB_HPP_
  27. #include <memory>
  28. #include "upb/upb.h"
  29. namespace upb
  30. {
  31. class Status
  32. {
  33. public:
  34. Status()
  35. {
  36. upb_Status_Clear(&status_);
  37. }
  38. upb_Status* ptr()
  39. {
  40. return &status_;
  41. }
  42. // Returns true if there is no error.
  43. bool ok() const
  44. {
  45. return upb_Status_IsOk(&status_);
  46. }
  47. // Guaranteed to be NULL-terminated.
  48. const char* error_message() const
  49. {
  50. return upb_Status_ErrorMessage(&status_);
  51. }
  52. // The error message will be truncated if it is longer than
  53. // _kUpb_Status_MaxMessage-4.
  54. void SetErrorMessage(const char* msg)
  55. {
  56. upb_Status_SetErrorMessage(&status_, msg);
  57. }
  58. void SetFormattedErrorMessage(const char* fmt, ...)
  59. {
  60. va_list args;
  61. va_start(args, fmt);
  62. upb_Status_VSetErrorFormat(&status_, fmt, args);
  63. va_end(args);
  64. }
  65. // Resets the status to a successful state with no message.
  66. void Clear()
  67. {
  68. upb_Status_Clear(&status_);
  69. }
  70. private:
  71. upb_Status status_;
  72. };
  73. class Arena
  74. {
  75. public:
  76. // A simple arena with no initial memory block and the default allocator.
  77. Arena() :
  78. ptr_(upb_Arena_New(), upb_Arena_Free)
  79. {
  80. }
  81. Arena(char* initial_block, size_t size) :
  82. ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global), upb_Arena_Free)
  83. {
  84. }
  85. upb_Arena* ptr()
  86. {
  87. return ptr_.get();
  88. }
  89. // Allows this arena to be used as a generic allocator.
  90. //
  91. // The arena does not need free() calls so when using Arena as an allocator
  92. // it is safe to skip them. However they are no-ops so there is no harm in
  93. // calling free() either.
  94. upb_alloc* allocator()
  95. {
  96. return upb_Arena_Alloc(ptr_.get());
  97. }
  98. // Add a cleanup function to run when the arena is destroyed.
  99. // Returns false on out-of-memory.
  100. template<class T>
  101. bool Own(T* obj)
  102. {
  103. return upb_Arena_AddCleanup(ptr_.get(), obj, [](void* obj)
  104. { delete static_cast<T*>(obj); });
  105. }
  106. void Fuse(Arena& other)
  107. {
  108. upb_Arena_Fuse(ptr(), other.ptr());
  109. }
  110. private:
  111. std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
  112. };
  113. // InlinedArena seeds the arenas with a predefined amount of memory. No
  114. // heap memory will be allocated until the initial block is exceeded.
  115. template<int N>
  116. class InlinedArena : public Arena
  117. {
  118. public:
  119. InlinedArena() :
  120. Arena(initial_block_, N)
  121. {
  122. }
  123. private:
  124. InlinedArena(const InlinedArena*) = delete;
  125. InlinedArena& operator=(const InlinedArena*) = delete;
  126. char initial_block_[N];
  127. };
  128. } // namespace upb
  129. #endif // UPB_HPP_