|
27 | 27 |
|
28 | 28 | #include <iostream> |
29 | 29 | #include <optional> |
| 30 | +#include <stdexcept> |
30 | 31 | #include <type_traits> |
31 | 32 | #include <utility> |
32 | 33 |
|
@@ -328,113 +329,50 @@ void ScientificDefaults<Child>::addDefaultsRecursively(OpenpmdStandard standard) |
328 | 329 | } |
329 | 330 | } |
330 | 331 |
|
331 | | -template <typename T> |
332 | | -struct to_scalar |
333 | | -{ |
334 | | - using type = T; |
335 | | -}; |
336 | | -template <typename T> |
337 | | -struct to_scalar<std::vector<T>> |
338 | | -{ |
339 | | - using type = T; |
340 | | -}; |
341 | | -template <typename T, size_t N> |
342 | | -struct to_scalar<std::array<T, N>> |
343 | | -{ |
344 | | - using type = T; |
345 | | -}; |
346 | | - |
347 | 332 | // 2, 0.0976562 |
348 | 333 | auto RequireScalar::operator()( |
349 | 334 | Attributable &record, char const *attrName, Attribute const &attr) |
350 | 335 | -> std::optional<error::ReadError> |
351 | 336 | { |
| 337 | + auto res = attr.requireScalar(); |
| 338 | + using res_t = std::optional<error::ReadError>; |
352 | 339 | return std::visit( |
353 | | - [&](auto const &attr_val) -> std::optional<error::ReadError> { |
354 | | - using actual_type = |
355 | | - std::remove_cv_t<std::remove_reference_t<decltype(attr_val)>>; |
356 | | - using target_type = typename to_scalar<actual_type>::type; |
357 | | - auto converted_or_error = attr.getOrError<target_type>(); |
358 | | - return std::visit( |
359 | | - auxiliary::overloaded{ |
360 | | - [&](target_type casted_val) |
361 | | - -> std::optional<error::ReadError> { |
362 | | - record.setAttribute<target_type>( |
363 | | - attrName, std::move(casted_val)); |
364 | | - return std::nullopt; |
365 | | - }, |
366 | | - [](std::runtime_error const &err) |
367 | | - -> std::optional<error::ReadError> { |
368 | | - return error::ReadError( |
369 | | - error::AffectedObject::Attribute, |
370 | | - error::Reason::UnexpectedContent, |
371 | | - std::nullopt, |
372 | | - std::string("Expected a scalar type: ") + |
373 | | - err.what()); |
374 | | - }}, |
375 | | - converted_or_error); |
376 | | - }, |
377 | | - attr.getVariant<attribute_types>()); |
| 340 | + auxiliary::overloaded{ |
| 341 | + [](std::runtime_error const &err) -> res_t { |
| 342 | + return error::ReadError( |
| 343 | + error::AffectedObject::Attribute, |
| 344 | + error::Reason::UnexpectedContent, |
| 345 | + std::nullopt, |
| 346 | + std::string("Expected a scalar type: ") + err.what()); |
| 347 | + }, |
| 348 | + [&](Attribute converted_attr) -> res_t { |
| 349 | + record.setAttribute(attrName, std::move(converted_attr)); |
| 350 | + return std::nullopt; |
| 351 | + }}, |
| 352 | + std::move(res)); |
378 | 353 | } |
379 | 354 |
|
380 | | -template <typename T> |
381 | | -struct to_vector |
382 | | -{ |
383 | | - using type = std::vector<T>; |
384 | | -}; |
385 | | -template <typename T> |
386 | | -struct to_vector<std::vector<T>> |
387 | | -{ |
388 | | - using type = std::vector<T>; |
389 | | -}; |
390 | | -template <typename T, size_t N> |
391 | | -struct to_vector<std::array<T, N>> |
392 | | -{ |
393 | | - using type = std::vector<T>; |
394 | | -}; |
395 | | - |
396 | 355 | // 1, 0.113281 |
397 | 356 | auto RequireVector::operator()( |
398 | 357 | Attributable &record, char const *attrName, Attribute const &attr) |
399 | 358 | -> std::optional<error::ReadError> |
400 | 359 | { |
| 360 | + auto res = attr.requireVector(); |
| 361 | + using res_t = std::optional<error::ReadError>; |
401 | 362 | return std::visit( |
402 | | - [&](auto const &attr_val) -> std::optional<error::ReadError> { |
403 | | - using actual_type = |
404 | | - std::remove_cv_t<std::remove_reference_t<decltype(attr_val)>>; |
405 | | - if constexpr (std::is_same_v<bool, actual_type>) |
406 | | - { |
| 363 | + auxiliary::overloaded{ |
| 364 | + [](std::runtime_error const &err) -> res_t { |
407 | 365 | return error::ReadError( |
408 | 366 | error::AffectedObject::Attribute, |
409 | 367 | error::Reason::UnexpectedContent, |
410 | 368 | std::nullopt, |
411 | | - "Expected a vector type, found a boolean."); |
412 | | - } |
413 | | - else |
414 | | - { |
415 | | - using target_type = typename to_vector<actual_type>::type; |
416 | | - auto converted_or_error = attr.getOrError<target_type>(); |
417 | | - return std::visit( |
418 | | - auxiliary::overloaded{ |
419 | | - [&](target_type casted_val) |
420 | | - -> std::optional<error::ReadError> { |
421 | | - record.setAttribute<target_type>( |
422 | | - attrName, std::move(casted_val)); |
423 | | - return std::nullopt; |
424 | | - }, |
425 | | - [](std::runtime_error const &err) |
426 | | - -> std::optional<error::ReadError> { |
427 | | - return error::ReadError( |
428 | | - error::AffectedObject::Attribute, |
429 | | - error::Reason::UnexpectedContent, |
430 | | - std::nullopt, |
431 | | - std::string("Expected a scalar type: ") + |
432 | | - err.what()); |
433 | | - }}, |
434 | | - converted_or_error); |
435 | | - } |
436 | | - }, |
437 | | - attr.getVariant<attribute_types>()); |
| 369 | + std::string("Expected a vector type: ") + err.what()); |
| 370 | + }, |
| 371 | + [&](Attribute converted_attr) -> res_t { |
| 372 | + record.setAttribute(attrName, std::move(converted_attr)); |
| 373 | + return std::nullopt; |
| 374 | + }}, |
| 375 | + std::move(res)); |
438 | 376 | } |
439 | 377 |
|
440 | 378 | // 3, 0.0117188 |
|
0 commit comments