diff options
| author | Yong He <yonghe@outlook.com> | 2019-01-31 13:35:03 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-31 13:35:03 -0800 |
| commit | bcb361db7c5a6f8baa9b2012b9ee9778421f1386 (patch) | |
| tree | 9e5e1703e3b06e109bae6db136bbc2e816f96a2f /source/core | |
| parent | c1fe5f295e843d10e24ae0d053fc3813a29aec89 (diff) | |
| parent | f20c64c348393602ed2a9c873386345cc4b493e8 (diff) | |
Merge branch 'master' into crashfix
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/smart-pointer.h | 41 |
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 |
