1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtCore/qglobal.h>
46 #include <QtCore/qtypeinfo.h>
56 Q_DECL_CONSTEXPR inline QFlag(int i);
57 Q_DECL_CONSTEXPR inline operator int() const { return i; }
59 Q_DECLARE_TYPEINFO(QFlag, Q_PRIMITIVE_TYPE);
61 Q_DECL_CONSTEXPR inline QFlag::QFlag(int ai) : i(ai) {}
63 class QIncompatibleFlag
67 Q_DECL_CONSTEXPR inline explicit QIncompatibleFlag(int i);
68 Q_DECL_CONSTEXPR inline operator int() const { return i; }
70 Q_DECLARE_TYPEINFO(QIncompatibleFlag, Q_PRIMITIVE_TYPE);
72 Q_DECL_CONSTEXPR inline QIncompatibleFlag::QIncompatibleFlag(int ai) : i(ai) {}
75 #ifndef Q_NO_TYPESAFE_FLAGS
77 template<typename Enum>
81 typedef int (Private::*Zero);
84 typedef Enum enum_type;
85 // compiler-generated copy/move ctor/assignment operators are fine!
87 inline QFlags(const QFlags &other);
88 inline QFlags &operator=(const QFlags &other);
90 Q_DECL_CONSTEXPR inline QFlags(Enum f) : i(f) {}
91 Q_DECL_CONSTEXPR inline QFlags(Zero = 0) : i(0) {}
92 Q_DECL_CONSTEXPR inline QFlags(QFlag f) : i(f) {}
94 inline QFlags &operator&=(int mask) { i &= mask; return *this; }
95 inline QFlags &operator&=(uint mask) { i &= mask; return *this; }
96 inline QFlags &operator|=(QFlags f) { i |= f.i; return *this; }
97 inline QFlags &operator|=(Enum f) { i |= f; return *this; }
98 inline QFlags &operator^=(QFlags f) { i ^= f.i; return *this; }
99 inline QFlags &operator^=(Enum f) { i ^= f; return *this; }
101 Q_DECL_CONSTEXPR inline operator int() const { return i; }
103 Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const { return QFlags(Enum(i | f.i)); }
104 Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const { return QFlags(Enum(i | f)); }
105 Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const { return QFlags(Enum(i ^ f.i)); }
106 Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const { return QFlags(Enum(i ^ f)); }
107 Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const { return QFlags(Enum(i & mask)); }
108 Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const { return QFlags(Enum(i & mask)); }
109 Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const { return QFlags(Enum(i & f)); }
110 Q_DECL_CONSTEXPR inline QFlags operator~() const { return QFlags(Enum(~i)); }
112 Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
114 Q_DECL_CONSTEXPR inline bool testFlag(Enum f) const { return (i & f) == f && (f != 0 || i == int(f) ); }
117 #define Q_DECLARE_FLAGS(Flags, Enum)\
118 typedef QFlags<Enum> Flags;
120 #define Q_DECLARE_INCOMPATIBLE_FLAGS(Flags) \
121 Q_DECL_CONSTEXPR inline QIncompatibleFlag operator|(Flags::enum_type f1, int f2) \
122 { return QIncompatibleFlag(int(f1) | f2); }
124 #define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
125 Q_DECL_CONSTEXPR inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) \
126 { return QFlags<Flags::enum_type>(f1) | f2; } \
127 Q_DECL_CONSTEXPR inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, QFlags<Flags::enum_type> f2) \
128 { return f2 | f1; } Q_DECLARE_INCOMPATIBLE_FLAGS(Flags)
131 #else /* Q_NO_TYPESAFE_FLAGS */
133 #define Q_DECLARE_FLAGS(Flags, Enum)\
135 #define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
137 #endif /* Q_NO_TYPESAFE_FLAGS */