So many changes. Broken.
[invirt/third/libt4.git] / include / log.h
1 #ifndef log_h
2 #define log_h
3
4 #include "include/types.h"
5 #include "include/paxos_protocol.h"
6 #include "include/rpc/marshall.h"
7
8 class log {
9     private:
10         string name;
11         std::map<string, std::function<void(unmarshall &)>> handlers;
12
13     public:
14         log(string _me);
15         string read();
16         void write(string s);
17         void replay();
18
19         struct label : public string { label(const char * s) : string(s) {} };
20 #define LABEL(_x_) inline operator log::label () const { return _x_; }
21
22         // XXX should be an atomic operation
23         template <class T>
24         void append(const T & t) {
25             std::ofstream(name, std::ios::app) << marshall{label(t), t};
26         }
27
28         template <class T>
29         void handler(std::function<void(T)> h) {
30             handlers[label(T())] = [h, this] (unmarshall & from) {
31                 auto entry = from.get<T>();
32                 h(entry);
33                 DEBUG_LOG << entry;
34             };
35         }
36 };
37
38 #endif