| 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::FieldStorage | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_FIELD_STORAGE_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_FIELD_STORAGE_HPP_ | ||
| 10 | |||
| 11 | #include <string> | ||
| 12 | #include <ranges> | ||
| 13 | #include <utility> | ||
| 14 | #include <map> | ||
| 15 | |||
| 16 | #include <gridformat/common/field.hpp> | ||
| 17 | #include <gridformat/common/exceptions.hpp> | ||
| 18 | |||
| 19 | namespace GridFormat { | ||
| 20 | |||
| 21 | /*! | ||
| 22 | * \ingroup Common | ||
| 23 | * \brief Class to store field instances by name. | ||
| 24 | */ | ||
| 25 | class FieldStorage { | ||
| 26 | public: | ||
| 27 | using Field = GridFormat::Field; | ||
| 28 | using FieldPtr = GridFormat::FieldPtr; | ||
| 29 | |||
| 30 | template<std::derived_from<Field> F> requires(!std::is_lvalue_reference_v<F>) | ||
| 31 | 28193 | void set(const std::string& name, F&& field) { | |
| 32 |
2/4✓ Branch 2 taken 14115 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 14115 times.
✗ Branch 6 not taken.
|
28193 | _fields.insert_or_assign(name, make_field_ptr(std::move(field))); |
| 33 | 28193 | } | |
| 34 | |||
| 35 | 267227 | void set(const std::string& name, FieldPtr field_ptr) { | |
| 36 | 267227 | _fields.insert_or_assign(name, field_ptr); | |
| 37 | 267227 | } | |
| 38 | |||
| 39 | 565060 | const Field& get(const std::string& name) const { | |
| 40 |
2/2✓ Branch 1 taken 565058 times.
✓ Branch 2 taken 2 times.
|
565060 | return *(get_ptr(name)); |
| 41 | } | ||
| 42 | |||
| 43 | 1070136 | FieldPtr get_ptr(const std::string& name) const { | |
| 44 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1070131 times.
|
1070136 | if (!_fields.contains(name)) |
| 45 |
2/4✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
|
5 | throw ValueError("No field with name " + name); |
| 46 | 1070131 | return _fields.at(name); | |
| 47 | } | ||
| 48 | |||
| 49 | 253189 | std::ranges::range auto field_names() const { | |
| 50 | 253189 | return std::views::keys(_fields); | |
| 51 | } | ||
| 52 | |||
| 53 | 10 | FieldPtr pop(const std::string& name) { | |
| 54 | 10 | auto field = get_ptr(name); | |
| 55 |
1/2✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
|
7 | _fields.erase(name); |
| 56 | 7 | return field; | |
| 57 | ✗ | } | |
| 58 | |||
| 59 | 1758 | void clear() { | |
| 60 | 1758 | _fields.clear(); | |
| 61 | 1758 | } | |
| 62 | |||
| 63 | private: | ||
| 64 | std::map<std::string, FieldPtr> _fields; | ||
| 65 | }; | ||
| 66 | |||
| 67 | } // namespace GridFormat | ||
| 68 | |||
| 69 | #endif // GRIDFORMAT_COMMON_FIELD_STORAGE_HPP_ | ||
| 70 |