Chris@102: // scoped_enum.hpp ---------------------------------------------------------// Chris@102: Chris@102: // Copyright Beman Dawes, 2009 Chris@102: // Copyright (C) 2011-2012 Vicente J. Botet Escriba Chris@102: // Copyright (C) 2012 Anthony Williams Chris@102: Chris@102: // Distributed under the Boost Software License, Version 1.0. Chris@102: // See http://www.boost.org/LICENSE_1_0.txt Chris@102: Chris@102: #ifndef BOOST_CORE_SCOPED_ENUM_HPP Chris@102: #define BOOST_CORE_SCOPED_ENUM_HPP Chris@102: Chris@102: #include Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: namespace boost Chris@102: { Chris@102: Chris@102: #ifdef BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: Chris@102: /** Chris@102: * Meta-function to get the native enum type associated to an enum class or its emulation. Chris@102: */ Chris@102: template Chris@102: struct native_type Chris@102: { Chris@102: /** Chris@102: * The member typedef type names the native enum type associated to the scoped enum, Chris@102: * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum. Chris@102: */ Chris@102: typedef typename EnumType::enum_type type; Chris@102: }; Chris@102: Chris@102: /** Chris@102: * Casts a scoped enum to its underlying type. Chris@102: * Chris@102: * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type. Chris@102: * @param v A scoped enum. Chris@102: * @returns The underlying type. Chris@102: * @throws No-throws. Chris@102: */ Chris@102: template Chris@102: UnderlyingType underlying_cast(EnumType v) Chris@102: { Chris@102: return v.get_underlying_value_(); Chris@102: } Chris@102: Chris@102: /** Chris@102: * Casts a scoped enum to its native enum type. Chris@102: * Chris@102: * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can. Chris@102: * Chris@102: * EnumType the scoped enum type Chris@102: * Chris@102: * @param v A scoped enum. Chris@102: * @returns The native enum value. Chris@102: * @throws No-throws. Chris@102: */ Chris@102: template Chris@102: inline Chris@102: typename EnumType::enum_type native_value(EnumType e) Chris@102: { Chris@102: return e.get_native_value_(); Chris@102: } Chris@102: Chris@102: #else // BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: Chris@102: template Chris@102: struct native_type Chris@102: { Chris@102: typedef EnumType type; Chris@102: }; Chris@102: Chris@102: template Chris@102: UnderlyingType underlying_cast(EnumType v) Chris@102: { Chris@102: return static_cast(v); Chris@102: } Chris@102: Chris@102: template Chris@102: inline Chris@102: EnumType native_value(EnumType e) Chris@102: { Chris@102: return e; Chris@102: } Chris@102: Chris@102: #endif // BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: } Chris@102: Chris@102: Chris@102: #ifdef BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: Chris@102: #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \ Chris@102: explicit operator underlying_type() const BOOST_NOEXCEPT { return get_underlying_value_(); } Chris@102: Chris@102: #else Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR Chris@102: Chris@102: #endif Chris@102: Chris@102: /** Chris@102: * Start a declaration of a scoped enum. Chris@102: * Chris@102: * @param EnumType The new scoped enum. Chris@102: * @param UnderlyingType The underlying type. Chris@102: */ Chris@102: #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType) \ Chris@102: struct EnumType { \ Chris@102: typedef void is_boost_scoped_enum_tag; \ Chris@102: typedef UnderlyingType underlying_type; \ Chris@102: EnumType() BOOST_NOEXCEPT {} \ Chris@102: explicit EnumType(underlying_type v) BOOST_NOEXCEPT : v_(v) {} \ Chris@102: underlying_type get_underlying_value_() const BOOST_NOEXCEPT { return v_; } \ Chris@102: BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \ Chris@102: private: \ Chris@102: underlying_type v_; \ Chris@102: typedef EnumType self_type; \ Chris@102: public: \ Chris@102: enum enum_type Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_END2() \ Chris@102: enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \ Chris@102: friend bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \ Chris@102: friend bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \ Chris@102: friend bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \ Chris@102: friend bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \ Chris@102: friend bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \ Chris@102: friend bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \ Chris@102: friend bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \ Chris@102: friend bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \ Chris@102: friend bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \ Chris@102: friend bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \ Chris@102: friend bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \ Chris@102: friend bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \ Chris@102: }; Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \ Chris@102: ; \ Chris@102: EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {} \ Chris@102: BOOST_SCOPED_ENUM_DECLARE_END2() Chris@102: Chris@102: /** Chris@102: * Starts a declaration of a scoped enum with the default int underlying type. Chris@102: * Chris@102: * @param EnumType The new scoped enum. Chris@102: */ Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \ Chris@102: BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int) Chris@102: Chris@102: /** Chris@102: * Name of the native enum type. Chris@102: * Chris@102: * @param EnumType The new scoped enum. Chris@102: */ Chris@102: #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type Chris@102: /** Chris@102: * Forward declares an scoped enum. Chris@102: * Chris@102: * @param EnumType The scoped enum. Chris@102: */ Chris@102: #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType Chris@102: Chris@102: #else // BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_END2() Chris@102: #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ; Chris@102: Chris@102: #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType Chris@102: #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType Chris@102: Chris@102: #endif // BOOST_NO_CXX11_SCOPED_ENUMS Chris@102: Chris@102: // Deprecated macros Chris@102: #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name) Chris@102: #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2() Chris@102: #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name) Chris@102: Chris@102: #endif // BOOST_CORE_SCOPED_ENUM_HPP