Skip to content
Permalink
Browse files

Port GDAL's cpl::down_cast to qgis::down_cast

Use qgis::down_cast<Derived*>(pointer_to_base) as equivalent of
static_cast<Derived*>(pointer_to_base) with safe checking in debug
mode.
  • Loading branch information
nyalldawson committed Mar 27, 2021
1 parent e2bf57e commit b4091edb9d32b0bf5566a004f712cae196ccc418
Showing with 22 additions and 0 deletions.
  1. +22 −0 src/core/qgis.h
@@ -377,6 +377,28 @@ inline double qgsRound( double number, int places )
namespace qgis
{

/**
* Use qgis::down_cast<Derived*>(pointer_to_base) as equivalent of
* static_cast<Derived*>(pointer_to_base) with safe checking in debug
* mode.
*
* Only works if no virtual inheritance is involved.
*
* Ported from GDAL's cpl::down_cast method.
*
* \param f pointer to a base class
* \return pointer to a derived class
*/
template<typename To, typename From> inline To down_cast( From *f )
{
static_assert(
( std::is_base_of<From,
typename std::remove_pointer<To>::type>::value ),
"target type not derived from source type" );
Q_ASSERT( f == nullptr || dynamic_cast<To>( f ) != nullptr );
return static_cast<To>( f );
}

template<class T>
QSet<T> listToSet( const QList<T> &list )
{

0 comments on commit b4091ed

Please sign in to comment.