pull/35579/head
Craig Tiller 8 months ago
parent 7d56818db5
commit 6f11268751
  1. 8
      src/core/lib/gprpp/down_cast.h
  2. 8
      src/core/lib/gprpp/dual_ref_counted.h
  3. 4
      src/core/lib/gprpp/orphanable.h
  4. 4
      src/core/lib/gprpp/ref_counted.h
  5. 2
      src/core/lib/gprpp/ref_counted_ptr.h
  6. 4
      src/core/lib/promise/context.h
  7. 4
      test/core/gprpp/down_cast_test.cc

@ -26,10 +26,10 @@
namespace grpc_core {
template <typename To, typename From>
inline To down_cast(From* f) {
inline To DownCast(From* f) {
static_assert(
std::is_base_of<From, typename std::remove_pointer<To>::type>::value,
"down_cast requires a base-to-derived relationship");
"DownCast requires a base-to-derived relationship");
// If we have RTTI & we're in debug, assert that the cast is legal.
#if ABSL_INTERNAL_HAS_RTTI
#ifndef NDEBUG
@ -40,8 +40,8 @@ inline To down_cast(From* f) {
}
template <typename To, typename From>
inline To down_cast(From& f) {
return *down_cast<typename std::remove_reference<To>::type*>(&f);
inline To DownCast(From& f) {
return *DownCast<typename std::remove_reference<To>::type*>(&f);
}
} // namespace grpc_core

@ -70,7 +70,7 @@ class DualRefCounted {
RefCountedPtr<Subclass> RefAsSubclass() {
IncrementRefCount();
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
template <
typename Subclass,
@ -79,7 +79,7 @@ class DualRefCounted {
const char* reason) {
IncrementRefCount(location, reason);
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
void Unref() {
@ -183,7 +183,7 @@ class DualRefCounted {
WeakRefCountedPtr<Subclass> WeakRefAsSubclass() {
IncrementWeakRefCount();
return WeakRefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
template <
typename Subclass,
@ -192,7 +192,7 @@ class DualRefCounted {
const char* reason) {
IncrementWeakRefCount(location, reason);
return WeakRefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
void WeakUnref() {

@ -104,7 +104,7 @@ class InternallyRefCounted : public Orphanable {
RefCountedPtr<Subclass> RefAsSubclass() {
IncrementRefCount();
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
template <
typename Subclass,
@ -113,7 +113,7 @@ class InternallyRefCounted : public Orphanable {
const char* reason) {
IncrementRefCount(location, reason);
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
GRPC_MUST_USE_RESULT RefCountedPtr<Child> RefIfNonZero() {

@ -312,7 +312,7 @@ class RefCounted : public Impl {
RefCountedPtr<Subclass> RefAsSubclass() {
IncrementRefCount();
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
template <
typename Subclass,
@ -321,7 +321,7 @@ class RefCounted : public Impl {
const char* reason) {
IncrementRefCount(location, reason);
return RefCountedPtr<Subclass>(
down_cast<Subclass*>(static_cast<Child*>(this)));
DownCast<Subclass*>(static_cast<Child*>(this)));
}
// RefIfNonZero() for mutable types.

@ -160,7 +160,7 @@ class RefCountedPtr {
template <typename Y,
std::enable_if_t<std::is_base_of<T, Y>::value, bool> = true>
RefCountedPtr<Y> TakeAsSubclass() {
return RefCountedPtr<Y>(down_cast<Y*>(release()));
return RefCountedPtr<Y>(DownCast<Y*>(release()));
}
template <typename Y,

@ -36,7 +36,7 @@ struct ContextType;
// Some contexts can be subclassed. If the subclass is set as that context
// then GetContext<Base>() will return the base, and GetContext<Derived>() will
// down_cast to the derived type.
// DownCast to the derived type.
// Specializations of this type should be created for each derived type, and
// should have a single using statement Base pointing to the derived base class.
// Example:
@ -84,7 +84,7 @@ class Context<T, absl::void_t<typename ContextSubclass<T>::Base>>
public:
using Context<typename ContextSubclass<T>::Base>::Context;
static T* get() {
return down_cast<T*>(Context<typename ContextSubclass<T>::Base>::get());
return DownCast<T*>(Context<typename ContextSubclass<T>::Base>::get());
}
};

@ -32,8 +32,8 @@ class Derived : public Base {
TEST(DownCastTest, DownCast) {
Derived d;
Base* b = &d;
EXPECT_EQ(down_cast<Derived*>(b)->i, 3);
EXPECT_EQ(down_cast<Derived&>(*b).i, 3);
EXPECT_EQ(DownCast<Derived*>(b)->i, 3);
EXPECT_EQ(DownCast<Derived&>(*b).i, 3);
}
} // namespace

Loading…
Cancel
Save