PiperOrigin-RevId: 663441859pull/17828/head
parent
e49ce62d1e
commit
cb845a5962
5 changed files with 158 additions and 85 deletions
@ -0,0 +1,20 @@ |
||||
# Copyright (c) 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 |
||||
|
||||
# begin:google_only |
||||
# package(default_applicable_licenses = ["//src/google/protobuf:license"]) |
||||
# end:google_only |
||||
|
||||
cc_library( |
||||
name = "template_help", |
||||
hdrs = ["template_help.h"], |
||||
compatible_with = ["//buildenv/target:non_prod"], |
||||
visibility = ["//hpb:__subpackages__"], |
||||
deps = [ |
||||
"//hpb:ptr", |
||||
], |
||||
) |
@ -0,0 +1,49 @@ |
||||
// 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_TEMPLATE_HELP_H__ |
||||
#define GOOGLE_PROTOBUF_HPB_TEMPLATE_HELP_H__ |
||||
|
||||
#include <type_traits> |
||||
|
||||
#include "google/protobuf/hpb/ptr.h" |
||||
|
||||
namespace hpb { |
||||
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
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_HPB_TEMPLATE_HELP_H__
|
@ -0,0 +1,78 @@ |
||||
// 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 <memory> |
||||
#include <type_traits> |
||||
|
||||
class upb_Message; |
||||
class upb_Arena; |
||||
|
||||
namespace hpb { |
||||
|
||||
template <typename T> |
||||
using Proxy = std::conditional_t<std::is_const<T>::value, |
||||
typename std::remove_const_t<T>::CProxy, |
||||
typename T::Proxy>; |
||||
|
||||
// Provides convenient access to Proxy and CProxy message types.
|
||||
//
|
||||
// Using rebinding and handling of const, Ptr<Message> and Ptr<const Message>
|
||||
// allows copying const with T* const and avoids using non-copyable Proxy types
|
||||
// directly.
|
||||
template <typename T> |
||||
class Ptr final { |
||||
public: |
||||
Ptr() = delete; |
||||
|
||||
// Implicit conversions
|
||||
Ptr(T* m) : p_(m) {} // NOLINT
|
||||
Ptr(const Proxy<T>* p) : p_(*p) {} // NOLINT
|
||||
Ptr(Proxy<T> p) : p_(p) {} // NOLINT
|
||||
Ptr(const Ptr& m) = default; |
||||
|
||||
Ptr& operator=(Ptr v) & { |
||||
Proxy<T>::Rebind(p_, v.p_); |
||||
return *this; |
||||
} |
||||
|
||||
Proxy<T> operator*() const { return p_; } |
||||
Proxy<T>* operator->() const { |
||||
return const_cast<Proxy<T>*>(std::addressof(p_)); |
||||
} |
||||
|
||||
#ifdef __clang__ |
||||
#pragma clang diagnostic push |
||||
#pragma clang diagnostic ignored "-Wclass-conversion" |
||||
#endif |
||||
template <typename U = T, std::enable_if_t<!std::is_const<U>::value, int> = 0> |
||||
operator Ptr<const T>() const { |
||||
Proxy<const T> p(p_); |
||||
return Ptr<const T>(&p); |
||||
} |
||||
#ifdef __clang__ |
||||
#pragma clang diagnostic pop |
||||
#endif |
||||
|
||||
private: |
||||
Ptr(upb_Message* msg, upb_Arena* arena) : p_(msg, arena) {} // NOLINT
|
||||
|
||||
friend class Ptr<const T>; |
||||
friend typename T::Access; |
||||
|
||||
Proxy<T> p_; |
||||
}; |
||||
|
||||
// Suppress -Wctad-maybe-unsupported with our manual deduction guide
|
||||
template <typename T> |
||||
Ptr(T* m) -> Ptr<T>; |
||||
|
||||
} // namespace hpb
|
||||
|
||||
#endif // GOOGLE_PROTOBUF_HPB_PTR_H__
|
Loading…
Reference in new issue