include/simdb/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
14 / 19
73.7%
Branches:
11 / 32
34.4%
Functions:
3 / 3
100.0%

apps/argos/Timestamps.hpp
Line Branch Exec Source
1 // <Timestamps.hpp> -*- C++ -*-
2
3 #pragma once
4
5 #include "simdb/schema/SchemaDef.hpp"
6 #include "simdb/sqlite/DatabaseManager.hpp"
7
8 #include <functional>
9
10 namespace simdb::argos {
11
12 /// \class Timestamp
13 /// \brief Timestamp that can get current time values via a backpointer,
14 /// C-style function, or a std::function
15 class Timestamp
16 {
17 public:
18 /// \brief Construct with a backpointer to get the current time value
19 5 Timestamp(const uint64_t* backpointer) :
20 5 backpointer_(backpointer)
21 {
22
1/9
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 14 taken 5 times.
✗ Branch 5 → 6 not taken.
✗ Branch 6 → 7 not taken.
✗ Branch 7 → 8 not taken.
✗ Branch 8 → 9 not taken.
✗ Branch 9 → 10 not taken.
✗ Branch 10 → 11 not taken.
✗ Branch 12 → 13 not taken.
5 simdb_assert(backpointer);
23 5 }
24
25 /// \brief Construct with a C-style function pointer to get the current time value
26 Timestamp(uint64_t (*fn)()) :
27 cfuncpointer_(fn)
28 {
29 simdb_assert(fn);
30 }
31
32 /// \brief Construct with a std::function to get the current time value
33 Timestamp(std::function<uint64_t()> fn) :
34 stdfunction_(fn)
35 {
36 simdb_assert(fn);
37 }
38
39 /// Read the current simulation time
40 41873 uint64_t getTime() const
41 {
42
1/2
✓ Branch 2 → 3 taken 41873 times.
✗ Branch 2 → 4 not taken.
41873 if (backpointer_)
43 {
44 41873 return *backpointer_;
45 }
46
0/2
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 7 not taken.
✗ if (cfuncpointer_)
47 {
48 ✗ return cfuncpointer_();
49 }
50 ✗ return stdfunction_();
51 }
52
53 /// Create or look up a Timestamps table row for the given simulation time; return rowid.
54 3005 static int createTimestampInDatabase(DatabaseManager* db_mgr, uint64_t time)
55 {
56 // Ensure we don't create multiple entries in the Timestamps table
57 // that have the same Timestamp column value (it will throw; must
58 // be unique).
59 //
60 // Note that this is called on the DB thread and it is not a big
61 // performance issue to query alongside the INSERT.
62
1/1
✓ Branch 2 → 3 taken 3005 times.
3005 auto query = db_mgr->createQuery("Timestamps");
63
1/1
✓ Branch 4 → 5 taken 3005 times.
3005 query->addConstraintForUInt64("Timestamp", Constraints::EQUAL, time);
64
65 int id;
66
1/1
✓ Branch 6 → 7 taken 3005 times.
3005 query->select("Id", id);
67
68
3/4
✓ Branch 8 → 9 taken 3005 times.
✓ Branch 9 → 10 taken 3005 times.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 24 taken 3005 times.
3005 if (query->getResultSet().getNextRecord())
69 {
70
0/9
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 23 not taken.
✗ Branch 14 → 15 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 16 → 17 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 18 → 19 not taken.
✗ Branch 19 → 20 not taken.
✗ Branch 21 → 22 not taken.
✗ simdb_assert(id > 0);
71 ✗ return id;
72 }
73
74
3/3
✓ Branch 24 → 25 taken 3005 times.
✓ Branch 26 → 27 taken 3005 times.
✓ Branch 27 → 28 taken 3005 times.
3005 return db_mgr->INSERT(SQL_TABLE("Timestamps"), SQL_VALUES(time))->getId();
75 3005 }
76
77 private:
78 const uint64_t* backpointer_ = nullptr;
79 uint64_t (*cfuncpointer_)() = nullptr;
80 std::function<uint64_t()> stdfunction_ = nullptr;
81 };
82
83 } // namespace simdb::argos
84