mirror of
https://github.com/boostorg/detail.git
synced 2025-07-31 04:47:15 +02:00
Use C++11 underlying_type trait in BOOST_BITMASK.
This bumps the language requirement to C++11, although for compilers that support the __underlying_type intrinsic this may work even in C++03 mode. The operators generated by BOOST_BITMASK now use proper underlying types for casts, which means the operators will no longer truncate bits from large enums. Also mark the operators as noexcept and constexpr (since C++14).
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
// boost/detail/bitmask.hpp ------------------------------------------------//
|
||||
|
||||
// Copyright Beman Dawes 2006
|
||||
// Copyright Andrey Semashev 2025
|
||||
|
||||
// Distributed under the Boost Software License, Version 1.0
|
||||
// http://www.boost.org/LICENSE_1_0.txt
|
||||
@ -18,41 +19,71 @@
|
||||
#define BOOST_BITMASK_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
#define BOOST_BITMASK(Bitmask) \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator| (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
|
||||
| static_cast<boost::int_least32_t>(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator& (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
|
||||
& static_cast<boost::int_least32_t>(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator^ (Bitmask x , Bitmask y ) \
|
||||
{ return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
|
||||
^ static_cast<boost::int_least32_t>(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator~ (Bitmask x ) \
|
||||
{ return static_cast<Bitmask>(~static_cast<boost::int_least32_t>(x)); } \
|
||||
\
|
||||
inline Bitmask & operator&=(Bitmask& x , Bitmask y) \
|
||||
{ x = x & y ; return x ; } \
|
||||
\
|
||||
inline Bitmask & operator|=(Bitmask& x , Bitmask y) \
|
||||
{ x = x | y ; return x ; } \
|
||||
\
|
||||
inline Bitmask & operator^=(Bitmask& x , Bitmask y) \
|
||||
{ x = x ^ y ; return x ; } \
|
||||
\
|
||||
/* Boost extensions to [bitmask.types] */ \
|
||||
\
|
||||
inline BOOST_CONSTEXPR bool operator!(Bitmask x) \
|
||||
{ return !static_cast<int>(x); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR bool bitmask_set(Bitmask x) \
|
||||
#if defined(__has_builtin)
|
||||
#if __has_builtin(__underlying_type)
|
||||
#define BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(enum_type) __underlying_type(enum_type)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE) && defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 40700)
|
||||
#define BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(enum_type) __underlying_type(enum_type)
|
||||
#endif
|
||||
|
||||
#if !defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE)
|
||||
#include <type_traits>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
namespace bitmask {
|
||||
|
||||
#if defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE)
|
||||
template< typename Enum >
|
||||
using underlying_type_t = BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(Enum);
|
||||
#elif (BOOST_CXX_VERSION >= 201402)
|
||||
using std::underlying_type_t;
|
||||
#else
|
||||
template< typename Enum >
|
||||
using underlying_type_t = typename std::underlying_type< Enum >::type;
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
#undef BOOST_BITMASK_DETAIL_UNDERLYING_TYPE
|
||||
|
||||
#define BOOST_BITMASK(Bitmask) \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator| (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
|
||||
| static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator& (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
|
||||
& static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator^ (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
|
||||
^ static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR Bitmask operator~ (Bitmask x) BOOST_NOEXCEPT \
|
||||
{ return static_cast< Bitmask >(~static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x)); } \
|
||||
\
|
||||
inline BOOST_CXX14_CONSTEXPR Bitmask& operator&=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ x = x & y; return x; } \
|
||||
\
|
||||
inline BOOST_CXX14_CONSTEXPR Bitmask& operator|=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ x = x | y; return x; } \
|
||||
\
|
||||
inline BOOST_CXX14_CONSTEXPR Bitmask& operator^=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
|
||||
{ x = x ^ y; return x; } \
|
||||
\
|
||||
/* Boost extensions to [bitmask.types] */ \
|
||||
\
|
||||
inline BOOST_CONSTEXPR bool operator!(Bitmask x) BOOST_NOEXCEPT \
|
||||
{ return !static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x); } \
|
||||
\
|
||||
inline BOOST_CONSTEXPR bool bitmask_set(Bitmask x) BOOST_NOEXCEPT \
|
||||
{ return !!x; }
|
||||
|
||||
#endif // BOOST_BITMASK_HPP
|
||||
|
||||
|
Reference in New Issue
Block a user