MIRA
Namespaces | Macros
EnumToFlags.h File Reference

Macros for generating logical operators for using enum values as flags. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

 mira
 specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
 

Macros

#define MIRA_ENUM_TO_FLAGS(EnumType)
 Macro that can be used with enums that contain flags. More...
 
#define MIRA_ENUM_TO_FLAGS_INCLASS(EnumType)
 Macro that can be used with enums that contain flags. More...
 

Detailed Description

Macros for generating logical operators for using enum values as flags.

Author
Erik Einhorn
Date
2010/06/28

Macro Definition Documentation

◆ MIRA_ENUM_TO_FLAGS

#define MIRA_ENUM_TO_FLAGS (   EnumType)
Value:
inline EnumType \
operator&(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) & static_cast<int>(b)); } \
\
inline EnumType \
operator|(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) | static_cast<int>(b)); } \
\
inline EnumType \
operator^(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) ^ static_cast<int>(b)); } \
\
inline EnumType& \
operator|=(EnumType& a, EnumType b) \
{ return a = a | b; } \
\
inline EnumType& \
operator&=(EnumType& a, EnumType b) \
{ return a = a & b; } \
\
inline EnumType& \
operator^=(EnumType& a, EnumType b) \
{ return a = a ^ b; } \
\
inline EnumType \
operator~(EnumType a) \
{ return EnumType(~static_cast<int>(a)); }
LogCustomizableFilter::AndOperator< Derived1, Derived2 > operator &(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by and.
Definition: LogCustomizableFilter.h:296
LogCustomizableFilter::OrOperator< Derived1, Derived2 > operator|(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by or.
Definition: LogCustomizableFilter.h:309

Macro that can be used with enums that contain flags.

The macro generates code for the logical operators &,|,^,~ to allow the enum values to be used as normal flags which would lead to compiler errors if these operators were not defined.

Example:

enum MyFlags
{
flag1 = 0x0001,
flag2 = 0x0002,
flag3 = 0x0004,
flag4 = 0x0008
}
MIRA_ENUM_TO_FLAGS(MyFlags) // generate logical operators for enum values
...
MyFlags combined = flag1 | flag2 & flag4;
if(combined & flag3) {
...
}

◆ MIRA_ENUM_TO_FLAGS_INCLASS

#define MIRA_ENUM_TO_FLAGS_INCLASS (   EnumType)
Value:
friend inline EnumType \
operator&(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) & static_cast<int>(b)); } \
\
friend inline EnumType \
operator|(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) | static_cast<int>(b)); } \
\
friend inline EnumType \
operator^(EnumType a, EnumType b) \
{ return EnumType(static_cast<int>(a) ^ static_cast<int>(b)); } \
\
friend inline EnumType& \
operator|=(EnumType& a, EnumType b) \
{ return a = a | b; } \
\
friend inline EnumType& \
operator&=(EnumType& a, EnumType b) \
{ return a = a & b; } \
\
friend inline EnumType& \
operator^=(EnumType& a, EnumType b) \
{ return a = a ^ b; } \
\
friend inline EnumType \
operator~(EnumType a) \
{ return EnumType(~static_cast<int>(a)); } \
LogCustomizableFilter::AndOperator< Derived1, Derived2 > operator &(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by and.
Definition: LogCustomizableFilter.h:296
LogCustomizableFilter::OrOperator< Derived1, Derived2 > operator|(const LogCustomizableFilter::CustomFilter< Derived1 > &f1, const LogCustomizableFilter::CustomFilter< Derived2 > &f2)
Operator to combine filters by or.
Definition: LogCustomizableFilter.h:309

Macro that can be used with enums that contain flags.

The macro generates code for the logical operators &,|,^,~ to allow the enum values to be used as normal flags which would lead to compiler errors if these operators were not defined. This macro works just as the MIRA_ENUM_TO_FLAGS macro but can be used for enums that are defined in a class.

Example:

class MyClass
{
public:
enum MyFlags
{
flag1 = 0x0001,
flag2 = 0x0002,
flag3 = 0x0004,
flag4 = 0x0008
}
MIRA_ENUM_TO_FLAGS_INCLASS(MyFlags) // generate logical operators for enum values
};
...
MyClass::MyFlags combined = flag1 | flag2 & flag 4;
if(combined & flag3) {
...
}