GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/range_field.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 21 22 95.5%
Functions: 55 55 100.0%
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 56104 explicit RangeField(_R&& r, const Precision<T>& = {})
31 56104 : _range{std::forward<_R>(r)}
32 56104 {}
33
34 private:
35 178898 MDLayout _layout() const {
36
3/6
✓ Branch 2 taken 89449 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 89449 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 89449 times.
✗ Branch 9 not taken.
178898 return MDLayout{{Ranges::size(_range)}}.template with_sub_layout_from<std::ranges::range_value_t<R>>();
37 }
38
39 178884 DynamicPrecision _precision() const {
40 178884 return {Precision<T>{}};
41 }
42
43 55816 Serialization _serialized() const {
44
2/4
✓ Branch 1 taken 27908 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 27908 times.
✗ Branch 5 not taken.
55816 const std::size_t num_bytes = _layout().number_of_entries()*sizeof(T);
45
1/2
✓ Branch 1 taken 27908 times.
✗ Branch 2 not taken.
55816 Serialization result(num_bytes);
46 55816 auto it = result.as_span().begin();
47
1/2
✓ Branch 1 taken 27908 times.
✗ Branch 2 not taken.
55816 _fill(it, _range);
48 55816 return result;
49 }
50
51 template<std::output_iterator<std::byte> It, std::ranges::range _R>
52 55824 void _fill(It& out, _R&& r) const {
53
1/2
✓ Branch 1 taken 27912 times.
✗ Branch 2 not taken.
356186 std::ranges::for_each(r, [&] (const auto& entry) { _fill(out, entry); });
54 55824 }
55
56 template<std::output_iterator<std::byte> It, Concepts::Scalar RV>
57 600716 void _fill(It& out, const RV& range_value) const {
58 600716 const T value = static_cast<T>(range_value);
59 600716 const std::byte* bytes = reinterpret_cast<const std::byte*>(&value);
60
1/2
✓ Branch 1 taken 300358 times.
✗ Branch 2 not taken.
600716 out = std::copy_n(bytes, sizeof(T), out);
61 600716 }
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