-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.cpp
47 lines (34 loc) · 1.05 KB
/
tree.cpp
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
#include "tree.h"
#include <tomlplusplus/toml.h>
#include <sstream>
using namespace tog;
namespace tog {
Tree::Tree(std::unordered_map<std::string, Handle<Blob>> &&blobs,
std::unordered_map<std::string, Handle<Tree>> &&trees)
: _blobs{std::move(blobs)}, _trees{std::move(trees)} {}
const std::vector<unsigned char> &Tree::serialize() {
// serialize lazily
if (_serialized) {
return *_serialized;
}
// encode as toml
auto blobs_toml = toml::table();
for (const auto &[name, blob] : _blobs) {
blobs_toml.insert(name, blob.hash());
}
auto trees_toml = toml::table();
for (const auto &[name, tree] : _trees) {
trees_toml.insert(name, tree.hash());
}
auto tree_toml = toml::table{{
{"blobs", blobs_toml},
{"trees", trees_toml},
}};
// Serialize to string
std::stringstream stream{};
stream << tree_toml;
std::string tree_str = stream.str();
_serialized.emplace(tree_str.begin(), tree_str.end());
return *_serialized;
}
} // namespace tog