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 | 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 | 41873 | if (backpointer_) | |
| 43 | { | ||
| 44 | 41873 | return *backpointer_; | |
| 45 | } | ||
| 46 | ✗ | 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 | 3005 | auto query = db_mgr->createQuery("Timestamps"); | |
| 63 | 3005 | query->addConstraintForUInt64("Timestamp", Constraints::EQUAL, time); | |
| 64 | |||
| 65 | int id; | ||
| 66 | 3005 | query->select("Id", id); | |
| 67 | |||
| 68 | 3005 | if (query->getResultSet().getNextRecord()) | |
| 69 | { | ||
| 70 | ✗ | simdb_assert(id > 0); | |
| 71 | ✗ | return id; | |
| 72 | } | ||
| 73 | |||
| 74 | 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 |