Skip to content

Commit

Permalink
ENH: Make index type of VectorContainer SizeValueType by default
Browse files Browse the repository at this point in the history
By default, assume `ElementIdentifier = SizeValueType`, for an
`itk::VectorContainer<ElementType>`.

Conceptually like:

    template <typename TElementIdentifier = SizeValueType, typename TElement>
    class VectorContainer;

Implemented by moving the old `VectorContainer` definition into the
`itk::detail` namespace, and introducing a new
`itk::VectorContainer<T1, T2>` alias template.
  • Loading branch information
N-Dekker committed Oct 3, 2024
1 parent 3b04ad1 commit 4b5e5be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
19 changes: 19 additions & 0 deletions Modules/Core/Common/include/itkVectorContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
#include "itkObject.h"
#include "itkObjectFactory.h"

#include <type_traits> // For is_void_v.
#include <utility>
#include <vector>

namespace itk
{
namespace detail
{
/** \class VectorContainer
* \brief Define a front-end to the STL "vector" container that conforms to the
* IndexedContainerInterface.
Expand Down Expand Up @@ -561,6 +564,22 @@ class ITK_TEMPLATE_EXPORT VectorContainer
, VectorType(first, last)
{}
};
} // namespace detail


/** Alias template, allowing to use `itk::VectorContainer<TElement>` without having to explicitly specify its
* `ElementIdentifier` type.
*
* The template parameters `T1` and `T2` allow specifying the index type and the element type, as follows:
*
* \tparam T1 The index type OR (when `T2` is `void`) the element type.
*
* \tparam T2 The element type OR `void`. When `T2` is `void`, the element type is specified by the first template
* argument (T1), and the index type will be `SizeValueType`.
*/
template <typename T1, typename T2 = void>
using VectorContainer = detail::VectorContainer<std::conditional_t<std::is_void_v<T2>, SizeValueType, T1>,
std::conditional_t<std::is_void_v<T2>, T1, T2>>;


/** Makes a VectorContainer that has a copy of the specified `std::vector`. */
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkVectorContainer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include "itkNumericTraits.h"

namespace itk
namespace itk::detail
{

template <typename TElementIdentifier, typename TElement>
Expand Down Expand Up @@ -183,6 +183,6 @@ template <typename TElementIdentifier, typename TElement>
void
VectorContainer<TElementIdentifier, TElement>::Squeeze()
{}
} // end namespace itk
} // namespace itk::detail

#endif
6 changes: 3 additions & 3 deletions Modules/Core/Common/test/itkVectorContainerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
using TestedElementIdentifierType = size_t;

// Test template instantiations for various TElement template arguments:
template class itk::VectorContainer<TestedElementIdentifierType, int>;
template class itk::VectorContainer<TestedElementIdentifierType, bool>;
template class itk::VectorContainer<TestedElementIdentifierType, std::string>;
template class itk::detail::VectorContainer<TestedElementIdentifierType, int>;
template class itk::detail::VectorContainer<TestedElementIdentifierType, bool>;
template class itk::detail::VectorContainer<TestedElementIdentifierType, std::string>;


namespace
Expand Down

0 comments on commit 4b5e5be

Please sign in to comment.