]> git.sesse.net Git - plocate/blobdiff - serializer.h
Move Serializer into its own file.
[plocate] / serializer.h
diff --git a/serializer.h b/serializer.h
new file mode 100644 (file)
index 0000000..580e91d
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef _SERIALIZER_H
+#define _SERIALIZER_H 1
+
+#include <assert.h>
+#include <bits/stdint-uintn.h>
+#include <queue>
+#include <string>
+
+#include "options.h"
+
+class ResultReceiver {
+public:
+       virtual ~ResultReceiver() = default;
+       virtual void print(uint64_t seq, uint64_t skip, const std::string msg) = 0;
+};
+
+class Serializer : public ResultReceiver {
+public:
+       ~Serializer() { assert(limit_left <= 0 || pending.empty()); }
+       void print(uint64_t seq, uint64_t skip, const std::string msg) override;
+
+private:
+       uint64_t next_seq = 0;
+       struct Element {
+               uint64_t seq, skip;
+               std::string msg;
+
+               bool operator<(const Element &other) const
+               {
+                       return seq > other.seq;
+               }
+       };
+       std::priority_queue<Element> pending;
+};
+
+#endif  // !defined(_SERIALIZER_H)