-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathRootId.h
91 lines (72 loc) · 2.28 KB
/
RootId.h
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#pragma once
#include <string>
#include "eden/fs/model/Hash.h"
namespace facebook::eden {
/**
* Each BackingStore implementation defines the meaning of its root. For
* example, for Mercurial, that's a 20-byte commit hash. Roots may have a
* different representation than tree IDs, so allow the BackingStore to define
* the semantics.
*
* RootId should generally be human-readable (e.g. hex strings) because it is
* printed to logs with C escaping rules.
*/
class RootId {
public:
RootId() = default;
explicit RootId(std::string id) : id_{std::move(id)} {}
RootId(const RootId&) = default;
RootId(RootId&&) = default;
RootId& operator=(const RootId&) = default;
RootId& operator=(RootId&&) = default;
const std::string& value() const {
return id_;
}
friend bool operator==(const RootId& lhs, const RootId& rhs) {
return lhs.id_ == rhs.id_;
}
friend bool operator!=(const RootId& lhs, const RootId& rhs) {
return lhs.id_ != rhs.id_;
}
friend bool operator<(const RootId& lhs, const RootId& rhs) {
return lhs.id_ < rhs.id_;
}
private:
std::string id_;
};
std::ostream& operator<<(std::ostream& os, const RootId& rootId);
// Make folly::to<string>(RootId) work.
void toAppend(const RootId&, std::string* result);
/**
* The meaning of a RootId is defined by the BackingStore implementation. Allow
* it to also define how root IDs are parsed and rendered at API boundaries such
* as Thrift.
*/
class RootIdCodec {
public:
virtual ~RootIdCodec() = default;
virtual RootId parseRootId(folly::StringPiece rootId) = 0;
virtual std::string renderRootId(const RootId& rootId) = 0;
};
} // namespace facebook::eden
namespace std {
template <>
struct hash<facebook::eden::RootId> {
std::size_t operator()(const facebook::eden::RootId& rootId) const {
return std::hash<std::string>{}(rootId.value());
}
};
} // namespace std
template <>
struct fmt::formatter<facebook::eden::RootId> : formatter<std::string> {
template <typename Context>
auto format(const facebook::eden::RootId& id, Context& ctx) const {
return formatter<std::string>::format(id.value(), ctx);
}
};