upb.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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" AND
  16. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. // DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  19. // DIRECT, 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_ok(&status_); }
  36. // Guaranteed to be NULL-terminated.
  37. const char *error_message() const { return upb_status_errmsg(&status_); }
  38. // The error message will be truncated if it is longer than
  39. // UPB_STATUS_MAX_MESSAGE-4.
  40. void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); }
  41. void SetFormattedErrorMessage(const char *fmt, ...) {
  42. va_list args;
  43. va_start(args, fmt);
  44. upb_status_vseterrf(&status_, fmt, args);
  45. va_end(args);
  46. }
  47. // Resets the status to a successful state with no message.
  48. void Clear() { upb_status_clear(&status_); }
  49. private:
  50. upb_status status_;
  51. };
  52. class Arena {
  53. public:
  54. // A simple arena with no initial memory block and the default allocator.
  55. Arena() : ptr_(upb_arena_new(), upb_arena_free) {}
  56. Arena(char *initial_block, size_t size)
  57. : ptr_(upb_arena_init(initial_block, size, &upb_alloc_global),
  58. upb_arena_free) {}
  59. upb_arena* ptr() { return ptr_.get(); }
  60. // Allows this arena to be used as a generic allocator.
  61. //
  62. // The arena does not need free() calls so when using Arena as an allocator
  63. // it is safe to skip them. However they are no-ops so there is no harm in
  64. // calling free() either.
  65. upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); }
  66. // Add a cleanup function to run when the arena is destroyed.
  67. // Returns false on out-of-memory.
  68. template <class T>
  69. bool Own(T *obj) {
  70. return upb_arena_addcleanup(ptr_.get(), obj, [](void* obj) {
  71. delete static_cast<T*>(obj);
  72. });
  73. }
  74. void Fuse(Arena& other) { upb_arena_fuse(ptr(), other.ptr()); }
  75. private:
  76. std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
  77. };
  78. // InlinedArena seeds the arenas with a predefined amount of memory. No
  79. // heap memory will be allocated until the initial block is exceeded.
  80. template <int N>
  81. class InlinedArena : public Arena {
  82. public:
  83. InlinedArena() : Arena(initial_block_, N) {}
  84. private:
  85. InlinedArena(const InlinedArena*) = delete;
  86. InlinedArena& operator=(const InlinedArena*) = delete;
  87. char initial_block_[N];
  88. };
  89. } // namespace upb
  90. #endif // UPB_HPP_