GCC Code Coverage Report


Directory: gridformat/
File: gridformat/encoding/encoded_field.hpp
Date: 2024-11-10 16:24:00
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 6 6 100.0%
Branches: 0 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 Encoding
6 * \brief Wraps a field and makes it streamable using encoding
7 */
8 #ifndef GRIDFORMAT_ENCODING_ENCODED_FIELD_HPP_
9 #define GRIDFORMAT_ENCODING_ENCODED_FIELD_HPP_
10
11 #include <utility>
12 #include <ostream>
13 #include <concepts>
14 #include <type_traits>
15
16 #include <gridformat/common/field.hpp>
17 #include <gridformat/common/precision.hpp>
18 #include <gridformat/common/exceptions.hpp>
19 #include <gridformat/encoding/concepts.hpp>
20
21 namespace GridFormat {
22
23 /*!
24 * \ingroup Encoding
25 * \brief Wraps a field and makes it streamable using encoding
26 */
27 template<std::derived_from<Field> F, Concepts::Encoder<std::ostream> Encoder>
28 class EncodedField {
29 public:
30 template<std::convertible_to<const F&> _F>
31 requires(std::is_lvalue_reference_v<_F>)
32 72820 EncodedField(_F&& field, Encoder enc)
33 72820 : _field(field)
34 72820 , _encoder(std::move(enc))
35 72820 {}
36
37 36410 friend std::ostream& operator<<(std::ostream& s, const EncodedField& field) {
38 36410 auto encoded = field._encoder(s);
39 109230 field._field.visit_field_values([&] <typename T> (std::span<const T> data) {
40 36410 encoded.write(data);
41 });
42 36410 return s;
43 }
44
45 private:
46 const F& _field;
47 Encoder _encoder;
48 };
49
50 template<typename F, typename Enc>
51 EncodedField(F&&, Enc&&) -> EncodedField<std::remove_cvref_t<F>, std::remove_cvref_t<Enc>>;
52
53 } // namespace GridFormat
54
55 #endif // GRIDFORMAT_ENCODING_ENCODED_FIELD_HPP_
56