-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.h
220 lines (176 loc) · 3.44 KB
/
mode.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
struct Mode : std::string
{
bool has(const char &m) const { return find(m) != std::string::npos; }
bool has(const Delta &d) const { return has(std::get<Delta::MODE>(d)); }
bool none(const Mode &m) const;
bool all(const Mode &m) const;
bool any(const Mode &m) const;
bool valid_chan(const Server &s) const;
bool valid_user(const Server &s) const;
bool add(const char &m) &;
void add(const Mode &m) &;
bool add(const Delta &d) &; // adding negative delta removes the mode
void add(const Deltas &d) &; // ^
bool rm(const char &m) &;
void rm(const Mode &m) &;
bool rm(const Delta &d) &; // rm of positive or negative delta always removes the mode
void rm(const Deltas &d) &; // ^
bool delta(const std::string &str) &;
template<class T> Mode &operator+=(T&& m) &; // add()
template<class T> Mode &operator-=(T&& m) &; // rm()
template<class T> Mode operator+(T&& m); // add()
template<class T> Mode operator-(T&& m); // rm()
Mode(void) = default;
explicit Mode(const std::string &mode);
Mode(const char &mode): std::string(1,mode) {}
Mode(const char *const &mode): Mode(std::string(mode)) {}
explicit Mode(const Delta &d): Mode(std::get<d.MODE>(d)) {}
};
inline
Mode::Mode(const std::string &mode):
std::string(!mode.empty() && (mode.at(0) == '+' || mode.at(0) == '-')? mode.substr(1) : mode)
{
}
template<class T>
Mode Mode::operator-(T&& m)
{
Mode ret(*this);
ret -= std::forward<T>(m);
return ret;
}
template<class T>
Mode Mode::operator+(T&& m)
{
Mode ret(*this);
ret += std::forward<T>(m);
return ret;
}
template<class T>
Mode &Mode::operator-=(T&& m)
&
{
rm(std::forward<T>(m));
return *this;
}
template<class T>
Mode &Mode::operator+=(T&& m) &
{
add(std::forward<T>(m));
return *this;
}
inline
bool Mode::delta(const std::string &str)
& try
{
if(str.at(0) == '-')
for(size_t i(1); i < str.size(); i++)
rm(str.at(i));
else
for(size_t i(1); i < str.size(); i++)
add(str.at(i));
return true;
}
catch(const std::out_of_range &e)
{
return false;
}
inline
void Mode::add(const Deltas &deltas)
&
{
for(const Delta &d : deltas)
add(d);
}
inline
bool Mode::add(const Delta &d)
&
{
using std::get;
return get<Delta::SIGN>(d)? add(get<Delta::MODE>(d)):
rm(get<Delta::MODE>(d));
}
inline
void Mode::add(const Mode &m)
&
{
for(const char &c : m)
add(c);
}
inline
bool Mode::add(const char &m)
&
{
if(!has(m))
{
push_back(m);
return true;
}
else return false;
}
inline
void Mode::rm(const Deltas &deltas)
&
{
for(const Delta &d : deltas)
rm(d);
}
inline
bool Mode::rm(const Delta &d)
&
{
return rm(std::get<Delta::MODE>(d));
}
inline
void Mode::rm(const Mode &m)
&
{
for(const char &c : m)
rm(c);
}
inline
bool Mode::rm(const char &m)
&
{
if(has(m))
{
erase(find(m));
return true;
}
else return false;
}
inline
bool Mode::any(const Mode &m)
const
{
return std::any_of(m.begin(),m.end(),[this]
(const char &c)
{
return has(c);
});
}
inline
bool Mode::all(const Mode &m)
const
{
return std::all_of(m.begin(),m.end(),[this]
(const char &c)
{
return has(c);
});
}
inline
bool Mode::none(const Mode &m)
const
{
return std::none_of(m.begin(),m.end(),[this]
(const char &c)
{
return has(c);
});
}