include/simdb/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
34 / 38
89.5%
Branches:
30 / 37
81.1%
Functions:
7 / 7
100.0%

apps/argos/EnumInspector.hpp
Line Branch Exec Source
1 // <EnumInspector.hpp> -*- C++ -*-
2
3 #pragma once
4
5 #include "simdb/sqlite/DatabaseManager.hpp"
6 #include "simdb/utils/Demangle.hpp"
7 #include "simdb/utils/TypeTraits.hpp"
8
9 #include <memory>
10 #include <optional>
11 #include <unordered_map>
12 #include <vector>
13
14 namespace simdb::argos {
15
16 //! This class allows us to track collected enum values (int) and their corresponding
17 //! stringified values (operator<<) and store them in the database for the Argos UI
18 //! to use. Note that enums without operator<< are just shown in Argos as their int
19 //! values.
20 class EnumInspector
21 {
22 public:
23 3 template <typename E> std::enable_if_t<type_traits::has_ostream_operator_v<E>, void> inspect(E val)
24 {
25
3/6
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 8 taken 2 times.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 13 not taken.
4 static EnumMap<E>* map = [this] {
26
1/1
✓ Branch 3 → 4 taken 1 time.
1 auto& slot = enum_maps_[simdb::demangle_type<E>()];
27
1/2
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 11 not taken.
1 if (!slot)
28 {
29
1/1
✓ Branch 7 → 8 taken 1 time.
1 slot = std::make_unique<EnumMap<E>>();
30 }
31 1 return static_cast<EnumMap<E>*>(slot.get());
32
1/1
✓ Branch 5 → 6 taken 1 time.
1 }();
33 3 map->inspect(val);
34 3 }
35
36 3 template <typename E> std::enable_if_t<!type_traits::has_ostream_operator_v<E>, void> inspect(E) {}
37
38 5 void serializeEnumMaps(DatabaseManager* db_mgr)
39 {
40
2/2
✓ Branch 11 → 4 taken 1 time.
✓ Branch 11 → 12 taken 5 times.
6 for (auto& [_, map] : enum_maps_)
41 {
42
1/1
✓ Branch 8 → 9 taken 1 time.
1 map->dumpEnumMap(db_mgr);
43 }
44 5 }
45
46 private:
47 class EnumMapBase
48 {
49 public:
50 1 virtual ~EnumMapBase() = default;
51 virtual void dumpEnumMap(simdb::DatabaseManager* db_mgr) const = 0;
52 };
53
54 template <typename E, typename = void> class EnumMap : public EnumMapBase
55 {
56 public:
57 void inspect(E) {}
58 void dumpEnumMap(simdb::DatabaseManager*) const override final {}
59 };
60
61 template <typename E>
62 class EnumMap<E, std::enable_if_t<type_traits::has_ostream_operator_v<E>>> : public EnumMapBase
63 {
64 public:
65 3 void inspect(E val)
66 {
67
4/6
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 7 taken 1 time.
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 3 times.
3 if (last_seen_ && *last_seen_ == val)
68 {
69 ✗ return;
70 }
71
72
2/2
✓ Branch 18 → 12 taken 3 times.
✓ Branch 18 → 19 taken 3 times.
6 for (E seen : all_seen_)
73 {
74
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 16 taken 3 times.
3 if (seen == val)
75 {
76 ✗ last_seen_ = val;
77 ✗ return;
78 }
79 }
80
81 3 all_seen_.push_back(val);
82 3 last_seen_ = val;
83 }
84
85 1 void dumpEnumMap(DatabaseManager* db_mgr) const override final
86 {
87 using underlying_t = std::underlying_type_t<E>;
88 1 const auto itype = demangle_type<underlying_t>();
89 1 const auto enum_name = demangle_type<E>();
90
5/5
✓ Branch 4 → 5 taken 1 time.
✓ Branch 5 → 6 taken 1 time.
✓ Branch 6 → 7 taken 1 time.
✓ Branch 8 → 9 taken 1 time.
✓ Branch 9 → 10 taken 1 time.
1 const auto enum_id = db_mgr->INSERT(SQL_TABLE("CollectedEnums"), SQL_VALUES(enum_name, itype))->getId();
91
92
2/2
✓ Branch 18 → 19 taken 1 time.
✓ Branch 19 → 20 taken 1 time.
1 auto inserter = db_mgr->prepareINSERT(SQL_TABLE("EnumMembers"));
93
2/2
✓ Branch 35 → 23 taken 3 times.
✓ Branch 35 → 36 taken 1 time.
4 for (auto e : all_seen_)
94 {
95
1/1
✓ Branch 24 → 25 taken 3 times.
3 std::ostringstream oss;
96
1/1
✓ Branch 25 → 26 taken 3 times.
3 oss << e;
97
98
1/1
✓ Branch 26 → 27 taken 3 times.
3 const auto member_name = oss.str();
99 3 const auto raw_enum_val = static_cast<underlying_t>(e);
100 3 const auto raw_enum_str = std::to_string(raw_enum_val);
101
102
1/1
✓ Branch 29 → 30 taken 3 times.
3 inserter->createRecordWithColValues(enum_id, member_name, raw_enum_str);
103 }
104 1 }
105
106 private:
107 std::vector<E> all_seen_;
108 std::optional<E> last_seen_;
109 };
110
111 std::unordered_map<std::string, std::unique_ptr<EnumMapBase>> enum_maps_;
112 };
113
114 } // namespace simdb::argos
115