|
31 | 31 |
|
32 | 32 | #include <toml.hpp> |
33 | 33 |
|
| 34 | +#include <algorithm> |
34 | 35 | #include <exception> |
35 | 36 | #include <iostream> |
36 | 37 | #include <optional> |
@@ -957,31 +958,54 @@ void JSONIOHandlerImpl::writeAttribute( |
957 | 958 | m_dirty.emplace(file); |
958 | 959 | } |
959 | 960 |
|
960 | | -void JSONIOHandlerImpl::readDataset( |
961 | | - Writable *writable, Parameter<Operation::READ_DATASET> ¶meters) |
| 961 | +namespace |
962 | 962 | { |
963 | | - switch (m_mode) |
| 963 | + struct FillWithZeroes |
964 | 964 | { |
965 | | - case IOMode::Template: |
966 | | - throw error::WrongAPIUsage( |
967 | | - "Cannot write chunks in Template mode of JSON backend."); |
968 | | - case IOMode::Dataset: |
969 | | - break; |
970 | | - } |
| 965 | + template <typename T> |
| 966 | + static void call(void *ptr, Extent const &extent) |
| 967 | + { |
| 968 | + T *casted = static_cast<T *>(ptr); |
| 969 | + size_t flattenedExtent = std::accumulate( |
| 970 | + extent.begin(), extent.end(), 1, [](size_t left, size_t right) { |
| 971 | + return left * right; |
| 972 | + }); |
| 973 | + std::fill_n(casted, flattenedExtent, T{}); |
| 974 | + } |
971 | 975 |
|
| 976 | + static constexpr char const *errorMsg = |
| 977 | + "[JSON Backend] Fill with zeroes."; |
| 978 | + }; |
| 979 | +} // namespace |
| 980 | + |
| 981 | +void JSONIOHandlerImpl::readDataset( |
| 982 | + Writable *writable, Parameter<Operation::READ_DATASET> ¶meters) |
| 983 | +{ |
972 | 984 | refreshFileFromParent(writable); |
973 | 985 | setAndGetFilePosition(writable); |
974 | 986 | auto &j = obtainJsonContents(writable); |
975 | 987 | verifyDataset(parameters, j); |
976 | 988 |
|
977 | | - try |
978 | | - { |
979 | | - switchType<DatasetReader>(parameters.dtype, j["data"], parameters); |
980 | | - } |
981 | | - catch (json::basic_json::type_error &) |
| 989 | + switch (m_mode) |
982 | 990 | { |
983 | | - throw std::runtime_error( |
984 | | - "[JSON] The given path does not contain a valid dataset."); |
| 991 | + case IOMode::Template: |
| 992 | + std::cerr << "[Warning] Cannot read chunks in Template mode of JSON " |
| 993 | + "backend. Will fill with zeroes instead." |
| 994 | + << std::endl; |
| 995 | + switchNonVectorType<FillWithZeroes>( |
| 996 | + parameters.dtype, parameters.data.get(), parameters.extent); |
| 997 | + return; |
| 998 | + case IOMode::Dataset: |
| 999 | + try |
| 1000 | + { |
| 1001 | + switchType<DatasetReader>(parameters.dtype, j["data"], parameters); |
| 1002 | + } |
| 1003 | + catch (json::basic_json::type_error &) |
| 1004 | + { |
| 1005 | + throw std::runtime_error( |
| 1006 | + "[JSON] The given path does not contain a valid dataset."); |
| 1007 | + } |
| 1008 | + break; |
985 | 1009 | } |
986 | 1010 | } |
987 | 1011 |
|
|
0 commit comments