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
37namespace rttr
38{
39
40namespace detail
41{
42class enum_flag;
43}
44
85template<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
101
106
114
116
124
132
140
148
156
164
172
180
182
190
191
199
207
215
223
224
232
240
248
255
262
263 private:
264 enum_type m_value;
265};
266
267namespace detail
268{
269
274class 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) \
306using Flags = rttr::enum_flags<Enum>;
307
308#define RTTR_DECLARE_ENUM_FLAGS_OPERATORS(Flags) \
309RTTR_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} \
313RTTR_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} \
317RTTR_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_
The array_range class provides a view into an underlying data structure with lower and upper limits.
Definition array_range.h:64
The enum_flags class template is used to store OR-combinations of enum values in a type-safe way.
Definition enum_flags.h:87
enum_type * zero
Definition enum_flags.h:95
constexpr bool test_flag(Enum flag) const noexcept
This will test whether the given flag flag was set.
constexpr enum_flags(zero=nullptr) noexcept
Creates an enum_flags object with no flags set.
detail::conditional_t< std::is_signed< typename std::underlying_type< Enum >::type >::value, int32_t, uint32_t > enum_type
Definition enum_flags.h:92
Definition access_levels.h:34
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...