From 15a521e8143e8e6adcdf558fb1ea31668964ac3c Mon Sep 17 00:00:00 2001 From: ncteisen Date: Fri, 11 May 2018 12:42:38 -0400 Subject: [PATCH] Add single point of maintanence for New Delete friends --- src/core/ext/filters/client_channel/lb_policy.h | 4 +--- src/core/ext/filters/client_channel/resolver.h | 4 +--- src/core/lib/gprpp/memory.h | 11 +++++++++++ src/core/lib/gprpp/orphanable.h | 8 ++------ src/core/lib/gprpp/ref_counted.h | 8 ++------ 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h index 454e00a6907..dab4466b21a 100644 --- a/src/core/ext/filters/client_channel/lb_policy.h +++ b/src/core/ext/filters/client_channel/lb_policy.h @@ -162,9 +162,7 @@ class LoadBalancingPolicy GRPC_ABSTRACT_BASE_CLASS protected: - // So Delete() can access our protected dtor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE explicit LoadBalancingPolicy(const Args& args); virtual ~LoadBalancingPolicy(); diff --git a/src/core/ext/filters/client_channel/resolver.h b/src/core/ext/filters/client_channel/resolver.h index 02380314dd9..c7e37e4468e 100644 --- a/src/core/ext/filters/client_channel/resolver.h +++ b/src/core/ext/filters/client_channel/resolver.h @@ -105,9 +105,7 @@ class Resolver : public InternallyRefCountedWithTracing { GRPC_ABSTRACT_BASE_CLASS protected: - // So Delete() can access our protected dtor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE /// Does NOT take ownership of the reference to \a combiner. // TODO(roth): Once we have a C++-like interface for combiners, this diff --git a/src/core/lib/gprpp/memory.h b/src/core/lib/gprpp/memory.h index ba2f5466758..1354109bf3d 100644 --- a/src/core/lib/gprpp/memory.h +++ b/src/core/lib/gprpp/memory.h @@ -27,6 +27,17 @@ #include #include +// Add this to a class that want to use Delete(), but has a private or +// protected destructor. +#define GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE \ + template \ + friend void Delete(T*); +// Add this to a class that want to use New(), but has a private or +// protected constructor. +#define GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW \ + template \ + friend T* New(Args&&...); + namespace grpc_core { // The alignment of memory returned by gpr_malloc(). diff --git a/src/core/lib/gprpp/orphanable.h b/src/core/lib/gprpp/orphanable.h index 73a73995c7f..d0ec9b6461d 100644 --- a/src/core/lib/gprpp/orphanable.h +++ b/src/core/lib/gprpp/orphanable.h @@ -83,9 +83,7 @@ class InternallyRefCounted : public Orphanable { GRPC_ABSTRACT_BASE_CLASS protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE // Allow RefCountedPtr<> to access Unref() and IncrementRefCount(). friend class RefCountedPtr; @@ -128,9 +126,7 @@ class InternallyRefCountedWithTracing : public Orphanable { GRPC_ABSTRACT_BASE_CLASS protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE // Allow RefCountedPtr<> to access Unref() and IncrementRefCount(). friend class RefCountedPtr; diff --git a/src/core/lib/gprpp/ref_counted.h b/src/core/lib/gprpp/ref_counted.h index c67e3f315ca..ddac5bd4755 100644 --- a/src/core/lib/gprpp/ref_counted.h +++ b/src/core/lib/gprpp/ref_counted.h @@ -65,9 +65,7 @@ class RefCounted { GRPC_ABSTRACT_BASE_CLASS protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE RefCounted() { gpr_ref_init(&refs_, 1); } @@ -135,9 +133,7 @@ class RefCountedWithTracing { GRPC_ABSTRACT_BASE_CLASS protected: - // Allow Delete() to access destructor. - template - friend void Delete(T*); + GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE RefCountedWithTracing() : RefCountedWithTracing(static_cast(nullptr)) {}