Skip to content

Commit

Permalink
'Polyfill' std::make_unique to qgis::make_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 13, 2017
1 parent 24a9c52 commit 85321b4
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/core/qgis.h
Expand Up @@ -30,6 +30,8 @@
#include <QHash>
#include <cstdlib>
#include <cfloat>
#include <memory>
#include <type_traits>
#include <cmath>
#include <qnumeric.h>

Expand Down Expand Up @@ -282,6 +284,7 @@ inline double qgsRound( double number, double places )
*/
namespace qgis
{
// as_const

/**
* Adds const to non-const objects.
Expand All @@ -298,6 +301,42 @@ namespace qgis

template <typename T>
void as_const( const T && ) = delete;

// make_unique - from https://stackoverflow.com/a/17902439/1861260

template<class T> struct _Unique_if
{
typedef std::unique_ptr<T> _Single_object;
};

template<class T> struct _Unique_if<T[]>
{
typedef std::unique_ptr<T[]> _Unknown_bound;
};

template<class T, size_t N> struct _Unique_if<T[N]>
{
typedef void _Known_bound;
};

template<class T, class... Args>
typename _Unique_if<T>::_Single_object
make_unique( Args &&... args )
{
return std::unique_ptr<T>( new T( std::forward<Args>( args )... ) );
}

template<class T>
typename _Unique_if<T>::_Unknown_bound
make_unique( size_t n )
{
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>( new U[n]() );
}

template<class T, class... Args>
typename _Unique_if<T>::_Known_bound
make_unique( Args &&... ) = delete;
}
///@endcond
#endif
Expand Down

0 comments on commit 85321b4

Please sign in to comment.