Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:14

0001 //  scoped_enum.hpp  ---------------------------------------------------------//
0002 
0003 //  Copyright Beman Dawes, 2009
0004 //  Copyright (C) 2011-2012 Vicente J. Botet Escriba
0005 //  Copyright (C) 2012 Anthony Williams
0006 
0007 //  Distributed under the Boost Software License, Version 1.0.
0008 //  See http://www.boost.org/LICENSE_1_0.txt
0009 
0010 #ifndef BOOST_CORE_SCOPED_ENUM_HPP
0011 #define BOOST_CORE_SCOPED_ENUM_HPP
0012 
0013 #include <boost/config.hpp>
0014 
0015 #ifdef BOOST_HAS_PRAGMA_ONCE
0016 #pragma once
0017 #endif
0018 
0019 namespace boost
0020 {
0021 
0022 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
0023 
0024   /**
0025    * Meta-function to get the native enum type associated to an enum class or its emulation.
0026    */
0027   template <typename EnumType>
0028   struct native_type
0029   {
0030     /**
0031      * The member typedef type names the native enum type associated to the scoped enum,
0032      * which is it self if the compiler supports scoped enums or EnumType::enum_type if it is an emulated scoped enum.
0033      */
0034     typedef typename EnumType::enum_type type;
0035   };
0036 
0037   /**
0038    * Casts a scoped enum to its underlying type.
0039    *
0040    * This function is useful when working with scoped enum classes, which doens't implicitly convert to the underlying type.
0041    * @param v A scoped enum.
0042    * @returns The underlying type.
0043    * @throws No-throws.
0044    */
0045   template <typename UnderlyingType, typename EnumType>
0046   inline
0047   BOOST_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BOOST_NOEXCEPT
0048   {
0049     return v.get_underlying_value_();
0050   }
0051 
0052   /**
0053    * Casts a scoped enum to its native enum type.
0054    *
0055    * This function is useful to make programs portable when the scoped enum emulation can not be use where native enums can.
0056    *
0057    * EnumType the scoped enum type
0058    *
0059    * @param v A scoped enum.
0060    * @returns The native enum value.
0061    * @throws No-throws.
0062    */
0063   template <typename EnumType>
0064   inline
0065   BOOST_CONSTEXPR typename EnumType::enum_type native_value(EnumType e) BOOST_NOEXCEPT
0066   {
0067     return e.get_native_value_();
0068   }
0069 
0070 #else  // BOOST_NO_CXX11_SCOPED_ENUMS
0071 
0072   template <typename EnumType>
0073   struct native_type
0074   {
0075     typedef EnumType type;
0076   };
0077 
0078   template <typename UnderlyingType, typename EnumType>
0079   inline
0080   BOOST_CONSTEXPR UnderlyingType underlying_cast(EnumType v) BOOST_NOEXCEPT
0081   {
0082     return static_cast<UnderlyingType>(v);
0083   }
0084 
0085   template <typename EnumType>
0086   inline
0087   BOOST_CONSTEXPR EnumType native_value(EnumType e) BOOST_NOEXCEPT
0088   {
0089     return e;
0090   }
0091 
0092 #endif // BOOST_NO_CXX11_SCOPED_ENUMS
0093 }
0094 
0095 
0096 #ifdef BOOST_NO_CXX11_SCOPED_ENUMS
0097 
0098 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
0099 
0100 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR \
0101      explicit BOOST_CONSTEXPR operator underlying_type() const BOOST_NOEXCEPT { return get_underlying_value_(); }
0102 
0103 #else
0104 
0105 #define BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR
0106 
0107 #endif
0108 
0109 /**
0110  * Start a declaration of a scoped enum.
0111  *
0112  * @param EnumType The new scoped enum.
0113  * @param UnderlyingType The underlying type.
0114  */
0115 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType, UnderlyingType)    \
0116     struct EnumType {                                                   \
0117         typedef void is_boost_scoped_enum_tag;                          \
0118         typedef UnderlyingType underlying_type;                         \
0119         EnumType() BOOST_NOEXCEPT {}                                    \
0120         explicit BOOST_CONSTEXPR EnumType(underlying_type v) BOOST_NOEXCEPT : v_(v) {}                 \
0121         BOOST_CONSTEXPR underlying_type get_underlying_value_() const BOOST_NOEXCEPT { return v_; }    \
0122         BOOST_SCOPED_ENUM_UT_DECLARE_CONVERSION_OPERATOR                \
0123     private:                                                            \
0124         underlying_type v_;                                             \
0125         typedef EnumType self_type;                                     \
0126     public:                                                             \
0127         enum enum_type
0128 
0129 #define BOOST_SCOPED_ENUM_DECLARE_END2() \
0130         BOOST_CONSTEXPR enum_type get_native_value_() const BOOST_NOEXCEPT { return enum_type(v_); } \
0131         friend BOOST_CONSTEXPR bool operator ==(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==enum_type(rhs.v_); } \
0132         friend BOOST_CONSTEXPR bool operator ==(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)==rhs; } \
0133         friend BOOST_CONSTEXPR bool operator ==(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs==enum_type(rhs.v_); } \
0134         friend BOOST_CONSTEXPR bool operator !=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=enum_type(rhs.v_); } \
0135         friend BOOST_CONSTEXPR bool operator !=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)!=rhs; } \
0136         friend BOOST_CONSTEXPR bool operator !=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs!=enum_type(rhs.v_); } \
0137         friend BOOST_CONSTEXPR bool operator <(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<enum_type(rhs.v_); } \
0138         friend BOOST_CONSTEXPR bool operator <(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<rhs; } \
0139         friend BOOST_CONSTEXPR bool operator <(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<enum_type(rhs.v_); } \
0140         friend BOOST_CONSTEXPR bool operator <=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=enum_type(rhs.v_); } \
0141         friend BOOST_CONSTEXPR bool operator <=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)<=rhs; } \
0142         friend BOOST_CONSTEXPR bool operator <=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs<=enum_type(rhs.v_); } \
0143         friend BOOST_CONSTEXPR bool operator >(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>enum_type(rhs.v_); } \
0144         friend BOOST_CONSTEXPR bool operator >(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>rhs; } \
0145         friend BOOST_CONSTEXPR bool operator >(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>enum_type(rhs.v_); } \
0146         friend BOOST_CONSTEXPR bool operator >=(self_type lhs, self_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=enum_type(rhs.v_); } \
0147         friend BOOST_CONSTEXPR bool operator >=(self_type lhs, enum_type rhs) BOOST_NOEXCEPT { return enum_type(lhs.v_)>=rhs; } \
0148         friend BOOST_CONSTEXPR bool operator >=(enum_type lhs, self_type rhs) BOOST_NOEXCEPT { return lhs>=enum_type(rhs.v_); } \
0149     };
0150 
0151 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) \
0152     ; \
0153     BOOST_CONSTEXPR EnumType(enum_type v) BOOST_NOEXCEPT : v_(v) {}                 \
0154     BOOST_SCOPED_ENUM_DECLARE_END2()
0155 
0156 /**
0157  * Starts a declaration of a scoped enum with the default int underlying type.
0158  *
0159  * @param EnumType The new scoped enum.
0160  */
0161 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) \
0162   BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,int)
0163 
0164 /**
0165  * Name of the native enum type.
0166  *
0167  * @param EnumType The new scoped enum.
0168  */
0169 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType::enum_type
0170 /**
0171  * Forward declares an scoped enum.
0172  *
0173  * @param EnumType The scoped enum.
0174  */
0175 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) struct EnumType
0176 
0177 #else  // BOOST_NO_CXX11_SCOPED_ENUMS
0178 
0179 #define BOOST_SCOPED_ENUM_UT_DECLARE_BEGIN(EnumType,UnderlyingType) enum class EnumType : UnderlyingType
0180 #define BOOST_SCOPED_ENUM_DECLARE_BEGIN(EnumType) enum class EnumType
0181 #define BOOST_SCOPED_ENUM_DECLARE_END2()
0182 #define BOOST_SCOPED_ENUM_DECLARE_END(EnumType) ;
0183 
0184 #define BOOST_SCOPED_ENUM_NATIVE(EnumType) EnumType
0185 #define BOOST_SCOPED_ENUM_FORWARD_DECLARE(EnumType) enum class EnumType
0186 
0187 #endif  // BOOST_NO_CXX11_SCOPED_ENUMS
0188 
0189 // Deprecated macros
0190 #define BOOST_SCOPED_ENUM_START(name) BOOST_SCOPED_ENUM_DECLARE_BEGIN(name)
0191 #define BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_DECLARE_END2()
0192 #define BOOST_SCOPED_ENUM(name) BOOST_SCOPED_ENUM_NATIVE(name)
0193 
0194 #endif  // BOOST_CORE_SCOPED_ENUM_HPP