add Poco::makeUnique()

This commit is contained in:
Alex Fabijanic 2017-10-20 18:31:13 -05:00
parent 151304135e
commit 51c28b5c5a
14 changed files with 107 additions and 4 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">

View File

@ -1079,6 +1079,7 @@
<ClInclude Include="include\Poco\LogStream.h" />
<ClInclude Include="include\Poco\LRUCache.h" />
<ClInclude Include="include\Poco\LRUStrategy.h" />
<ClInclude Include="include\Poco\MakeUnique.h" />
<ClInclude Include="include\Poco\Manifest.h" />
<ClInclude Include="include\Poco\MD4Engine.h" />
<ClInclude Include="include\Poco\MD5Engine.h" />

View File

@ -1895,6 +1895,9 @@
<ClInclude Include="include\Poco\NamedEvent_WIN32.h">
<Filter>Processes\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Poco\MakeUnique.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="src\pocomsg.rc">

View File

@ -1087,6 +1087,7 @@
<ClInclude Include="include\Poco\LogStream.h" />
<ClInclude Include="include\Poco\LRUCache.h" />
<ClInclude Include="include\Poco\LRUStrategy.h" />
<ClInclude Include="include\Poco\MakeUnique.h" />
<ClInclude Include="include\Poco\Manifest.h" />
<ClInclude Include="include\Poco\MD4Engine.h" />
<ClInclude Include="include\Poco\MD5Engine.h" />

View File

@ -1901,6 +1901,9 @@
<ClInclude Include="include\Poco\ChecksumImpl.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Poco\MakeUnique.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="src\pocomsg.rc">

View File

@ -1077,6 +1077,7 @@
<ClInclude Include="include\Poco\LogStream.h" />
<ClInclude Include="include\Poco\LRUCache.h" />
<ClInclude Include="include\Poco\LRUStrategy.h" />
<ClInclude Include="include\Poco\MakeUnique.h" />
<ClInclude Include="include\Poco\Manifest.h" />
<ClInclude Include="include\Poco\MD4Engine.h" />
<ClInclude Include="include\Poco\MD5Engine.h" />

View File

@ -1895,6 +1895,9 @@
<ClInclude Include="include\Poco\ChecksumImpl.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Poco\MakeUnique.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="src\pocomsg.rc">

View File

@ -1077,6 +1077,7 @@
<ClInclude Include="include\Poco\LogStream.h" />
<ClInclude Include="include\Poco\LRUCache.h" />
<ClInclude Include="include\Poco\LRUStrategy.h" />
<ClInclude Include="include\Poco\MakeUnique.h" />
<ClInclude Include="include\Poco\Manifest.h" />
<ClInclude Include="include\Poco\MD4Engine.h" />
<ClInclude Include="include\Poco\MD5Engine.h" />

View File

@ -1895,6 +1895,9 @@
<ClInclude Include="include\Poco\ChecksumImpl.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\Poco\MakeUnique.h">
<Filter>Core\Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="src\pocomsg.rc">

View File

@ -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 <cstddef>
#include <memory>
#include <type_traits>
#include <utility>
namespace Poco {
template<class T> struct UniqueIf
{
typedef std::unique_ptr<T> SingleObject;
};
template<class T> struct UniqueIf<T[]>
{
typedef std::unique_ptr<T[]> UnknownBound;
};
template<class T, size_t N> struct UniqueIf<T[N]>
{
typedef void KnownBound;
};
template<class T, class... Args>
typename UniqueIf<T>::SingleObject makeUnique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T>
typename UniqueIf<T>::UnknownBound makeUnique(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 UniqueIf<T>::KnownBound makeUnique(Args&&...) = delete;
} // namespace Poco
#endif // Foundation_MakeUnique_INCLUDED

View File

@ -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 <iostream>
#include <sstream>
@ -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<int>() == 0);
assert (*makeUnique<int>(1729) == 1729);
assert (*makeUnique<std::string>() == "");
assert (*makeUnique<std::string>("meow") == "meow");
assert (*makeUnique<std::string>(6, 'z') == "zzzzzz");
auto up = makeUnique<int[]>(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;
}

View File

@ -34,6 +34,7 @@ public:
void testNullable();
void testAscii();
void testChecksum64();
void testMakeUnique();
void setUp();
void tearDown();