diff options
author | Hein-Pieter van Braam <hp@tmm.cx> | 2017-08-24 21:58:13 +0200 |
---|---|---|
committer | Hein-Pieter van Braam <hp@tmm.cx> | 2017-08-24 23:08:24 +0200 |
commit | 4aa2c18cb428ffde05c67987926736a9ca62703b (patch) | |
tree | e7daf34bc35fac8768dda5eda81520ea8d27ff6b /core/object.h | |
parent | f61e8695c9952928d19e6896bc412b638927b25c (diff) |
Add a static version of Object::cast_to()
This is to prepare to replace all instances of the member version of
cast_to().
Diffstat (limited to 'core/object.h')
-rw-r--r-- | core/object.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/object.h b/core/object.h index 43d77ed49d..ab1421e357 100644 --- a/core/object.h +++ b/core/object.h @@ -558,6 +558,46 @@ public: void add_change_receptor(Object *p_receptor); void remove_change_receptor(Object *p_receptor); + template <class T> + static T *cast_to(Object *p_object) { +#ifdef DEBUG_ENABLED + // TODO there are some legitimate reasons to pass NULL as p_object. + // we need to figure out how to deal with that in debug mode. + // This code will return NULL for a NULL input in release mode also. + ERR_FAIL_COND_V(p_object == NULL, NULL); +#endif +#ifndef NO_SAFE_CAST + return dynamic_cast<T *>(p_object); +#else + if (!p_object) + return NULL; + if (p_pobject->is_class_ptr(T::get_class_ptr_static())) + return static_cast<T *>(p_pobject); + else + return NULL; +#endif + } + + template <class T> + static const T *cast_to(const Object *p_object) { +#ifdef DEBUG_ENABLED + // TODO there are some legitimate reasons to pass NULL as p_object. + // we need to figure out how to deal with that in debug mode. + // This code will return NULL for a NULL input in release mode also. + ERR_FAIL_COND_V(p_object == NULL, NULL); +#endif +#ifndef NO_SAFE_CAST + return dynamic_cast<const T *>(p_object); +#else + if (!p_object) + return NULL; + if (p_pobject->is_class_ptr(T::get_class_ptr_static())) + return static_cast<const T *>(p_object); + else + return NULL; +#endif + } + // TODO: ensure 'this' is never NULL since it's UB, but by now, avoid warning flood #ifdef __clang__ #pragma clang diagnostic push |