GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/field_storage.hpp
Date: 2026-10-12 17:50:57
Exec Total Coverage
Lines: 21 22 95.5%
Functions: 16 16 100.0%
Branches: 9 14 64.3%

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 42756 void set(const std::string& name, F&& field) {
32
2/4
✓ Branch 2 taken 21397 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 21397 times.
✗ Branch 6 not taken.
42756 _fields.insert_or_assign(name, make_field_ptr(std::move(field)));
33 42756 }
34
35 273924 void set(const std::string& name, FieldPtr field_ptr) {
36 273924 _fields.insert_or_assign(name, field_ptr);
37 273924 }
38
39 571814 const Field& get(const std::string& name) const {
40
2/2
✓ Branch 1 taken 571812 times.
✓ Branch 2 taken 2 times.
571814 return *(get_ptr(name));
41 }
42
43 1084233 FieldPtr get_ptr(const std::string& name) const {
44
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1084228 times.
1084233 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 1084228 return _fields.at(name);
47 }
48
49 253658 std::ranges::range auto field_names() const {
50 253658 return std::views::keys(_fields);
51 }
52
53 13 FieldPtr pop(const std::string& name) {
54 13 auto field = get_ptr(name);
55
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 _fields.erase(name);
56 10 return field;
57 ✗ }
58
59 1776 void clear() {
60 1776 _fields.clear();
61 1776 }
62
63 private:
64 std::map<std::string, FieldPtr> _fields;
65 };
66
67 } // namespace GridFormat
68
69 #endif // GRIDFORMAT_COMMON_FIELD_STORAGE_HPP_
70