1
// scoped_enum_emulation.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
11
[section:scoped_enums Scoped Enums]
13
Generates C++0x scoped enums if the feature is present, otherwise emulates C++0x
14
scoped enums with C++03 namespaces and enums. The Boost.Config BOOST_NO_CXX11_SCOPED_ENUMS
15
macro is used to detect feature support.
17
See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf for a
18
description of the scoped enum feature. Note that the committee changed the name
19
from strongly typed enum to scoped enum.
21
Some of the enumerations defined in the standard library are scoped enums.
23
enum class future_errc
26
future_already_retrieved,
27
promise_already_satisfied,
31
On compilers that don't support them, the library provides two emulations:
35
* Able to specify the underlying type.
36
* explicit conversion to/from underlying type.
37
* The wrapper is not a C++03 enum type.
39
The user can declare declare these types as
41
BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
44
future_already_retrieved,
45
promise_already_satisfied,
48
BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
50
These macros allows to use 'future_errc' in almost all the cases as an scoped enum.
52
future_errc err = future_errc::no_state;
54
There are however some limitations:
56
* The type is not a C++ enum, so 'is_enum<future_errc>' will be false_type.
57
* The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some macros.
63
case future_errc::broken_promise:
68
switch (boost::native_value(ev))
70
case future_errc::broken_promise:
74
#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
76
struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc> : public true_type { };
81
#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
83
struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc::enum_type > : public true_type { };
89
BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(algae, char) { green, red, cyan }; BOOST_SCOPED_ENUM_DECLARE_END(algae)
91
algae sample( algae::red );
92
void foo( algae color );
94
sample = algae::green;
98
Caution: only the syntax is emulated; the semantics are not emulated and
99
the syntax emulation doesn't include being able to specify the underlying
102
The literal scoped emulation is via struct rather than namespace to allow use within classes.
103
Thanks to Andrey Semashev for pointing that out.
104
However the type is an real C++03 enum and so convertible implicitly to an int.
108
BOOST_SCOPED_ENUM_START(algae) { green, red, cyan }; BOOST_SCOPED_ENUM_END
110
BOOST_SCOPED_ENUM(algae) sample( algae::red );
111
void foo( BOOST_SCOPED_ENUM(algae) color );
113
sample = algae::green;
116
Helpful comments and suggestions were also made by Kjell Elster, Phil Endecott,
117
Joel Falcou, Mathias Gaunard, Felipe Magno de Almeida, Matt Calabrese, Vicente
118
Botet, and Daniel James.
124
#ifndef BOOST_SCOPED_ENUM_EMULATION_HPP
125
#define BOOST_SCOPED_ENUM_EMULATION_HPP
127
#include <boost/config.hpp>
128
#include <boost/detail/workaround.hpp>
133
#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
135
* Meta-function to get the underlying type of a scoped enum.
137
* Requires EnumType must be an enum type or the emulation of a scoped enum
139
template <typename EnumType>
140
struct underlying_type
143
* The member typedef type names the underlying type of EnumType. It is EnumType::underlying_type when the EnumType is an emulated scoped enum,
144
* std::underlying_type<EnumType>::type when the standard library std::underlying_type is provided.
146
* The user will need to specialize it when the compiler supports scoped enums but don't provides std::underlying_type.
148
typedef typename EnumType::underlying_type type;
152
* Meta-function to get the native enum type associated to an enum class or its emulation.
154
template <typename EnumType>
158
* The member typedef type names the native enum type associated to the scoped enum,
159
* which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
161
typedef typename EnumType::enum_type type;
165
* Casts a scoped enum to its underlying type.
167
* This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
168
* @param v A scoped enum.
169
* @returns The underlying type.
172
template <typename UnderlyingType, typename EnumType>
173
UnderlyingType underlying_cast(EnumType v)
175
return v.get_underlying_value_();
179
* Casts a scoped enum to its native enum type.
181
* This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
183
* EnumType the scoped enum type
185
* @param v A scoped enum.
186
* @returns The native enum value.
189
template <typename EnumType>
191
typename EnumType::enum_type native_value(EnumType e)
193
return e.native_value_();
196
#else // BOOST_NO_CXX11_SCOPED_ENUMS
198
template <typename EnumType>
199
struct underlying_type
201
//typedef typename std::underlying_type<EnumType>::type type;
204
template <typename EnumType>
207
typedef EnumType type;
210
template <typename UnderlyingType, typename EnumType>
211
UnderlyingType underlying_cast(EnumType v)
213
return static_cast<UnderlyingType>(v);
216
template <typename EnumType>
218
EnumType native_value(EnumType e)
227
#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
229
#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
231
#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
232
explicit operator underlying_type() const { return get_underlying_value_(); }
236
#define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
241
* Start a declaration of a scoped enum.
243
* @param EnumType The new scoped enum.
244
* @param UnderlyingType The underlying type.
246
#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \
248
typedef UnderlyingType underlying_type; \
249
EnumType() BOOST_NOEXCEPT {} \
250
explicit EnumType(underlying_type v) : v_(v) {} \
251
underlying_type get_underlying_value_() const { return v_; } \
252
BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
254
underlying_type v_; \
255
typedef EnumType self_type; \
259
#define BOOST_SCOPED_ENUM_DECLARE_END2() \
260
enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
261
operator enum_type() const BOOST_NOEXCEPT { return get_native_value_(); } \
262
friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
263
friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
264
friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
265
friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
266
friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
267
friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
268
friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
269
friend bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
270
friend bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
271
friend bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
272
friend bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
273
friend bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
274
friend bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
275
friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
276
friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
277
friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
278
friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
279
friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
282
#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
284
EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \
285
BOOST_SCOPED_ENUM_DECLARE_END2()
288
* Starts a declaration of a scoped enum with the default int underlying type.
290
* @param EnumType The new scoped enum.
292
#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
293
BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
296
* Name of the native enum type.
298
* @param NT The new scoped enum.
300
#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
302
* Forward declares an scoped enum.
304
* @param NT The scoped enum.
306
#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
308
#else // BOOST_NO_CXX11_SCOPED_ENUMS
310
#define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType:UnderlyingType
311
#define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
312
#define BOOST_SCOPED_ENUM_DECLARE_END2()
313
#define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
315
#define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
316
#define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
318
#endif // BOOST_NO_CXX11_SCOPED_ENUMS
320
#define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
321
#define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
322
#define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
324
//#ifdef BOOST_NO_CXX11_SCOPED_ENUMS
326
//# define BOOST_SCOPED_ENUM_START(name) struct name { enum enum_type
327
//# define BOOST_SCOPED_ENUM_END };
328
//# define BOOST_SCOPED_ENUM(name) name::enum_type
332
//# define BOOST_SCOPED_ENUM_START(name) enum class name
333
//# define BOOST_SCOPED_ENUM_END
334
//# define BOOST_SCOPED_ENUM(name) name
337
#endif // BOOST_SCOPED_ENUM_EMULATION_HPP