Skip to content

Commit ef5579d

Browse files
cv3dalalek
authored andcommitted
Merge pull request opencv#12310 from cv3d:chunks/enum_interface
* Cleanup macros and enable expansion of `__VA_ARGS__` for Visual Studio * Macros for enum-arguments backwards compatibility * Convert struct Param to enum struct * Enabled ParamType.type for enum types * Enabled `cv.read` and `cv.write` for enum types * Rename unnamed enum to AAKAZE.DescriptorType * Rename unnamed enum to AccessFlag * Rename unnamed enum to AgastFeatureDetector.DetectorType * Convert struct DrawMatchesFlags to enum struct * Rename unnamed enum to FastFeatureDetector.DetectorType * Rename unnamed enum to Formatter.FormatType * Rename unnamed enum to HOGDescriptor.HistogramNormType * Rename unnamed enum to DescriptorMatcher.MatcherType * Rename unnamed enum to KAZE.DiffusivityType * Rename unnamed enum to ORB.ScoreType * Rename unnamed enum to UMatData.MemoryFlag * Rename unnamed enum to _InputArray.KindFlag * Rename unnamed enum to _OutputArray.DepthMask * Convert normType enums to static const NormTypes * Avoid conflicts with ElemType * Rename unnamed enum to DescriptorStorageFormat
1 parent 84ae809 commit ef5579d

51 files changed

Lines changed: 567 additions & 333 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/core/include/opencv2/core.hpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2997,7 +2997,8 @@ class CV_EXPORTS Formatted
29972997
class CV_EXPORTS Formatter
29982998
{
29992999
public:
3000-
enum { FMT_DEFAULT = 0,
3000+
enum FormatType {
3001+
FMT_DEFAULT = 0,
30013002
FMT_MATLAB = 1,
30023003
FMT_CSV = 2,
30033004
FMT_PYTHON = 3,
@@ -3014,7 +3015,7 @@ class CV_EXPORTS Formatter
30143015
virtual void set64fPrecision(int p = 16) = 0;
30153016
virtual void setMultiline(bool ml = true) = 0;
30163017

3017-
static Ptr<Formatter> get(int fmt = FMT_DEFAULT);
3018+
static Ptr<Formatter> get(Formatter::FormatType fmt = FMT_DEFAULT);
30183019

30193020
};
30203021

@@ -3037,7 +3038,7 @@ String& operator << (String& out, const Mat& mtx)
30373038

30383039
class CV_EXPORTS Algorithm;
30393040

3040-
template<typename _Tp> struct ParamType {};
3041+
template<typename _Tp, typename _EnumTp = void> struct ParamType {};
30413042

30423043

30433044
/** @brief This is a base class for all more or less complex algorithms in OpenCV
@@ -3150,9 +3151,9 @@ class CV_EXPORTS_W Algorithm
31503151
void writeFormat(FileStorage& fs) const;
31513152
};
31523153

3153-
struct Param {
3154-
enum { INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6, FLOAT=7,
3155-
UNSIGNED_INT=8, UINT64=9, UCHAR=11, SCALAR=12 };
3154+
enum struct Param {
3155+
INT=0, BOOLEAN=1, REAL=2, STRING=3, MAT=4, MAT_VECTOR=5, ALGORITHM=6, FLOAT=7,
3156+
UNSIGNED_INT=8, UINT64=9, UCHAR=11, SCALAR=12
31563157
};
31573158

31583159

@@ -3162,95 +3163,104 @@ template<> struct ParamType<bool>
31623163
typedef bool const_param_type;
31633164
typedef bool member_type;
31643165

3165-
enum { type = Param::BOOLEAN };
3166+
static const Param type = Param::BOOLEAN;
31663167
};
31673168

31683169
template<> struct ParamType<int>
31693170
{
31703171
typedef int const_param_type;
31713172
typedef int member_type;
31723173

3173-
enum { type = Param::INT };
3174+
static const Param type = Param::INT;
31743175
};
31753176

31763177
template<> struct ParamType<double>
31773178
{
31783179
typedef double const_param_type;
31793180
typedef double member_type;
31803181

3181-
enum { type = Param::REAL };
3182+
static const Param type = Param::REAL;
31823183
};
31833184

31843185
template<> struct ParamType<String>
31853186
{
31863187
typedef const String& const_param_type;
31873188
typedef String member_type;
31883189

3189-
enum { type = Param::STRING };
3190+
static const Param type = Param::STRING;
31903191
};
31913192

31923193
template<> struct ParamType<Mat>
31933194
{
31943195
typedef const Mat& const_param_type;
31953196
typedef Mat member_type;
31963197

3197-
enum { type = Param::MAT };
3198+
static const Param type = Param::MAT;
31983199
};
31993200

32003201
template<> struct ParamType<std::vector<Mat> >
32013202
{
32023203
typedef const std::vector<Mat>& const_param_type;
32033204
typedef std::vector<Mat> member_type;
32043205

3205-
enum { type = Param::MAT_VECTOR };
3206+
static const Param type = Param::MAT_VECTOR;
32063207
};
32073208

32083209
template<> struct ParamType<Algorithm>
32093210
{
32103211
typedef const Ptr<Algorithm>& const_param_type;
32113212
typedef Ptr<Algorithm> member_type;
32123213

3213-
enum { type = Param::ALGORITHM };
3214+
static const Param type = Param::ALGORITHM;
32143215
};
32153216

32163217
template<> struct ParamType<float>
32173218
{
32183219
typedef float const_param_type;
32193220
typedef float member_type;
32203221

3221-
enum { type = Param::FLOAT };
3222+
static const Param type = Param::FLOAT;
32223223
};
32233224

32243225
template<> struct ParamType<unsigned>
32253226
{
32263227
typedef unsigned const_param_type;
32273228
typedef unsigned member_type;
32283229

3229-
enum { type = Param::UNSIGNED_INT };
3230+
static const Param type = Param::UNSIGNED_INT;
32303231
};
32313232

32323233
template<> struct ParamType<uint64>
32333234
{
32343235
typedef uint64 const_param_type;
32353236
typedef uint64 member_type;
32363237

3237-
enum { type = Param::UINT64 };
3238+
static const Param type = Param::UINT64;
32383239
};
32393240

32403241
template<> struct ParamType<uchar>
32413242
{
32423243
typedef uchar const_param_type;
32433244
typedef uchar member_type;
32443245

3245-
enum { type = Param::UCHAR };
3246+
static const Param type = Param::UCHAR;
32463247
};
32473248

32483249
template<> struct ParamType<Scalar>
32493250
{
32503251
typedef const Scalar& const_param_type;
32513252
typedef Scalar member_type;
32523253

3253-
enum { type = Param::SCALAR };
3254+
static const Param type = Param::SCALAR;
3255+
};
3256+
3257+
template<typename _Tp>
3258+
struct ParamType<_Tp, typename std::enable_if< std::is_enum<_Tp>::value >::type>
3259+
{
3260+
typedef typename std::underlying_type<_Tp>::type const_param_type;
3261+
typedef typename std::underlying_type<_Tp>::type member_type;
3262+
3263+
static const Param type = Param::INT;
32543264
};
32553265

32563266
//! @} core_basic

modules/core/include/opencv2/core/base.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,17 @@ configurations while CV_DbgAssert is only retained in the Debug configuration.
440440
#endif
441441

442442
#define CV_Assert_1 CV_Assert
443-
#define CV_Assert_2( expr1, expr2 ) CV_Assert_1(expr1); CV_Assert_1(expr2)
444-
#define CV_Assert_3( expr1, expr2, expr3 ) CV_Assert_2(expr1, expr2); CV_Assert_1(expr3)
445-
#define CV_Assert_4( expr1, expr2, expr3, expr4 ) CV_Assert_3(expr1, expr2, expr3); CV_Assert_1(expr4)
446-
#define CV_Assert_5( expr1, expr2, expr3, expr4, expr5 ) CV_Assert_4(expr1, expr2, expr3, expr4); CV_Assert_1(expr5)
447-
#define CV_Assert_6( expr1, expr2, expr3, expr4, expr5, expr6 ) CV_Assert_5(expr1, expr2, expr3, expr4, expr5); CV_Assert_1(expr6)
448-
#define CV_Assert_7( expr1, expr2, expr3, expr4, expr5, expr6, expr7 ) CV_Assert_6(expr1, expr2, expr3, expr4, expr5, expr6 ); CV_Assert_1(expr7)
449-
#define CV_Assert_8( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8 ) CV_Assert_7(expr1, expr2, expr3, expr4, expr5, expr6, expr7 ); CV_Assert_1(expr8)
450-
#define CV_Assert_9( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9 ) CV_Assert_8(expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8 ); CV_Assert_1(expr9)
451-
#define CV_Assert_10( expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9, expr10 ) CV_Assert_9(expr1, expr2, expr3, expr4, expr5, expr6, expr7, expr8, expr9 ); CV_Assert_1(expr10)
452-
453-
#define CV_Assert_N(...) do { __CV_CAT(CV_Assert_, __CV_VA_NUM_ARGS(__VA_ARGS__)) (__VA_ARGS__); } while(0)
443+
#define CV_Assert_2( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_1( __VA_ARGS__ ))
444+
#define CV_Assert_3( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_2( __VA_ARGS__ ))
445+
#define CV_Assert_4( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_3( __VA_ARGS__ ))
446+
#define CV_Assert_5( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_4( __VA_ARGS__ ))
447+
#define CV_Assert_6( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_5( __VA_ARGS__ ))
448+
#define CV_Assert_7( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_6( __VA_ARGS__ ))
449+
#define CV_Assert_8( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_7( __VA_ARGS__ ))
450+
#define CV_Assert_9( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_8( __VA_ARGS__ ))
451+
#define CV_Assert_10( expr, ... ) CV_Assert_1(expr); __CV_EXPAND(CV_Assert_9( __VA_ARGS__ ))
452+
453+
#define CV_Assert_N(...) do { __CV_EXPAND(__CV_CAT(CV_Assert_, __CV_VA_NUM_ARGS(__VA_ARGS__)) (__VA_ARGS__)); } while(0)
454454

455455
//! @endcond
456456

@@ -467,7 +467,7 @@ configurations while CV_DbgAssert is only retained in the Debug configuration.
467467
*/
468468
struct CV_EXPORTS Hamming
469469
{
470-
enum { normType = NORM_HAMMING };
470+
static const NormTypes normType = NORM_HAMMING;
471471
typedef unsigned char ValueType;
472472
typedef int ResultType;
473473

modules/core/include/opencv2/core/cvdef.h

Lines changed: 137 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ namespace cv { namespace debug_build_guard { } using namespace debug_build_guard
8080
#endif
8181

8282
#define __CV_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
83-
#define __CV_VA_NUM_ARGS(...) __CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
83+
#define __CV_VA_NUM_ARGS(...) __CV_EXPAND(__CV_VA_NUM_ARGS_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
8484

8585
// undef problematic defines sometimes defined by system headers (windows.h in particular)
8686
#undef small
@@ -330,6 +330,142 @@ Cv64suf;
330330
# define MAX(a,b) ((a) < (b) ? (b) : (a))
331331
#endif
332332

333+
///////////////////////////////////////// Enum operators ///////////////////////////////////////
334+
335+
/**
336+
337+
Provides compatibility operators for both classical and C++11 enum classes,
338+
as well as exposing the C++11 enum class members for backwards compatibility
339+
340+
@code
341+
// Provides operators required for flag enums
342+
CV_ENUM_FLAGS(AccessFlag);
343+
344+
// Exposes the listed members of the enum class AccessFlag to the current namespace
345+
CV_ENUM_CLASS_EXPOSE(AccessFlag, ACCESS_READ [, ACCESS_WRITE [, ...] ]);
346+
@endcode
347+
*/
348+
349+
#define __CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST) \
350+
static const EnumType MEMBER_CONST = EnumType::MEMBER_CONST; \
351+
352+
#define __CV_ENUM_CLASS_EXPOSE_2(EnumType, MEMBER_CONST, ...) \
353+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
354+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_1(EnumType, __VA_ARGS__)); \
355+
356+
#define __CV_ENUM_CLASS_EXPOSE_3(EnumType, MEMBER_CONST, ...) \
357+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
358+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_2(EnumType, __VA_ARGS__)); \
359+
360+
#define __CV_ENUM_CLASS_EXPOSE_4(EnumType, MEMBER_CONST, ...) \
361+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
362+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_3(EnumType, __VA_ARGS__)); \
363+
364+
#define __CV_ENUM_CLASS_EXPOSE_5(EnumType, MEMBER_CONST, ...) \
365+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
366+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_4(EnumType, __VA_ARGS__)); \
367+
368+
#define __CV_ENUM_CLASS_EXPOSE_6(EnumType, MEMBER_CONST, ...) \
369+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
370+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_5(EnumType, __VA_ARGS__)); \
371+
372+
#define __CV_ENUM_CLASS_EXPOSE_7(EnumType, MEMBER_CONST, ...) \
373+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
374+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_6(EnumType, __VA_ARGS__)); \
375+
376+
#define __CV_ENUM_CLASS_EXPOSE_8(EnumType, MEMBER_CONST, ...) \
377+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
378+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_7(EnumType, __VA_ARGS__)); \
379+
380+
#define __CV_ENUM_CLASS_EXPOSE_9(EnumType, MEMBER_CONST, ...) \
381+
__CV_ENUM_CLASS_EXPOSE_1(EnumType, MEMBER_CONST); \
382+
__CV_EXPAND(__CV_ENUM_CLASS_EXPOSE_8(EnumType, __VA_ARGS__)); \
383+
384+
#define __CV_ENUM_FLAGS_LOGICAL_NOT(EnumType) \
385+
static inline bool operator!(const EnumType& val) \
386+
{ \
387+
typedef std::underlying_type<EnumType>::type UnderlyingType; \
388+
return !static_cast<UnderlyingType>(val); \
389+
} \
390+
391+
#define __CV_ENUM_FLAGS_LOGICAL_NOT_EQ(Arg1Type, Arg2Type) \
392+
static inline bool operator!=(const Arg1Type& a, const Arg2Type& b) \
393+
{ \
394+
return static_cast<int>(a) != static_cast<int>(b); \
395+
} \
396+
397+
#define __CV_ENUM_FLAGS_LOGICAL_EQ(Arg1Type, Arg2Type) \
398+
static inline bool operator==(const Arg1Type& a, const Arg2Type& b) \
399+
{ \
400+
return static_cast<int>(a) == static_cast<int>(b); \
401+
} \
402+
403+
#define __CV_ENUM_FLAGS_BITWISE_NOT(EnumType) \
404+
static inline EnumType operator~(const EnumType& val) \
405+
{ \
406+
typedef std::underlying_type<EnumType>::type UnderlyingType; \
407+
return static_cast<EnumType>(~static_cast<UnderlyingType>(val)); \
408+
} \
409+
410+
#define __CV_ENUM_FLAGS_BITWISE_OR(EnumType, Arg1Type, Arg2Type) \
411+
static inline EnumType operator|(const Arg1Type& a, const Arg2Type& b) \
412+
{ \
413+
typedef std::underlying_type<EnumType>::type UnderlyingType; \
414+
return static_cast<EnumType>(static_cast<UnderlyingType>(a) | static_cast<UnderlyingType>(b)); \
415+
} \
416+
417+
#define __CV_ENUM_FLAGS_BITWISE_AND(EnumType, Arg1Type, Arg2Type) \
418+
static inline EnumType operator&(const Arg1Type& a, const Arg2Type& b) \
419+
{ \
420+
typedef std::underlying_type<EnumType>::type UnderlyingType; \
421+
return static_cast<EnumType>(static_cast<UnderlyingType>(a) & static_cast<UnderlyingType>(b)); \
422+
} \
423+
424+
#define __CV_ENUM_FLAGS_BITWISE_XOR(EnumType, Arg1Type, Arg2Type) \
425+
static inline EnumType operator^(const Arg1Type& a, const Arg2Type& b) \
426+
{ \
427+
typedef std::underlying_type<EnumType>::type UnderlyingType; \
428+
return static_cast<EnumType>(static_cast<UnderlyingType>(a) ^ static_cast<UnderlyingType>(b)); \
429+
} \
430+
431+
#define __CV_ENUM_FLAGS_BITWISE_OR_EQ(EnumType, Arg1Type) \
432+
static inline EnumType& operator|=(EnumType& _this, const Arg1Type& val) \
433+
{ \
434+
_this = static_cast<EnumType>(static_cast<int>(_this) | static_cast<int>(val)); \
435+
return _this; \
436+
} \
437+
438+
#define __CV_ENUM_FLAGS_BITWISE_AND_EQ(EnumType, Arg1Type) \
439+
static inline EnumType& operator&=(EnumType& _this, const Arg1Type& val) \
440+
{ \
441+
_this = static_cast<EnumType>(static_cast<int>(_this) & static_cast<int>(val)); \
442+
return _this; \
443+
} \
444+
445+
#define __CV_ENUM_FLAGS_BITWISE_XOR_EQ(EnumType, Arg1Type) \
446+
static inline EnumType& operator^=(EnumType& _this, const Arg1Type& val) \
447+
{ \
448+
_this = static_cast<EnumType>(static_cast<int>(_this) ^ static_cast<int>(val)); \
449+
return _this; \
450+
} \
451+
452+
#define CV_ENUM_CLASS_EXPOSE(EnumType, ...) \
453+
__CV_EXPAND(__CV_CAT(__CV_ENUM_CLASS_EXPOSE_, __CV_VA_NUM_ARGS(__VA_ARGS__))(EnumType, __VA_ARGS__)); \
454+
455+
#define CV_ENUM_FLAGS(EnumType) \
456+
__CV_ENUM_FLAGS_LOGICAL_NOT (EnumType); \
457+
__CV_ENUM_FLAGS_LOGICAL_EQ (EnumType, int); \
458+
__CV_ENUM_FLAGS_LOGICAL_NOT_EQ (EnumType, int); \
459+
\
460+
__CV_ENUM_FLAGS_BITWISE_NOT (EnumType); \
461+
__CV_ENUM_FLAGS_BITWISE_OR (EnumType, EnumType, EnumType); \
462+
__CV_ENUM_FLAGS_BITWISE_AND (EnumType, EnumType, EnumType); \
463+
__CV_ENUM_FLAGS_BITWISE_XOR (EnumType, EnumType, EnumType); \
464+
\
465+
__CV_ENUM_FLAGS_BITWISE_OR_EQ (EnumType, EnumType); \
466+
__CV_ENUM_FLAGS_BITWISE_AND_EQ (EnumType, EnumType); \
467+
__CV_ENUM_FLAGS_BITWISE_XOR_EQ (EnumType, EnumType); \
468+
333469
/****************************************************************************************\
334470
* static analysys *
335471
\****************************************************************************************/

0 commit comments

Comments
 (0)