From 3d278ee75fa88634b23a3dbee42db9bd52a1c617 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Apr 2021 17:50:37 +0200 Subject: [PATCH] Avoid warnings about operations on different enums in C++20 mode Arithmetic operations on the elements of different enums are deprecated in C++20 and the latest compiler versions warn about them. While individual warnings could be fixed in wxWidgets itself (although it would be quite an effort to do it for all ~500 of them), it wouldn't help with the same warnings in the applications using wx, so prefer to do it by explicitly defining the operations on the enums that can be combined with each other by using wxALLOW_COMBINING_ENUMS() macro, except for a single warning in wxTar code where it's easier to just not use an anum at all. --- include/wx/aui/auibook.h | 1 + include/wx/defs.h | 32 ++++++++++++++++++++++++++++ include/wx/richtext/richtextbuffer.h | 2 ++ include/wx/toolbar.h | 4 +++- src/common/tarstrm.cpp | 4 +--- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/include/wx/aui/auibook.h b/include/wx/aui/auibook.h index dc3f509510..23d66ade71 100644 --- a/include/wx/aui/auibook.h +++ b/include/wx/aui/auibook.h @@ -55,6 +55,7 @@ enum wxAuiNotebookOption wxAUI_NB_MIDDLE_CLICK_CLOSE }; +wxALLOW_COMBINING_ENUMS(wxAuiNotebookOption, wxBorder) diff --git a/include/wx/defs.h b/include/wx/defs.h index d98dac5354..07533d00f6 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -1255,6 +1255,28 @@ typedef double wxDouble; /* Geometric flags */ /* ---------------------------------------------------------------------------- */ +/* + In C++20 operations on the elements of different enums are deprecated and + many compilers (clang 10+, gcc 11+, MSVS 2019) warn about combining them, + as a lot of existing code using them does, so we provide explicit operators + for doing this, that do the same thing as would happen without them, but + without the warnings. + */ +#if defined(__cplusplus) && (__cplusplus >= 202002L) + #define wxALLOW_COMBINING_ENUMS_IMPL(en1, en2) \ + inline int operator|(en1 v1, en2 v2) \ + { return static_cast(v1) | static_cast(v2); } \ + inline int operator+(en1 v1, en2 v2) \ + { return static_cast(v1) + static_cast(v2); } + + #define wxALLOW_COMBINING_ENUMS(en1, en2) \ + wxALLOW_COMBINING_ENUMS_IMPL(en1, en2) \ + wxALLOW_COMBINING_ENUMS_IMPL(en2, en1) +#else /* !C++ 20 */ + /* Don't bother doing anything in this case. */ + #define wxALLOW_COMBINING_ENUMS(en1, en2) +#endif /* C++ 20 */ + enum wxGeometryCentre { wxCENTRE = 0x0001, @@ -1380,6 +1402,16 @@ enum wxBorder /* This makes it easier to specify a 'normal' border for a control */ #define wxDEFAULT_CONTROL_BORDER wxBORDER_SUNKEN +/* + Elements of these enums can be combined with each other when using + wxSizer::Add() overload not using wxSizerFlags. + */ +wxALLOW_COMBINING_ENUMS(wxAlignment, wxDirection) +wxALLOW_COMBINING_ENUMS(wxAlignment, wxGeometryCentre) +wxALLOW_COMBINING_ENUMS(wxAlignment, wxStretch) +wxALLOW_COMBINING_ENUMS(wxDirection, wxStretch) +wxALLOW_COMBINING_ENUMS(wxDirection, wxGeometryCentre) + /* ---------------------------------------------------------------------------- */ /* Window style flags */ /* ---------------------------------------------------------------------------- */ diff --git a/include/wx/richtext/richtextbuffer.h b/include/wx/richtext/richtextbuffer.h index 13e1dfb867..132667c50c 100644 --- a/include/wx/richtext/richtextbuffer.h +++ b/include/wx/richtext/richtextbuffer.h @@ -345,6 +345,8 @@ enum wxTextBoxAttrPosition wxTEXT_BOX_ATTR_POSITION_MASK = 0x00F0 }; +wxALLOW_COMBINING_ENUMS(wxTextAttrUnits, wxTextAttrValueFlags) + /** @class wxTextAttrDimension diff --git a/include/wx/toolbar.h b/include/wx/toolbar.h index 3aacb1f27d..f6a3a766f0 100644 --- a/include/wx/toolbar.h +++ b/include/wx/toolbar.h @@ -17,7 +17,7 @@ // wxToolBar style flags // ---------------------------------------------------------------------------- -enum +enum wxToolBarStyleFlags { // lay out the toolbar horizontally wxTB_HORIZONTAL = wxHORIZONTAL, // == 0x0004 @@ -61,6 +61,8 @@ enum wxTB_DEFAULT_STYLE = wxTB_HORIZONTAL }; +wxALLOW_COMBINING_ENUMS(wxToolBarStyleFlags, wxBorder) + #if wxUSE_TOOLBAR #include "wx/tbarbase.h" // the base class for all toolbars diff --git a/src/common/tarstrm.cpp b/src/common/tarstrm.cpp index 30b584e41d..7324b3ac3c 100644 --- a/src/common/tarstrm.cpp +++ b/src/common/tarstrm.cpp @@ -58,9 +58,7 @@ enum { TAR_NUMFIELDS }; -enum { - TAR_BLOCKSIZE = 512 -}; +static const int TAR_BLOCKSIZE = 512; // checksum type enum {