summary refs log tree commit diff stats
path: root/database.h
diff options
context:
space:
mode:
Diffstat (limited to 'database.h')
-rw-r--r--database.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/database.h b/database.h new file mode 100644 index 0000000..d8d9b67 --- /dev/null +++ b/database.h
@@ -0,0 +1,40 @@
1#ifndef DATABASE_H_0A27B356
2#define DATABASE_H_0A27B356
3
4#include <functional>
5#include <map>
6#include <mutex>
7#include <optional>
8#include <random>
9#include <string>
10#include <vector>
11
12class database {
13 public:
14 // Feel free to throw an exception if the subscriber should be killed.
15 using subscribe_callback_type = std::function<void(const std::string&)>;
16
17 std::string create(std::mt19937& rng);
18
19 void subscribe(const std::string& token, subscribe_callback_type callback);
20 void post(const std::string& token, const std::string& msg);
21
22 void setResult(const std::string& token, const std::string& result);
23 const std::string& getResult(const std::string& token);
24
25 void mark_done(const std::string& token);
26 bool is_done(const std::string& token);
27
28 private:
29 struct request {
30 std::vector<std::optional<subscribe_callback_type>> subscribers;
31 std::string result;
32 bool done = false;
33 };
34
35 std::mutex mutex_;
36
37 std::map<std::string, request> requests_;
38};
39
40#endif /* end of include guard: DATABASE_H_0A27B356 */