Add static_assert to upcasting constructor; add rcp_dynamic_cast()
This commit is contained in:
1 parent
ce921e31be
commit
7880adef89
2 files changed
+36
-1
No files matched your search
+9
-1
@@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <type_traits>
|
||||
|
||||
/**
|
||||
* Reference-counting pointer.
|
||||
@@ -97,10 +98,11 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/* Casting constructor: allows rcp<Base> -> rcp<Derived> */
|
||||
/* Upcasting constructor: allows rcp<Derived> -> rcp<Base> */
|
||||
template <typename U>
|
||||
rcp(const rcp<U> & other) : ptr(static_cast<T *>(other.get_raw()))
|
||||
{
|
||||
static_assert(std::is_base_of<T, U>::value, "rcp: implicit cast must be an upcast");
|
||||
if (ptr)
|
||||
{
|
||||
ptr->rcp_inc();
|
||||
@@ -154,6 +156,12 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
rcp<T> rcp_dynamic_cast(const rcp<U> & other)
|
||||
{
|
||||
return rcp<T>(dynamic_cast<T *>(other.get_raw()));
|
||||
}
|
||||
|
||||
#define rcp_managed_root(classname) \
|
||||
public: \
|
||||
void rcp_inc() const \
|
||||
|
||||
Reference in new issue
Block a user