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 | 25350 | explicit LazyField(_S&& source, | |
37 | MDLayout layout, | ||
38 | DynamicPrecision prec, | ||
39 | CallBack&& cb) | ||
40 | 32078 | : _source{std::forward<_S>(source)} | |
41 |
1/2✓ Branch 2 taken 12675 times.
✗ Branch 3 not taken.
|
25350 | , _md_layout{std::move(layout)} |
42 | 25350 | , _scalar_precision{std::move(prec)} | |
43 |
1/2✓ Branch 4 taken 12402 times.
✗ Branch 5 not taken.
|
50700 | , _serialization_callback{std::forward<CallBack>(cb)} |
44 | 25350 | {} | |
45 | |||
46 | private: | ||
47 | 83458 | MDLayout _layout() const override { | |
48 | 83458 | return _md_layout; | |
49 | } | ||
50 | |||
51 | 47707 | DynamicPrecision _precision() const override { | |
52 | 47707 | return _scalar_precision; | |
53 | } | ||
54 | |||
55 | 10791 | Serialization _serialized() const override { | |
56 | 10791 | 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 |