From 11c547d1e94fa620f527c3590174e6e25ab21883 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 31 Jan 2019 10:14:26 -0500 Subject: Feature/as refactor (#817) * Made dynamicCast a free function. * Replace As with as or dynamicCast depending on if it is a type. * Fix problem with using non smart pointer cast. * Removed legacy asXXXX methods. * Remove As from Type. * Removed As from Qual type -> made coercable into Type*, such that can just use free 'as'. * Remove left over QualType::As() impl. * Remove As from SyntaxNodeBase. * Made as for instructions implemented by dynamicCast. * Replace As on DeclRef. Use the global as<> to do the cast. * Add const safe versions of dynamicCast and as for IRInst --- source/core/smart-pointer.h | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'source/core/smart-pointer.h') diff --git a/source/core/smart-pointer.h b/source/core/smart-pointer.h index dd00acabd..d026388c0 100644 --- a/source/core/smart-pointer.h +++ b/source/core/smart-pointer.h @@ -63,30 +63,31 @@ namespace Slang { return referenceCount; } - - // Use instead of dynamic_cast as it allows for replacement without using Rtti in the future - template - SLANG_FORCE_INLINE const T* dynamicCast() const - { - return dynamic_cast(this); - } - template - SLANG_FORCE_INLINE T* dynamicCast() - { - return dynamic_cast(this); - } }; - inline void addReference(RefObject* obj) + SLANG_FORCE_INLINE void addReference(RefObject* obj) { if(obj) obj->addReference(); } - inline void releaseReference(RefObject* obj) + SLANG_FORCE_INLINE void releaseReference(RefObject* obj) { if(obj) obj->releaseReference(); } + // For straight dynamic cast. + // Use instead of dynamic_cast as it allows for replacement without using Rtti in the future + template + SLANG_FORCE_INLINE T* dynamicCast(RefObject* obj) { return dynamic_cast(obj); } + template + SLANG_FORCE_INLINE const T* dynamicCast(const RefObject* obj) { return dynamic_cast(obj); } + + // Like a dynamicCast, but allows a type to implement a specific implementation that is suitable for it + template + SLANG_FORCE_INLINE T* as(RefObject* obj) { return dynamicCast(obj); } + template + SLANG_FORCE_INLINE const T* as(const RefObject* obj) { return dynamicCast(obj); } + // "Smart" pointer to a reference-counted object template struct RefPtr @@ -182,9 +183,15 @@ namespace Slang } template - RefPtr As() const + RefPtr dynamicCast() const + { + return RefPtr(Slang::dynamicCast(pointer)); + } + + template + RefPtr as() const { - return RefPtr(pointer->template dynamicCast()); + return RefPtr(Slang::as(pointer)); } ~RefPtr() @@ -238,4 +245,4 @@ namespace Slang }; } -#endif \ No newline at end of file +#endif -- cgit v1.2.3