CopperSpice API  1.7.2
QFlags< E > Class Template Reference

The QFlags class provides a type-safe way of storing OR-combinations of enum values. More...

Public Typedefs

using enum_type = E
 

Public Methods

constexpr QFlags ()
 
constexpr QFlags (const QFlags &other)
 
constexpr QFlags (E value)
 
constexpr QFlags (EmptyFlag_Type)
 
 QFlags (QFlag flag)
 
constexpr QFlags (std::initializer_list< E > list)
 
constexpr QFlags (std::nullptr_t)
 
constexpr operator int_type () const
 
constexpr bool operator! () const
 
constexpr QFlags operator& (E value) const
 
constexpr QFlags operator& (sint_type mask) const
 
constexpr QFlags operator& (uint_type mask) const
 
QFlags & operator&= (sint_type mask)
 
QFlags & operator&= (uint_type mask)
 
QFlags & operator= (const QFlags &other)
 
constexpr QFlags operator^ (E value) const
 
constexpr QFlags operator^ (QFlags other) const
 
QFlags & operator^= (E value)
 
QFlags & operator^= (QFlags other)
 
constexpr QFlags operator| (E value) const
 
constexpr QFlags operator| (QFlags other) const
 
QFlags & operator|= (E value)
 
QFlags & operator|= (QFlags other)
 
constexpr QFlags operator~ () const
 
bool testFlag (E value) const
 

Detailed Description

template<typename E>
class QFlags< E >

The QFlags class provides a type safe way of storing OR-combinations of enum values. The QFlags<E> class is a template class, where E is an enum type. QFlags is used throughout CopperSpice for storing combinations of enum values.

The traditional C++ approach for storing OR-combinations of enum values is to use an int or uint variable. The inconvenience with this approach is that there's no type checking at all; any enum value can be OR'd with any other enum value and passed on to a function that takes an int or uint.

CopperSpice uses QFlags to provide type safety. For example, the Qt::Alignment type is simply a typedef for QFlags<Qt::AlignmentFlag>. QLabel::setAlignment() takes a Qt::Alignment parameter, which means any combination of Qt::AlignmentFlag values,or 0, is legal.

label->setAlignment(Qt::AlignLeft | Qt::AlignTop);

If you try to pass a value from another enum or just a plain integer other than 0, the compiler will report an error. If you need to cast integer values to flags in a untyped fashion, you can use the explicit QFlags constructor as cast operator.

If you want to use QFlags for your own enum types, use the Q_DECLARE_FLAGS() and Q_DECLARE_OPERATORS_FOR_FLAGS().

class MyClass {
public:
enum Option {
NoOptions = 0x0,
ShowTabs = 0x1,
ShowAll = 0x2,
SqueezeBlank = 0x4
};
Q_DECLARE_FLAGS(Options, Option)
...
};
Q_DECLARE_OPERATORS_FOR_FLAGS(MyClass::Options)

You can then use the MyClass::Options type to store combinations of MyClass::Option values.

Flags and the Meta Object System

The Q_DECLARE_FLAGS() macro does not expose the flags to the meta object system, so they can not be used by CsScript or edited in "Qt Designer". To make the flags available for these purposes, the CS_FLAG() macro must be used:

CS_FLAG(Options)

Naming Convention

A sensible naming convention for enum types and associated QFlags types is to give a singular name to the enum type (e.g., Option) and a plural name to the QFlags type (e.g., Options). When a singular name is desired for the QFlags type (e.g., Alignment), you can use Flag as the suffix for the enum type (e.g., AlignmentFlag).

See also
QFlag

Member Typedef Documentation

template<typename E >
QFlags< E >::enum_type

Typedef for the E template type.

Constructor & Destructor Documentation

template<typename E >
constexpr QFlags< E >::QFlags ( )
inlineconstexpr

Constructs a QFlags object with no flags set.

template<typename E >
constexpr QFlags< E >::QFlags ( EmptyFlag_Type  )
inlineconstexpr

Constructs a QFlags object with no flags set.

QFlags<WindowType> type = Qt::EmptyFlag;
template<typename E >
constexpr QFlags< E >::QFlags ( std::nullptr_t  )
inlineconstexpr

Constructs a QFlags object with no flags set.

Note
This constructor should not be used and may be deprecated in a future release. Use the QFlags(EmptyFlag_Type) constructor instead.
template<typename E >
constexpr QFlags< E >::QFlags ( std::initializer_list< E >  list)
inlineconstexpr

Constructs a QFlags object using the values in list.

template<typename E >
constexpr QFlags< E >::QFlags ( value)
inlineconstexpr

Constructs a QFlags object storing the given value.

template<typename E >
QFlags< E >::QFlags ( QFlag  flag)
inline

Constructs a QFlags object initialized with the given integer flag.

template<typename E >
constexpr QFlags< E >::QFlags ( const QFlags< E > &  other)
inlineconstexpr

Constructs a copy of other.

Method Documentation

template<typename E >
constexpr QFlags< E >::operator int_type ( ) const
inlineconstexpr

Returns the value stored in the QFlags object as an integer.

template<typename E >
constexpr bool QFlags< E >::operator! ( ) const
inlineconstexpr

Returns true if no flag is set (i.e., if the value stored by the QFlags object is 0), otherwise returns false.

template<typename E >
constexpr QFlags QFlags< E >::operator& ( value) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise AND operation on this object and value.

template<typename E >
constexpr QFlags QFlags< E >::operator& ( sint_type  mask) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise AND operation on this object and mask.

See also
operator&=(), operator|(), operator^(), operator~()
template<typename E >
constexpr QFlags QFlags< E >::operator& ( uint_type  mask) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise AND operation on this object and mask.

template<typename E >
QFlags & QFlags< E >::operator&= ( sint_type  mask)
inline

Performs a bitwise AND operation with mask and stores the result in this QFlags object. Returns a reference to this object.

See also
operator&(), operator|=(), operator^=()
template<typename E >
QFlags & QFlags< E >::operator&= ( uint_type  mask)
inline

Performs a bitwise AND operation with mask and stores the result in this QFlags object. Returns a reference to this object.

template<typename E >
QFlags & QFlags< E >::operator= ( const QFlags< E > &  other)
inline

Copy assigns from other and returns a reference to this object.

template<typename E >
constexpr QFlags QFlags< E >::operator^ ( value) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise XOR operation on this object and value.

template<typename E >
constexpr QFlags QFlags< E >::operator^ ( QFlags< E >  other) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise XOR operation on this object and other.

See also
operator^=(), operator&(), operator|(), operator~()
template<typename E >
QFlags & QFlags< E >::operator^= ( value)
inline

Performs a bitwise XOR operation with value and stores the result in this QFlags object. Returns a reference to this object.

template<typename E >
QFlags & QFlags< E >::operator^= ( QFlags< E >  other)
inline

Performs a bitwise XOR operation with other and stores the result in this QFlags object. Returns a reference to this object.

See also
operator^(), operator&=(), operator|=()
template<typename E >
constexpr QFlags QFlags< E >::operator| ( value) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise OR operation on this object and value.

template<typename E >
constexpr QFlags QFlags< E >::operator| ( QFlags< E >  other) const
inlineconstexpr

Returns a QFlags object containing the result of the bitwise OR operation on this object and other.

See also
operator|=(), operator^(), operator&(), operator~()
template<typename E >
QFlags & QFlags< E >::operator|= ( value)
inline

Performs a bitwise OR operation with value and stores the result in this QFlags object. Returns a reference to this object.

template<typename E >
QFlags & QFlags< E >::operator|= ( QFlags< E >  other)
inline

Performs a bitwise OR operation with other and stores the result in this QFlags object. Returns a reference to this object.

See also
operator|(), operator&=(), operator^=()
template<typename E >
constexpr QFlags QFlags< E >::operator~ ( ) const
inlineconstexpr

Returns a QFlags object that contains the bitwise negation of this object.

See also
operator&(), operator|(), operator^()
template<typename E >
bool QFlags< E >::testFlag ( value) const
inline

Returns true if the value is set, otherwise false.