1 /* Copyright (C) 2015-2017 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef COMMON_ENUM_FLAGS_H
19 #define COMMON_ENUM_FLAGS_H
21 /* Type-safe wrapper for enum flags. enum flags are enums where the
22 values are bits that are meant to be ORed together.
24 This allows writing code like the below, while with raw enums this
25 would fail to compile without casts to enum type at the assignments
35 DEF_ENUM_FLAGS_TYPE(enum some_flag, some_flags)
37 some_flags f = flag_val1 | flag_val2;
40 It's also possible to assign literal zero to an enum flags variable
41 (meaning, no flags), dispensing adding an awkward explicit "no
42 value" value to the enumeration. For example:
45 f |= flag_val3 | flag_val4;
47 Note that literal integers other than zero fail to compile:
49 some_flags f = 1; // error
54 /* Traits type used to prevent the global operator overloads from
55 instantiating for non-flag enums. */
56 template<typename T
> struct enum_flags_type
{};
58 /* Use this to mark an enum as flags enum. It defines FLAGS as
59 enum_flags wrapper class for ENUM, and enables the global operator
60 overloads for ENUM. */
61 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
62 typedef enum_flags<enum_type> flags_type; \
64 struct enum_flags_type<enum_type> \
66 typedef enum_flags<enum_type> type; \
69 /* Until we can rely on std::underlying type being universally
70 available (C++11), roll our own for enums. */
71 template<int size
, bool sign
> class integer_for_size
{ typedef void type
; };
72 template<> struct integer_for_size
<1, 0> { typedef uint8_t type
; };
73 template<> struct integer_for_size
<2, 0> { typedef uint16_t type
; };
74 template<> struct integer_for_size
<4, 0> { typedef uint32_t type
; };
75 template<> struct integer_for_size
<8, 0> { typedef uint64_t type
; };
76 template<> struct integer_for_size
<1, 1> { typedef int8_t type
; };
77 template<> struct integer_for_size
<2, 1> { typedef int16_t type
; };
78 template<> struct integer_for_size
<4, 1> { typedef int32_t type
; };
79 template<> struct integer_for_size
<8, 1> { typedef int64_t type
; };
82 struct enum_underlying_type
85 integer_for_size
<sizeof (T
), static_cast<bool>(T (-1) < T (0))>::type
94 typedef typename enum_underlying_type
<enum_type
>::type underlying_type
;
97 /* Private type used to support initializing flag types with zero:
101 but not other integers:
105 The way this works is that we define an implicit constructor that
106 takes a pointer to this private type. Since nothing can
107 instantiate an object of this type, the only possible pointer to
108 pass to the constructor is the NULL pointer, or, zero. */
112 underlying_value () const
118 /* Allow default construction. */
120 : m_enum_value ((enum_type
) 0)
123 enum_flags (const enum_flags
&other
)
124 : m_enum_value (other
.m_enum_value
)
127 enum_flags
&operator= (const enum_flags
&other
)
129 m_enum_value
= other
.m_enum_value
;
133 /* If you get an error saying these two overloads are ambiguous,
134 then you tried to mix values of different enum types. */
135 enum_flags (enum_type e
)
138 enum_flags (struct enum_flags::zero_type
*zero
)
139 : m_enum_value ((enum_type
) 0)
142 enum_flags
&operator&= (enum_type e
)
144 m_enum_value
= (enum_type
) (underlying_value () & e
);
147 enum_flags
&operator|= (enum_type e
)
149 m_enum_value
= (enum_type
) (underlying_value () | e
);
152 enum_flags
&operator^= (enum_type e
)
154 m_enum_value
= (enum_type
) (underlying_value () ^ e
);
158 operator enum_type () const
163 enum_flags
operator& (enum_type e
) const
165 return (enum_type
) (underlying_value () & e
);
167 enum_flags
operator| (enum_type e
) const
169 return (enum_type
) (underlying_value () | e
);
171 enum_flags
operator^ (enum_type e
) const
173 return (enum_type
) (underlying_value () ^ e
);
175 enum_flags
operator~ () const
177 return (enum_type
) ~underlying_value ();
181 /* Stored as enum_type because GDB knows to print the bit flags
182 neatly if the enum values look like bit flags. */
183 enum_type m_enum_value
;
186 /* Global operator overloads. */
188 template <typename enum_type
>
189 typename enum_flags_type
<enum_type
>::type
190 operator& (enum_type e1
, enum_type e2
)
192 return enum_flags
<enum_type
> (e1
) & e2
;
195 template <typename enum_type
>
196 typename enum_flags_type
<enum_type
>::type
197 operator| (enum_type e1
, enum_type e2
)
199 return enum_flags
<enum_type
> (e1
) | e2
;
202 template <typename enum_type
>
203 typename enum_flags_type
<enum_type
>::type
204 operator^ (enum_type e1
, enum_type e2
)
206 return enum_flags
<enum_type
> (e1
) ^ e2
;
209 template <typename enum_type
>
210 typename enum_flags_type
<enum_type
>::type
211 operator~ (enum_type e
)
213 return ~enum_flags
<enum_type
> (e
);
216 #else /* __cplusplus */
218 /* In C, the flags type is just a typedef for the enum type. */
220 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
221 typedef enum_type flags_type
223 #endif /* __cplusplus */
225 #endif /* COMMON_ENUM_FLAGS_H */