blob: a0a5da54209cc4dc0905e3291447b6ce5b88ced3 (
plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#pragma once
#include <cstdint>
#include <fstream>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <boost/optional.hpp>
#include "3party/jansson/myjansson.hpp"
namespace generator
{
class JsonValue
{
public:
explicit JsonValue(json_t * value = nullptr) : m_handle{value} { }
explicit JsonValue(base::JSONPtr && value) : m_handle{std::move(value)} { }
JsonValue(JsonValue const &) = delete;
JsonValue & operator=(JsonValue const &) = delete;
operator json_t const * () const noexcept { return m_handle.get(); }
base::JSONPtr MakeDeepCopyJson() const
{
return base::JSONPtr{json_deep_copy(m_handle.get())};
}
private:
base::JSONPtr m_handle;
};
using KeyValue = std::pair<uint64_t, std::shared_ptr<JsonValue>>;
class KeyValueStorage
{
public:
explicit KeyValueStorage(std::string const & kvPath,
std::function<bool(KeyValue const &)> const & pred = DefaultPred);
KeyValueStorage(KeyValueStorage &&) = default;
KeyValueStorage & operator=(KeyValueStorage &&) = default;
KeyValueStorage(KeyValueStorage const &) = delete;
KeyValueStorage & operator=(KeyValueStorage const &) = delete;
std::shared_ptr<JsonValue> Find(uint64_t key) const;
size_t Size() const;
private:
static bool DefaultPred(KeyValue const &) { return true; }
static bool ParseKeyValueLine(std::string const & line, KeyValue & res, std::streamoff lineNumber);
std::unordered_map<uint64_t, std::shared_ptr<JsonValue>> m_values;
};
} // namespace generator
|