Bring in {UpbStrToStringView, UpbStrFromStringView}, MessageAlloc/Decode/Encode, and some internal template helpers into hpb

PiperOrigin-RevId: 655136424
pull/17572/head
Hong Shin 4 months ago committed by Copybara-Service
parent d6dd8f2ee4
commit be3d8cb7dc
  1. 4
      hpb/hpb.cc
  2. 123
      hpb/hpb.h
  3. 9
      hpb_generator/gen_accessors.cc
  4. 6
      hpb_generator/gen_repeated_fields.cc

@ -27,7 +27,7 @@
#include "upb/wire/decode.h"
#include "upb/wire/encode.h"
namespace protos {
namespace hpb {
// begin:google_only
absl::Status MessageAllocationError(SourceLocation loc) {
@ -80,7 +80,9 @@ absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc
// return absl::Status(absl::StatusCode::kUnknown, "Upb message parse error");
// }
// end:github_only
} // namespace hpb
namespace protos {
namespace internal {
using ::hpb::internal::upb_extension_locker_global;

@ -82,13 +82,7 @@ class Ptr final {
template <typename T>
Ptr(T* m) -> Ptr<T>;
} // namespace hpb
namespace protos {
using hpb::Arena;
using hpb::Ptr;
class ExtensionRegistry;
// TODO: b/354766950 - Move upb-specific chunks out of hpb header
inline absl::string_view UpbStrToStringView(upb_StringView str) {
return absl::string_view(str.data, str.size);
}
@ -102,12 +96,6 @@ inline upb_StringView UpbStrFromStringView(absl::string_view str,
return upb_StringView_FromDataAndSize(buffer, str_size);
}
template <typename T>
typename T::Proxy CreateMessage(::hpb::Arena& arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena.ptr()),
arena.ptr());
}
// begin:github_only
// // This type exists to work around an absl type that has not yet been
// // released.
@ -134,6 +122,48 @@ absl::Status MessageDecodeError(upb_DecodeStatus status,
absl::Status MessageEncodeError(upb_EncodeStatus status,
SourceLocation loc = SourceLocation::current());
namespace internal {
template <typename T>
struct RemovePtr;
template <typename T>
struct RemovePtr<Ptr<T>> {
using type = T;
};
template <typename T>
struct RemovePtr<T*> {
using type = T;
};
template <typename T>
using RemovePtrT = typename RemovePtr<T>::type;
template <typename T, typename U = RemovePtrT<T>,
typename = std::enable_if_t<!std::is_const_v<U>>>
using PtrOrRaw = T;
template <typename T>
using EnableIfHpbClass = std::enable_if_t<
std::is_base_of<typename T::Access, T>::value &&
std::is_base_of<typename T::Access, typename T::ExtendableType>::value>;
template <typename T>
using EnableIfMutableProto = std::enable_if_t<!std::is_const<T>::value>;
} // 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 {
struct PrivateAccess {
template <typename T>
@ -261,36 +291,13 @@ 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
template <typename T>
struct RemovePtr;
template <typename T>
struct RemovePtr<Ptr<T>> {
using type = T;
};
template <typename T>
struct RemovePtr<T*> {
using type = T;
};
template <typename T>
using RemovePtrT = typename RemovePtr<T>::type;
template <typename T, typename U = RemovePtrT<T>,
typename = std::enable_if_t<!std::is_const_v<U>>>
using PtrOrRaw = T;
template <typename T>
using EnableIfHpbClass = std::enable_if_t<
std::is_base_of<typename T::Access, T>::value &&
std::is_base_of<typename T::Access, typename T::ExtendableType>::value>;
template <typename T>
using EnableIfMutableProto = std::enable_if_t<!std::is_const<T>::value>;
} // namespace internal
typename T::Proxy CreateMessage(::hpb::Arena& arena) {
return typename T::Proxy(upb_Message_New(T::minitable(), arena.ptr()),
arena.ptr());
}
template <typename T>
void DeepCopy(Ptr<const T> source_message, Ptr<T> target_message) {
@ -328,7 +335,7 @@ void DeepCopy(const T* source_message, T* target_message) {
}
template <typename T>
void ClearMessage(internal::PtrOrRaw<T> message) {
void ClearMessage(hpb::internal::PtrOrRaw<T> message) {
auto ptr = Ptr(message);
auto minitable = internal::GetMiniTable(ptr);
upb_Message_Clear(internal::GetInternalMsg(ptr), minitable);
@ -360,7 +367,7 @@ class ExtensionRegistry {
};
template <typename T, typename Extendee, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
ABSL_MUST_USE_RESULT bool HasExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
@ -369,7 +376,7 @@ ABSL_MUST_USE_RESULT bool HasExtension(
}
template <typename T, typename Extendee, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
ABSL_MUST_USE_RESULT bool HasExtension(
const T* message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
@ -377,8 +384,8 @@ ABSL_MUST_USE_RESULT bool HasExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>,
typename = internal::EnableIfMutableProto<T>>
typename = hpb::internal::EnableIfHpbClass<T>,
typename = hpb::internal::EnableIfMutableProto<T>>
void ClearExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<T, Extension>& id) {
@ -388,7 +395,7 @@ void ClearExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
void ClearExtension(
T* message,
const ::protos::internal::ExtensionIdentifier<T, Extension>& id) {
@ -396,8 +403,8 @@ void ClearExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>,
typename = internal::EnableIfMutableProto<T>>
typename = hpb::internal::EnableIfHpbClass<T>,
typename = hpb::internal::EnableIfMutableProto<T>>
absl::Status SetExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
@ -410,8 +417,8 @@ absl::Status SetExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>,
typename = internal::EnableIfMutableProto<T>>
typename = hpb::internal::EnableIfHpbClass<T>,
typename = hpb::internal::EnableIfMutableProto<T>>
absl::Status SetExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
@ -424,8 +431,8 @@ absl::Status SetExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>,
typename = internal::EnableIfMutableProto<T>>
typename = hpb::internal::EnableIfHpbClass<T>,
typename = hpb::internal::EnableIfMutableProto<T>>
absl::Status SetExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
@ -440,7 +447,7 @@ absl::Status SetExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
absl::Status SetExtension(
T* message, const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
const Extension& value) {
@ -448,7 +455,7 @@ absl::Status SetExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
absl::Status SetExtension(
T* message, const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
Extension&& value) {
@ -457,7 +464,7 @@ absl::Status SetExtension(
}
template <typename T, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
absl::Status SetExtension(
T* message, const ::protos::internal::ExtensionIdentifier<T, Extension>& id,
Ptr<Extension> value) {
@ -465,7 +472,7 @@ absl::Status SetExtension(
}
template <typename T, typename Extendee, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
absl::StatusOr<Ptr<const Extension>> GetExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
@ -483,7 +490,7 @@ absl::StatusOr<Ptr<const Extension>> GetExtension(
}
template <typename T, typename Extendee, typename Extension,
typename = internal::EnableIfHpbClass<T>>
typename = hpb::internal::EnableIfHpbClass<T>>
absl::StatusOr<Ptr<const Extension>> GetExtension(
const T* message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {

@ -222,7 +222,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Output& output) {
output(
R"cc(
$1 $0::$2() const {
return ::protos::UpbStrToStringView($3_$4(msg_));
return ::hpb::UpbStrToStringView($3_$4(msg_));
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -231,7 +231,7 @@ void WriteAccessorsInSource(const protobuf::Descriptor* desc, Output& output) {
output(
R"cc(
void $0::set_$2($1 value) {
$4_set_$3(msg_, ::protos::UpbStrFromStringView(value, $5));
$4_set_$3(msg_, ::hpb::UpbStrFromStringView(value, $5));
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -347,9 +347,8 @@ void WriteMapAccessorDefinitions(const protobuf::Descriptor* message,
output(
R"cc(
bool $0::set_$1($2 key, $3 value) {
$5return $4_$7_set(msg_, $6,
::protos::UpbStrFromStringView(value, arena_),
arena_);
$5return $4_$7_set(
msg_, $6, ::hpb::UpbStrFromStringView(value, arena_), arena_);
}
)cc",
class_name, resolved_field_name, CppConstType(key), CppConstType(val),

@ -205,7 +205,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
size_t len;
auto* ptr = $3_mutable_$4(msg_, &len);
assert(index < len);
return ::protos::UpbStrToStringView(*(ptr + index));
return ::hpb::UpbStrToStringView(*(ptr + index));
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -220,7 +220,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
output(
R"cc(
bool $0::add_$2($1 val) {
return $3_add_$4(msg_, ::protos::UpbStrFromStringView(val, arena_), arena_);
return $3_add_$4(msg_, ::hpb::UpbStrFromStringView(val, arena_), arena_);
}
)cc",
class_name, CppConstType(field), resolved_field_name,
@ -231,7 +231,7 @@ void WriteRepeatedStringAccessor(const protobuf::Descriptor* message,
size_t len;
auto* ptr = $3_mutable_$4(msg_, &len);
assert(index < len);
*(ptr + index) = ::protos::UpbStrFromStringView(val, arena_);
*(ptr + index) = ::hpb::UpbStrFromStringView(val, arena_);
}
)cc",
class_name, CppConstType(field), resolved_field_name,

Loading…
Cancel
Save