diff --git a/src/myx/base/CMakeLists.txt b/src/myx/base/CMakeLists.txt index 7e857f8..2fa7acd 100644 --- a/src/myx/base/CMakeLists.txt +++ b/src/myx/base/CMakeLists.txt @@ -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} diff --git a/src/myx/base/enum_bitmask_operations.hpp b/src/myx/base/enum_bitmask_operations.hpp new file mode 100644 index 0000000..a93094d --- /dev/null +++ b/src/myx/base/enum_bitmask_operations.hpp @@ -0,0 +1,58 @@ +#ifndef MYX_BASE_ENUM_BITWISE_OPERATIONS_HPP_ +#define MYX_BASE_ENUM_BITWISE_OPERATIONS_HPP_ + + +#pragma once + +#include + +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_