Skip to content

Commit

Permalink
Port GDAL's cpl::down_cast to qgis::down_cast
Browse files Browse the repository at this point in the history
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 b4091ed
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/core/qgis.h
Expand Up @@ -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 )
{
Expand Down

0 comments on commit b4091ed

Please sign in to comment.