GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/range_field.hpp
Date: 2026-10-18 00:09:19
Exec Total Coverage
Lines: 21 22 95.5%
Functions: 87 88 98.9%
Branches: 9 18 50.0%

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 72060 explicit RangeField(_R&& r, const Precision<T>& = {})
31 72060 : _range{std::forward<_R>(r)}
32 72060 {}
33
34 private:
35 360418 MDLayout _layout() const {
36
3/6
✓ Branch 2 taken 180215 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 180215 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 180215 times.
✗ Branch 9 not taken.
360418 return MDLayout{{Ranges::size(_range)}}.template with_sub_layout_from<std::ranges::range_value_t<R>>();
37 }
38
39 316260 DynamicPrecision _precision() const {
40 316260 return {Precision<T>{}};
41 }
42
43 115148 Serialization _serialized() const {
44
2/4
✓ Branch 1 taken 57576 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57576 times.
✗ Branch 5 not taken.
115148 const std::size_t num_bytes = _layout().number_of_entries()*sizeof(T);
45
1/2
✓ Branch 1 taken 57576 times.
✗ Branch 2 not taken.
115148 Serialization result(num_bytes);
46 115148 auto it = result.as_span().begin();
47
1/2
✓ Branch 1 taken 57576 times.
✗ Branch 2 not taken.
115148 _fill(it, _range);
48 115148 return result;
49 ✗ }
50
51 template<std::output_iterator<std::byte> It, std::ranges::range _R>
52 115170 void _fill(It& out, _R&& r) const {
53
1/2
✓ Branch 1 taken 57587 times.
✗ Branch 2 not taken.
866814 std::ranges::for_each(r, [&] (const auto& entry) { _fill(out, entry); });
54 115170 }
55
56 template<std::output_iterator<std::byte> It, Concepts::Scalar RV>
57 1503234 void _fill(It& out, const RV& range_value) const {
58 1503234 const T value = static_cast<T>(range_value);
59 1503234 const std::byte* bytes = reinterpret_cast<const std::byte*>(&value);
60
1/2
✓ Branch 1 taken 751633 times.
✗ Branch 2 not taken.
1503234 out = std::copy_n(bytes, sizeof(T), out);
61 1503234 }
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