-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.h
132 lines (105 loc) · 3.61 KB
/
flags.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
class Flags
{
Mask mask;
Mode flags;
time_t time;
bool founder;
public:
using Type = Mask::Type;
auto &get_mask() const { return mask; }
auto &get_flags() const { return flags; }
auto &get_time() const { return time; }
auto &is_founder() const { return founder; }
bool has(const char &c) const { return flags.has(c); }
explicit operator const Mask &() const { return get_mask(); }
bool operator!() const { return flags.empty(); }
bool operator<(const Flags &o) const { return mask < std::string(o.mask); }
bool operator<(const Mask &o) const { return mask < std::string(o); }
bool operator==(const Flags &o) const { return mask == o.mask; }
bool operator==(const Mask &o) const { return mask == o; }
template<class... Delta> Flags &operator+=(Delta&&... delta) &;
template<class... Delta> Flags &operator-=(Delta&&... delta) &;
bool delta(const std::string &str) & { return flags.delta(str); }
void update(const time_t &time) & { this->time = time; }
explicit
Flags(const Mask &mask = Mask(),
const Mode &flags = Mode(),
const time_t &time = 0,
const bool &founder = false);
explicit
Flags(const Deltas &deltas,
const time_t &time,
const bool &founder = false);
explicit
Flags(const Delta &delta,
const time_t &time,
const bool &founder = false);
friend std::ostream &operator<<(std::ostream &s, const Flags &f);
};
inline
Flags::Flags(const Delta &delta,
const time_t &time,
const bool &founder):
Flags(std::get<Delta::MASK>(delta),
std::get<Delta::MODE>(delta),
time,
founder)
{
if(!std::get<Delta::SIGN>(delta))
throw Assertive("No reason to construct a negative flag at this time.");
}
inline
Flags::Flags(const Deltas &deltas,
const time_t &time,
const bool &founder):
mask(deltas.empty()? Mask() : std::get<Delta::MASK>(deltas.at(0))),
time(time),
founder(founder)
{
if(std::count(deltas.begin(),deltas.end(),false))
throw Assertive("No reason to construct a negative flag at this time.");
for(const Delta &d : deltas)
flags.push_back(std::get<Delta::MODE>(d));
}
inline
Flags::Flags(const Mask &mask,
const Mode &flags,
const time_t &time,
const bool &founder):
mask(mask),
flags(flags),
time(time),
founder(founder)
{
}
template<class... Delta>
Flags &Flags::operator+=(Delta&&... delta) &
{
flags.operator+=(std::forward<Delta>(delta)...);
return *this;
}
template<class... Delta>
Flags &Flags::operator-=(Delta&&... delta) &
{
flags.operator-=(std::forward<Delta>(delta)...);
return *this;
}
inline
std::ostream &operator<<(std::ostream &s,
const Flags &f)
{
std::string mstr = f.get_flags();
std::sort(mstr.begin(),mstr.end());
s << "+" << std::setw(16) << std::left << mstr;
s << " " << std::setw(64) << std::left << f.get_mask();
s << " (" << f.get_time() << ")";
if(f.is_founder())
s << " (FOUNDER)";
return s;
}