Chris@102
|
1 // scoped_enum.hpp ---------------------------------------------------------//
|
Chris@102
|
2
|
Chris@102
|
3 // Copyright Beman Dawes, 2009
|
Chris@102
|
4 // Copyright (C) 2011-2012 Vicente J. Botet Escriba
|
Chris@102
|
5 // Copyright (C) 2012 Anthony Williams
|
Chris@102
|
6
|
Chris@102
|
7 // Distributed under the Boost Software License, Version 1.0.
|
Chris@102
|
8 // See http://www.boost.org/LICENSE_1_0.txt
|
Chris@102
|
9
|
Chris@102
|
10 #ifndef BOOST_CORE_SCOPED_ENUM_HPP
|
Chris@102
|
11 #define BOOST_CORE_SCOPED_ENUM_HPP
|
Chris@102
|
12
|
Chris@102
|
13 #include <boost/config.hpp>
|
Chris@102
|
14
|
Chris@102
|
15 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@102
|
16 #pragma once
|
Chris@102
|
17 #endif
|
Chris@102
|
18
|
Chris@102
|
19 namespace boost
|
Chris@102
|
20 {
|
Chris@102
|
21
|
Chris@102
|
22 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
23
|
Chris@102
|
24 /**
|
Chris@102
|
25 * Meta-function to get the native enum type associated to an enum class or its emulation.
|
Chris@102
|
26 */
|
Chris@102
|
27 template <typename EnumType>
|
Chris@102
|
28 struct native_type
|
Chris@102
|
29 {
|
Chris@102
|
30 /**
|
Chris@102
|
31 * The member typedef type names the native enum type associated to the scoped enum,
|
Chris@102
|
32 * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
|
Chris@102
|
33 */
|
Chris@102
|
34 typedef typename EnumType::enum_type type;
|
Chris@102
|
35 };
|
Chris@102
|
36
|
Chris@102
|
37 /**
|
Chris@102
|
38 * Casts a scoped enum to its underlying type.
|
Chris@102
|
39 *
|
Chris@102
|
40 * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
|
Chris@102
|
41 * @param v A scoped enum.
|
Chris@102
|
42 * @returns The underlying type.
|
Chris@102
|
43 * @throws No-throws.
|
Chris@102
|
44 */
|
Chris@102
|
45 template <typename UnderlyingType, typename EnumType>
|
Chris@102
|
46 UnderlyingType underlying_cast(EnumType v)
|
Chris@102
|
47 {
|
Chris@102
|
48 return v.get_underlying_value_();
|
Chris@102
|
49 }
|
Chris@102
|
50
|
Chris@102
|
51 /**
|
Chris@102
|
52 * Casts a scoped enum to its native enum type.
|
Chris@102
|
53 *
|
Chris@102
|
54 * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
|
Chris@102
|
55 *
|
Chris@102
|
56 * EnumType the scoped enum type
|
Chris@102
|
57 *
|
Chris@102
|
58 * @param v A scoped enum.
|
Chris@102
|
59 * @returns The native enum value.
|
Chris@102
|
60 * @throws No-throws.
|
Chris@102
|
61 */
|
Chris@102
|
62 template <typename EnumType>
|
Chris@102
|
63 inline
|
Chris@102
|
64 typename EnumType::enum_type native_value(EnumType e)
|
Chris@102
|
65 {
|
Chris@102
|
66 return e.get_native_value_();
|
Chris@102
|
67 }
|
Chris@102
|
68
|
Chris@102
|
69 #else // BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
70
|
Chris@102
|
71 template <typename EnumType>
|
Chris@102
|
72 struct native_type
|
Chris@102
|
73 {
|
Chris@102
|
74 typedef EnumType type;
|
Chris@102
|
75 };
|
Chris@102
|
76
|
Chris@102
|
77 template <typename UnderlyingType, typename EnumType>
|
Chris@102
|
78 UnderlyingType underlying_cast(EnumType v)
|
Chris@102
|
79 {
|
Chris@102
|
80 return static_cast<UnderlyingType>(v);
|
Chris@102
|
81 }
|
Chris@102
|
82
|
Chris@102
|
83 template <typename EnumType>
|
Chris@102
|
84 inline
|
Chris@102
|
85 EnumType native_value(EnumType e)
|
Chris@102
|
86 {
|
Chris@102
|
87 return e;
|
Chris@102
|
88 }
|
Chris@102
|
89
|
Chris@102
|
90 #endif // BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
91 }
|
Chris@102
|
92
|
Chris@102
|
93
|
Chris@102
|
94 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
95
|
Chris@102
|
96 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
|
Chris@102
|
97
|
Chris@102
|
98 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
|
Chris@102
|
99 explicit operator underlying_type() const BOOST_NOEXCEPT { return get_underlying_value_(); }
|
Chris@102
|
100
|
Chris@102
|
101 #else
|
Chris@102
|
102
|
Chris@102
|
103 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
|
Chris@102
|
104
|
Chris@102
|
105 #endif
|
Chris@102
|
106
|
Chris@102
|
107 /**
|
Chris@102
|
108 * Start a declaration of a scoped enum.
|
Chris@102
|
109 *
|
Chris@102
|
110 * @param EnumType The new scoped enum.
|
Chris@102
|
111 * @param UnderlyingType The underlying type.
|
Chris@102
|
112 */
|
Chris@102
|
113 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
|
Chris@102
|
114 struct EnumType { \
|
Chris@102
|
115 typedef void is_boost_scoped_enum_tag; \
|
Chris@102
|
116 typedef UnderlyingType underlying_type; \
|
Chris@102
|
117 EnumType() BOOST_NOEXCEPT {} \
|
Chris@102
|
118 explicit EnumType(underlying_type v) BOOST_NOEXCEPT : v_(v) {} \
|
Chris@102
|
119 underlying_type get_underlying_value_() const BOOST_NOEXCEPT { return v_; } \
|
Chris@102
|
120 BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
|
Chris@102
|
121 private: \
|
Chris@102
|
122 underlying_type v_; \
|
Chris@102
|
123 typedef EnumType self_type; \
|
Chris@102
|
124 public: \
|
Chris@102
|
125 enum enum_type
|
Chris@102
|
126
|
Chris@102
|
127 #define BOOST_SCOPED_ENUM_DECLARE_END2() \
|
Chris@102
|
128 enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
|
Chris@102
|
129 friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
|
Chris@102
|
130 friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
|
Chris@102
|
131 friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
|
Chris@102
|
132 friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
|
Chris@102
|
133 friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
|
Chris@102
|
134 friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
|
Chris@102
|
135 friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
|
Chris@102
|
136 friend bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
|
Chris@102
|
137 friend bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
|
Chris@102
|
138 friend bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
|
Chris@102
|
139 friend bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
|
Chris@102
|
140 friend bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
|
Chris@102
|
141 friend bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
|
Chris@102
|
142 friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
|
Chris@102
|
143 friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
|
Chris@102
|
144 friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
|
Chris@102
|
145 friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
|
Chris@102
|
146 friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
|
Chris@102
|
147 };
|
Chris@102
|
148
|
Chris@102
|
149 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
|
Chris@102
|
150 ; \
|
Chris@102
|
151 EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
|
Chris@102
|
152 BOOST_SCOPED_ENUM_DECLARE_END2()
|
Chris@102
|
153
|
Chris@102
|
154 /**
|
Chris@102
|
155 * Starts a declaration of a scoped enum with the default int underlying type.
|
Chris@102
|
156 *
|
Chris@102
|
157 * @param EnumType The new scoped enum.
|
Chris@102
|
158 */
|
Chris@102
|
159 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
|
Chris@102
|
160 BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
|
Chris@102
|
161
|
Chris@102
|
162 /**
|
Chris@102
|
163 * Name of the native enum type.
|
Chris@102
|
164 *
|
Chris@102
|
165 * @param EnumType The new scoped enum.
|
Chris@102
|
166 */
|
Chris@102
|
167 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
|
Chris@102
|
168 /**
|
Chris@102
|
169 * Forward declares an scoped enum.
|
Chris@102
|
170 *
|
Chris@102
|
171 * @param EnumType The scoped enum.
|
Chris@102
|
172 */
|
Chris@102
|
173 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
|
Chris@102
|
174
|
Chris@102
|
175 #else // BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
176
|
Chris@102
|
177 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType
|
Chris@102
|
178 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
|
Chris@102
|
179 #define BOOST_SCOPED_ENUM_DECLARE_END2()
|
Chris@102
|
180 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
|
Chris@102
|
181
|
Chris@102
|
182 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
|
Chris@102
|
183 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
|
Chris@102
|
184
|
Chris@102
|
185 #endif // BOOST_NO_CXX11_SCOPED_ENUMS
|
Chris@102
|
186
|
Chris@102
|
187 // Deprecated macros
|
Chris@102
|
188 #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
|
Chris@102
|
189 #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
|
Chris@102
|
190 #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
|
Chris@102
|
191
|
Chris@102
|
192 #endif // BOOST_CORE_SCOPED_ENUM_HPP
|