GCC Code Coverage Report


Directory: gridformat/
File: gridformat/common/field_storage.hpp
Date: 2024-11-20 14:41:59
Exec Total Coverage
Lines: 21 22 95.5%
Functions: 10 10 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 27825 void set(const std::string& name, F&& field) {
32
2/4
✓ Branch 2 taken 13931 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 13931 times.
✗ Branch 6 not taken.
27825 _fields.insert_or_assign(name, make_field_ptr(std::move(field)));
33 27825 }
34
35 264921 void set(const std::string& name, FieldPtr field_ptr) {
36 264921 _fields.insert_or_assign(name, field_ptr);
37 264921 }
38
39 559250 const Field& get(const std::string& name) const {
40
2/2
✓ Branch 1 taken 559248 times.
✓ Branch 2 taken 2 times.
559250 return *(get_ptr(name));
41 }
42
43 1059900 FieldPtr get_ptr(const std::string& name) const {
44
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1059895 times.
1059900 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 1059895 return _fields.at(name);
47 }
48
49 250973 std::ranges::range auto field_names() const {
50 250973 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