Skip to content

Commit

Permalink
ENH: Mimic C++20 std::make_unique_for_overwrite for dynamic arrays
Browse files Browse the repository at this point in the history
Allows creating a dynamically allocated array that is automatically deleted, by
calling `make_unique_for_overwrite<T[]>(numberOfElements)`.
  • Loading branch information
N-Dekker committed Aug 30, 2022
1 parent 05adf2d commit 01e0201
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Modules/Core/Common/include/itkMakeUniqueForOverwrite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

#ifndef itkMakeUniqueForOverwrite_h
#define itkMakeUniqueForOverwrite_h

#include <memory> // For unique_ptr.

#if __cplusplus < 202002L

# include <type_traits> // For remove_extent_t, false_type, etc.

namespace itk
{

// Namespace of implementation details (not part of the public ITK interface).
namespace MakeUniqueForOverwriteImplDetail
{

// `is_unbounded_array` implementation for C++14/C++17. From C++20, `std::is_unbounded_array` is preferred.
template <typename>
struct is_unbounded_array : std::false_type
{};

template <typename T>
struct is_unbounded_array<T[]> : std::true_type
{};

} // namespace MakeUniqueForOverwriteImplDetail


/** `make_unique_for_overwrite` implementation for C++14/C++17, specifically for dynamically sized ("unbounded")
* arrays. From C++20, `std::make_unique_for_overwrite` is preferred.
*
* Example (mind the square brackets):
\code
using ElementType = int;
size_t numberOfElements{ 42 };
// Create a buffer that may be used to store the specified number of elements.
auto buffer = make_unique_for_overwrite<ElementType[]>(numberOfElements);
\endcode
*
*/
template <typename TUnboundedArray>
auto
make_unique_for_overwrite(const size_t numberOfElements)
{
// The template argument must be something like `ElementType[]`, having an empty pair of square brackets.
static_assert(MakeUniqueForOverwriteImplDetail::is_unbounded_array<TUnboundedArray>::value,
"The specified template argument must be an unbounded array type!");
return std::unique_ptr<TUnboundedArray>(new std::remove_extent_t<TUnboundedArray>[numberOfElements]);
}

} // namespace itk

#else

namespace itk
{
using ::std::make_unique_for_overwrite;
}

#endif

#endif // itkMakeUniqueForOverwrite_h
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ set(ITKCommonGTests
itkImageRegionGTest.cxx
itkIndexGTest.cxx
itkIndexRangeGTest.cxx
itkMakeUniqueForOverwriteGTest.cxx
itkMatrixGTest.cxx
itkMersenneTwisterRandomVariateGeneratorGTest.cxx
itkNeighborhoodAllocatorGTest.cxx
Expand Down
43 changes: 43 additions & 0 deletions Modules/Core/Common/test/itkMakeUniqueForOverwriteGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header file to be tested:
#include "itkMakeUniqueForOverwrite.h"
#include <gtest/gtest.h>
#include <numeric> // For iota.


// Tests that `make_unique_for_overwrite` creates an array of the specified dynamic size, whose elements can all be
// written to.
TEST(MakeUniqueForOverwrite, CreatesAnArrayThatCanBeWrittenTo)
{
for (std::size_t numberOfElements = 1; numberOfElements < 4; ++numberOfElements)
{
const auto data = itk::make_unique_for_overwrite<int[]>(numberOfElements);
ASSERT_NE(data, nullptr);

// Write the values { 0, 1, ... , N-1 }.
std::iota(data.get(), data.get() + numberOfElements, 0);

// Check that each value is correctly written to the corresponding element.
for (std::size_t i = 0; i < numberOfElements; ++i)
{
EXPECT_EQ(data[i], i);
}
}
}

0 comments on commit 01e0201

Please sign in to comment.