Макрос, предоставляющий возможность выполнять битовые операции в enum class
This commit is contained in:
parent
5a6cc75a58
commit
b6d33b8c29
@ -9,6 +9,7 @@ set(current_target_sources
|
||||
set(current_target_headers
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/config.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/limits.hpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/enum_bitmask_operations.hpp
|
||||
)
|
||||
|
||||
add_common_library(TARGET ${current_target} OUTPUT_NAME myx-${current_target}
|
||||
|
58
src/myx/base/enum_bitmask_operations.hpp
Normal file
58
src/myx/base/enum_bitmask_operations.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
#ifndef MYX_BASE_ENUM_BITWISE_OPERATIONS_HPP_
|
||||
#define MYX_BASE_ENUM_BITWISE_OPERATIONS_HPP_
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace myx {
|
||||
|
||||
namespace base {
|
||||
|
||||
template< typename Enum >
|
||||
struct EnableBitMaskOperators
|
||||
{
|
||||
static const bool enable = false;
|
||||
};
|
||||
|
||||
template< typename Enum >
|
||||
typename std::enable_if< EnableBitMaskOperators< Enum >::enable, Enum >::type
|
||||
operator |( Enum lhs, Enum rhs )
|
||||
{
|
||||
using underlying = typename std::underlying_type< Enum >::type;
|
||||
return( static_cast< Enum >(
|
||||
static_cast< underlying >( lhs ) |
|
||||
static_cast< underlying >( rhs )
|
||||
) );
|
||||
}
|
||||
|
||||
} // namespace base
|
||||
|
||||
} // namespace myx
|
||||
|
||||
/**
|
||||
* @brief Макрос, предоставляющий возможность выполнять битовые операции в enum class
|
||||
*
|
||||
* Источник: http://blog.bitwigglers.org/using-enum-classes-as-type-safe-bitmasks/
|
||||
* Пример использования:
|
||||
*
|
||||
* namespace ns {
|
||||
* enum class Permissions
|
||||
* {
|
||||
* Readable = 0x4,
|
||||
* Writeable = 0x2,
|
||||
* Executable = 0x1
|
||||
* };
|
||||
* }
|
||||
* ENABLE_BITMASK_OPERATORS(ns::Permissions)
|
||||
*/
|
||||
|
||||
#define ENABLE_BITMASK_OPERATORS( x ) \
|
||||
template<> \
|
||||
struct myx::base::EnableBitMaskOperators< x > \
|
||||
{ \
|
||||
static const bool enable = true; \
|
||||
};
|
||||
|
||||
#endif // ifndef MYX_BASE_ENUM_BITWISE_OPERATIONS_HPP_
|
Loading…
Reference in New Issue
Block a user