upb.hpp
Go to the documentation of this file.
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 
26 #ifndef UPB_HPP_
27 #define UPB_HPP_
28 
29 #include <memory>
30 
31 #include "upb/upb.h"
32 
33 namespace upb {
34 
35 class Status {
36  public:
38 
39  upb_Status* ptr() { return &status_; }
40 
41  // Returns true if there is no error.
42  bool ok() const { return upb_Status_IsOk(&status_); }
43 
44  // Guaranteed to be NULL-terminated.
45  const char* error_message() const {
47  }
48 
49  // The error message will be truncated if it is longer than
50  // _kUpb_Status_MaxMessage-4.
51  void SetErrorMessage(const char* msg) {
53  }
54  void SetFormattedErrorMessage(const char* fmt, ...) {
55  va_list args;
56  va_start(args, fmt);
58  va_end(args);
59  }
60 
61  // Resets the status to a successful state with no message.
63 
64  private:
66 };
67 
68 class Arena {
69  public:
70  // A simple arena with no initial memory block and the default allocator.
72  Arena(char* initial_block, size_t size)
73  : ptr_(upb_Arena_Init(initial_block, size, &upb_alloc_global),
74  upb_Arena_Free) {}
75 
76  upb_Arena* ptr() { return ptr_.get(); }
77 
78  // Allows this arena to be used as a generic allocator.
79  //
80  // The arena does not need free() calls so when using Arena as an allocator
81  // it is safe to skip them. However they are no-ops so there is no harm in
82  // calling free() either.
83  upb_alloc* allocator() { return upb_Arena_Alloc(ptr_.get()); }
84 
85  // Add a cleanup function to run when the arena is destroyed.
86  // Returns false on out-of-memory.
87  template <class T>
88  bool Own(T* obj) {
89  return upb_Arena_AddCleanup(ptr_.get(), obj,
90  [](void* obj) { delete static_cast<T*>(obj); });
91  }
92 
93  void Fuse(Arena& other) { upb_Arena_Fuse(ptr(), other.ptr()); }
94 
95  private:
96  std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
97 };
98 
99 // InlinedArena seeds the arenas with a predefined amount of memory. No
100 // heap memory will be allocated until the initial block is exceeded.
101 template <int N>
102 class InlinedArena : public Arena {
103  public:
105 
106  private:
107  InlinedArena(const InlinedArena*) = delete;
108  InlinedArena& operator=(const InlinedArena*) = delete;
109 
111 };
112 
113 } // namespace upb
114 
115 #endif // UPB_HPP_
upb::Status::SetErrorMessage
void SetErrorMessage(const char *msg)
Definition: upb.hpp:51
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
upb::Arena::ptr_
std::unique_ptr< upb_Arena, decltype(&upb_Arena_Free)> ptr_
Definition: upb.hpp:96
upb_Arena_Init
upb_Arena * upb_Arena_Init(void *mem, size_t n, upb_alloc *alloc)
Definition: upb/upb/upb.c:216
upb::Status::Clear
void Clear()
Definition: upb.hpp:62
upb_Status_ErrorMessage
const char * upb_Status_ErrorMessage(const upb_Status *status)
Definition: upb/upb/upb.c:52
upb_Arena_New
UPB_INLINE upb_Arena * upb_Arena_New(void)
Definition: upb/upb/upb.h:267
upb_Status_Clear
void upb_Status_Clear(upb_Status *status)
Definition: upb/upb/upb.c:44
T
#define T(upbtypeconst, upbtype, ctype, default_value)
upb_alloc_global
upb_alloc upb_alloc_global
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:2241
upb_Status_SetErrorMessage
void upb_Status_SetErrorMessage(upb_Status *status, const char *msg)
Definition: upb/upb/upb.c:56
upb_Status_VSetErrorFormat
void upb_Status_VSetErrorFormat(upb_Status *status, const char *fmt, va_list args)
Definition: upb/upb/upb.c:70
upb::InlinedArena::operator=
InlinedArena & operator=(const InlinedArena *)=delete
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
upb::Status::Status
Status()
Definition: upb.hpp:37
upb::InlinedArena::InlinedArena
InlinedArena()
Definition: upb.hpp:104
upb.h
upb::Arena::Fuse
void Fuse(Arena &other)
Definition: upb.hpp:93
upb_Arena_AddCleanup
bool upb_Arena_AddCleanup(upb_Arena *a, void *ud, upb_CleanupFunc *func)
Definition: upb/upb/upb.c:278
upb
Definition: def.hpp:38
upb::Status::ptr
upb_Status * ptr()
Definition: upb.hpp:39
upb_Status
Definition: upb/upb/upb.h:52
upb::Status::error_message
const char * error_message() const
Definition: upb.hpp:45
upb::Arena::Own
bool Own(T *obj)
Definition: upb.hpp:88
upb::Arena::Arena
Arena(char *initial_block, size_t size)
Definition: upb.hpp:72
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
upb::Arena::allocator
upb_alloc * allocator()
Definition: upb.hpp:83
upb_Arena_Alloc
UPB_INLINE upb_alloc * upb_Arena_Alloc(upb_Arena *a)
Definition: upb/upb/upb.h:192
upb::Arena::ptr
upb_Arena * ptr()
Definition: upb.hpp:76
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
upb::Arena
Definition: upb.hpp:68
upb::Arena::Arena
Arena()
Definition: upb.hpp:71
upb::InlinedArena
Definition: upb.hpp:102
N
#define N
Definition: sync_test.cc:37
upb::InlinedArena::initial_block_
char initial_block_[N]
Definition: upb.hpp:110
upb_Status_IsOk
bool upb_Status_IsOk(const upb_Status *status)
Definition: upb/upb/upb.c:50
upb_Arena
struct upb_Arena upb_Arena
Definition: upb/upb/upb.h:172
upb::Status::ok
bool ok() const
Definition: upb.hpp:42
upb::Status::SetFormattedErrorMessage
void SetFormattedErrorMessage(const char *fmt,...)
Definition: upb.hpp:54
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
upb_alloc
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.h:299
upb::Status
Definition: upb.hpp:35
upb_Arena
Definition: upb_internal.h:36
upb_Arena_Fuse
bool upb_Arena_Fuse(upb_Arena *a1, upb_Arena *a2)
Definition: upb/upb/upb.c:299
upb_Arena_Free
void upb_Arena_Free(upb_Arena *a)
Definition: upb/upb/upb.c:273
upb::Status::status_
upb_Status status_
Definition: upb.hpp:65


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:47