1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
/**
* @class vtkVariantCast
*
* Converts a vtkVariant to some other type. Wherever possible, implicit conversions are
* performed, so this method can be used to convert from nearly any type to a string, or
* from a string to nearly any type. Note that some conversions may fail at runtime, such
* as a conversion from the string "abc" to a numeric type.
*
* The optional 'valid' flag can be used by callers to verify whether conversion succeeded.
*
* @par Thanks:
* Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
*/
#ifndef vtkVariantCast_h
#define vtkVariantCast_h
#include "vtkVariant.h"
#include <typeinfo> // for warnings
VTK_ABI_NAMESPACE_BEGIN
template <typename T>
T vtkVariantCast(const vtkVariant& value, bool* valid = nullptr)
{
vtkGenericWarningMacro(<< "Cannot convert vtkVariant containing [" << value.GetTypeAsString()
<< "] "
<< "to unsupported type [" << typeid(T).name() << "]. "
<< "Create a vtkVariantCast<> specialization to eliminate this warning.");
if (valid)
*valid = false;
static T dummy;
return dummy;
}
template <>
inline char vtkVariantCast<char>(const vtkVariant& value, bool* valid)
{
return value.ToChar(valid);
}
template <>
inline signed char vtkVariantCast<signed char>(const vtkVariant& value, bool* valid)
{
return value.ToSignedChar(valid);
}
template <>
inline unsigned char vtkVariantCast<unsigned char>(const vtkVariant& value, bool* valid)
{
return value.ToUnsignedChar(valid);
}
template <>
inline short vtkVariantCast<short>(const vtkVariant& value, bool* valid)
{
return value.ToShort(valid);
}
template <>
inline unsigned short vtkVariantCast<unsigned short>(const vtkVariant& value, bool* valid)
{
return value.ToUnsignedShort(valid);
}
template <>
inline int vtkVariantCast<int>(const vtkVariant& value, bool* valid)
{
return value.ToInt(valid);
}
template <>
inline unsigned int vtkVariantCast<unsigned int>(const vtkVariant& value, bool* valid)
{
return value.ToUnsignedInt(valid);
}
template <>
inline long vtkVariantCast<long>(const vtkVariant& value, bool* valid)
{
return value.ToLong(valid);
}
template <>
inline unsigned long vtkVariantCast<unsigned long>(const vtkVariant& value, bool* valid)
{
return value.ToUnsignedLong(valid);
}
template <>
inline long long vtkVariantCast<long long>(const vtkVariant& value, bool* valid)
{
return value.ToLongLong(valid);
}
template <>
inline unsigned long long vtkVariantCast<unsigned long long>(const vtkVariant& value, bool* valid)
{
return value.ToUnsignedLongLong(valid);
}
template <>
inline float vtkVariantCast<float>(const vtkVariant& value, bool* valid)
{
return value.ToFloat(valid);
}
template <>
inline double vtkVariantCast<double>(const vtkVariant& value, bool* valid)
{
return value.ToDouble(valid);
}
template <>
inline vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)
{
if (valid)
*valid = true;
return value.ToString();
}
template <>
inline vtkVariant vtkVariantCast<vtkVariant>(const vtkVariant& value, bool* valid)
{
if (valid)
*valid = true;
return value;
}
VTK_ABI_NAMESPACE_END
#endif
// VTK-HeaderTest-Exclude: vtkVariantCast.h
|