enum_flags.h
Go to the documentation of this file.
1 /************************************************************************************
2 * *
3 * Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
4 * *
5 * This file is part of RTTR (Run Time Type Reflection) *
6 * License: MIT License *
7 * *
8 * Permission is hereby granted, free of charge, to any person obtaining *
9 * a copy of this software and associated documentation files (the "Software"), *
10 * to deal in the Software without restriction, including without limitation *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
12 * and/or sell copies of the Software, and to permit persons to whom the *
13 * Software is furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included in *
16 * all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
24 * SOFTWARE. *
25 * *
26 *************************************************************************************/
27 
28 #ifndef RTTR_ENUM_FLAGS_H_
29 #define RTTR_ENUM_FLAGS_H_
30 
31 #include "rttr/detail/base/core_prerequisites.h"
32 #include "rttr/detail/misc/std_type_traits.h"
33 
34 #include <type_traits>
35 #include <cstdint>
36 
37 namespace rttr
38 {
39 
40 namespace detail
41 {
42 class enum_flag;
43 }
44 
85 template<typename Enum>
87 {
88  static_assert(sizeof(Enum) <= sizeof(int32_t), "Cannot store enums value with the given type."
89  "Please use an enum which fits into an 'int32_t'." );
90  public:
91  using type = Enum;
92  using enum_type = detail::conditional_t<std::is_signed<typename std::underlying_type<Enum>::type>::value,
93  int32_t,
94  uint32_t>;
95  using zero = enum_type*;
96 
100  RTTR_CONSTEXPR RTTR_INLINE enum_flags(zero = nullptr) RTTR_NOEXCEPT;
101 
105  RTTR_CONSTEXPR RTTR_INLINE enum_flags(Enum flag) RTTR_NOEXCEPT;
106 
113  RTTR_CONSTEXPR RTTR_INLINE enum_flags(detail::enum_flag v) RTTR_NOEXCEPT;
114 
116 
123  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator&=(int mask) RTTR_NOEXCEPT;
124 
131  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator&=(uint32_t mask) RTTR_NOEXCEPT;
132 
139  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator&=(Enum mask) RTTR_NOEXCEPT;
140 
147  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator|=(enum_flags f) RTTR_NOEXCEPT;
148 
155  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator|=(Enum f) RTTR_NOEXCEPT;
156 
163  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator^=(enum_flags f) RTTR_NOEXCEPT;
164 
171  RTTR_CXX14_CONSTEXPR RTTR_INLINE enum_flags& operator^=(Enum f) RTTR_NOEXCEPT;
172 
179  RTTR_CONSTEXPR RTTR_INLINE operator enum_type() const RTTR_NOEXCEPT;
180 
182 
189  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator|(Enum f) const RTTR_NOEXCEPT;
190 
191 
198  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator|(enum_flags f) const RTTR_NOEXCEPT;
199 
206  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator^(Enum f) const RTTR_NOEXCEPT;
207 
214  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator^(enum_flags f) const RTTR_NOEXCEPT;
215 
222  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator&(Enum f) const RTTR_NOEXCEPT;
223 
224 
231  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator&(int mask) const RTTR_NOEXCEPT;
232 
239  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator&(uint32_t mask) const RTTR_NOEXCEPT;
240 
247  RTTR_CONSTEXPR RTTR_INLINE enum_flags operator~() const RTTR_NOEXCEPT;
248 
254  RTTR_CONSTEXPR RTTR_INLINE bool operator!() const RTTR_NOEXCEPT;
255 
261  RTTR_CONSTEXPR RTTR_INLINE bool test_flag(Enum flag) const RTTR_NOEXCEPT;
262 
263  private:
264  enum_type m_value;
265 };
266 
267 namespace detail
268 {
269 
274 class invalid_enum_flag
275 {
276  public:
277  RTTR_CONSTEXPR RTTR_INLINE explicit invalid_enum_flag(int v){}
278 };
279 
280 } // end namespace detail
281 
282 #ifdef DOXYGEN
283 
294 #define RTTR_DECLARE_FLAGS(Flags, Enum)
295 
301 #define RTTR_DECLARE_ENUM_FLAGS_OPERATORS(Flags)
302 
303 #else
304 
305 #define RTTR_DECLARE_FLAGS(Flags, Enum) \
306 using Flags = rttr::enum_flags<Enum>;
307 
308 #define RTTR_DECLARE_ENUM_FLAGS_OPERATORS(Flags) \
309 RTTR_CONSTEXPR RTTR_INLINE rttr::enum_flags<Flags::type> operator|(Flags::type lhs, Flags::type rhs) RTTR_NOEXCEPT \
310 { \
311  return (rttr::enum_flags<Flags::type>(lhs) | rhs); \
312 } \
313 RTTR_CONSTEXPR RTTR_INLINE rttr::enum_flags<Flags::type> operator|(Flags::type lhs, rttr::enum_flags<Flags::type> rhs) RTTR_NOEXCEPT \
314 { \
315  return (rhs | lhs); \
316 } \
317 RTTR_CONSTEXPR RTTR_INLINE rttr::detail::invalid_enum_flag operator|(Flags::type lhs, int rhs) \
318 { \
319  return rttr::detail::invalid_enum_flag(int(lhs) | rhs); \
320 }
321 
322 #endif
323 
324 } // end namespace rttr
325 
326 #include "rttr/detail/impl/enum_flags_impl.h"
327 
328 #endif // RTTR_ENUM_FLAGS_H_
Definition: access_levels.h:33
constexpr bool test_flag(Enum flag) const noexcept
This will test whether the given flag flag was set.
detail::enum_data< Enum_Type > value(string_view, Enum_Type value)
The value function should be used to add a mapping from enum name to value during the registration pr...
Enum type
Definition: enum_flags.h:91
enum_type * zero
Definition: enum_flags.h:95
constexpr enum_flags(zero=nullptr) noexcept
Creates an enum_flags object with no flags set.
The enum_flags class template is used to store OR-combinations of enum values in a type-safe way...
Definition: enum_flags.h:86
detail::conditional_t< std::is_signed< typename std::underlying_type< Enum >::type >::value, int32_t, uint32_t > enum_type
Definition: enum_flags.h:94