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