23 #include "openPMD/auxiliary/Variant.hpp" 24 #include "openPMD/Datatype.hpp" 32 #include <type_traits> 52 short, int, long, long long,
53 unsigned short, unsigned int, unsigned long, unsigned long long,
54 float, double, long double,
60 std::vector< long long >,
61 std::vector< unsigned char >,
62 std::vector< unsigned short >,
63 std::vector< unsigned int >,
64 std::vector< unsigned long >,
65 std::vector< unsigned long long >,
67 std::vector< double >,
68 std::vector< long double >,
69 std::vector< std::string >,
70 std::array< double, 7 >,
86 template<
typename U >
90 template< typename T, typename U, bool isConvertible = std::is_convertible<T, U>::value >
93 template<
typename T,
typename U >
96 template<
typename PV >
99 throw std::runtime_error(
"getCast: no cast possible.");
103 template<
typename T,
typename U >
106 template<
typename PV >
107 U operator()( PV pv )
109 return static_cast< U
>( *pv );
113 template<
typename T,
typename U >
116 static constexpr
bool convertible = std::is_convertible<T, U>::value;
118 template<
typename PV,
typename UU = U >
119 auto operator()( PV pv )
120 ->
typename std::enable_if< convertible, std::vector< UU > >::type
123 u.reserve( pv->size() );
124 std::copy( pv->begin(), pv->end(), std::back_inserter(u) );
128 template<
typename PV,
typename UU = U >
129 auto operator()( PV )
130 ->
typename std::enable_if< !convertible, std::vector< UU > >::type
132 throw std::runtime_error(
"getCast: no vector cast possible.");
142 template<
typename U >
148 if(
auto pvalue_c = variantSrc::get_if< char >( &v ) )
150 else if(
auto pvalue_uc = variantSrc::get_if< unsigned char >( &v ) )
152 else if(
auto pvalue_s = variantSrc::get_if< short >( &v ) )
154 else if(
auto pvalue_i = variantSrc::get_if< int >( &v ) )
156 else if(
auto pvalue_l = variantSrc::get_if< long >( &v ) )
158 else if(
auto pvalue_ll = variantSrc::get_if< long long >( &v ) )
160 else if(
auto pvalue_us = variantSrc::get_if< unsigned short >( &v ) )
162 else if(
auto pvalue_ui = variantSrc::get_if< unsigned int >( &v ) )
164 else if(
auto pvalue_ul = variantSrc::get_if< unsigned long >( &v ) )
166 else if(
auto pvalue_ull = variantSrc::get_if< unsigned long long >( &v ) )
168 else if(
auto pvalue_f = variantSrc::get_if< float >( &v ) )
170 else if(
auto pvalue_d = variantSrc::get_if< double >( &v ) )
172 else if(
auto pvalue_ld = variantSrc::get_if< long double >( &v ) )
174 else if(
auto pvalue_str = variantSrc::get_if< std::string >( &v ) )
177 else if(
auto pvalue_vc = variantSrc::get_if< std::vector< char > >( &v ) )
178 return DoConvert<std::vector< char >, U>{}(pvalue_vc);
179 else if(
auto pvalue_vuc = variantSrc::get_if< std::vector< unsigned char > >( &v ) )
180 return DoConvert<std::vector< unsigned char >, U>{}(pvalue_vuc);
181 else if(
auto pvalue_vs = variantSrc::get_if< std::vector< short > >( &v ) )
182 return DoConvert<std::vector< short >, U>{}(pvalue_vs);
183 else if(
auto pvalue_vi = variantSrc::get_if< std::vector< int > >( &v ) )
184 return DoConvert<std::vector< int >, U>{}(pvalue_vi);
185 else if(
auto pvalue_vl = variantSrc::get_if< std::vector< long > >( &v ) )
186 return DoConvert<std::vector< long >, U>{}(pvalue_vl);
187 else if(
auto pvalue_vll = variantSrc::get_if< std::vector< long long > >( &v ) )
188 return DoConvert<std::vector< long long >, U>{}(pvalue_vll);
189 else if(
auto pvalue_vus = variantSrc::get_if< std::vector< unsigned short > >( &v ) )
190 return DoConvert<std::vector< unsigned short >, U>{}(pvalue_vus);
191 else if(
auto pvalue_vui = variantSrc::get_if< std::vector< unsigned int > >( &v ) )
192 return DoConvert<std::vector< unsigned int >, U>{}(pvalue_vui);
193 else if(
auto pvalue_vul = variantSrc::get_if< std::vector< unsigned long > >( &v ) )
194 return DoConvert<std::vector< unsigned long >, U>{}(pvalue_vul);
195 else if(
auto pvalue_vull = variantSrc::get_if< std::vector< unsigned long long > >( &v ) )
196 return DoConvert<std::vector< unsigned long long >, U>{}(pvalue_vull);
197 else if(
auto pvalue_vf = variantSrc::get_if< std::vector< float > >( &v ) )
198 return DoConvert<std::vector< float >, U>{}(pvalue_vf);
199 else if(
auto pvalue_vd = variantSrc::get_if< std::vector< double > >( &v ) )
200 return DoConvert<std::vector< double >, U>{}(pvalue_vd);
201 else if(
auto pvalue_vld = variantSrc::get_if< std::vector< long double > >( &v ) )
202 return DoConvert<std::vector< long double >, U>{}(pvalue_vld);
203 else if(
auto pvalue_vstr = variantSrc::get_if< std::vector< std::string > >( &v ) )
204 return DoConvert<std::vector< std::string >, U>{}(pvalue_vstr);
206 else if(
auto pvalue_vad = variantSrc::get_if< std::array< double, 7 > >( &v ) )
207 return DoConvert<std::array< double, 7 >, U>{}(pvalue_vad);
208 else if(
auto pvalue_b = variantSrc::get_if< bool >( &v ) )
211 throw std::runtime_error(
"getCast: unknown Datatype.");
214 template<
typename U >
217 return getCast< U >( Variant::getResource() );
openPMD::auxiliary::Variant< Datatype, char, unsigned char, short, int, long, long long, unsigned short, unsigned int, unsigned long, unsigned long long, float, double, 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::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:57
U get() const
Retrieve a stored specific Attribute and cast if convertible.
Definition: Attribute.hpp:215
Definition: Attribute.hpp:91
Generic object to store a set of datatypes in without losing type safety.
Definition: Variant.hpp:44
U getCast(Attribute const &a)
Retrieve a stored specific Attribute and cast if convertible.
Definition: Attribute.hpp:144
Varidic datatype supporting at least all formats for attributes specified in the openPMD standard...
Definition: Attribute.hpp:49
Public definitions of openPMD-api.
Definition: Date.cpp:28
resource getResource() const
Retrieve the stored generic object.
Definition: Variant.hpp:78