diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-04-27 12:55:30 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-04-27 12:55:30 +1000 |
| commit | 5d7cbec34cd5e473d5768b39054d99bc72ddad62 (patch) | |
| tree | f83040e527dc025345b3786a298f11e914b5eb37 /src/database/include/db_task.hpp | |
| parent | fbe047a35fff100cb5f42d10984bccde137f586e (diff) | |
| download | tangara-fw-5d7cbec34cd5e473d5768b39054d99bc72ddad62.tar.gz | |
Move DB interactions to a background thread
Diffstat (limited to 'src/database/include/db_task.hpp')
| -rw-r--r-- | src/database/include/db_task.hpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/database/include/db_task.hpp b/src/database/include/db_task.hpp new file mode 100644 index 00000000..39f932b0 --- /dev/null +++ b/src/database/include/db_task.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <functional> +#include <future> +#include <memory> + +namespace database { + +auto StartDbTask() -> bool; +auto QuitDbTask() -> void; + +auto SendToDbTask(std::function<void(void)> fn) -> void; + +template <typename T> +auto RunOnDbTask(std::function<T(void)> fn) -> std::future<T> { + std::shared_ptr<std::promise<T>> promise = + std::make_shared<std::promise<T>>(); + SendToDbTask([=]() { promise->set_value(std::invoke(fn)); }); + return promise->get_future(); +} + +template <> +auto RunOnDbTask(std::function<void(void)> fn) -> std::future<void>; + +} // namespace database |
