InsightsOnce.h
Go to the documentation of this file.
1#ifndef INSIGHTS_ONCE_H
2#define INSIGHTS_ONCE_H
3
4namespace clang::insights {
5
6/// \brief A helper object which returns a boolean value just once and toggles it after the first query.
7///
8/// This allows to simplify code like this:
9/// \code
10/// bool first{true};
11/// for(... : ...) {
12/// if(first) {
13/// first = false;
14/// ...
15/// } else {
16/// ...
17/// }
18/// \endcode
19///
20/// into this:
21/// \code
22/// OnceTrue first{};
23/// for(... : ...) {
24/// if(first) {
25/// ...
26/// } else {
27/// ...
28/// }
29/// \endcode
30template<bool VALUE>
31class Once
32{
33public:
34 Once() = default;
35 Once(bool value)
36 : mValue{value}
37 {
38 }
39
40 operator bool()
41 {
42 if(VALUE == mValue) {
43 mValue = not VALUE;
44 return VALUE;
45 }
46
47 return not VALUE;
48 }
49
50private:
51 bool mValue{VALUE};
52};
53
54/// Returns true only once, following checks return false.
56/// Returns false only once, following checks return true.
58
59} // namespace clang::insights
60#endif /* INSIGHTS_ONCE_H */
A helper object which returns a boolean value just once and toggles it after the first query.