summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.76.0/src/enum_flags.h
blob: 4f4e33c55cd388e06d72b3baf80eb0a691de9cbd (plain)
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
/**
 * @file enum_flags.h
 * Operators for working with bit-flag enumerators.
 *
 * @author  Matthew Woehlke (but mostly "borrowed" from Qt)
 * @license GPL v2+
 */

#ifndef ENUM_FLAGS_H_INCLUDED
#define ENUM_FLAGS_H_INCLUDED

#include <type_traits>

#if __GNUC__ == 4 && !defined (__clang__)
#pragma GCC diagnostic push
#if __GNUC_MINOR__ < 9 || __GNUC_PATCHLEVEL__ < 2
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59624
#pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
#endif
#endif

#define UNC_DECLARE_FLAGS(flag_type, enum_type) \
   using flag_type = ::uncrustify::flags<enum_type>

#define UNC_DECLARE_OPERATORS_FOR_FLAGS(flag_type)                        \
   inline flag_type operator&(flag_type::enum_t f1, flag_type::enum_t f2) \
   { return(flag_type{ f1 } &f2); }                                       \
   inline flag_type operator|(flag_type::enum_t f1, flag_type::enum_t f2) \
   { return(flag_type{ f1 } | f2); }                                      \
   inline flag_type operator|(flag_type::enum_t f1, flag_type f2)         \
   { return(f2 | f1); }                                                   \
   inline void operator|(flag_type::enum_t f1, int f2) = delete

namespace uncrustify
{

//-----------------------------------------------------------------------------
template<typename Enum>
class flags
{
public:
   using enum_t = Enum;
   using int_t  = typename std::underlying_type<enum_t>::type;

   template<typename T> using integral =
      typename std::enable_if<std::is_integral<T>::value, bool>::type;

   inline flags() = default;
   inline flags(Enum flag)
      : m_i{static_cast<int_t>(flag)}
   {}

   inline bool operator==(Enum const &other)
   { return(m_i == static_cast<int_t>(other)); }
   inline bool operator==(flags const &other)
   { return(m_i == other.m_i); }
   inline bool operator!=(Enum const &other)
   { return(m_i != static_cast<int_t>(other)); }
   inline bool operator!=(flags const &other)
   { return(m_i != other.m_i); }

   template<typename T, integral<T> = true>
   inline flags &operator&=(T mask)
   { m_i &= static_cast<int_t>(mask); return(*this); }

   inline flags &operator|=(flags f)
   { m_i |= f.m_i; return(*this); }
   inline flags &operator|=(Enum f)
   { m_i |= f; return(*this); }

   inline flags &operator^=(flags f)
   { m_i ^= f.m_i; return(*this); }
   inline flags &operator^=(Enum f)
   { m_i ^= f; return(*this); }

   inline operator int_t() const { return(m_i); }
   inline operator enum_t() const { return(static_cast<enum_t>(m_i)); }

   inline flags operator&(Enum f) const
   { flags g; g.m_i = m_i & static_cast<int_t>(f); return(g); }
   inline flags operator&(flags f) const
   { flags g; g.m_i = m_i & static_cast<int_t>(f); return(g); }

   template<typename T, integral<T> = true>
   inline flags operator&(T mask) const
   { flags g; g.m_i = m_i & static_cast<int_t>(mask); return(g); }

   inline flags operator|(flags f) const
   { flags g; g.m_i = m_i | f.m_i; return(g); }
   inline flags operator|(Enum f) const
   { flags g; g.m_i = m_i | static_cast<int_t>(f); return(g); }

   inline flags operator^(flags f) const
   { flags g; g.m_i = m_i ^ f.m_i; return(g); }
   inline flags operator^(Enum f) const
   { flags g; g.m_i = m_i ^ static_cast<int_t>(f); return(g); }

   inline int_t operator~() const
   { return(~m_i); }

   inline operator bool() const { return(!!m_i); }
   inline bool operator!() const { return(!m_i); }

   inline bool test(flags f) const { return((*this & f) == f); }
   inline bool test(Enum f) const { return((*this & f) == f); }

   inline bool test_any() const { return(m_i != 0); }
   inline bool test_any(flags f) const { return((*this & f).test_any()); }

protected:
   int_t m_i = 0;
};

} // namespace uncrustify

#if __GNUC__ == 4 && !defined (__clang__)
#pragma GCC diagnostic pop
#endif

#endif