// 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 UPB_PROTOS_PROTOS_H_ #define UPB_PROTOS_PROTOS_H_ #include #include #include "absl/status/status.h" #include "absl/status/statusor.h" #include "upb/base/status.hpp" #include "upb/mem/arena.hpp" #include "upb/message/copy.h" #include "upb/message/internal/accessors.h" #include "upb/message/internal/extension.h" #include "upb/mini_table/extension.h" #include "upb/wire/decode.h" #include "upb/wire/encode.h" namespace protos { using Arena = ::upb::Arena; class ExtensionRegistry; template using Proxy = std::conditional_t::value, typename std::remove_const_t::CProxy, typename T::Proxy>; // Provides convenient access to Proxy and CProxy message types. // // Using rebinding and handling of const, Ptr and Ptr // allows copying const with T* const and avoids using non-copyable Proxy types // directly. template class Ptr final { public: Ptr() = delete; // Implicit conversions Ptr(T* m) : p_(m) {} // NOLINT Ptr(const Proxy* p) : p_(*p) {} // NOLINT Ptr(Proxy p) : p_(p) {} // NOLINT Ptr(const Ptr& m) = default; Ptr& operator=(Ptr v) & { Proxy::Rebind(p_, v.p_); return *this; } Proxy operator*() const { return p_; } Proxy* operator->() const { return const_cast*>(std::addressof(p_)); } #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wclass-conversion" #endif template ::value, int> = 0> operator Ptr() const { Proxy p(p_); return Ptr(&p); } #ifdef __clang__ #pragma clang diagnostic pop #endif private: Ptr(upb_Message* msg, upb_Arena* arena) : p_(msg, arena) {} // NOLINT friend class Ptr; friend typename T::Access; Proxy p_; }; inline absl::string_view UpbStrToStringView(upb_StringView str) { return absl::string_view(str.data, str.size); } // TODO: update bzl and move to upb runtime / protos.cc. inline upb_StringView UpbStrFromStringView(absl::string_view str, upb_Arena* arena) { const size_t str_size = str.size(); char* buffer = static_cast(upb_Arena_Malloc(arena, str_size)); memcpy(buffer, str.data(), str_size); return upb_StringView_FromDataAndSize(buffer, str_size); } template typename T::Proxy CreateMessage(::protos::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. struct SourceLocation { static SourceLocation current() { return {}; } absl::string_view file_name() { return ""; } int line() { return 0; } }; // end:github_only // begin:google_only // using SourceLocation = absl::SourceLocation; // end:google_only absl::Status MessageAllocationError( SourceLocation loc = SourceLocation::current()); absl::Status ExtensionNotFoundError( int extension_number, SourceLocation loc = SourceLocation::current()); absl::Status MessageDecodeError(upb_DecodeStatus status, SourceLocation loc = SourceLocation::current()); absl::Status MessageEncodeError(upb_EncodeStatus status, SourceLocation loc = SourceLocation::current()); namespace internal { struct PrivateAccess { template static auto* GetInternalMsg(T&& message) { return message->msg(); } template static auto Proxy(upb_Message* p, upb_Arena* arena) { return typename T::Proxy(p, arena); } template static auto CProxy(const upb_Message* p, upb_Arena* arena) { return typename T::CProxy(p, arena); } }; template auto* GetInternalMsg(T&& message) { return PrivateAccess::GetInternalMsg(std::forward(message)); } template T CreateMessage() { return T(); } template typename T::Proxy CreateMessageProxy(upb_Message* msg, upb_Arena* arena) { return typename T::Proxy(msg, arena); } template typename T::CProxy CreateMessage(const upb_Message* msg, upb_Arena* arena) { return PrivateAccess::CProxy(msg, arena); } class ExtensionMiniTableProvider { public: constexpr explicit ExtensionMiniTableProvider( const upb_MiniTableExtension* mini_table_ext) : mini_table_ext_(mini_table_ext) {} const upb_MiniTableExtension* mini_table_ext() const { return mini_table_ext_; } private: const upb_MiniTableExtension* mini_table_ext_; }; // ------------------------------------------------------------------- // ExtensionIdentifier // This is the type of actual extension objects. E.g. if you have: // extend Foo { // optional MyExtension bar = 1234; // } // then "bar" will be defined in C++ as: // ExtensionIdentifier bar(&namespace_bar_ext); template class ExtensionIdentifier : public ExtensionMiniTableProvider { public: using Extension = ExtensionType; using Extendee = ExtendeeType; constexpr explicit ExtensionIdentifier( const upb_MiniTableExtension* mini_table_ext) : ExtensionMiniTableProvider(mini_table_ext) {} }; template upb_Arena* GetArena(Ptr message) { return static_cast(message->GetInternalArena()); } template upb_Arena* GetArena(T* message) { return static_cast(message->GetInternalArena()); } template const upb_MiniTable* GetMiniTable(const T*) { return T::minitable(); } template const upb_MiniTable* GetMiniTable(Ptr) { return T::minitable(); } upb_ExtensionRegistry* GetUpbExtensions( const ExtensionRegistry& extension_registry); absl::StatusOr Serialize(const upb_Message* message, const upb_MiniTable* mini_table, upb_Arena* arena, int options); bool HasExtensionOrUnknown(const upb_Message* msg, const upb_MiniTableExtension* eid); const upb_Extension* GetOrPromoteExtension(upb_Message* msg, const upb_MiniTableExtension* eid, upb_Arena* arena); void DeepCopy(upb_Message* target, const upb_Message* source, const upb_MiniTable* mini_table, upb_Arena* arena); upb_Message* DeepClone(const upb_Message* source, const upb_MiniTable* mini_table, upb_Arena* arena); absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena, const upb_MiniTableExtension* ext, upb_Message* extension, upb_Arena* extension_arena); absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena, const upb_MiniTableExtension* ext, const upb_Message* extension); } // namespace internal template void DeepCopy(Ptr source_message, Ptr target_message) { static_assert(!std::is_const_v); ::protos::internal::DeepCopy( internal::GetInternalMsg(target_message), internal::GetInternalMsg(source_message), T::minitable(), static_cast(target_message->GetInternalArena())); } template typename T::Proxy CloneMessage(Ptr message, upb_Arena* arena) { return internal::PrivateAccess::Proxy( ::protos::internal::DeepClone(internal::GetInternalMsg(message), T::minitable(), arena), arena); } template void DeepCopy(Ptr source_message, T* target_message) { static_assert(!std::is_const_v); DeepCopy(source_message, protos::Ptr(target_message)); } template void DeepCopy(const T* source_message, Ptr target_message) { static_assert(!std::is_const_v); DeepCopy(protos::Ptr(source_message), target_message); } template void DeepCopy(const T* source_message, T* target_message) { static_assert(!std::is_const_v); DeepCopy(protos::Ptr(source_message), protos::Ptr(target_message)); } template void ClearMessage(Ptr message) { static_assert(!std::is_const_v, ""); upb_Message_Clear(internal::GetInternalMsg(message), T::minitable()); } template void ClearMessage(T* message) { ClearMessage(protos::Ptr(message)); } class ExtensionRegistry { public: ExtensionRegistry( const std::vector& 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 using EnableIfProtosClass = std::enable_if_t< std::is_base_of::value && std::is_base_of::value>; template using EnableIfMutableProto = std::enable_if_t::value>; template > ABSL_MUST_USE_RESULT bool HasExtension( Ptr message, const ::protos::internal::ExtensionIdentifier& id) { return ::protos::internal::HasExtensionOrUnknown( ::protos::internal::GetInternalMsg(message), id.mini_table_ext()); } template > ABSL_MUST_USE_RESULT bool HasExtension( const T* message, const ::protos::internal::ExtensionIdentifier& id) { return HasExtension(protos::Ptr(message), id); } template , typename = EnableIfMutableProto> void ClearExtension( Ptr message, const ::protos::internal::ExtensionIdentifier& id) { static_assert(!std::is_const_v, ""); _upb_Message_ClearExtensionField(internal::GetInternalMsg(message), id.mini_table_ext()); } template > void ClearExtension( T* message, const ::protos::internal::ExtensionIdentifier& id) { ClearExtension(::protos::Ptr(message), id); } template , typename = EnableIfMutableProto> absl::Status SetExtension( Ptr message, const ::protos::internal::ExtensionIdentifier& id, const Extension& value) { static_assert(!std::is_const_v); auto* message_arena = static_cast(message->GetInternalArena()); return ::protos::internal::SetExtension(internal::GetInternalMsg(message), message_arena, id.mini_table_ext(), internal::GetInternalMsg(&value)); } template , typename = EnableIfMutableProto> absl::Status SetExtension( Ptr message, const ::protos::internal::ExtensionIdentifier& id, Extension&& value) { Extension ext = std::move(value); static_assert(!std::is_const_v); auto* message_arena = static_cast(message->GetInternalArena()); auto* extension_arena = static_cast(ext.GetInternalArena()); return ::protos::internal::MoveExtension( internal::GetInternalMsg(message), message_arena, id.mini_table_ext(), internal::GetInternalMsg(&ext), extension_arena); } template > absl::Status SetExtension( T* message, const ::protos::internal::ExtensionIdentifier& id, const Extension& value) { return ::protos::SetExtension(::protos::Ptr(message), id, value); } template > absl::Status SetExtension( T* message, const ::protos::internal::ExtensionIdentifier& id, Extension&& value) { return ::protos::SetExtension(::protos::Ptr(message), id, std::forward(value)); } template > absl::StatusOr> GetExtension( Ptr message, const ::protos::internal::ExtensionIdentifier& id) { // TODO: Fix const correctness issues. const upb_Extension* ext = ::protos::internal::GetOrPromoteExtension( const_cast(internal::GetInternalMsg(message)), id.mini_table_ext(), ::protos::internal::GetArena(message)); if (!ext) { return ExtensionNotFoundError( upb_MiniTableExtension_Number(id.mini_table_ext())); } return Ptr(::protos::internal::CreateMessage( (upb_Message*)ext->data.ptr, ::protos::internal::GetArena(message))); } template > absl::StatusOr> GetExtension( const T* message, const ::protos::internal::ExtensionIdentifier& id) { return GetExtension(protos::Ptr(message), id); } template ABSL_MUST_USE_RESULT bool Parse(Ptr message, absl::string_view bytes) { static_assert(!std::is_const_v); upb_Message_Clear(internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message), /* extreg= */ nullptr, /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; } template ABSL_MUST_USE_RESULT bool Parse( Ptr message, absl::string_view bytes, const ::protos::ExtensionRegistry& extension_registry) { static_assert(!std::is_const_v); upb_Message_Clear(internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message), /* extreg= */ ::protos::internal::GetUpbExtensions(extension_registry), /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; } template ABSL_MUST_USE_RESULT bool Parse( T* message, absl::string_view bytes, const ::protos::ExtensionRegistry& extension_registry) { static_assert(!std::is_const_v); return Parse(protos::Ptr(message, bytes, extension_registry)); } template ABSL_MUST_USE_RESULT bool Parse(T* message, absl::string_view bytes) { static_assert(!std::is_const_v); upb_Message_Clear(internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message)); auto* arena = static_cast(message->GetInternalArena()); return upb_Decode(bytes.data(), bytes.size(), internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message), /* extreg= */ nullptr, /* options= */ 0, arena) == kUpb_DecodeStatus_Ok; } template absl::StatusOr Parse(absl::string_view bytes, int options = 0) { T message; auto* arena = static_cast(message.GetInternalArena()); upb_DecodeStatus status = upb_Decode(bytes.data(), bytes.size(), message.msg(), ::protos::internal::GetMiniTable(&message), /* extreg= */ nullptr, /* options= */ 0, arena); if (status == kUpb_DecodeStatus_Ok) { return message; } return MessageDecodeError(status); } template absl::StatusOr Parse(absl::string_view bytes, const ::protos::ExtensionRegistry& extension_registry, int options = 0) { T message; auto* arena = static_cast(message.GetInternalArena()); upb_DecodeStatus status = upb_Decode(bytes.data(), bytes.size(), message.msg(), ::protos::internal::GetMiniTable(&message), ::protos::internal::GetUpbExtensions(extension_registry), /* options= */ 0, arena); if (status == kUpb_DecodeStatus_Ok) { return message; } return MessageDecodeError(status); } template absl::StatusOr Serialize(const T* message, upb::Arena& arena, int options = 0) { return ::protos::internal::Serialize( internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message), arena.ptr(), options); } template absl::StatusOr Serialize(Ptr message, upb::Arena& arena, int options = 0) { return ::protos::internal::Serialize( internal::GetInternalMsg(message), ::protos::internal::GetMiniTable(message), arena.ptr(), options); } } // namespace protos #endif // UPB_PROTOS_PROTOS_H_