From 51c28b5c5a79a5eace735e2eaea76fb0d58fe462 Mon Sep 17 00:00:00 2001 From: Alex Fabijanic Date: Fri, 20 Oct 2017 18:31:13 -0500 Subject: [PATCH] add Poco::makeUnique() --- CppUnit/CppUnit_vs140.vcxproj.filters | 2 +- CppUnit/CppUnit_x64_vs140.vcxproj.filters | 2 +- CppUnit/CppUnit_x64_vs150.vcxproj.filters | 2 +- Foundation/Foundation_vs140.vcxproj | 1 + Foundation/Foundation_vs140.vcxproj.filters | 3 + Foundation/Foundation_vs150.vcxproj | 1 + Foundation/Foundation_vs150.vcxproj.filters | 3 + Foundation/Foundation_x64_vs140.vcxproj | 1 + .../Foundation_x64_vs140.vcxproj.filters | 3 + Foundation/Foundation_x64_vs150.vcxproj | 1 + .../Foundation_x64_vs150.vcxproj.filters | 3 + Foundation/include/Poco/MakeUnique.h | 69 +++++++++++++++++++ Foundation/testsuite/src/CoreTest.cpp | 19 ++++- Foundation/testsuite/src/CoreTest.h | 1 + 14 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 Foundation/include/Poco/MakeUnique.h diff --git a/CppUnit/CppUnit_vs140.vcxproj.filters b/CppUnit/CppUnit_vs140.vcxproj.filters index 21d0807bd4..e03653555f 100644 --- a/CppUnit/CppUnit_vs140.vcxproj.filters +++ b/CppUnit/CppUnit_vs140.vcxproj.filters @@ -1,4 +1,4 @@ - + diff --git a/CppUnit/CppUnit_x64_vs140.vcxproj.filters b/CppUnit/CppUnit_x64_vs140.vcxproj.filters index 70980c693b..27de8e1f7c 100644 --- a/CppUnit/CppUnit_x64_vs140.vcxproj.filters +++ b/CppUnit/CppUnit_x64_vs140.vcxproj.filters @@ -1,4 +1,4 @@ - + diff --git a/CppUnit/CppUnit_x64_vs150.vcxproj.filters b/CppUnit/CppUnit_x64_vs150.vcxproj.filters index 70980c693b..27de8e1f7c 100644 --- a/CppUnit/CppUnit_x64_vs150.vcxproj.filters +++ b/CppUnit/CppUnit_x64_vs150.vcxproj.filters @@ -1,4 +1,4 @@ - + diff --git a/Foundation/Foundation_vs140.vcxproj b/Foundation/Foundation_vs140.vcxproj index b4395aa23f..202da55f61 100644 --- a/Foundation/Foundation_vs140.vcxproj +++ b/Foundation/Foundation_vs140.vcxproj @@ -1079,6 +1079,7 @@ + diff --git a/Foundation/Foundation_vs140.vcxproj.filters b/Foundation/Foundation_vs140.vcxproj.filters index ccc1c66909..db30fe804d 100644 --- a/Foundation/Foundation_vs140.vcxproj.filters +++ b/Foundation/Foundation_vs140.vcxproj.filters @@ -1895,6 +1895,9 @@ Processes\Header Files + + Core\Header Files + diff --git a/Foundation/Foundation_vs150.vcxproj b/Foundation/Foundation_vs150.vcxproj index 21ab56e306..a7e3a763a4 100644 --- a/Foundation/Foundation_vs150.vcxproj +++ b/Foundation/Foundation_vs150.vcxproj @@ -1087,6 +1087,7 @@ + diff --git a/Foundation/Foundation_vs150.vcxproj.filters b/Foundation/Foundation_vs150.vcxproj.filters index 9c7e751233..967ed60036 100644 --- a/Foundation/Foundation_vs150.vcxproj.filters +++ b/Foundation/Foundation_vs150.vcxproj.filters @@ -1901,6 +1901,9 @@ Core\Header Files + + Core\Header Files + diff --git a/Foundation/Foundation_x64_vs140.vcxproj b/Foundation/Foundation_x64_vs140.vcxproj index c18d76f053..dbe8a10509 100644 --- a/Foundation/Foundation_x64_vs140.vcxproj +++ b/Foundation/Foundation_x64_vs140.vcxproj @@ -1077,6 +1077,7 @@ + diff --git a/Foundation/Foundation_x64_vs140.vcxproj.filters b/Foundation/Foundation_x64_vs140.vcxproj.filters index 727c14708e..67e28edb40 100644 --- a/Foundation/Foundation_x64_vs140.vcxproj.filters +++ b/Foundation/Foundation_x64_vs140.vcxproj.filters @@ -1895,6 +1895,9 @@ Core\Header Files + + Core\Header Files + diff --git a/Foundation/Foundation_x64_vs150.vcxproj b/Foundation/Foundation_x64_vs150.vcxproj index 661e337513..f659e5e587 100644 --- a/Foundation/Foundation_x64_vs150.vcxproj +++ b/Foundation/Foundation_x64_vs150.vcxproj @@ -1077,6 +1077,7 @@ + diff --git a/Foundation/Foundation_x64_vs150.vcxproj.filters b/Foundation/Foundation_x64_vs150.vcxproj.filters index 727c14708e..67e28edb40 100644 --- a/Foundation/Foundation_x64_vs150.vcxproj.filters +++ b/Foundation/Foundation_x64_vs150.vcxproj.filters @@ -1895,6 +1895,9 @@ Core\Header Files + + Core\Header Files + diff --git a/Foundation/include/Poco/MakeUnique.h b/Foundation/include/Poco/MakeUnique.h new file mode 100644 index 0000000000..15741603a4 --- /dev/null +++ b/Foundation/include/Poco/MakeUnique.h @@ -0,0 +1,69 @@ +// +// MakeUnique.h +// +// Library: Foundation +// Package: Core +// Module: MakeUnique +// +// Definition of the MakeUnique template class. This is essentially std::make_unique +// for pre-C++14 compilers. +// +// Code adapted for naming convention from https://isocpp.org/files/papers/N3656.txt +// +// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// SPDX-License-Identifier: BSL-1.0 +// + + +#ifndef Foundation_MakeUnique_INCLUDED +#define Foundation_MakeUnique_INCLUDED + + +#include "Poco/Foundation.h" +#include +#include +#include +#include + + +namespace Poco { + + + template struct UniqueIf + { + typedef std::unique_ptr SingleObject; + }; + + template struct UniqueIf + { + typedef std::unique_ptr UnknownBound; + }; + + template struct UniqueIf + { + typedef void KnownBound; + }; + + template + typename UniqueIf::SingleObject makeUnique(Args&&... args) + { + return std::unique_ptr(new T(std::forward(args)...)); + } + + template + typename UniqueIf::UnknownBound makeUnique(size_t n) + { + typedef typename std::remove_extent::type U; + return std::unique_ptr(new U[n]()); + } + + template + typename UniqueIf::KnownBound makeUnique(Args&&...) = delete; + + +} // namespace Poco + + +#endif // Foundation_MakeUnique_INCLUDED diff --git a/Foundation/testsuite/src/CoreTest.cpp b/Foundation/testsuite/src/CoreTest.cpp index 0db50f0d5d..2eef271b29 100644 --- a/Foundation/testsuite/src/CoreTest.cpp +++ b/Foundation/testsuite/src/CoreTest.cpp @@ -24,6 +24,7 @@ #include "Poco/BasicEvent.h" #include "Poco/Delegate.h" #include "Poco/Checksum.h" +#include "Poco/MakeUnique.h" #include "Poco/Exception.h" #include #include @@ -45,6 +46,7 @@ using Poco::delegate; using Poco::NullType; using Poco::InvalidAccessException; using Poco::Checksum; +using Poco::makeUnique; namespace @@ -581,7 +583,6 @@ void CoreTest::testAscii() } - void CoreTest::testChecksum64() { Poco::Checksum checksum64_0(Checksum::TYPE_CRC64); @@ -634,6 +635,21 @@ void CoreTest::testChecksum64() } +void CoreTest::testMakeUnique() +{ + assert (*makeUnique() == 0); + assert (*makeUnique(1729) == 1729); + assert (*makeUnique() == ""); + assert (*makeUnique("meow") == "meow"); + assert (*makeUnique(6, 'z') == "zzzzzz"); + + auto up = makeUnique(5); + + for (int i = 0; i < 5; ++i) up[i] = i; + for (int i = 0; i < 5; ++i) assert (up[i] == i); +} + + void CoreTest::setUp() { _readableToNot = 0; @@ -661,6 +677,7 @@ CppUnit::Test* CoreTest::suite() CppUnit_addTest(pSuite, CoreTest, testNullable); CppUnit_addTest(pSuite, CoreTest, testAscii); CppUnit_addTest(pSuite, CoreTest, testChecksum64); + CppUnit_addTest(pSuite, CoreTest, testMakeUnique); return pSuite; } diff --git a/Foundation/testsuite/src/CoreTest.h b/Foundation/testsuite/src/CoreTest.h index fbb67f293e..7e24161fc4 100644 --- a/Foundation/testsuite/src/CoreTest.h +++ b/Foundation/testsuite/src/CoreTest.h @@ -34,6 +34,7 @@ class CoreTest: public CppUnit::TestCase void testNullable(); void testAscii(); void testChecksum64(); + void testMakeUnique(); void setUp(); void tearDown();