Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Use std::make_unique instead of our polyfill or naked new

The use we previously used the polyfill or naked new is that we
supported C++11, which did not yet have `std::make_unique`. However,
with the move to C++14 as the minimum, `std::make_unique` can be
expected to be always available.
Commit
ea6db670632204bace889f646ca08bfa58bddeab
Author
Martin Hořeňovský ·
Parents
a7b3e087a03f

Files · Download .tar.gz

Changes

 examples/300-Gen-OwnGenerator.cpp         |  4 +++-
 examples/301-Gen-MapTypeConversion.cpp    |  4 +---
 src/catch2/catch_enum_values_registry.cpp |  2 +-
 src/catch2/catch_generators.hpp           | 14 +++-----------
 src/catch2/catch_generators_generic.hpp   | 12 ++++++------
 src/catch2/catch_generators_specific.hpp  | 12 ++++++------
 src/catch2/catch_reporter_registrars.hpp  |  2 +-
 src/catch2/catch_stream.cpp               |  2 +-
 8 files changed, 22 insertions(+), 30 deletions(-)

diff --git a/examples/300-Gen-OwnGenerator.cpp b/examples/300-Gen-OwnGenerator.cpp
index 427d7b24..0cb80c2f 100644
--- a/examples/300-Gen-OwnGenerator.cpp
+++ b/examples/300-Gen-OwnGenerator.cpp
@@ -40,7 +40,9 @@ int const& RandomIntGenerator::get() const {
 // Notice that it returns an instance of GeneratorWrapper<int>, which
 // is a value-wrapper around std::unique_ptr<IGenerator<int>>.
 Catch::Generators::GeneratorWrapper<int> random(int low, int high) {
-    return Catch::Generators::GeneratorWrapper<int>(std::unique_ptr<Catch::Generators::IGenerator<int>>(new RandomIntGenerator(low, high)));
+    return Catch::Generators::GeneratorWrapper<int>(
+        std::make_unique<RandomIntGenerator>(low, high)
+    );
 }
 
 // The two sections in this test case are equivalent, but the first one
diff --git a/examples/301-Gen-MapTypeConversion.cpp b/examples/301-Gen-MapTypeConversion.cpp
index dee50822..66f582d6 100644
--- a/examples/301-Gen-MapTypeConversion.cpp
+++ b/examples/301-Gen-MapTypeConversion.cpp
@@ -38,9 +38,7 @@ std::string const& LineGenerator::get() const {
 // is a value-wrapper around std::unique_ptr<IGenerator<std::string>>.
 Catch::Generators::GeneratorWrapper<std::string> lines(std::string /* ignored for example */) {
     return Catch::Generators::GeneratorWrapper<std::string>(
-        std::unique_ptr<Catch::Generators::IGenerator<std::string>>(
-            new LineGenerator()
-        )
+        std::make_unique<LineGenerator>()
     );
 }
 
diff --git a/src/catch2/catch_enum_values_registry.cpp b/src/catch2/catch_enum_values_registry.cpp
index f7d11860..79261bcf 100644
--- a/src/catch2/catch_enum_values_registry.cpp
+++ b/src/catch2/catch_enum_values_registry.cpp
@@ -52,7 +52,7 @@ namespace Catch {
         }
 
         std::unique_ptr<EnumInfo> makeEnumInfo( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
-            std::unique_ptr<EnumInfo> enumInfo( new EnumInfo );
+            auto enumInfo = std::make_unique<EnumInfo>();
             enumInfo->m_name = enumName;
             enumInfo->m_values.reserve( values.size() );
 
diff --git a/src/catch2/catch_generators.hpp b/src/catch2/catch_generators.hpp
index 3756a719..359db5f4 100644
--- a/src/catch2/catch_generators.hpp
+++ b/src/catch2/catch_generators.hpp
@@ -33,14 +33,6 @@ public:
 
 namespace Generators {
 
-    // !TBD move this into its own location?
-    namespace pf{
-        template<typename T, typename... Args>
-        std::unique_ptr<T> make_unique( Args&&... args ) {
-            return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
-        }
-    }
-
     template<typename T>
     struct IGenerator : GeneratorUntypedBase {
         virtual ~IGenerator() = default;
@@ -104,11 +96,11 @@ namespace Generators {
 
     template <typename T>
     GeneratorWrapper<T> value(T&& value) {
-        return GeneratorWrapper<T>(pf::make_unique<SingleValueGenerator<T>>(std::forward<T>(value)));
+        return GeneratorWrapper<T>(std::make_unique<SingleValueGenerator<T>>(std::forward<T>(value)));
     }
     template <typename T>
     GeneratorWrapper<T> values(std::initializer_list<T> values) {
-        return GeneratorWrapper<T>(pf::make_unique<FixedValuesGenerator<T>>(values));
+        return GeneratorWrapper<T>(std::make_unique<FixedValuesGenerator<T>>(values));
     }
 
     template<typename T>
@@ -193,7 +185,7 @@ namespace Generators {
 
         IGeneratorTracker& tracker = acquireGeneratorTracker( lineInfo );
         if (!tracker.hasGenerator()) {
-            tracker.setGenerator(pf::make_unique<Generators<UnderlyingType>>(generatorExpression()));
+            tracker.setGenerator(std::make_unique<Generators<UnderlyingType>>(generatorExpression()));
         }
 
         auto const& generator = static_cast<IGenerator<UnderlyingType> const&>( *tracker.getGenerator() );
diff --git a/src/catch2/catch_generators_generic.hpp b/src/catch2/catch_generators_generic.hpp
index 3cfa9202..5a8708dc 100644
--- a/src/catch2/catch_generators_generic.hpp
+++ b/src/catch2/catch_generators_generic.hpp
@@ -46,7 +46,7 @@ namespace Generators {
 
     template <typename T>
     GeneratorWrapper<T> take(size_t target, GeneratorWrapper<T>&& generator) {
-        return GeneratorWrapper<T>(pf::make_unique<TakeGenerator<T>>(target, std::move(generator)));
+        return GeneratorWrapper<T>(std::make_unique<TakeGenerator<T>>(target, std::move(generator)));
     }
 
 
@@ -87,7 +87,7 @@ namespace Generators {
 
     template <typename T, typename Predicate>
     GeneratorWrapper<T> filter(Predicate&& pred, GeneratorWrapper<T>&& generator) {
-        return GeneratorWrapper<T>(std::unique_ptr<IGenerator<T>>(pf::make_unique<FilterGenerator<T, Predicate>>(std::forward<Predicate>(pred), std::move(generator))));
+        return GeneratorWrapper<T>(std::unique_ptr<IGenerator<T>>(std::make_unique<FilterGenerator<T, Predicate>>(std::forward<Predicate>(pred), std::move(generator))));
     }
 
     template <typename T>
@@ -143,7 +143,7 @@ namespace Generators {
 
     template <typename T>
     GeneratorWrapper<T> repeat(size_t repeats, GeneratorWrapper<T>&& generator) {
-        return GeneratorWrapper<T>(pf::make_unique<RepeatGenerator<T>>(repeats, std::move(generator)));
+        return GeneratorWrapper<T>(std::make_unique<RepeatGenerator<T>>(repeats, std::move(generator)));
     }
 
     template <typename T, typename U, typename Func>
@@ -176,14 +176,14 @@ namespace Generators {
     template <typename Func, typename U, typename T = FunctionReturnType<Func, U>>
     GeneratorWrapper<T> map(Func&& function, GeneratorWrapper<U>&& generator) {
         return GeneratorWrapper<T>(
-            pf::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator))
+            std::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator))
         );
     }
 
     template <typename T, typename U, typename Func>
     GeneratorWrapper<T> map(Func&& function, GeneratorWrapper<U>&& generator) {
         return GeneratorWrapper<T>(
-            pf::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator))
+            std::make_unique<MapGenerator<T, U, Func>>(std::forward<Func>(function), std::move(generator))
         );
     }
 
@@ -226,7 +226,7 @@ namespace Generators {
     template <typename T>
     GeneratorWrapper<std::vector<T>> chunk(size_t size, GeneratorWrapper<T>&& generator) {
         return GeneratorWrapper<std::vector<T>>(
-            pf::make_unique<ChunkGenerator<T>>(size, std::move(generator))
+            std::make_unique<ChunkGenerator<T>>(size, std::move(generator))
         );
     }
 
diff --git a/src/catch2/catch_generators_specific.hpp b/src/catch2/catch_generators_specific.hpp
index 0e497c3e..acca7d97 100644
--- a/src/catch2/catch_generators_specific.hpp
+++ b/src/catch2/catch_generators_specific.hpp
@@ -68,7 +68,7 @@ typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::va
 GeneratorWrapper<T>>::type
 random(T a, T b) {
     return GeneratorWrapper<T>(
-        pf::make_unique<RandomIntegerGenerator<T>>(a, b)
+        std::make_unique<RandomIntegerGenerator<T>>(a, b)
     );
 }
 
@@ -77,7 +77,7 @@ typename std::enable_if<std::is_floating_point<T>::value,
 GeneratorWrapper<T>>::type
 random(T a, T b) {
     return GeneratorWrapper<T>(
-        pf::make_unique<RandomFloatingGenerator<T>>(a, b)
+        std::make_unique<RandomFloatingGenerator<T>>(a, b)
     );
 }
 
@@ -118,13 +118,13 @@ public:
 template <typename T>
 GeneratorWrapper<T> range(T const& start, T const& end, T const& step) {
     static_assert(std::is_arithmetic<T>::value && !std::is_same<T, bool>::value, "Type must be numeric");
-    return GeneratorWrapper<T>(pf::make_unique<RangeGenerator<T>>(start, end, step));
+    return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end, step));
 }
 
 template <typename T>
 GeneratorWrapper<T> range(T const& start, T const& end) {
     static_assert(std::is_integral<T>::value && !std::is_same<T, bool>::value, "Type must be an integer");
-    return GeneratorWrapper<T>(pf::make_unique<RangeGenerator<T>>(start, end));
+    return GeneratorWrapper<T>(std::make_unique<RangeGenerator<T>>(start, end));
 }
 
 
@@ -158,13 +158,13 @@ template <typename InputIterator,
           typename InputSentinel,
           typename ResultType = typename std::iterator_traits<InputIterator>::value_type>
 GeneratorWrapper<ResultType> from_range(InputIterator from, InputSentinel to) {
-    return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(from, to));
+    return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(from, to));
 }
 
 template <typename Container,
           typename ResultType = typename Container::value_type>
 GeneratorWrapper<ResultType> from_range(Container const& cnt) {
-    return GeneratorWrapper<ResultType>(pf::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
+    return GeneratorWrapper<ResultType>(std::make_unique<IteratorGenerator<ResultType>>(cnt.begin(), cnt.end()));
 }
 
 
diff --git a/src/catch2/catch_reporter_registrars.hpp b/src/catch2/catch_reporter_registrars.hpp
index cbc4fb73..4aecd518 100644
--- a/src/catch2/catch_reporter_registrars.hpp
+++ b/src/catch2/catch_reporter_registrars.hpp
@@ -40,7 +40,7 @@ namespace Catch {
         class ListenerFactory : public IReporterFactory {
 
             IStreamingReporterPtr create( ReporterConfig const& config ) const override {
-                return std::unique_ptr<T>( new T( config ) );
+                return std::make_unique<T>(config);
             }
             std::string getDescription() const override {
                 return std::string();
diff --git a/src/catch2/catch_stream.cpp b/src/catch2/catch_stream.cpp
index 6c8e2828..23a43001 100644
--- a/src/catch2/catch_stream.cpp
+++ b/src/catch2/catch_stream.cpp
@@ -108,7 +108,7 @@ namespace Catch {
             mutable std::ostream m_os;
         public:
             DebugOutStream()
-            :   m_streamBuf( new StreamBufImpl<OutputDebugWriter>() ),
+            :   m_streamBuf( std::make_unique<StreamBufImpl<OutputDebugWriter>>() ),
                 m_os( m_streamBuf.get() )
             {}