23 #include "openPMD/Datatype.hpp" 24 #include "openPMD/auxiliary/TypeTraits.hpp" 25 #include "openPMD/auxiliary/Variant.hpp" 35 #include <type_traits> 73 std::complex<long double>,
79 std::vector<long long>,
80 std::vector<unsigned char>,
81 std::vector<unsigned short>,
82 std::vector<unsigned int>,
83 std::vector<unsigned long>,
84 std::vector<unsigned long long>,
87 std::vector<long double>,
88 std::vector<std::complex<float> >,
89 std::vector<std::complex<double> >,
90 std::vector<std::complex<long double> >,
91 std::vector<signed char>,
92 std::vector<std::string>,
93 std::array<double, 7>,
109 template <
typename T>
122 template <
typename U>
136 template <
typename U>
142 template <
typename T,
typename U>
143 auto doConvert(
T *pv) -> std::variant<U, std::runtime_error>
146 if constexpr (std::is_convertible_v<T, U>)
148 return {
static_cast<U
>(*pv)};
150 else if constexpr (auxiliary::IsVector_v<T> && auxiliary::IsVector_v<U>)
152 if constexpr (std::is_convertible_v<
153 typename T::value_type,
154 typename U::value_type>)
157 res.reserve(pv->size());
158 std::copy(pv->begin(), pv->end(), std::back_inserter(res));
164 std::runtime_error(
"getCast: no vector cast possible.")};
170 else if constexpr (auxiliary::IsArray_v<T> && auxiliary::IsVector_v<U>)
172 if constexpr (std::is_convertible_v<
173 typename T::value_type,
174 typename U::value_type>)
177 res.reserve(pv->size());
178 std::copy(pv->begin(), pv->end(), std::back_inserter(res));
183 return {std::runtime_error(
184 "getCast: no array to vector conversion possible.")};
190 else if constexpr (auxiliary::IsVector_v<T> && auxiliary::IsArray_v<U>)
192 if constexpr (std::is_convertible_v<
193 typename T::value_type,
194 typename U::value_type>)
197 if (res.size() != pv->size())
199 return std::runtime_error(
200 "getCast: no vector to array conversion possible " 202 "requested array size).");
204 for (
size_t i = 0; i < res.size(); ++i)
206 res[i] =
static_cast<typename U::value_type
>((*pv)[i]);
212 return {std::runtime_error(
213 "getCast: no vector to array conversion possible.")};
217 else if constexpr (auxiliary::IsVector_v<U>)
219 if constexpr (std::is_convertible_v<T, typename U::value_type>)
223 res.push_back(static_cast<typename U::value_type>(*pv));
228 return {std::runtime_error(
229 "getCast: no scalar to vector conversion possible.")};
234 return {std::runtime_error(
"getCast: no cast possible.")};
236 #if defined(__INTEL_COMPILER) 244 #pragma warning(disable : 1011) 246 #pragma warning(default : 1011) 252 template <
typename U>
255 auto eitherValueOrError = std::visit(
256 [](
auto &&containedValue) -> std::variant<U, std::runtime_error> {
257 using containedType = std::decay_t<decltype(containedValue)>;
258 return detail::doConvert<containedType, U>(&containedValue);
260 Variant::getResource());
262 [](
auto &&containedValue) -> U {
263 using T = std::decay_t<decltype(containedValue)>;
264 if constexpr (std::is_same_v<T, std::runtime_error>)
266 throw std::move(containedValue);
270 return std::move(containedValue);
273 std::move(eitherValueOrError));
276 template <
typename U>
279 auto eitherValueOrError = std::visit(
280 [](
auto &&containedValue) -> std::variant<U, std::runtime_error> {
281 using containedType = std::decay_t<decltype(containedValue)>;
282 return detail::doConvert<containedType, U>(&containedValue);
284 Variant::getResource());
286 [](
auto &&containedValue) -> std::optional<U> {
287 using T = std::decay_t<decltype(containedValue)>;
288 if constexpr (std::is_same_v<T, std::runtime_error>)
294 return {std::move(containedValue)};
297 std::move(eitherValueOrError));
openPMD::auxiliary::Variant< Datatype, char, unsigned char, signed char, short, int, long, long long, unsigned short, unsigned int, unsigned long, unsigned long long, float, double, long double, std::complex< float >, std::complex< double >, std::complex< long double >, std::string, std::vector< char >, std::vector< short >, std::vector< int >, std::vector< long >, std::vector< long long >, std::vector< unsigned char >, std::vector< unsigned short >, std::vector< unsigned int >, std::vector< unsigned long >, std::vector< unsigned long long >, std::vector< float >, std::vector< double >, std::vector< long double >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< std::complex< long double > >, std::vector< signed char >, std::vector< std::string >, std::array< double, 7 >, bool >::Variant Variant(resource r)
Construct a lightweight wrapper around a generic object that indicates the concrete datatype of the s...
Definition: Variant.hpp:54
U get() const
Retrieve a stored specific Attribute and cast if convertible.
Definition: Attribute.hpp:253
Generic object to store a set of datatypes in without losing type safety.
Definition: Variant.hpp:39
std::optional< U > getOptional() const
Retrieve a stored specific Attribute and cast if convertible.
Definition: Attribute.hpp:277
Varidic datatype supporting at least all formats for attributes specified in the openPMD standard...
Definition: Attribute.hpp:54
Definition: Container.cpp:51
Public definitions of openPMD-api.
Attribute(T &&val)
Compiler bug: CUDA (nvcc) releases 11.0.3 (v11.0.221), 11.1 (v11.1.105): no instance of constructor "...
Definition: Attribute.hpp:110