summaryrefslogtreecommitdiffstats
path: root/source/core/smart-pointer.h
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2019-01-31 10:14:26 -0500
committerGitHub <noreply@github.com>2019-01-31 10:14:26 -0500
commit11c547d1e94fa620f527c3590174e6e25ab21883 (patch)
tree008ab5b447af351d2a30fb86c768c9e8e8d8030e /source/core/smart-pointer.h
parent4db0aba1edc5fd55b91457481bae119ef66dde89 (diff)
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
Diffstat (limited to 'source/core/smart-pointer.h')
-rw-r--r--source/core/smart-pointer.h41
1 files changed, 24 insertions, 17 deletions
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<typename T>
- SLANG_FORCE_INLINE const T* dynamicCast() const
- {
- return dynamic_cast<const T*>(this);
- }
- template<typename T>
- SLANG_FORCE_INLINE T* dynamicCast()
- {
- return dynamic_cast<T*>(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 <typename T>
+ SLANG_FORCE_INLINE T* dynamicCast(RefObject* obj) { return dynamic_cast<T*>(obj); }
+ template <typename T>
+ SLANG_FORCE_INLINE const T* dynamicCast(const RefObject* obj) { return dynamic_cast<const T*>(obj); }
+
+ // Like a dynamicCast, but allows a type to implement a specific implementation that is suitable for it
+ template <typename T>
+ SLANG_FORCE_INLINE T* as(RefObject* obj) { return dynamicCast<T>(obj); }
+ template <typename T>
+ SLANG_FORCE_INLINE const T* as(const RefObject* obj) { return dynamicCast<T>(obj); }
+
// "Smart" pointer to a reference-counted object
template<typename T>
struct RefPtr
@@ -182,9 +183,15 @@ namespace Slang
}
template<typename U>
- RefPtr<U> As() const
+ RefPtr<U> dynamicCast() const
+ {
+ return RefPtr<U>(Slang::dynamicCast<U>(pointer));
+ }
+
+ template<typename U>
+ RefPtr<U> as() const
{
- return RefPtr<U>(pointer->template dynamicCast<U>());
+ return RefPtr<U>(Slang::as<U>(pointer));
}
~RefPtr()
@@ -238,4 +245,4 @@ namespace Slang
};
}
-#endif \ No newline at end of file
+#endif