VTK  9.7.1
vtkDataArrayMeta.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 
4 #ifndef vtkDataArrayMeta_h
5 #define vtkDataArrayMeta_h
6 
7 #include "vtkAssume.h"
8 #include "vtkDataArray.h"
9 #include "vtkDebugRangeIterators.h"
10 #include "vtkMeta.h"
11 #include "vtkSetGet.h"
12 #include "vtkType.h"
13 
14 #include <type_traits>
15 #include <utility>
16 
23 // When enabled, extra debugging checks are enabled for the iterators.
24 // Specifically:
25 // - Specializations are disabled (All code uses the generic implementation).
26 // - Additional assertions are inserted to ensure correct runtime usage.
27 // - Performance-related annotations (e.g. force inlining) are disabled.
28 #if defined(VTK_DEBUG_RANGE_ITERATORS)
29 #define VTK_ITER_ASSERT(x, msg) assert((x) && msg)
30 #else
31 #define VTK_ITER_ASSERT(x, msg)
32 #endif
33 
34 #if (defined(VTK_ALWAYS_OPTIMIZE_ARRAY_ITERATORS) || !defined(VTK_DEBUG_RANGE_ITERATORS)) && \
35  !defined(VTK_COMPILER_MSVC) && !defined(__PPC64__)
36 #define VTK_ITER_INLINE VTK_ALWAYS_INLINE
37 #define VTK_ITER_ASSUME VTK_ASSUME_NO_ASSERT
38 #define VTK_ITER_OPTIMIZE_START VTK_ALWAYS_OPTIMIZE_START
39 #define VTK_ITER_OPTIMIZE_END VTK_ALWAYS_OPTIMIZE_END
40 #else
41 #define VTK_ITER_INLINE inline
42 #define VTK_ITER_ASSUME VTK_ASSUME
43 #define VTK_ITER_OPTIMIZE_START
44 #define VTK_ITER_OPTIMIZE_END
45 #endif
46 
48 
49 // For IsAOSDataArray:
50 VTK_ABI_NAMESPACE_BEGIN
51 template <typename ValueType>
53 VTK_ABI_NAMESPACE_END
54 
55 namespace vtk
56 {
57 VTK_ABI_NAMESPACE_BEGIN
58 
59 // Typedef for data array indices:
60 using ComponentIdType = int;
63 VTK_ABI_NAMESPACE_END
64 
65 namespace detail
66 {
67 VTK_ABI_NAMESPACE_BEGIN
68 
69 //------------------------------------------------------------------------------
70 // Used by ranges/iterators when tuple size is unknown at compile time
71 static constexpr ComponentIdType DynamicTupleSize = 0;
72 
73 //------------------------------------------------------------------------------
74 // Detect data array value types
75 template <typename T>
76 struct IsVtkArray
77 {
78  static constexpr bool value =
79  std::is_base_of_v<vtkAbstractArray, T> && !std::is_same_v<vtkAbstractArray, T>;
80 };
81 
82 template <typename T>
84 
85 //------------------------------------------------------------------------------
86 // Detect data array value types
87 template <typename T>
88 struct IsVtkDataArray : std::is_base_of<vtkDataArray, T>
89 {
90 };
91 
92 template <typename T>
94 
95 //------------------------------------------------------------------------------
96 // If a value is a valid tuple size
97 template <ComponentIdType Size>
98 struct IsValidTupleSize : std::integral_constant<bool, (Size > 0 || Size == DynamicTupleSize)>
99 {
100 };
101 
102 template <ComponentIdType TupleSize>
104 
105 //------------------------------------------------------------------------------
106 // If a value is a non-dynamic tuple size
107 template <ComponentIdType Size>
108 struct IsStaticTupleSize : std::integral_constant<bool, (Size > 0)>
109 {
110 };
111 
112 template <ComponentIdType TupleSize>
114 
115 //------------------------------------------------------------------------------
116 // If two values are valid non-dynamic tuple sizes:
117 template <ComponentIdType S1, ComponentIdType S2>
119  : std::integral_constant<bool, (IsStaticTupleSize<S1>::value && IsStaticTupleSize<S2>::value)>
120 {
121 };
122 
123 template <ComponentIdType S1, ComponentIdType S2, typename T = void>
126 
127 //------------------------------------------------------------------------------
128 // If either of the tuple sizes is not statically defined
129 template <ComponentIdType S1, ComponentIdType S2>
131  : std::integral_constant<bool, (!IsStaticTupleSize<S1>::value || !IsStaticTupleSize<S2>::value)>
132 {
133 };
134 
135 template <ComponentIdType S1, ComponentIdType S2, typename T = void>
138 
139 //------------------------------------------------------------------------------
140 // Helper that switches between a storageless integral constant for known
141 // sizes, and a runtime variable for variable sizes.
142 template <ComponentIdType TupleSize>
143 struct GenericTupleSize : public std::integral_constant<ComponentIdType, TupleSize>
144 {
145  static_assert(IsValidTupleSize<TupleSize>::value, "Invalid tuple size.");
146 
147 private:
148  using Superclass = std::integral_constant<ComponentIdType, TupleSize>;
149 
150 public:
151  // Need to construct from array for specialization.
152  using Superclass::Superclass;
153  VTK_ITER_INLINE GenericTupleSize() noexcept = default;
155 };
156 
157 // Specialize for dynamic types, mimicking integral_constant API:
158 template <>
159 struct GenericTupleSize<DynamicTupleSize>
160 {
162 
164  : value(0)
165  {
166  }
168  : value(array->GetNumberOfComponents())
169  {
170  }
171 
172  VTK_ITER_INLINE operator value_type() const noexcept { return value; }
173  VTK_ITER_INLINE value_type operator()() const noexcept { return value; }
174 
176 };
177 
178 template <typename ArrayType, typename ForceValueTypeForVtkDataArray = double>
180 {
181  using APIType = typename ArrayType::ValueType;
182 };
183 template <typename ForceValueTypeForVtkDataArray>
184 struct GetAPITypeImpl<vtkDataArray, ForceValueTypeForVtkDataArray>
185 {
186  using APIType = ForceValueTypeForVtkDataArray;
187 };
188 
189 VTK_ABI_NAMESPACE_END
190 } // end namespace detail
191 
192 VTK_ABI_NAMESPACE_BEGIN
193 
194 //------------------------------------------------------------------------------
195 // Typedef for double if vtkDataArray, or the array's ValueType for subclasses.
196 template <typename ArrayType, typename ForceValueTypeForVtkDataArray = double,
198 using GetAPIType =
200 
201 VTK_ABI_NAMESPACE_END
202 
203 //------------------------------------------------------------------------------
204 namespace detail
205 {
206 VTK_ABI_NAMESPACE_BEGIN
207 
208 template <typename ArrayType>
210 {
212  static constexpr bool value = std::is_base_of<vtkAOSDataArrayTemplate<APIType>, ArrayType>::value;
213 };
214 
215 VTK_ABI_NAMESPACE_END
216 } // end namespace detail
217 
218 VTK_ABI_NAMESPACE_BEGIN
219 //------------------------------------------------------------------------------
220 // True if ArrayType inherits some specialization of vtkAOSDataArrayTemplate
221 template <typename ArrayType>
223 
224 VTK_ABI_NAMESPACE_END
225 } // end namespace vtk
226 
228 
229 #endif // vtkDataArrayMeta_h
230 
231 // VTK-HeaderTest-Exclude: vtkDataArrayMeta.h
std::enable_if_t< IsValidTupleSize< TupleSize >::value > EnableIfValidTupleSize
vtkIdType TupleIdType
std::enable_if_t< IsStaticTupleSize< TupleSize >::value > EnableIfStaticTupleSize
int ComponentIdType
std::enable_if_t< IsEitherTupleSizeDynamic< S1, S2 >::value, T > EnableIfEitherTupleSizeIsDynamic
typename ArrayType::ValueType APIType
Abstract superclass for all arrays.
typename detail::GetAPITypeImpl< ArrayType, ForceValueTypeForVtkDataArray >::APIType GetAPIType
VTK_ITER_INLINE GenericTupleSize() noexcept=default
std::integral_constant< bool, detail::IsAOSDataArrayImpl< ArrayType >::value > IsAOSDataArray
VTK_ITER_INLINE value_type operator()() const noexcept
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
VTK_ITER_INLINE GenericTupleSize(vtkAbstractArray *) noexcept
int vtkIdType
Definition: vtkType.h:363
vtkIdType ValueIdType
std::enable_if_t< IsVtkArray< T >::value > EnableIfVtkArray
This file contains a variety of metaprogramming constructs for working with vtk types.
#define VTK_ITER_INLINE
Array-Of-Structs implementation of vtkGenericDataArray.
#define VTK_ITER_OPTIMIZE_END
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
static constexpr ComponentIdType DynamicTupleSize
static constexpr bool value
GetAPIType< ArrayType > APIType
VTK_ITER_INLINE GenericTupleSize(vtkAbstractArray *array)
std::enable_if_t< IsVtkDataArray< T >::value > EnableIfVtkDataArray
Compiler hint macros for assumptions and branch prediction.
#define VTK_ITER_OPTIMIZE_START
typename std::enable_if< AreStaticTupleSizes< S1, S2 >::value, T >::type EnableIfStaticTupleSizes