* Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Description: bitmask enum class.
*/
#ifndef DATASYSTEM_COMMON_UTIL_BITMASK_ENUM_H
#define DATASYSTEM_COMMON_UTIL_BITMASK_ENUM_H
#include <cstdint>
#include <memory>
#include <type_traits>
#include <utility>
#define ENABLE_BITMASK_ENUM_OPS(T) \
constexpr bool bitmask_ops(T) \
{ \
return true; \
}
#define TESTFLAG(lhs, rhs) (((lhs) & (rhs)) == (rhs))
#define TESTANYFLAG(lhs, rhs) (((lhs) & (rhs)) > 0)
#define SETFLAG(target, source) (target) |= (source)
#define CLEARFLAG(target, source) (target) &= ~(source)
#define INITFLAG(target, source) (target) = (source)
inline uint32_t ClearUint32OddBits(uint32_t bitmap)
{
uint32_t evenMask = 0x55555555;
return bitmap &= evenMask;
}
inline uint32_t ClearUint32EvenBits(uint32_t bitmap)
{
uint32_t oddMask = 0xAAAAAAAA;
return bitmap &= oddMask;
}
namespace datasystem {
template <typename T>
constexpr bool bitmask_ops(T)
{
return false;
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T>::type operator|(T lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
return static_cast<T>(static_cast<enum_under_type>(lhs) | static_cast<enum_under_type>(rhs));
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T>::type operator&(T lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
return static_cast<T>(static_cast<enum_under_type>(lhs) & static_cast<enum_under_type>(rhs));
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T>::type operator^(T lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
return static_cast<T>(static_cast<enum_under_type>(lhs) ^ static_cast<enum_under_type>(rhs));
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T>::type operator~(T lhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
return static_cast<T>(~static_cast<enum_under_type>(lhs));
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T &>::type operator|=(T &lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
lhs = static_cast<T>(static_cast<enum_under_type>(lhs) | static_cast<enum_under_type>(rhs));
return lhs;
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T &>::type operator&=(T &lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
lhs = static_cast<T>(static_cast<enum_under_type>(lhs) & static_cast<enum_under_type>(rhs));
return lhs;
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), T &>::type operator^=(T &lhs, T rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
lhs = static_cast<T>(static_cast<enum_under_type>(lhs) ^ static_cast<enum_under_type>(rhs));
return lhs;
}
template <typename T>
typename std::enable_if<bitmask_ops(T()), bool>::type operator>(const T &lhs, int rhs)
{
using enum_under_type = typename std::underlying_type<T>::type;
return static_cast<enum_under_type>(lhs) > static_cast<enum_under_type>(rhs);
}
}
#endif