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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class Status {
  31. public:
  32. Status() { upb_Status_Clear(&status_); }
  33. upb_Status* ptr() { return &status_; }
  34. // Returns true if there is no error.
  35. bool ok() const { return upb_Status_IsOk(&status_); }
  36. // Guaranteed to be NULL-terminated.
  37. const char* error_message() const {
  38. return upb_Status_ErrorMessage(&status_);
  39. }
  40. // The error message will be truncated if it is longer than
  41. // _kUpb_Status_MaxMessage-4.
  42. void SetErrorMessage(const char* msg) {
  43. upb_Status_SetErrorMessage(&status_, msg);
  44. }
  45. void SetFormattedErrorMessage(const char* fmt, ...) {
  46. va_list args;
  47. va_start(args, fmt);
  48. upb_Status_VSetErrorFormat(&status_, fmt, args);
  49. va_end(args);
  50. }
  51. // Resets the status to a successful state with no message.
  52. void Clear() { upb_Status_Clear(&status_); }
  53. private:
  54. upb_Status status_;
  55. };
  56. class Arena {
  57. public:
  58. // A simple arena with no initial memory block and the default allocator.
  59. Arena() : ptr_(upb_Arena_New(), upb_Arena_Free) {}
  60. Arena(char* initial_block, size_t size)
  61. : ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global),
  62. upb_Arena_Free) {}
  63. upb_Arena* ptr() { return ptr_.get(); }
  64. // Allows this arena to be used as a generic allocator.
  65. //
  66. // The arena does not need free() calls so when using Arena as an allocator
  67. // it is safe to skip them. However they are no-ops so there is no harm in
  68. // calling free() either.
  69. upb_alloc* allocator() { return upb_Arena_Alloc(ptr_.get()); }
  70. // Add a cleanup function to run when the arena is destroyed.
  71. // Returns false on out-of-memory.
  72. template <class T>
  73. bool Own(T* obj) {
  74. return upb_Arena_AddCleanup(ptr_.get(), obj,
  75. [](void* obj) { delete static_cast<T*>(obj); });
  76. }
  77. void Fuse(Arena& other) { upb_Arena_Fuse(ptr(), other.ptr()); }
  78. private:
  79. std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
  80. };
  81. // InlinedArena seeds the arenas with a predefined amount of memory. No
  82. // heap memory will be allocated until the initial block is exceeded.
  83. template <int N>
  84. class InlinedArena : public Arena {
  85. public:
  86. InlinedArena() : Arena(initial_block_, N) {}
  87. private:
  88. InlinedArena(const InlinedArena*) = delete;
  89. InlinedArena& operator=(const InlinedArena*) = delete;
  90. char initial_block_[N];
  91. };
  92. } // namespace upb
  93. #endif // UPB_HPP_