Skip to content

Commit 11c547d

Browse files
authored
Feature/as refactor (shader-slang#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
1 parent 4db0aba commit 11c547d

17 files changed

+879
-875
lines changed

source/core/smart-pointer.h

+24-17
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,31 @@ namespace Slang
6363
{
6464
return referenceCount;
6565
}
66-
67-
// Use instead of dynamic_cast as it allows for replacement without using Rtti in the future
68-
template<typename T>
69-
SLANG_FORCE_INLINE const T* dynamicCast() const
70-
{
71-
return dynamic_cast<const T*>(this);
72-
}
73-
template<typename T>
74-
SLANG_FORCE_INLINE T* dynamicCast()
75-
{
76-
return dynamic_cast<T*>(this);
77-
}
7866
};
7967

80-
inline void addReference(RefObject* obj)
68+
SLANG_FORCE_INLINE void addReference(RefObject* obj)
8169
{
8270
if(obj) obj->addReference();
8371
}
8472

85-
inline void releaseReference(RefObject* obj)
73+
SLANG_FORCE_INLINE void releaseReference(RefObject* obj)
8674
{
8775
if(obj) obj->releaseReference();
8876
}
8977

78+
// For straight dynamic cast.
79+
// Use instead of dynamic_cast as it allows for replacement without using Rtti in the future
80+
template <typename T>
81+
SLANG_FORCE_INLINE T* dynamicCast(RefObject* obj) { return dynamic_cast<T*>(obj); }
82+
template <typename T>
83+
SLANG_FORCE_INLINE const T* dynamicCast(const RefObject* obj) { return dynamic_cast<const T*>(obj); }
84+
85+
// Like a dynamicCast, but allows a type to implement a specific implementation that is suitable for it
86+
template <typename T>
87+
SLANG_FORCE_INLINE T* as(RefObject* obj) { return dynamicCast<T>(obj); }
88+
template <typename T>
89+
SLANG_FORCE_INLINE const T* as(const RefObject* obj) { return dynamicCast<T>(obj); }
90+
9091
// "Smart" pointer to a reference-counted object
9192
template<typename T>
9293
struct RefPtr
@@ -182,9 +183,15 @@ namespace Slang
182183
}
183184

184185
template<typename U>
185-
RefPtr<U> As() const
186+
RefPtr<U> dynamicCast() const
187+
{
188+
return RefPtr<U>(Slang::dynamicCast<U>(pointer));
189+
}
190+
191+
template<typename U>
192+
RefPtr<U> as() const
186193
{
187-
return RefPtr<U>(pointer->template dynamicCast<U>());
194+
return RefPtr<U>(Slang::as<U>(pointer));
188195
}
189196

190197
~RefPtr()
@@ -238,4 +245,4 @@ namespace Slang
238245
};
239246
}
240247

241-
#endif
248+
#endif

0 commit comments

Comments
 (0)