// Protocol Buffers - Google's data interchange format // Copyright 2024 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 GOOGLE_PROTOBUF_HPB_PTR_H__ #define GOOGLE_PROTOBUF_HPB_PTR_H__ #include #include class upb_Message; class upb_Arena; namespace hpb { 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_; }; // Suppress -Wctad-maybe-unsupported with our manual deduction guide template Ptr(T* m) -> Ptr; } // namespace hpb #endif // GOOGLE_PROTOBUF_HPB_PTR_H__