diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index f78e49c63..7ac0c6a56 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -98,6 +98,7 @@ set(ENUMS_HEADER_FILES set(UTILS_HEADER_FILES enum-generator.h + enum-mask.h general.h magic-macros.h traits.h diff --git a/include/linphone/utils/enum-mask.h b/include/linphone/utils/enum-mask.h new file mode 100644 index 000000000..4f619351d --- /dev/null +++ b/include/linphone/utils/enum-mask.h @@ -0,0 +1,175 @@ +/* + * enum-mask.h + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef _ENUM_MASK_H_ +#define _ENUM_MASK_H_ + +#include + +#include "linphone/utils/general.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +template +class EnumMask { + static_assert(std::is_enum::value, "EnumMask must be used with enum type. Logic no?"); + static_assert(sizeof(T) <= sizeof(int), "EnumMask supports only int, short or char values."); + +public: + // EnumMask's type. Can be int or unsigned int. + // See: http://en.cppreference.com/w/cpp/types/underlying_type + typedef typename std::conditional< + std::is_signed::type>::value, int, unsigned int + >::type StorageType; + + typedef std::integral_constant Zero; + + // Zero initializer: Take 0 value or no value. + constexpr EnumMask (int *zero = 0) : mMask(0) {} + + constexpr EnumMask (T value) : mMask(StorageType(value)) {} + constexpr EnumMask (std::initializer_list mask) : mMask(init(mask.begin(), mask.end())) {} + + constexpr operator StorageType () const { + return mMask; + } + + constexpr bool isSet (T value) const { + return isSet(StorageType(value)); + } + + inline EnumMask &set (T value) { + *this |= value; + return *this; + } + + inline EnumMask &unset (T value) { + *this &= ~value; + return *this; + } + + constexpr bool operator! () const { + return !mMask; + } + + inline EnumMask &operator&= (int mask) { + mMask &= mask; + return *this; + } + + inline EnumMask &operator&= (unsigned int mask) { + mMask &= mask; + return *this; + } + + inline EnumMask &operator&= (T mask) { + mMask &= StorageType(mask); + return *this; + } + + inline EnumMask &operator|= (EnumMask mask) { + mMask |= mask.mMask; + return *this; + } + + inline EnumMask &operator|= (T mask) { + mMask |= StorageType(mask); + return *this; + } + + inline EnumMask &operator^= (EnumMask mask) { + mMask ^= mask.mMask; + return *this; + } + + inline EnumMask &operator^= (T mask) { + mMask ^= StorageType(mask); + return *this; + } + + constexpr EnumMask operator& (int mask) const { + return MaskBuilder(mMask & mask); + } + + constexpr EnumMask operator& (unsigned int mask) const { + return MaskBuilder(mMask & mask); + } + + constexpr EnumMask operator& (T mask) const { + return MaskBuilder(mMask & StorageType(mask)); + } + + constexpr EnumMask operator| (EnumMask mask) const { + return MaskBuilder(mMask | mask.mMask); + } + + constexpr EnumMask operator| (T mask) const { + return MaskBuilder(mMask | StorageType(mask)); + } + + constexpr EnumMask operator^ (EnumMask mask) const { + return MaskBuilder(mMask ^ mask.mMask); + } + + constexpr EnumMask operator^ (T mask) const { + return MaskBuilder(mMask ^ StorageType(mask)); + } + + constexpr EnumMask operator~ () const { + return MaskBuilder(~mMask); + } + +private: + class MaskBuilder { + public: + constexpr MaskBuilder (int mask) : mMask(mask) {} + + constexpr operator int () const { + return mMask; + } + + constexpr operator unsigned int () const { + return static_cast(mMask); + } + + private: + int mMask; + }; + + constexpr EnumMask (MaskBuilder builder) : mMask(builder) {} + + constexpr bool isSet (StorageType value) const { + return (mMask & value) == value && (value || mMask == 0); + } + + static constexpr StorageType init ( + typename std::initializer_list::const_iterator begin, + typename std::initializer_list::const_iterator end + ) { + return begin != end ? (StorageType(*begin) | init(begin + 1, end)) : StorageType(0); + } + + StorageType mMask; +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _ENUM_MASK_H_