summary refs log tree commit diff stats
path: root/database.h
blob: d8d9b676c68f44753b11e322d8a88b1c45f2c856 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef DATABASE_H_0A27B356
#define DATABASE_H_0A27B356

#include <functional>
#include <map>
#include <mutex>
#include <optional>
#include <random>
#include <string>
#include <vector>

class database {
 public:
  // Feel free to throw an exception if the subscriber should be killed.
  using subscribe_callback_type = std::function<void(const std::string&)>;

  std::string create(std::mt19937& rng);

  void subscribe(const std::string& token, subscribe_callback_type callback);
  void post(const std::string& token, const std::string& msg);

  void setResult(const std::string& token, const std::string& result);
  const std::string& getResult(const std::string& token);

  void mark_done(const std::string& token);
  bool is_done(const std::string& token);

 private:
  struct request {
    std::vector<std::optional<subscribe_callback_type>> subscribers;
    std::string result;
    bool done = false;
  };

  std::mutex mutex_;

  std::map<std::string, request> requests_;
};

#endif /* end of include guard: DATABASE_H_0A27B356 */