| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Common | ||
| 6 | * \copydoc GridFormat::LazyField | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_LAZY_FIELD_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_LAZY_FIELD_HPP_ | ||
| 10 | |||
| 11 | #include <utility> | ||
| 12 | #include <concepts> | ||
| 13 | #include <type_traits> | ||
| 14 | |||
| 15 | #include <gridformat/common/field.hpp> | ||
| 16 | #include <gridformat/common/md_layout.hpp> | ||
| 17 | #include <gridformat/common/precision.hpp> | ||
| 18 | #include <gridformat/common/serialization.hpp> | ||
| 19 | |||
| 20 | namespace GridFormat { | ||
| 21 | |||
| 22 | /*! | ||
| 23 | * \ingroup Common | ||
| 24 | * \brief Field implementation that reads values lazily from a source upon request. | ||
| 25 | */ | ||
| 26 | template<typename S> | ||
| 27 | class LazyField : public Field { | ||
| 28 | public: | ||
| 29 | using SourceType = std::remove_cvref_t<S>; | ||
| 30 | using SourceReferenceType = std::add_lvalue_reference_t<std::add_const_t<SourceType>>; | ||
| 31 | using SerializationCallBack = std::function<Serialization(SourceReferenceType)>; | ||
| 32 | |||
| 33 | template<typename _S, typename CallBack> | ||
| 34 | requires(std::constructible_from<SerializationCallBack, CallBack> and | ||
| 35 | std::convertible_to<_S, S>) | ||
| 36 | 25582 | explicit LazyField(_S&& source, | |
| 37 | MDLayout layout, | ||
| 38 | DynamicPrecision prec, | ||
| 39 | CallBack&& cb) | ||
| 40 | 32314 | : _source{std::forward<_S>(source)} | |
| 41 |
1/2✓ Branch 2 taken 12791 times.
✗ Branch 3 not taken.
|
25582 | , _md_layout{std::move(layout)} |
| 42 | 25582 | , _scalar_precision{std::move(prec)} | |
| 43 |
1/2✓ Branch 4 taken 12406 times.
✗ Branch 5 not taken.
|
51164 | , _serialization_callback{std::forward<CallBack>(cb)} |
| 44 | 25582 | {} | |
| 45 | |||
| 46 | private: | ||
| 47 | 83842 | MDLayout _layout() const override { | |
| 48 | 83842 | return _md_layout; | |
| 49 | } | ||
| 50 | |||
| 51 | 47925 | DynamicPrecision _precision() const override { | |
| 52 | 47925 | return _scalar_precision; | |
| 53 | } | ||
| 54 | |||
| 55 | 10907 | Serialization _serialized() const override { | |
| 56 | 10907 | return _serialization_callback(_source); | |
| 57 | } | ||
| 58 | |||
| 59 | S _source; | ||
| 60 | MDLayout _md_layout; | ||
| 61 | DynamicPrecision _scalar_precision; | ||
| 62 | SerializationCallBack _serialization_callback; | ||
| 63 | }; | ||
| 64 | |||
| 65 | template<typename S, typename CB> | ||
| 66 | LazyField(S&&, MDLayout, DynamicPrecision, CB&&) -> LazyField< | ||
| 67 | std::conditional_t< | ||
| 68 | std::is_lvalue_reference_v<S>, | ||
| 69 | S, | ||
| 70 | std::remove_cvref_t<S> | ||
| 71 | > | ||
| 72 | >; | ||
| 73 | |||
| 74 | } // namespace GridFormat | ||
| 75 | |||
| 76 | #endif // GRIDFORMAT_COMMON_LAZY_FIELD_HPP_ | ||
| 77 |