VTK: Common/Core/vtkDataArrayMeta.h Source File

| VTK 9.3.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 #define VTK_ITER_INLINE VTK_ALWAYS_INLINE
36 #define VTK_ITER_ASSUME VTK_ASSUME_NO_ASSERT
37 #define VTK_ITER_OPTIMIZE_START VTK_ALWAYS_OPTIMIZE_START
38 #define VTK_ITER_OPTIMIZE_END VTK_ALWAYS_OPTIMIZE_START
39 #else
40 #define VTK_ITER_INLINE inline
41 #define VTK_ITER_ASSUME VTK_ASSUME
42 #define VTK_ITER_OPTIMIZE_START
43 #define VTK_ITER_OPTIMIZE_END
44 #endif
45
46 VTK_ITER_OPTIMIZE_START
47
48 // For IsAOSDataArray:
49 VTK_ABI_NAMESPACE_BEGIN
50 template <typename ValueType>
51 class vtkAOSDataArrayTemplate;
52 VTK_ABI_NAMESPACE_END
53
54 namespace vtk
55 {
56 VTK_ABI_NAMESPACE_BEGIN
57
58 // Typedef for data array indices:
59 using ComponentIdType = int;
60 using TupleIdType = vtkIdType;
61 using ValueIdType = vtkIdType;
62 VTK_ABI_NAMESPACE_END
63
64 namespace detail
65 {
66 VTK_ABI_NAMESPACE_BEGIN
67
68 //------------------------------------------------------------------------------
69 // Used by ranges/iterators when tuple size is unknown at compile time
70 static constexpr ComponentIdType DynamicTupleSize = 0;
71
72 //------------------------------------------------------------------------------
73 // Detect data array value types
74 template <typename T>
75 struct IsVtkDataArray : std::is_base_of<vtkDataArray, T>
76 {
77 };
78
79 template <typename T>
80 using EnableIfVtkDataArray = typename std::enable_if<IsVtkDataArray<T>::value>::type;
81
82 //------------------------------------------------------------------------------
83 // If a value is a valid tuple size
84 template <ComponentIdType Size>
85 struct IsValidTupleSize : std::integral_constant<bool, (Size > 0 || Size == DynamicTupleSize)>
86 {
87 };
88
89 template <ComponentIdType TupleSize>
90 using EnableIfValidTupleSize = typename std::enable_if<IsValidTupleSize<TupleSize>::value>::type;
91
92 //------------------------------------------------------------------------------
93 // If a value is a non-dynamic tuple size
94 template <ComponentIdType Size>
95 struct IsStaticTupleSize : std::integral_constant<bool, (Size > 0)>
96 {
97 };
98
99 template <ComponentIdType TupleSize>
100 using EnableIfStaticTupleSize = typename std::enable_if<IsStaticTupleSize<TupleSize>::value>::type;
101
102 //------------------------------------------------------------------------------
103 // If two values are valid non-dynamic tuple sizes:
104 template <ComponentIdType S1, ComponentIdType S2>
105 struct AreStaticTupleSizes
106 : std::integral_constant<bool, (IsStaticTupleSize<S1>::value && IsStaticTupleSize<S2>::value)>
107 {
108 };
109
110 template <ComponentIdType S1, ComponentIdType S2, typename T = void>
111 using EnableIfStaticTupleSizes =
112 typename std::enable_if<AreStaticTupleSizes<S1, S2>::value, T>::type;
113
114 //------------------------------------------------------------------------------
115 // If either of the tuple sizes is not statically defined
116 template <ComponentIdType S1, ComponentIdType S2>
117 struct IsEitherTupleSizeDynamic
118 : std::integral_constant<bool, (!IsStaticTupleSize<S1>::value || !IsStaticTupleSize<S2>::value)>
119 {
120 };
121
122 template <ComponentIdType S1, ComponentIdType S2, typename T = void>
123 using EnableIfEitherTupleSizeIsDynamic =
124 typename std::enable_if<IsEitherTupleSizeDynamic<S1, S2>::value, T>::type;
125
126 //------------------------------------------------------------------------------
127 // Helper that switches between a storageless integral constant for known
128 // sizes, and a runtime variable for variable sizes.
129 template <ComponentIdType TupleSize>
130 struct GenericTupleSize : public std::integral_constant<ComponentIdType, TupleSize>
131 {
132 static_assert(IsValidTupleSize<TupleSize>::value, "Invalid tuple size.");
133
134 private:
135 using Superclass = std::integral_constant<ComponentIdType, TupleSize>;
136
137 public:
138 // Need to construct from array for specialization.
139 using Superclass::Superclass;
140 VTK_ITER_INLINE GenericTupleSize() noexcept = default;
141 VTK_ITER_INLINE GenericTupleSize(vtkDataArray*) noexcept {}
142 };
143
144 // Specialize for dynamic types, mimicking integral_constant API:
145 template <>
146 struct GenericTupleSize<DynamicTupleSize>
147 {
148 using value_type = ComponentIdType;
149
150 VTK_ITER_INLINE GenericTupleSize() noexcept
151 : value(0)
152 {
153 }
154 VTK_ITER_INLINE explicit GenericTupleSize(vtkDataArray* array)
155 : value(array->GetNumberOfComponents())
156 {
157 }
158
159 VTK_ITER_INLINE operator value_type() const noexcept { return value; }
160 VTK_ITER_INLINE value_type operator()() const noexcept { return value; }
161
162 ComponentIdType value;
163 };
164
165 template <typename ArrayType>
166 struct GetAPITypeImpl
167 {
168 using APIType = typename ArrayType::ValueType;
169 };
170 template <>
171 struct GetAPITypeImpl<vtkDataArray>
172 {
173 using APIType = double;
174 };
175
176 VTK_ABI_NAMESPACE_END
177 } // end namespace detail
178
179 VTK_ABI_NAMESPACE_BEGIN
180
181 //------------------------------------------------------------------------------
182 // Typedef for double if vtkDataArray, or the array's ValueType for subclasses.
183 template <typename ArrayType, typename = detail::EnableIfVtkDataArray<ArrayType>>
184 using GetAPIType = typename detail::GetAPITypeImpl<ArrayType>::APIType;
185
186 VTK_ABI_NAMESPACE_END
187
188 //------------------------------------------------------------------------------
189 namespace detail
190 {
191 VTK_ABI_NAMESPACE_BEGIN
192
193 template <typename ArrayType>
194 struct IsAOSDataArrayImpl
195 {
196 using APIType = GetAPIType<ArrayType>;
197 static constexpr bool value = std::is_base_of<vtkAOSDataArrayTemplate<APIType>, ArrayType>::value;
198 };
199
200 VTK_ABI_NAMESPACE_END
201 } // end namespace detail
202
203 VTK_ABI_NAMESPACE_BEGIN
204 //------------------------------------------------------------------------------
205 // True if ArrayType inherits some specialization of vtkAOSDataArrayTemplate
206 template <typename ArrayType>
207 using IsAOSDataArray = std::integral_constant<bool, detail::IsAOSDataArrayImpl<ArrayType>::value>;
208
209 VTK_ABI_NAMESPACE_END
210 } // end namespace vtk
211
212 VTK_ITER_OPTIMIZE_END
213
214 #endif // vtkDataArrayMeta_h
215
216 // VTK-HeaderTest-Exclude: vtkDataArrayMeta.h
vtkAOSDataArrayTemplate
Array-Of-Structs implementation of vtkGenericDataArray.
Definition: vtkAOSDataArrayTemplate.h:37
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:54
detail
Definition: vtkCellTreeLocator.h:59
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::type
@ type
Definition: vtkX3D.h:516
vtk::detail::EnableIfValidTupleSize
typename std::enable_if< IsValidTupleSize< TupleSize >::value >::type EnableIfValidTupleSize
Definition: vtkDataArrayMeta.h:90
vtk::detail::EnableIfStaticTupleSize
typename std::enable_if< IsStaticTupleSize< TupleSize >::value >::type EnableIfStaticTupleSize
Definition: vtkDataArrayMeta.h:100
vtk::detail::EnableIfVtkDataArray
typename std::enable_if< IsVtkDataArray< T >::value >::type EnableIfVtkDataArray
Definition: vtkDataArrayMeta.h:80
vtk::detail::EnableIfStaticTupleSizes
typename std::enable_if< AreStaticTupleSizes< S1, S2 >::value, T >::type EnableIfStaticTupleSizes
Definition: vtkDataArrayMeta.h:112
vtk::detail::DynamicTupleSize
static constexpr ComponentIdType DynamicTupleSize
Definition: vtkDataArrayMeta.h:70
vtk::detail::EnableIfEitherTupleSizeIsDynamic
typename std::enable_if< IsEitherTupleSizeDynamic< S1, S2 >::value, T >::type EnableIfEitherTupleSizeIsDynamic
Definition: vtkDataArrayMeta.h:124
vtk
Specialization of tuple ranges and iterators for vtkAOSDataArrayTemplate.
Definition: vtkSMPThreadLocalAPI.h:30
vtk::GetAPIType
typename detail::GetAPITypeImpl< ArrayType >::APIType GetAPIType
Definition: vtkDataArrayMeta.h:184
vtk::ValueIdType
vtkIdType ValueIdType
Definition: vtkDataArrayMeta.h:61
vtk::TupleIdType
vtkIdType TupleIdType
Definition: vtkDataArrayMeta.h:60
vtk::IsAOSDataArray
std::integral_constant< bool, detail::IsAOSDataArrayImpl< ArrayType >::value > IsAOSDataArray
Definition: vtkDataArrayMeta.h:207
vtk::ComponentIdType
int ComponentIdType
Definition: vtkDataArrayMeta.h:59
vtk::detail::AreStaticTupleSizes
Definition: vtkDataArrayMeta.h:107
vtk::detail::GenericTupleSize< DynamicTupleSize >::GenericTupleSize
VTK_ITER_INLINE GenericTupleSize() noexcept
Definition: vtkDataArrayMeta.h:150
vtk::detail::GenericTupleSize< DynamicTupleSize >::value
ComponentIdType value
Definition: vtkDataArrayMeta.h:162
vtk::detail::GenericTupleSize< DynamicTupleSize >::operator()
VTK_ITER_INLINE value_type operator()() const noexcept
Definition: vtkDataArrayMeta.h:160
vtk::detail::GenericTupleSize< DynamicTupleSize >::value_type
ComponentIdType value_type
Definition: vtkDataArrayMeta.h:148
vtk::detail::GenericTupleSize< DynamicTupleSize >::GenericTupleSize
VTK_ITER_INLINE GenericTupleSize(vtkDataArray *array)
Definition: vtkDataArrayMeta.h:154
vtk::detail::GenericTupleSize
Definition: vtkDataArrayMeta.h:131
vtk::detail::GenericTupleSize::GenericTupleSize
VTK_ITER_INLINE GenericTupleSize() noexcept=default
vtk::detail::GetAPITypeImpl< vtkDataArray >::APIType
double APIType
Definition: vtkDataArrayMeta.h:173
vtk::detail::GetAPITypeImpl
Definition: vtkDataArrayMeta.h:167
vtk::detail::GetAPITypeImpl::APIType
typename ArrayType::ValueType APIType
Definition: vtkDataArrayMeta.h:168
vtk::detail::IsAOSDataArrayImpl
Definition: vtkDataArrayMeta.h:195
vtk::detail::IsAOSDataArrayImpl::value
static constexpr bool value
Definition: vtkDataArrayMeta.h:197
vtk::detail::IsAOSDataArrayImpl::APIType
GetAPIType< ArrayType > APIType
Definition: vtkDataArrayMeta.h:196
vtk::detail::IsEitherTupleSizeDynamic
Definition: vtkDataArrayMeta.h:119
vtk::detail::IsStaticTupleSize
Definition: vtkDataArrayMeta.h:96
vtk::detail::IsValidTupleSize
Definition: vtkDataArrayMeta.h:86
vtk::detail::IsVtkDataArray
Definition: vtkDataArrayMeta.h:76
vtkAssume.h
VTK_ITER_OPTIMIZE_START
#define VTK_ITER_OPTIMIZE_START
Definition: vtkDataArrayMeta.h:42
VTK_ITER_INLINE
#define VTK_ITER_INLINE
Definition: vtkDataArrayMeta.h:40
VTK_ITER_OPTIMIZE_END
#define VTK_ITER_OPTIMIZE_END
Definition: vtkDataArrayMeta.h:43
vtkDataArray.h
vtkMeta.h
This file contains a variety of metaprogramming constructs for working with vtk types.
vtkType.h
vtkIdType
int vtkIdType
Definition: vtkType.h:315