Skip to content

Commit 7acfe93

Browse files
committed
IBackendObject cast function
1 parent 6bd2303 commit 7acfe93

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

include/nbl/video/decl/IBackendObject.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ class IBackendObject
2323
bool isCompatibleDevicewise(const IBackendObject* other) const;
2424

2525
bool wasCreatedBy(const ILogicalDevice* device) const;
26+
27+
template<typename derived_t_ptr, typename base_t_ptr>
28+
static inline derived_t_ptr cast(base_t_ptr base, const ILogicalDevice* device)
29+
{
30+
using base_t = std::remove_pointer_t<base_t_ptr>;
31+
using derived_t = std::remove_pointer_t<derived_t_ptr>;
32+
static_assert(std::is_base_of_v<IBackendObject, base_t>,"base_t should be derived from IBackendObject");
33+
static_assert(std::is_base_of_v<base_t,derived_t>,"derived_t should be derived from base_t");
34+
if (!base->wasCreatedBy(device))
35+
return nullptr;
36+
return static_cast<derived_t_ptr>(base);
37+
}
38+
39+
template<typename derived_t_ptr, typename base_t_ptr, typename other_t_ptr>
40+
static inline derived_t_ptr cast(base_t_ptr base, const other_t_ptr& compatibleWith)
41+
{
42+
return cast<derived_t_ptr,base_t_ptr>(base,compatibleWith->getOriginDevice());
43+
}
2644

2745
const ILogicalDevice* getOriginDevice() const;
2846

0 commit comments

Comments
 (0)