QFlags是个模板类,目的是提供类型安全的enum,在头文件qflags.h
中定义,传统上C++使用int或uint存储enum值,却没有作类型检查。
QFlag是一个基础类,还用到了QTypeInfo类,定义如下:
// 在C++11中Q_DECL_CONSTEXPR为constexpr
// 在C++11中Q_DECL_NOTHROW为noexcept
// 重载了operator()
class QFlag
{
int i;
public:
Q_DECL_CONSTEXPR inline QFlag(int ai) Q_DECL_NOTHROW : i(ai) {}
Q_DECL_CONSTEXPR inline operator int() const Q_DECL_NOTHROW { return i; }
#if !defined(Q_CC_MSVC)
# if !defined(__LP64__) && !defined(Q_QDOC)
Q_DECL_CONSTEXPR inline QFlag(long ai) Q_DECL_NOTHROW : i(int(ai)) {}
Q_DECL_CONSTEXPR inline QFlag(ulong ai) Q_DECL_NOTHROW : i(int(long(ai))) {}
# endif
Q_DECL_CONSTEXPR inline QFlag(uint ai) Q_DECL_NOTHROW : i(int(ai)) {}
Q_DECL_CONSTEXPR inline QFlag(short ai) Q_DECL_NOTHROW : i(int(ai)) {}
Q_DECL_CONSTEXPR inline QFlag(ushort ai) Q_DECL_NOTHROW : i(int(uint(ai))) {}
Q_DECL_CONSTEXPR inline operator uint() const Q_DECL_NOTHROW { return uint(i); }
#endif
};
Q_DECLARE_TYPEINFO(QFlag, Q_PRIMITIVE_TYPE);
// TYPEINFO flags
// Qt6: merge movable and relocatable once QList no longer depends on it
enum {
Q_COMPLEX_TYPE = 0,
Q_PRIMITIVE_TYPE = 0x1,
Q_STATIC_TYPE = 0,
Q_MOVABLE_TYPE = 0x2,
Q_DUMMY_TYPE = 0x4,
Q_RELOCATABLE_TYPE = 0x8
};
// 模板特化类QTypeInfo
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS) \
template<> \
Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
// 详细记载了类型的相关信息
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \
class QTypeInfo \
{ \
public: \
enum { \
isSpecialized = true, \
isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0), \
isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \
isRelocatable = !isStatic || ((FLAGS) & Q_RELOCATABLE_TYPE), \
isLarge = (sizeof(TYPE)>sizeof(void*)), \
isPointer = false, \
isIntegral = std::is_integral< TYPE >::value, \
isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \
sizeOf = sizeof(TYPE) \
}; \
static inline const char *name() { return #TYPE; } \
}
QIncompatibleFlag与QFlag的区别是,前者只支持int类型,定义如下:
class QIncompatibleFlag
{
int i;
public:
Q_DECL_CONSTEXPR inline explicit QIncompatibleFlag(int i) Q_DECL_NOTHROW;
Q_DECL_CONSTEXPR inline operator int() const Q_DECL_NOTHROW { return i; }
};
Q_DECLARE_TYPEINFO(QIncompatibleFlag, Q_PRIMITIVE_TYPE);
Q_DECL_CONSTEXPR inline QIncompatibleFlag::QIncompatibleFlag(int ai) Q_DECL_NOTHROW : i(ai) {}
QFlags定义及说明如下:
// 默认是支持类型安全的QFlags的
#ifndef Q_NO_TYPESAFE_FLAGS
template<typename Enum>
class QFlags
{
// 在C++11中Q_STATIC_ASSERT_X为static_assert,用于静态断言即编译时断言。
Q_STATIC_ASSERT_X((sizeof(Enum) <= sizeof(int)),
"QFlags uses an int as storage, so an enum with underlying "
"long long will overflow.");
Q_STATIC_ASSERT_X((std::is_enum::value), "QFlags is only usable on enumeration types.");
// Private结构的成员指针,指向int。
struct Private;
typedef int (Private::*Zero);
// 用于QDataStream输入输出的两个友元函数
template <typename E> friend QDataStream &operator>>(QDataStream &, QFlags &);
template <typename E> friend QDataStream &operator<<(QDataStream &, QFlags);
public:
#if defined(Q_CC_MSVC) || defined(Q_QDOC)
typedef int Int; // 定义Int类型
#else
typedef typename std::conditional<
std::is_unsigned<typename std::underlying_type::type>::value,
unsigned int,
signed int
>::type Int; // 定义Int类型
#endif
typedef Enum enum_type;
// compiler-generated copy/move ctor/assignment operators are fine!
// 几个构造函数
// 构造函数的参数可以为Enum、Zero、QFlag以及std::initializer_list
// 在C++11中Q_NULLPTR为nullptr
#ifdef Q_QDOC
Q_DECL_CONSTEXPR inline QFlags(const QFlags &other);
Q_DECL_CONSTEXPR inline QFlags &operator=(const QFlags &other);
#endif
Q_DECL_CONSTEXPR inline QFlags(Enum f) Q_DECL_NOTHROW : i(Int(f)) {}
Q_DECL_CONSTEXPR inline QFlags(Zero = Q_NULLPTR) Q_DECL_NOTHROW : i(0) {}
Q_DECL_CONSTEXPR inline QFlags(QFlag f) Q_DECL_NOTHROW : i(f) {}
#ifdef Q_COMPILER_INITIALIZER_LISTS
Q_DECL_CONSTEXPR inline QFlags(std::initializer_list flags) Q_DECL_NOTHROW
: i(initializer_list_helper(flags.begin(), flags.end())) {}
#endif
// 几个复合赋值操作符重载函数
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator&=(int mask) Q_DECL_NOTHROW { i &= mask; return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator&=(uint mask) Q_DECL_NOTHROW { i &= mask; return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator&=(Enum mask) Q_DECL_NOTHROW { i &= Int(mask); return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator|=(QFlags f) Q_DECL_NOTHROW { i |= f.i; return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator|=(Enum f) Q_DECL_NOTHROW { i |= Int(f); return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator^=(QFlags f) Q_DECL_NOTHROW { i ^= f.i; return *this; }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &operator^=(Enum f) Q_DECL_NOTHROW { i ^= Int(f); return *this; }
// operator()重载函数
Q_DECL_CONSTEXPR inline operator Int() const Q_DECL_NOTHROW { return i; }
// 几个位操作符重载函数
Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const Q_DECL_NOTHROW { return QFlags(QFlag(i | f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const Q_DECL_NOTHROW { return QFlags(QFlag(i | Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const Q_DECL_NOTHROW { return QFlags(QFlag(i ^ f.i)); }
Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const Q_DECL_NOTHROW { return QFlags(QFlag(i ^ Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const Q_DECL_NOTHROW { return QFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const Q_DECL_NOTHROW { return QFlags(QFlag(i & mask)); }
Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const Q_DECL_NOTHROW { return QFlags(QFlag(i & Int(f))); }
Q_DECL_CONSTEXPR inline QFlags operator~() const Q_DECL_NOTHROW { return QFlags(QFlag(~i)); }
// 逻辑操作符!重载函数
Q_DECL_CONSTEXPR inline bool operator!() const Q_DECL_NOTHROW { return !i; }
// 设置与获取flag
Q_DECL_CONSTEXPR inline bool testFlag(Enum f) const Q_DECL_NOTHROW { return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f) ); }
Q_DECL_RELAXED_CONSTEXPR inline QFlags &setFlag(Enum f, bool on = true) Q_DECL_NOTHROW
{
return on ? (*this |= f) : (*this &= ~f);
}
private:
#ifdef Q_COMPILER_INITIALIZER_LISTS
// C++11中的初始化列表
// 递归
Q_DECL_CONSTEXPR static inline Int initializer_list_helper(typename std::initializer_list::const_iterator it,
typename std::initializer_list::const_iterator end)
Q_DECL_NOTHROW
{
return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
}
#endif
// 存储Enum的地方
Int i;
};
// 使用了QFlags需要使用Q_DECLARE_FLAGS进行声明
#ifndef Q_MOC_RUN
#define Q_DECLARE_FLAGS(Flags, Enum)\
typedef QFlags Flags;
#endif
// Flags支持的其它按位或操作符重载函数
#define Q_DECLARE_INCOMPATIBLE_FLAGS(Flags) \
Q_DECL_CONSTEXPR inline QIncompatibleFlag operator|(Flags::enum_type f1, int f2) Q_DECL_NOTHROW \
{ return QIncompatibleFlag(int(f1) | f2); }
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
Q_DECL_CONSTEXPR inline QFlags operator|(Flags::enum_type f1, Flags::enum_type f2) Q_DECL_NOTHROW \
{ return QFlags(f1) | f2; } \
Q_DECL_CONSTEXPR inline QFlags operator|(Flags::enum_type f1, QFlags f2) Q_DECL_NOTHROW \
{ return f2 | f1; } Q_DECLARE_INCOMPATIBLE_FLAGS(Flags)
#else /* Q_NO_TYPESAFE_FLAGS */
// 一般情况下即没有类型安全的QFlags对enum的支持时使用unit存储enum值
#ifndef Q_MOC_RUN
#define Q_DECLARE_FLAGS(Flags, Enum)\
typedef uint Flags;
#endif
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
#endif /* Q_NO_TYPESAFE_FLAGS */