@@ -330,6 +330,22 @@ void ScientificDefaults<Child>::addDefaultsRecursively(OpenpmdStandard standard)
330330
331331namespace
332332{
333+ template <typename T>
334+ struct to_scalar
335+ {
336+ using type = T;
337+ };
338+ template <typename T>
339+ struct to_scalar <std::vector<T>>
340+ {
341+ using type = T;
342+ };
343+ template <typename T, size_t N>
344+ struct to_scalar <std::array<T, N>>
345+ {
346+ using type = T;
347+ };
348+
333349 ConfigAttribute::process_attribute_type require_scalar =
334350 [](Attributable &record,
335351 char const *attrName,
@@ -338,40 +354,46 @@ namespace
338354 [&](auto const &attr_val) -> std::optional<error::ReadError> {
339355 using actual_type = std::remove_cv_t <
340356 std::remove_reference_t <decltype (attr_val)>>;
341- if constexpr (
342- auxiliary::IsVector_v<actual_type> ||
343- auxiliary::IsArray_v<actual_type>)
344- {
345- using base_type = typename actual_type::value_type;
346- auto converted_or_error =
347- detail::doConvert<actual_type, base_type>(&attr_val);
348- return std::visit (
349- auxiliary::overloaded{
350- [&](base_type casted_val)
351- -> std::optional<error::ReadError> {
352- record.setAttribute <base_type>(
353- attrName, std::move (casted_val));
354- return std::nullopt ;
355- },
356- [](std::runtime_error const &err)
357- -> std::optional<error::ReadError> {
358- return error::ReadError (
359- error::AffectedObject::Attribute,
360- error::Reason::UnexpectedContent,
361- std::nullopt ,
362- std::string (" Expected a scalar type: " ) +
363- err.what ());
364- }},
365- converted_or_error);
366- }
367- else
368- {
369- return std::nullopt ;
370- }
357+ using target_type = typename to_scalar<actual_type>::type;
358+ auto converted_or_error = attr.getOrError <target_type>();
359+ return std::visit (
360+ auxiliary::overloaded{
361+ [&](target_type casted_val)
362+ -> std::optional<error::ReadError> {
363+ record.setAttribute <target_type>(
364+ attrName, std::move (casted_val));
365+ return std::nullopt ;
366+ },
367+ [](std::runtime_error const &err)
368+ -> std::optional<error::ReadError> {
369+ return error::ReadError (
370+ error::AffectedObject::Attribute,
371+ error::Reason::UnexpectedContent,
372+ std::nullopt ,
373+ std::string (" Expected a scalar type: " ) +
374+ err.what ());
375+ }},
376+ converted_or_error);
371377 },
372378 attr.getVariant <attribute_types>());
373379 };
374380
381+ template <typename T>
382+ struct to_vector
383+ {
384+ using type = std::vector<T>;
385+ };
386+ template <typename T>
387+ struct to_vector <std::vector<T>>
388+ {
389+ using type = std::vector<T>;
390+ };
391+ template <typename T, size_t N>
392+ struct to_vector <std::array<T, N>>
393+ {
394+ using type = std::vector<T>;
395+ };
396+
375397 ConfigAttribute::process_attribute_type require_vector =
376398 [](Attributable &record,
377399 char const *attrName,
@@ -380,26 +402,23 @@ namespace
380402 [&](auto const &attr_val) -> std::optional<error::ReadError> {
381403 using actual_type = std::remove_cv_t <
382404 std::remove_reference_t <decltype (attr_val)>>;
383- if constexpr (std::is_same_v<actual_type, bool >)
405+ if constexpr (std::is_same_v<bool , actual_type >)
384406 {
385407 return error::ReadError (
386408 error::AffectedObject::Attribute,
387409 error::Reason::UnexpectedContent,
388410 std::nullopt ,
389411 " Expected a vector type, found a boolean." );
390412 }
391- else if constexpr (!auxiliary::IsVector_v<actual_type>)
413+ else
392414 {
393- using base_type = auxiliary::ScalarType_t<actual_type>;
394-
395- auto converted_or_error =
396- detail::doConvert<actual_type, std::vector<base_type>>(
397- &attr_val);
415+ using target_type = typename to_vector<actual_type>::type;
416+ auto converted_or_error = attr.getOrError <target_type>();
398417 return std::visit (
399418 auxiliary::overloaded{
400- [&](std::vector<base_type> casted_val)
419+ [&](target_type casted_val)
401420 -> std::optional<error::ReadError> {
402- record.setAttribute <std::vector<base_type> >(
421+ record.setAttribute <target_type >(
403422 attrName, std::move (casted_val));
404423 return std::nullopt ;
405424 },
@@ -414,10 +433,6 @@ namespace
414433 }},
415434 converted_or_error);
416435 }
417- else
418- {
419- return std::nullopt ;
420- }
421436 },
422437 attr.getVariant <attribute_types>());
423438 };
0 commit comments