| 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::RangeField | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_RANGE_FIELD_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_RANGE_FIELD_HPP_ | ||
| 10 | |||
| 11 | #include <concepts> | ||
| 12 | #include <type_traits> | ||
| 13 | #include <utility> | ||
| 14 | #include <algorithm> | ||
| 15 | #include <iterator> | ||
| 16 | |||
| 17 | #include <gridformat/common/field.hpp> | ||
| 18 | #include <gridformat/common/ranges.hpp> | ||
| 19 | #include <gridformat/common/md_layout.hpp> | ||
| 20 | #include <gridformat/common/precision.hpp> | ||
| 21 | #include <gridformat/common/serialization.hpp> | ||
| 22 | #include <gridformat/common/type_traits.hpp> | ||
| 23 | |||
| 24 | namespace GridFormat { | ||
| 25 | |||
| 26 | template<std::ranges::forward_range R, Concepts::Scalar T = MDRangeScalar<R>> | ||
| 27 | class RangeField : public Field { | ||
| 28 | public: | ||
| 29 | template<std::convertible_to<R> _R> | ||
| 30 | 56656 | explicit RangeField(_R&& r, const Precision<T>& = {}) | |
| 31 | 56656 | : _range{std::forward<_R>(r)} | |
| 32 | 56656 | {} | |
| 33 | |||
| 34 | private: | ||
| 35 | 180662 | MDLayout _layout() const { | |
| 36 |
3/6✓ Branch 2 taken 90331 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 90331 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 90331 times.
✗ Branch 9 not taken.
|
180662 | return MDLayout{{Ranges::size(_range)}}.template with_sub_layout_from<std::ranges::range_value_t<R>>(); |
| 37 | } | ||
| 38 | |||
| 39 | 180648 | DynamicPrecision _precision() const { | |
| 40 | 180648 | return {Precision<T>{}}; | |
| 41 | } | ||
| 42 | |||
| 43 | 56368 | Serialization _serialized() const { | |
| 44 |
2/4✓ Branch 1 taken 28184 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 28184 times.
✗ Branch 5 not taken.
|
56368 | const std::size_t num_bytes = _layout().number_of_entries()*sizeof(T); |
| 45 |
1/2✓ Branch 1 taken 28184 times.
✗ Branch 2 not taken.
|
56368 | Serialization result(num_bytes); |
| 46 | 56368 | auto it = result.as_span().begin(); | |
| 47 |
1/2✓ Branch 1 taken 28184 times.
✗ Branch 2 not taken.
|
56368 | _fill(it, _range); |
| 48 | 56368 | return result; | |
| 49 | ✗ | } | |
| 50 | |||
| 51 | template<std::output_iterator<std::byte> It, std::ranges::range _R> | ||
| 52 | 56376 | void _fill(It& out, _R&& r) const { | |
| 53 |
1/2✓ Branch 1 taken 28188 times.
✗ Branch 2 not taken.
|
360326 | std::ranges::for_each(r, [&] (const auto& entry) { _fill(out, entry); }); |
| 54 | 56376 | } | |
| 55 | |||
| 56 | template<std::output_iterator<std::byte> It, Concepts::Scalar RV> | ||
| 57 | 607892 | void _fill(It& out, const RV& range_value) const { | |
| 58 | 607892 | const T value = static_cast<T>(range_value); | |
| 59 | 607892 | const std::byte* bytes = reinterpret_cast<const std::byte*>(&value); | |
| 60 |
1/2✓ Branch 1 taken 303946 times.
✗ Branch 2 not taken.
|
607892 | out = std::copy_n(bytes, sizeof(T), out); |
| 61 | 607892 | } | |
| 62 | |||
| 63 | R _range; | ||
| 64 | }; | ||
| 65 | |||
| 66 | template<std::ranges::range R> requires(std::is_lvalue_reference_v<R>) | ||
| 67 | RangeField(R&&) -> RangeField<std::add_const_t<R>>; | ||
| 68 | template<std::ranges::range R, Concepts::Scalar T> requires(std::is_lvalue_reference_v<R>) | ||
| 69 | RangeField(R&&, const Precision<T>&) -> RangeField<std::add_const_t<R>, T>; | ||
| 70 | |||
| 71 | template<std::ranges::range R> requires(!std::is_lvalue_reference_v<R>) | ||
| 72 | RangeField(R&&) -> RangeField<std::remove_cvref_t<R>>; | ||
| 73 | template<std::ranges::range R, Concepts::Scalar T> requires(!std::is_lvalue_reference_v<R>) | ||
| 74 | RangeField(R&&, const Precision<T>&) -> RangeField<std::remove_cvref_t<R>, T>; | ||
| 75 | |||
| 76 | } // namespace GridFormat | ||
| 77 | |||
| 78 | #endif // GRIDFORMAT_COMMON_RANGE_FIELD_HPP_ | ||
| 79 |