-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Mimic C++20
std::make_unique_for_overwrite
for dynamic arrays
Allows creating a dynamically allocated array that is automatically deleted, by calling `make_unique_for_overwrite<T[]>(numberOfElements)`.
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Modules/Core/Common/test/itkMakeUniqueForOverwriteGTest.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |