VTK-m  1.8
ArrayHandleVirtual.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_cont_ArrayHandleVirtual_h
11 #define vtk_m_cont_ArrayHandleVirtual_h
12 
13 #include <vtkm/cont/vtkm_cont_export.h>
14 
15 #include <vtkm/cont/ArrayHandle.h>
19 
21 
22 #include <memory>
23 
24 #ifdef VTKM_NO_DEPRECATED_VIRTUAL
25 #error "ArrayHandleVirtual is removed. Do not include ArrayHandleVirtual.h"
26 #endif
27 
28 namespace vtkm
29 {
30 namespace cont
31 {
32 
34 
35 template <typename T>
36 VTKM_ARRAY_HANDLE_DEPRECATED(T, vtkm::cont::StorageTagVirtual);
37 
38 template <typename T>
39 class VTKM_ALWAYS_EXPORT VTKM_DEPRECATED(1.6) ArrayHandleVirtual
40  : public vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagVirtual>
41 {
42  using StorageType = vtkm::cont::internal::Storage<T, vtkm::cont::StorageTagVirtual>;
43 
44 public:
45  VTKM_ARRAY_HANDLE_SUBCLASS(ArrayHandleVirtual,
46  (ArrayHandleVirtual<T>),
48 
62  template <typename S>
63  ArrayHandleVirtual(const vtkm::cont::ArrayHandle<T, S>& ah)
64  : Superclass(StorageType(ah))
65  {
66  using is_base = std::is_base_of<StorageType, S>;
67  static_assert(!is_base::value, "Wrong specialization for ArrayHandleVirtual selected");
68  }
69 
72  template <typename ArrayHandleType>
73  VTKM_CONT bool IsType() const
74  {
75  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
76 
77  //We need to determine if we are checking that `ArrayHandleType`
78  //is a virtual array handle since that is an easy check.
79  //Or if we have to go ask the storage if they are holding
80  //
81  using ST = typename ArrayHandleType::StorageTag;
82  using is_base = std::is_same<vtkm::cont::StorageTagVirtual, ST>;
83 
84  //Determine if the Value type of the virtual and ArrayHandleType
85  //are the same. This an easy compile time check, and doesn't need
86  // to be done at runtime.
87  using VT = typename ArrayHandleType::ValueType;
88  using same_value_type = std::is_same<T, VT>;
89 
90  return this->IsSameType<ArrayHandleType>(same_value_type{}, is_base{});
91  }
92 
97  template <typename ArrayHandleType>
98  VTKM_CONT ArrayHandleType Cast() const
99  {
100  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);
101  //We need to determine if we are checking that `ArrayHandleType`
102  //is a virtual array handle since that is an easy check.
103  //Or if we have to go ask the storage if they are holding
104  //
105  using ST = typename ArrayHandleType::StorageTag;
106  using is_base = std::is_same<vtkm::cont::StorageTagVirtual, ST>;
107 
108  //Determine if the Value type of the virtual and ArrayHandleType
109  //are the same. This an easy compile time check, and doesn't need
110  // to be done at runtime.
111  using VT = typename ArrayHandleType::ValueType;
112  using same_value_type = std::is_same<T, VT>;
113 
114  return this->CastToType<ArrayHandleType>(same_value_type{}, is_base{});
115  }
116 
119  VTKM_CONT ArrayHandleVirtual<T> NewInstance() const
120  {
121  return ArrayHandleVirtual<T>(this->GetStorage().NewInstance());
122  }
123 
124 private:
125  template <typename ArrayHandleType>
126  inline bool IsSameType(std::false_type, std::true_type) const
127  {
128  return false;
129  }
130  template <typename ArrayHandleType>
131  inline bool IsSameType(std::false_type, std::false_type) const
132  {
133  return false;
134  }
135 
136  template <typename ArrayHandleType>
137  inline bool IsSameType(std::true_type vtkmNotUsed(valueTypesMatch),
138  std::true_type vtkmNotUsed(inheritsFromArrayHandleVirtual)) const
139  {
140  //The type being past has no requirements in being the most derived type
141  //so the typeid info won't match but dynamic_cast will work
142  auto casted = dynamic_cast<const ArrayHandleType*>(this);
143  return casted != nullptr;
144  }
145 
146  template <typename ArrayHandleType>
147  inline bool IsSameType(std::true_type vtkmNotUsed(valueTypesMatch),
148  std::false_type vtkmNotUsed(notFromArrayHandleVirtual)) const
149  {
150  auto* storage = this->GetStorage().GetStorageVirtual();
151  if (!storage)
152  {
153  return false;
154  }
155  using S = typename ArrayHandleType::StorageTag;
156  return storage->template IsType<vtkm::cont::internal::detail::StorageVirtualImpl<T, S>>();
157  }
158 
159  template <typename ArrayHandleType>
160  inline ArrayHandleType CastToType(std::false_type vtkmNotUsed(valueTypesMatch),
161  std::true_type vtkmNotUsed(notFromArrayHandleVirtual)) const
162  {
163  VTKM_LOG_CAST_FAIL(*this, ArrayHandleType);
164  throwFailedDynamicCast("ArrayHandleVirtual", vtkm::cont::TypeToString<ArrayHandleType>());
165  return ArrayHandleType{};
166  }
167 
168  template <typename ArrayHandleType>
169  inline ArrayHandleType CastToType(std::false_type vtkmNotUsed(valueTypesMatch),
170  std::false_type vtkmNotUsed(notFromArrayHandleVirtual)) const
171  {
172  VTKM_LOG_CAST_FAIL(*this, ArrayHandleType);
173  throwFailedDynamicCast("ArrayHandleVirtual", vtkm::cont::TypeToString<ArrayHandleType>());
174  return ArrayHandleType{};
175  }
176 
177  template <typename ArrayHandleType>
178  inline ArrayHandleType CastToType(
179  std::true_type vtkmNotUsed(valueTypesMatch),
180  std::true_type vtkmNotUsed(inheritsFromArrayHandleVirtual)) const
181  {
182  //The type being passed has no requirements in being the most derived type
183  //so the typeid info won't match but dynamic_cast will work
184  const ArrayHandleType* derived = dynamic_cast<const ArrayHandleType*>(this);
185  if (!derived)
186  {
187  VTKM_LOG_CAST_FAIL(*this, ArrayHandleType);
188  throwFailedDynamicCast("ArrayHandleVirtual", vtkm::cont::TypeToString<ArrayHandleType>());
189  }
190  VTKM_LOG_CAST_SUCC(*this, derived);
191  return *derived;
192  }
193 
194  template <typename ArrayHandleType>
195  ArrayHandleType CastToType(std::true_type vtkmNotUsed(valueTypesMatch),
196  std::false_type vtkmNotUsed(notFromArrayHandleVirtual)) const;
197 };
198 
199 
200 //=============================================================================
202 template <typename T, typename S>
203 VTKM_CONT vtkm::cont::ArrayHandleVirtual<T> make_ArrayHandleVirtual(
205 {
206  return vtkm::cont::ArrayHandleVirtual<T>(ah);
207 }
208 
209 //=============================================================================
210 // Free function casting helpers
211 
214 template <typename ArrayHandleType, typename T>
215 VTKM_CONT inline bool IsType(
217 {
218  return static_cast<vtkm::cont::ArrayHandleVirtual<T>>(virtHandle)
219  .template IsType<ArrayHandleType>();
220 }
221 
226 template <typename ArrayHandleType, typename T>
227 VTKM_CONT inline ArrayHandleType Cast(
229 {
230  return static_cast<vtkm::cont::ArrayHandleVirtual<T>>(virtHandle)
231  .template Cast<ArrayHandleType>();
232 }
233 //=============================================================================
234 // Specializations of serialization related classes
236 template <typename T>
237 struct SerializableTypeString<vtkm::cont::ArrayHandleVirtual<T>>
238 {
239  static VTKM_CONT const std::string& Get()
240  {
241  static std::string name = "AH_Virtual<" + SerializableTypeString<T>::Get() + ">";
242  return name;
243  }
244 };
245 
246 template <typename T>
247 struct SerializableTypeString<vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagVirtual>>
248  : public SerializableTypeString<vtkm::cont::ArrayHandleVirtual<T>>
249 {
250 };
251 
252 #ifndef vtk_m_cont_ArrayHandleVirtual_cxx
253 
254 #define VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(T) \
255  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<T, StorageTagVirtual>; \
256  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandleVirtual<T>; \
257  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<vtkm::Vec<T, 2>, StorageTagVirtual>; \
258  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandleVirtual<vtkm::Vec<T, 2>>; \
259  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<vtkm::Vec<T, 3>, StorageTagVirtual>; \
260  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandleVirtual<vtkm::Vec<T, 3>>; \
261  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandle<vtkm::Vec<T, 4>, StorageTagVirtual>; \
262  extern template class VTKM_CONT_TEMPLATE_EXPORT ArrayHandleVirtual<vtkm::Vec<T, 4>>
263 
264 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(char);
265 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Int8);
266 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::UInt8);
267 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Int16);
268 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::UInt16);
269 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Int32);
270 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::UInt32);
271 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Int64);
272 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::UInt64);
273 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Float32);
274 VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT(vtkm::Float64);
275 
276 #undef VTK_M_ARRAY_HANDLE_VIRTUAL_EXPORT
277 
278 #endif //vtk_m_cont_ArrayHandleVirtual_cxx
280 }
281 } //namespace vtkm::cont
282 
284 
285 #endif //vtk_m_cont_ArrayHandleVirtual_h
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:287
ArrayHandle.h
vtkm::cont::VTKM_DEPRECATED
struct VTKM_DEPRECATED(1.6, "Use vtkm::ColorSpace.") ColorSpace
Definition: cont/ColorTable.h:36
vtkm
VTKM_NO_DEPRECATED_VIRTUAL.
Definition: Algorithms.h:18
VTKM_ARRAY_HANDLE_SUBCLASS
#define VTKM_ARRAY_HANDLE_SUBCLASS(classname, fullclasstype, superclass)
Macro to make default methods in ArrayHandle subclasses.
Definition: ArrayHandle.h:227
StorageVirtual.h
VTKM_LOG_CAST_FAIL
#define VTKM_LOG_CAST_FAIL(inObj, outType)
Convenience macro for logging a failed cast of dynamic object.
Definition: Logging.h:261
vtkm::Get
VTKM_SUPPRESS_EXEC_WARNINGS VTKM_EXEC_CONT auto Get(const vtkm::Tuple< Ts... > &tuple) -> decltype(tuple.template Get< Index >())
Retrieve the object from a vtkm::Tuple at the given index.
Definition: Tuple.h:83
DeviceAdapterTag.h
vtkm::Int16
int16_t Int16
Definition: Types.h:158
VTKM_DEPRECATED_SUPPRESS_END
#define VTKM_DEPRECATED_SUPPRESS_END
Definition: Deprecated.h:123
vtkm::cont::throwFailedDynamicCast
VTKM_SILENCE_WEAK_VTABLE_WARNING_END VTKM_CONT_EXPORT void throwFailedDynamicCast(const std::string &baseType, const std::string &derivedType)
Throws an ErrorBadType exception with the following message: Cast failed: baseType --> derivedType".
vtkm::cont::Cast
VTKM_CONT ArrayHandleType Cast(const vtkm::cont::UnknownArrayHandle &array)
Returns variant cast to the given ArrayHandle type.
Definition: UnknownArrayHandle.h:1149
ArrayHandleUniformPointCoordinates.h
vtkm::Int8
int8_t Int8
Definition: Types.h:156
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Definition: ArrayHandle.h:133
ArrayHandleDeprecated.h
VTKM_LOG_CAST_SUCC
#define VTKM_LOG_CAST_SUCC(inObj, outObj)
Convenience macro for logging the successful cast of dynamic object.
Definition: Logging.h:260
VTKM_ARRAY_HANDLE_DEPRECATED
#define VTKM_ARRAY_HANDLE_DEPRECATED(ValueType_, StorageTag_)
Definition: ArrayHandleDeprecated.h:1358
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::UInt8
uint8_t UInt8
Definition: Types.h:157
vtkmNotUsed
#define vtkmNotUsed(parameter_name)
Simple macro to identify a parameter as unused.
Definition: ExportMacros.h:128
VTKM_DEPRECATED_SUPPRESS_BEGIN
#define VTKM_DEPRECATED_SUPPRESS_BEGIN
Definition: Deprecated.h:122
vtkm::UInt32
uint32_t UInt32
Definition: Types.h:161
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::cont::IsType
VTKM_CONT bool IsType(const vtkm::cont::UnknownArrayHandle &array)
Returns true if variant matches the type of ArrayHandleType.
Definition: UnknownArrayHandle.h:1139
VTKM_ALWAYS_EXPORT
#define VTKM_ALWAYS_EXPORT
Definition: ExportMacros.h:92
vtkm::UInt16
uint16_t UInt16
Definition: Types.h:159