From 09eff033d90106b496c18b402b83d6c447bf4b13 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 6 Jan 2024 20:11:30 +0100 Subject: [PATCH] Hide operator<<() overloads used for wxVariant support too Add new wxDECLARE_VARIANT_OBJECT_EXPORTED() macro defining these operators as friend functions inside the class declaration and replace all uses of DECLARE_VARIANT_OBJECT_EXPORTED() inside the core library with the new macro to avoid defining any operator<<() overloads in the global scope. Also add wxIMPLEMENT_VARIANT_OBJECT() for consistency, even though it is not really needed. --- include/wx/bitmap.h | 13 ++++--------- include/wx/colour.h | 12 +++--------- include/wx/dvrenderers.h | 8 ++++---- include/wx/generic/icon.h | 2 ++ include/wx/icon.h | 12 +----------- include/wx/image.h | 13 ++++--------- include/wx/msw/icon.h | 2 ++ include/wx/propgrid/advprops.h | 4 ++-- include/wx/variant.h | 25 ++++++++++++++++++++++++- interface/wx/variant.h | 26 +++++++++++++++++--------- 10 files changed, 63 insertions(+), 54 deletions(-) diff --git a/include/wx/bitmap.h b/include/wx/bitmap.h index 3a9f63aa1f..185513b31a 100644 --- a/include/wx/bitmap.h +++ b/include/wx/bitmap.h @@ -18,6 +18,7 @@ #include "wx/gdicmn.h" // for wxBitmapType #include "wx/colour.h" #include "wx/image.h" +#include "wx/variant.h" class WXDLLIMPEXP_FWD_CORE wxBitmap; class WXDLLIMPEXP_FWD_CORE wxBitmapHandler; @@ -28,15 +29,6 @@ class WXDLLIMPEXP_FWD_CORE wxMask; class WXDLLIMPEXP_FWD_CORE wxPalette; class WXDLLIMPEXP_FWD_CORE wxPixelDataBase; -// ---------------------------------------------------------------------------- -// wxVariant support -// ---------------------------------------------------------------------------- - -#if wxUSE_VARIANT -#include "wx/variant.h" -DECLARE_VARIANT_OBJECT_EXPORTED(wxBitmap,WXDLLIMPEXP_CORE) -#endif - // ---------------------------------------------------------------------------- // wxMask represents the transparent area of the bitmap // ---------------------------------------------------------------------------- @@ -100,6 +92,9 @@ public: // Rescale the given bitmap to the requested size. static void Rescale(wxBitmap& bmp, const wxSize& sizeNeeded); + + // wxVariant support + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxBitmap, WXDLLIMPEXP_CORE); }; diff --git a/include/wx/colour.h b/include/wx/colour.h index 344ecc9741..cf1d679890 100644 --- a/include/wx/colour.h +++ b/include/wx/colour.h @@ -13,6 +13,7 @@ #include "wx/defs.h" #include "wx/gdiobj.h" +#include "wx/variant.h" class WXDLLIMPEXP_FWD_CORE wxColour; @@ -53,15 +54,6 @@ const unsigned char wxALPHA_OPAQUE = 0xff; #define wxTransparentColour wxColour(0, 0, 0, wxALPHA_TRANSPARENT) #define wxTransparentColor wxTransparentColour -// ---------------------------------------------------------------------------- -// wxVariant support -// ---------------------------------------------------------------------------- - -#if wxUSE_VARIANT -#include "wx/variant.h" -DECLARE_VARIANT_OBJECT_EXPORTED(wxColour,WXDLLIMPEXP_CORE) -#endif - //----------------------------------------------------------------------------- // wxColourBase: this class has no data members, just some functions to avoid // code redundancy in all native wxColour implementations @@ -188,6 +180,8 @@ public: wxColour ChangeLightness(int ialpha) const; wxColour& MakeDisabled(unsigned char brightness = 255); + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxColour, WXDLLIMPEXP_CORE); + protected: // Some ports need Init() and while we don't, provide a stub so that the // ports which don't need it are not forced to define it diff --git a/include/wx/dvrenderers.h b/include/wx/dvrenderers.h index 8832396fca..e44e874d6f 100644 --- a/include/wx/dvrenderers.h +++ b/include/wx/dvrenderers.h @@ -67,6 +67,8 @@ public: return !IsSameAs(other); } + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_CORE); + private: wxString m_text; wxBitmapBundle m_bitmap; @@ -74,8 +76,6 @@ private: wxDECLARE_DYNAMIC_CLASS(wxDataViewIconText); }; -DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewIconText, WXDLLIMPEXP_CORE) - // ---------------------------------------------------------------------------- // wxDataViewCheckIconText: value class used by wxDataViewCheckIconTextRenderer // ---------------------------------------------------------------------------- @@ -94,14 +94,14 @@ public: wxCheckBoxState GetCheckedState() const { return m_checkedState; } void SetCheckedState(wxCheckBoxState state) { m_checkedState = state; } + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewCheckIconText, WXDLLIMPEXP_CORE); + private: wxCheckBoxState m_checkedState; wxDECLARE_DYNAMIC_CLASS(wxDataViewCheckIconText); }; -DECLARE_VARIANT_OBJECT_EXPORTED(wxDataViewCheckIconText, WXDLLIMPEXP_CORE) - // ---------------------------------------------------------------------------- // wxDataViewRendererBase // ---------------------------------------------------------------------------- diff --git a/include/wx/generic/icon.h b/include/wx/generic/icon.h index 0138dfeb5b..14fe70acfe 100644 --- a/include/wx/generic/icon.h +++ b/include/wx/generic/icon.h @@ -51,6 +51,8 @@ public: // ctors, assignment operators...), but it's ok to have such function void CopyFromBitmap(const wxBitmap& bmp); + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxIcon, WXDLLIMPEXP_CORE); + private: wxDECLARE_DYNAMIC_CLASS(wxIcon); }; diff --git a/include/wx/icon.h b/include/wx/icon.h index dbc1ed5f4b..d8576e9e04 100644 --- a/include/wx/icon.h +++ b/include/wx/icon.h @@ -11,7 +11,7 @@ #define _WX_ICON_H_BASE_ #include "wx/iconloc.h" - +#include "wx/variant.h" // a more readable way to tell #define wxICON_SCREEN_DEPTH (-1) @@ -57,15 +57,5 @@ #define wxICON_IS_BITMAP #endif -//----------------------------------------------------------------------------- -// wxVariant support -//----------------------------------------------------------------------------- - -#if wxUSE_VARIANT -#include "wx/variant.h" -DECLARE_VARIANT_OBJECT_EXPORTED(wxIcon,WXDLLIMPEXP_CORE) -#endif - - #endif // _WX_ICON_H_BASE_ diff --git a/include/wx/image.h b/include/wx/image.h index 0f1ad39a29..8e34f651a2 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -18,6 +18,7 @@ #include "wx/gdicmn.h" #include "wx/hashmap.h" #include "wx/arrstr.h" +#include "wx/variant.h" #if wxUSE_STREAMS # include "wx/stream.h" @@ -98,15 +99,6 @@ class WXDLLIMPEXP_FWD_CORE wxImageHandler; class WXDLLIMPEXP_FWD_CORE wxImage; class WXDLLIMPEXP_FWD_CORE wxPalette; -//----------------------------------------------------------------------------- -// wxVariant support -//----------------------------------------------------------------------------- - -#if wxUSE_VARIANT -#include "wx/variant.h" -DECLARE_VARIANT_OBJECT_EXPORTED(wxImage,WXDLLIMPEXP_CORE) -#endif - //----------------------------------------------------------------------------- // wxImageHandler //----------------------------------------------------------------------------- @@ -594,6 +586,9 @@ public: static HSVValue RGBtoHSV(const RGBValue& rgb); static RGBValue HSVtoRGB(const HSVValue& hsv); + // wxVariant support + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxImage, WXDLLIMPEXP_CORE); + protected: static wxList sm_handlers; diff --git a/include/wx/msw/icon.h b/include/wx/msw/icon.h index bb13e27d98..fea6ff68a4 100644 --- a/include/wx/msw/icon.h +++ b/include/wx/msw/icon.h @@ -77,6 +77,8 @@ public: // ctors, assignment operators...), but it's ok to have such function void CopyFromBitmap(const wxBitmap& bmp); + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxIcon, WXDLLIMPEXP_CORE); + protected: virtual wxGDIImageRefData *CreateData() const override { diff --git a/include/wx/propgrid/advprops.h b/include/wx/propgrid/advprops.h index f5375997be..d4838fefa5 100644 --- a/include/wx/propgrid/advprops.h +++ b/include/wx/propgrid/advprops.h @@ -113,6 +113,8 @@ public: Init( cpv.m_type, cpv.m_colour ); } + wxDECLARE_VARIANT_OBJECT_EXPORTED(wxColourPropertyValue, WXDLLIMPEXP_PROPGRID); + private: wxDECLARE_DYNAMIC_CLASS(wxColourPropertyValue); }; @@ -121,8 +123,6 @@ private: bool WXDLLIMPEXP_PROPGRID operator==(const wxColourPropertyValue&, const wxColourPropertyValue&); -DECLARE_VARIANT_OBJECT_EXPORTED(wxColourPropertyValue, WXDLLIMPEXP_PROPGRID) - // ----------------------------------------------------------------------- // Property representing wxFont. diff --git a/include/wx/variant.h b/include/wx/variant.h index 4d5f1fe44c..74a623a1ce 100644 --- a/include/wx/variant.h +++ b/include/wx/variant.h @@ -470,7 +470,16 @@ REGISTER_WXANY_CONVERSION(T, CLASSNAME) #endif // wxUSE_ANY/!wxUSE_ANY +// Note: these macros must be used inside "classname" declaration. +#define wxDECLARE_VARIANT_OBJECT(classname) \ + wxDECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) +#define wxDECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) \ + friend expdecl classname& operator<<(classname &object, const wxVariant &variant); \ + friend expdecl wxVariant& operator<<(wxVariant &variant, const classname &object) + +// These macros are deprecated, consider using wxDECLARE_VARIANT_OBJECT() above +// instead. #define DECLARE_VARIANT_OBJECT(classname) \ DECLARE_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) @@ -478,6 +487,13 @@ REGISTER_WXANY_CONVERSION(T, CLASSNAME) expdecl classname& operator << ( classname &object, const wxVariant &variant ); \ expdecl wxVariant& operator << ( wxVariant &variant, const classname &object ); +// These macros use "wx" prefix and require a semicolon after them for +// consistency with the rest of wx macros, but are otherwise the same as the +// older IMPLEMENT_VARIANT_XXX macros. +#define wxIMPLEMENT_VARIANT_OBJECT(classname) \ + IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) \ + struct wxDummyVariantStructFwdDecl /* to force a semicolon */ + #define IMPLEMENT_VARIANT_OBJECT(classname) \ IMPLEMENT_VARIANT_OBJECT_EXPORTED(classname, wxEMPTY_PARAMETER_VALUE) @@ -578,6 +594,13 @@ bool classname##VariantData::Eq(wxVariantData& data) const \ extern wxVariant WXDLLIMPEXP_BASE wxNullVariant; -#endif // wxUSE_VARIANT +#else // !wxUSE_VARIANT + +// Define these macros to allow using them without checking for wxUSE_VARIANT +// and simply do nothing in them in this case. +#define wxDECLARE_VARIANT_OBJECT(classname) +#define wxDECLARE_VARIANT_OBJECT_EXPORTED(classname,expdecl) + +#endif // wxUSE_VARIANT/!wxUSE_VARIANT #endif // _WX_VARIANT_H_ diff --git a/interface/wx/variant.h b/interface/wx/variant.h index 08c61c49f8..60b7158c90 100644 --- a/interface/wx/variant.h +++ b/interface/wx/variant.h @@ -42,21 +42,24 @@ required. Note that as of wxWidgets 2.7.1, wxVariant is - @ref overview_refcount "reference counted". Additionally, the convenience - macros DECLARE_VARIANT_OBJECT() and IMPLEMENT_VARIANT_OBJECT() were added - so that adding (limited) support for conversion to and from wxVariant can - be very easily implemented without modifying either wxVariant or the class - to be stored by wxVariant. Since assignment operators cannot be declared - outside the class, the shift left operators are used like this: + @ref overview_refcount "reference counted". + + Convenience macros wxDECLARE_VARIANT_OBJECT() and wxIMPLEMENT_VARIANT_OBJECT() + allow easily adding support for conversion to and from wxVariant to custom + classes. The first of these macros must be used inside the class declaration + and the second one outside of it in the implementation file, e.g. @code // in the header file - DECLARE_VARIANT_OBJECT(MyClass) + class MyClass : public wxObject { + ... + wxDECLARE_VARIANT_OBJECT(MyClass); + }; // in the implementation file - IMPLEMENT_VARIANT_OBJECT(MyClass) + wxIMPLEMENT_VARIANT_OBJECT(MyClass); - // in the user code + // and then objects of MyClass can be used with wxVariant like this: wxVariant variant; MyClass value; variant << value; @@ -79,6 +82,11 @@ - wxBitmap - wxBitmapBundle + @note There also are legacy versions of the above macros without `wx` + prefix, working in a slightly different way. Please use the new + versions in the new code and consider replacing any existing use of + the legacy macros with the new ones. + Note that as of wxWidgets 2.9.0, wxVariantData no longer inherits from wxObject and wxVariant no longer uses the type-unsafe wxList class for list operations but the type-safe wxVariantList class. Also, wxVariantData now