| 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 | 73808 | EncodedField(_F&& field, Encoder enc) | |
| 33 | 73808 | : _field(field) | |
| 34 | 73808 | , _encoder(std::move(enc)) | |
| 35 | 73808 | {} | |
| 36 | |||
| 37 | 36904 | friend std::ostream& operator<<(std::ostream& s, const EncodedField& field) { | |
| 38 | 36904 | auto encoded = field._encoder(s); | |
| 39 | 110712 | field._field.visit_field_values([&] <typename T> (std::span<const T> data) { | |
| 40 | 36904 | encoded.write(data); | |
| 41 | }); | ||
| 42 | 36904 | 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 |