|
|
|
@ -23,6 +23,7 @@ |
|
|
|
|
#include "upb/wire/encode.h" |
|
|
|
|
|
|
|
|
|
namespace hpb { |
|
|
|
|
class ExtensionRegistry; |
|
|
|
|
using Arena = ::upb::Arena; |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
@ -232,22 +233,6 @@ class ExtensionIdentifier : public ExtensionMiniTableProvider { |
|
|
|
|
friend class PrivateAccess; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
} // namespace hpb
|
|
|
|
|
|
|
|
|
|
namespace protos { |
|
|
|
|
using hpb::Arena; |
|
|
|
|
using hpb::ExtensionNotFoundError; |
|
|
|
|
using hpb::MessageAllocationError; |
|
|
|
|
using hpb::MessageDecodeError; |
|
|
|
|
using hpb::MessageEncodeError; |
|
|
|
|
using hpb::Ptr; |
|
|
|
|
using hpb::SourceLocation; |
|
|
|
|
class ExtensionRegistry; |
|
|
|
|
|
|
|
|
|
namespace internal { |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
upb_Arena* GetArena(Ptr<T> message) { |
|
|
|
|
return static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
@ -294,8 +279,45 @@ absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena, |
|
|
|
|
absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena, |
|
|
|
|
const upb_MiniTableExtension* ext, |
|
|
|
|
const upb_Message* extension); |
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
|
|
class ExtensionRegistry { |
|
|
|
|
public: |
|
|
|
|
ExtensionRegistry( |
|
|
|
|
const std::vector<const internal::ExtensionMiniTableProvider*>& |
|
|
|
|
extensions, |
|
|
|
|
const upb::Arena& arena) |
|
|
|
|
: registry_(upb_ExtensionRegistry_New(arena.ptr())) { |
|
|
|
|
if (registry_) { |
|
|
|
|
for (const auto& ext_provider : extensions) { |
|
|
|
|
const auto* ext = ext_provider->mini_table_ext(); |
|
|
|
|
bool success = upb_ExtensionRegistry_AddArray(registry_, &ext, 1); |
|
|
|
|
if (!success) { |
|
|
|
|
registry_ = nullptr; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
friend upb_ExtensionRegistry* ::hpb::internal::GetUpbExtensions( |
|
|
|
|
const ExtensionRegistry& extension_registry); |
|
|
|
|
upb_ExtensionRegistry* registry_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
} // namespace hpb
|
|
|
|
|
|
|
|
|
|
namespace protos { |
|
|
|
|
using hpb::Arena; |
|
|
|
|
using hpb::ExtensionNotFoundError; |
|
|
|
|
using hpb::MessageAllocationError; |
|
|
|
|
using hpb::MessageDecodeError; |
|
|
|
|
using hpb::MessageEncodeError; |
|
|
|
|
using hpb::Ptr; |
|
|
|
|
using hpb::SourceLocation; |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
typename T::Proxy CreateMessage(::hpb::Arena& arena) { |
|
|
|
|
return typename T::Proxy(upb_Message_New(T::minitable(), arena.ptr()), |
|
|
|
@ -305,7 +327,7 @@ typename T::Proxy CreateMessage(::hpb::Arena& arena) { |
|
|
|
|
template <typename T> |
|
|
|
|
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) { |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
::protos::internal::DeepCopy( |
|
|
|
|
::hpb::internal::DeepCopy( |
|
|
|
|
hpb::internal::GetInternalMsg(target_message), |
|
|
|
|
hpb::internal::GetInternalMsg(source_message), T::minitable(), |
|
|
|
|
static_cast<upb_Arena*>(target_message->GetInternalArena())); |
|
|
|
@ -313,9 +335,9 @@ void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) { |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
typename T::Proxy CloneMessage(Ptr<T> message, upb_Arena* arena) { |
|
|
|
|
return hpb::internal::PrivateAccess::Proxy<T>( |
|
|
|
|
::protos::internal::DeepClone(hpb::internal::GetInternalMsg(message), |
|
|
|
|
T::minitable(), arena), |
|
|
|
|
return ::hpb::internal::PrivateAccess::Proxy<T>( |
|
|
|
|
::hpb::internal::DeepClone(::hpb::internal::GetInternalMsg(message), |
|
|
|
|
T::minitable(), arena), |
|
|
|
|
arena); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -340,41 +362,16 @@ void DeepCopy(const T* source_message, T* target_message) { |
|
|
|
|
template <typename T> |
|
|
|
|
void ClearMessage(hpb::internal::PtrOrRaw<T> message) { |
|
|
|
|
auto ptr = Ptr(message); |
|
|
|
|
auto minitable = internal::GetMiniTable(ptr); |
|
|
|
|
auto minitable = hpb::internal::GetMiniTable(ptr); |
|
|
|
|
upb_Message_Clear(hpb::internal::GetInternalMsg(ptr), minitable); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class ExtensionRegistry { |
|
|
|
|
public: |
|
|
|
|
ExtensionRegistry( |
|
|
|
|
const std::vector<const ::hpb::internal::ExtensionMiniTableProvider*>& |
|
|
|
|
extensions, |
|
|
|
|
const upb::Arena& arena) |
|
|
|
|
: registry_(upb_ExtensionRegistry_New(arena.ptr())) { |
|
|
|
|
if (registry_) { |
|
|
|
|
for (const auto& ext_provider : extensions) { |
|
|
|
|
const auto* ext = ext_provider->mini_table_ext(); |
|
|
|
|
bool success = upb_ExtensionRegistry_AddArray(registry_, &ext, 1); |
|
|
|
|
if (!success) { |
|
|
|
|
registry_ = nullptr; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
friend upb_ExtensionRegistry* ::protos::internal::GetUpbExtensions( |
|
|
|
|
const ExtensionRegistry& extension_registry); |
|
|
|
|
upb_ExtensionRegistry* registry_; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extendee, typename Extension, |
|
|
|
|
typename = hpb::internal::EnableIfHpbClass<T>> |
|
|
|
|
ABSL_MUST_USE_RESULT bool HasExtension( |
|
|
|
|
Ptr<T> message, |
|
|
|
|
const ::hpb::internal::ExtensionIdentifier<Extendee, Extension>& id) { |
|
|
|
|
return ::protos::internal::HasExtensionOrUnknown( |
|
|
|
|
return ::hpb::internal::HasExtensionOrUnknown( |
|
|
|
|
::hpb::internal::GetInternalMsg(message), id.mini_table_ext()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -413,9 +410,9 @@ absl::Status SetExtension( |
|
|
|
|
const Extension& value) { |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
return ::protos::internal::SetExtension( |
|
|
|
|
hpb::internal::GetInternalMsg(message), message_arena, |
|
|
|
|
id.mini_table_ext(), hpb::internal::GetInternalMsg(&value)); |
|
|
|
|
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message), |
|
|
|
|
message_arena, id.mini_table_ext(), |
|
|
|
|
hpb::internal::GetInternalMsg(&value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extension, |
|
|
|
@ -427,9 +424,9 @@ absl::Status SetExtension( |
|
|
|
|
Ptr<Extension> value) { |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
return ::protos::internal::SetExtension( |
|
|
|
|
hpb::internal::GetInternalMsg(message), message_arena, |
|
|
|
|
id.mini_table_ext(), hpb::internal::GetInternalMsg(value)); |
|
|
|
|
return ::hpb::internal::SetExtension(hpb::internal::GetInternalMsg(message), |
|
|
|
|
message_arena, id.mini_table_ext(), |
|
|
|
|
hpb::internal::GetInternalMsg(value)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extension, |
|
|
|
@ -443,10 +440,10 @@ absl::Status SetExtension( |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
auto* message_arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
auto* extension_arena = static_cast<upb_Arena*>(ext.GetInternalArena()); |
|
|
|
|
return ::protos::internal::MoveExtension( |
|
|
|
|
hpb::internal::GetInternalMsg(message), message_arena, |
|
|
|
|
id.mini_table_ext(), hpb::internal::GetInternalMsg(&ext), |
|
|
|
|
extension_arena); |
|
|
|
|
return ::hpb::internal::MoveExtension(hpb::internal::GetInternalMsg(message), |
|
|
|
|
message_arena, id.mini_table_ext(), |
|
|
|
|
hpb::internal::GetInternalMsg(&ext), |
|
|
|
|
extension_arena); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extension, |
|
|
|
@ -481,15 +478,15 @@ absl::StatusOr<Ptr<const Extension>> GetExtension( |
|
|
|
|
const ::hpb::internal::ExtensionIdentifier<Extendee, Extension>& id) { |
|
|
|
|
// TODO: Fix const correctness issues.
|
|
|
|
|
upb_MessageValue value; |
|
|
|
|
const bool ok = ::protos::internal::GetOrPromoteExtension( |
|
|
|
|
const bool ok = ::hpb::internal::GetOrPromoteExtension( |
|
|
|
|
const_cast<upb_Message*>(::hpb::internal::GetInternalMsg(message)), |
|
|
|
|
id.mini_table_ext(), ::protos::internal::GetArena(message), &value); |
|
|
|
|
id.mini_table_ext(), ::hpb::internal::GetArena(message), &value); |
|
|
|
|
if (!ok) { |
|
|
|
|
return ExtensionNotFoundError( |
|
|
|
|
upb_MiniTableExtension_Number(id.mini_table_ext())); |
|
|
|
|
} |
|
|
|
|
return Ptr<const Extension>(::hpb::internal::CreateMessage<Extension>( |
|
|
|
|
(upb_Message*)value.msg_val, ::protos::internal::GetArena(message))); |
|
|
|
|
(upb_Message*)value.msg_val, ::hpb::internal::GetArena(message))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extendee, typename Extension, |
|
|
|
@ -504,11 +501,11 @@ 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::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message)); |
|
|
|
|
::hpb::internal::GetMiniTable(message)); |
|
|
|
|
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
return upb_Decode(bytes.data(), bytes.size(), |
|
|
|
|
::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message), |
|
|
|
|
::hpb::internal::GetMiniTable(message), |
|
|
|
|
/* extreg= */ nullptr, /* options= */ 0, |
|
|
|
|
arena) == kUpb_DecodeStatus_Ok; |
|
|
|
|
} |
|
|
|
@ -516,23 +513,23 @@ ABSL_MUST_USE_RESULT bool Parse(Ptr<T> message, absl::string_view bytes) { |
|
|
|
|
template <typename T> |
|
|
|
|
ABSL_MUST_USE_RESULT bool Parse( |
|
|
|
|
Ptr<T> message, absl::string_view bytes, |
|
|
|
|
const ::protos::ExtensionRegistry& extension_registry) { |
|
|
|
|
const ::hpb::ExtensionRegistry& extension_registry) { |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
upb_Message_Clear(::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message)); |
|
|
|
|
::hpb::internal::GetMiniTable(message)); |
|
|
|
|
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
return upb_Decode(bytes.data(), bytes.size(), |
|
|
|
|
::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message), |
|
|
|
|
::hpb::internal::GetMiniTable(message), |
|
|
|
|
/* extreg= */ |
|
|
|
|
::protos::internal::GetUpbExtensions(extension_registry), |
|
|
|
|
::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 ::protos::ExtensionRegistry& extension_registry) { |
|
|
|
|
const ::hpb::ExtensionRegistry& extension_registry) { |
|
|
|
|
static_assert(!std::is_const_v<T>); |
|
|
|
|
return Parse(Ptr(message, bytes, extension_registry)); |
|
|
|
|
} |
|
|
|
@ -541,11 +538,11 @@ 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::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message)); |
|
|
|
|
::hpb::internal::GetMiniTable(message)); |
|
|
|
|
auto* arena = static_cast<upb_Arena*>(message->GetInternalArena()); |
|
|
|
|
return upb_Decode(bytes.data(), bytes.size(), |
|
|
|
|
::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message), |
|
|
|
|
::hpb::internal::GetMiniTable(message), |
|
|
|
|
/* extreg= */ nullptr, /* options= */ 0, |
|
|
|
|
arena) == kUpb_DecodeStatus_Ok; |
|
|
|
|
} |
|
|
|
@ -556,7 +553,7 @@ absl::StatusOr<T> Parse(absl::string_view bytes, int options = 0) { |
|
|
|
|
auto* arena = static_cast<upb_Arena*>(message.GetInternalArena()); |
|
|
|
|
upb_DecodeStatus status = |
|
|
|
|
upb_Decode(bytes.data(), bytes.size(), message.msg(), |
|
|
|
|
::protos::internal::GetMiniTable(&message), |
|
|
|
|
::hpb::internal::GetMiniTable(&message), |
|
|
|
|
/* extreg= */ nullptr, /* options= */ 0, arena); |
|
|
|
|
if (status == kUpb_DecodeStatus_Ok) { |
|
|
|
|
return message; |
|
|
|
@ -566,14 +563,14 @@ absl::StatusOr<T> Parse(absl::string_view bytes, int options = 0) { |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
absl::StatusOr<T> Parse(absl::string_view bytes, |
|
|
|
|
const ::protos::ExtensionRegistry& extension_registry, |
|
|
|
|
const ::hpb::ExtensionRegistry& extension_registry, |
|
|
|
|
int options = 0) { |
|
|
|
|
T message; |
|
|
|
|
auto* arena = static_cast<upb_Arena*>(message.GetInternalArena()); |
|
|
|
|
upb_DecodeStatus status = |
|
|
|
|
upb_Decode(bytes.data(), bytes.size(), message.msg(), |
|
|
|
|
::protos::internal::GetMiniTable(&message), |
|
|
|
|
::protos::internal::GetUpbExtensions(extension_registry), |
|
|
|
|
::hpb::internal::GetMiniTable(&message), |
|
|
|
|
::hpb::internal::GetUpbExtensions(extension_registry), |
|
|
|
|
/* options= */ 0, arena); |
|
|
|
|
if (status == kUpb_DecodeStatus_Ok) { |
|
|
|
|
return message; |
|
|
|
@ -584,17 +581,17 @@ absl::StatusOr<T> Parse(absl::string_view bytes, |
|
|
|
|
template <typename T> |
|
|
|
|
absl::StatusOr<absl::string_view> Serialize(const T* message, upb::Arena& arena, |
|
|
|
|
int options = 0) { |
|
|
|
|
return ::protos::internal::Serialize( |
|
|
|
|
::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message), arena.ptr(), options); |
|
|
|
|
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::hpb::internal::GetMiniTable(message), |
|
|
|
|
arena.ptr(), options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T> |
|
|
|
|
absl::StatusOr<absl::string_view> Serialize(Ptr<T> message, upb::Arena& arena, |
|
|
|
|
int options = 0) { |
|
|
|
|
return ::protos::internal::Serialize( |
|
|
|
|
::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::protos::internal::GetMiniTable(message), arena.ptr(), options); |
|
|
|
|
return ::hpb::internal::Serialize(::hpb::internal::GetInternalMsg(message), |
|
|
|
|
::hpb::internal::GetMiniTable(message), |
|
|
|
|
arena.ptr(), options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename T, typename Extension> |
|
|
|
|