1 // scoped_enum.hpp ---------------------------------------------------------//
3 // Copyright Beman Dawes, 2009
4 // Copyright (C) 2011-2012 Vicente J. Botet Escriba
5 // Copyright (C) 2012 Anthony Williams
7 // Distributed under the Boost Software License, Version 1.0.
8 // See http://www.boost.org/LICENSE_1_0.txt
10 #ifndef BOOST_CORE_SCOPED_ENUM_HPP
11 #define BOOST_CORE_SCOPED_ENUM_HPP
13 #include <boost/config.hpp>
15 #ifdef BOOST_HAS_PRAGMA_ONCE
22 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
25 * Meta-function to get the native enum type associated to an enum class or its emulation.
27 template <typename EnumType>
31 * The member typedef type names the native enum type associated to the scoped enum,
32 * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
34 typedef typename EnumType::enum_type type;
38 * Casts a scoped enum to its underlying type.
40 * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
41 * @param v A scoped enum.
42 * @returns The underlying type.
45 template <typename UnderlyingType, typename EnumType>
46 UnderlyingType underlying_cast(EnumType v)
48 return v.get_underlying_value_();
52 * Casts a scoped enum to its native enum type.
54 * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
56 * EnumType the scoped enum type
58 * @param v A scoped enum.
59 * @returns The native enum value.
62 template <typename EnumType>
64 typename EnumType::enum_type native_value(EnumType e)
66 return e.get_native_value_();
69 #else // BOOST_NO_CXX11_SCOPED_ENUMS
71 template <typename EnumType>
74 typedef EnumType type;
77 template <typename UnderlyingType, typename EnumType>
78 UnderlyingType underlying_cast(EnumType v)
80 return static_cast<UnderlyingType>(v);
83 template <typename EnumType>
85 EnumType native_value(EnumType e)
90 #endif // BOOST_NO_CXX11_SCOPED_ENUMS
94 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
96 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
98 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
99 explicit operator underlying_type() const BOOST_NOEXCEPT { return get_underlying_value_(); }
103 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
108 * Start a declaration of a scoped enum.
110 * @param EnumType The new scoped enum.
111 * @param UnderlyingType The underlying type.
113 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
115 typedef void is_boost_scoped_enum_tag; \
116 typedef UnderlyingType underlying_type; \
117 EnumType() BOOST_NOEXCEPT {} \
118 explicit EnumType(underlying_type v) BOOST_NOEXCEPT : v_(v) {} \
119 underlying_type get_underlying_value_() const BOOST_NOEXCEPT { return v_; } \
120 BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
122 underlying_type v_; \
123 typedef EnumType self_type; \
127 #define BOOST_SCOPED_ENUM_DECLARE_END2() \
128 enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
129 friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
130 friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
131 friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
132 friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
133 friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
134 friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
135 friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
136 friend bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
137 friend bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
138 friend bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
139 friend bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
140 friend bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
141 friend bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
142 friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
143 friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
144 friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
145 friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
146 friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
149 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
151 EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
152 BOOST_SCOPED_ENUM_DECLARE_END2()
155 * Starts a declaration of a scoped enum with the default int underlying type.
157 * @param EnumType The new scoped enum.
159 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
160 BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
163 * Name of the native enum type.
165 * @param EnumType The new scoped enum.
167 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
169 * Forward declares an scoped enum.
171 * @param EnumType The scoped enum.
173 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
175 #else // BOOST_NO_CXX11_SCOPED_ENUMS
177 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType
178 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
179 #define BOOST_SCOPED_ENUM_DECLARE_END2()
180 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
182 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
183 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
185 #endif // BOOST_NO_CXX11_SCOPED_ENUMS
188 #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
189 #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
190 #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
192 #endif // BOOST_CORE_SCOPED_ENUM_HPP