-
Notifications
You must be signed in to change notification settings - Fork 15.5k
/
hpb.h
184 lines (161 loc) · 6.58 KB
/
hpb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef PROTOBUF_HPB_HPB_H_
#define PROTOBUF_HPB_HPB_H_
#include <cstdint>
#include <type_traits>
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/hpb/arena.h"
#include "google/protobuf/hpb/backend/upb/interop.h"
#include "google/protobuf/hpb/extension.h"
#include "google/protobuf/hpb/internal/internal.h"
#include "google/protobuf/hpb/internal/message_lock.h"
#include "google/protobuf/hpb/internal/template_help.h"
#include "google/protobuf/hpb/ptr.h"
#include "google/protobuf/hpb/status.h"
#include "upb/mini_table/extension.h"
#include "upb/wire/decode.h"
#ifdef HPB_BACKEND_UPB
#include "google/protobuf/hpb/backend/upb/upb.h"
#else
#error hpb backend must be specified
#endif
namespace hpb {
#ifdef HPB_BACKEND_UPB
namespace backend = ::hpb::internal::backend::upb;
#endif
template <typename T>
typename T::Proxy CreateMessage(hpb::Arena& arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena.ptr()),
arena.ptr());
}
template <typename T>
typename T::Proxy CloneMessage(Ptr<T> message, upb_Arena* arena) {
return ::hpb::internal::PrivateAccess::Proxy<T>(
::hpb::internal::DeepClone(hpb::interop::upb::GetMessage(message),
T::minitable(), arena),
arena);
}
template <typename T>
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
::hpb::internal::DeepCopy(hpb::interop::upb::GetMessage(target_message),
hpb::interop::upb::GetMessage(source_message),
T::minitable(),
hpb::interop::upb::GetArena(target_message));
}
template <typename T>
void DeepCopy(Ptr<const T> source_message, T* target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(source_message, Ptr(target_message));
}
template <typename T>
void DeepCopy(const T* source_message, Ptr<T> target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(Ptr(source_message), target_message);
}
template <typename T>
void DeepCopy(const T* source_message, T* target_message) {
static_assert(!std::is_const_v<T>);
DeepCopy(Ptr(source_message), Ptr(target_message));
}
template <typename T>
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
backend::ClearMessage(message);
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(Ptr<T> message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = hpb::interop::upb::GetArena(message);
return upb_Decode(bytes.data(), bytes.size(),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(
Ptr<T> message, absl::string_view bytes,
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = hpb::interop::upb::GetArena(message);
return upb_Decode(bytes.data(), bytes.size(),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena) == kUpb_DecodeStatus_Ok;
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(
T* message, absl::string_view bytes,
const ::hpb::ExtensionRegistry& extension_registry) {
static_assert(!std::is_const_v<T>);
return Parse(Ptr(message, bytes, extension_registry));
}
template <typename T>
ABSL_MUST_USE_RESULT bool Parse(T* message, absl::string_view bytes) {
static_assert(!std::is_const_v<T>);
upb_Message_Clear(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message));
auto* arena = hpb::interop::upb::GetArena(message);
return upb_Decode(bytes.data(), bytes.size(),
hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
/* extreg= */ nullptr, /* options= */ 0,
arena) == kUpb_DecodeStatus_Ok;
}
template <typename T>
absl::StatusOr<T> Parse(absl::string_view bytes, int options = 0) {
T message;
auto* arena = hpb::interop::upb::GetArena(&message);
upb_DecodeStatus status =
upb_Decode(bytes.data(), bytes.size(), message.msg(),
::hpb::interop::upb::GetMiniTable(&message),
/* extreg= */ nullptr, /* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
return message;
}
return MessageDecodeError(status);
}
template <typename T>
absl::StatusOr<T> Parse(absl::string_view bytes,
const ::hpb::ExtensionRegistry& extension_registry,
int options = 0) {
T message;
auto* arena = hpb::interop::upb::GetArena(&message);
upb_DecodeStatus status =
upb_Decode(bytes.data(), bytes.size(), message.msg(),
::hpb::interop::upb::GetMiniTable(&message),
::hpb::internal::GetUpbExtensions(extension_registry),
/* options= */ 0, arena);
if (status == kUpb_DecodeStatus_Ok) {
return message;
}
return MessageDecodeError(status);
}
template <typename T>
absl::StatusOr<absl::string_view> Serialize(const T* message, hpb::Arena& arena,
int options = 0) {
return ::hpb::internal::Serialize(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}
template <typename T>
absl::StatusOr<absl::string_view> Serialize(Ptr<T> message, hpb::Arena& arena,
int options = 0) {
return ::hpb::internal::Serialize(hpb::interop::upb::GetMessage(message),
::hpb::interop::upb::GetMiniTable(message),
arena.ptr(), options);
}
} // namespace hpb
#endif // PROTOBUF_HPB_HPB_H_