From 427b3c80d09ec02bca92444d0d4eb736127ebd67 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Sun, 17 Aug 2025 09:33:13 +0200 Subject: [PATCH] Use std::as_const instead of const_cast Change-Id: I5ec5708fe09b6aba6dc680f3e3f2135fb6451c88 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189810 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- include/svx/svdlayer.hxx | 3 ++- oox/source/dump/dumperbase.cxx | 2 +- sc/source/core/data/column2.cxx | 3 ++- sfx2/source/doc/Metadatable.cxx | 3 +-- svx/source/svdraw/svdlayer.cxx | 2 +- sw/inc/txatbase.hxx | 5 +++-- sw/source/core/doc/doccorr.cxx | 4 +++- sw/source/core/draw/dcontact.cxx | 7 ++++--- sw/source/core/edit/edredln.cxx | 4 +++- sw/source/core/inc/txtfrm.hxx | 5 ++--- sw/source/filter/html/htmltab.cxx | 4 ++-- unoxml/source/events/eventdispatcher.cxx | 10 +++++----- vcl/source/bitmap/bmpfast.cxx | 4 +++- vcl/source/treelist/treelist.cxx | 3 ++- vcl/source/window/layout.cxx | 14 +++++++------- 15 files changed, 41 insertions(+), 32 deletions(-) diff --git a/include/svx/svdlayer.hxx b/include/svx/svdlayer.hxx index 8201478a2fd8..d0c79c3b9736 100644 --- a/include/svx/svdlayer.hxx +++ b/include/svx/svdlayer.hxx @@ -24,6 +24,7 @@ #include #include #include +#include #include /** @@ -142,7 +143,7 @@ public: SdrLayer* GetLayer(const OUString& rName); const SdrLayer* GetLayer(const OUString& rName) const; SdrLayerID GetLayerID(const OUString& rName) const; - SdrLayer* GetLayerPerID(SdrLayerID nID) { return const_cast(const_cast(this)->GetLayerPerID(nID)); } + SdrLayer* GetLayerPerID(SdrLayerID nID) { return const_cast(std::as_const(*this).GetLayerPerID(nID)); } const SdrLayer* GetLayerPerID(SdrLayerID nID) const; void SetControlLayerName(const OUString& rNewName); diff --git a/oox/source/dump/dumperbase.cxx b/oox/source/dump/dumperbase.cxx index 736748be6d6a..18a4e8fb3f80 100644 --- a/oox/source/dump/dumperbase.cxx +++ b/oox/source/dump/dumperbase.cxx @@ -188,7 +188,7 @@ OUStringVector ItemFormat::parse( std::u16string_view rFormatStr ) OUStringVector aFormatVec; StringHelper::convertStringToStringList( aFormatVec, rFormatStr, false ); OUStringVector::const_iterator aIt = parse( aFormatVec ); - return OUStringVector( aIt, const_cast< const OUStringVector& >( aFormatVec ).end() ); + return OUStringVector( aIt, std::as_const( aFormatVec ).end() ); } // append string to string ---------------------------------------------------- diff --git a/sc/source/core/data/column2.cxx b/sc/source/core/data/column2.cxx index cb88956f7ec6..906b51d691d3 100644 --- a/sc/source/core/data/column2.cxx +++ b/sc/source/core/data/column2.cxx @@ -68,6 +68,7 @@ #include #include #include +#include // factor from font size to optimal cell height (text width) #define SC_ROT_BREAK_FACTOR 6 @@ -2226,7 +2227,7 @@ const ScPostIt* ScColumn::GetCellNote( sc::ColumnBlockConstPosition& rBlockPos, ScPostIt* ScColumn::GetCellNote( sc::ColumnBlockConstPosition& rBlockPos, SCROW nRow ) { - return const_cast(const_cast(this)->GetCellNote( rBlockPos, nRow )); + return const_cast(std::as_const(*this).GetCellNote( rBlockPos, nRow )); } void ScColumn::SetCellNote(SCROW nRow, std::unique_ptr pNote) diff --git a/sfx2/source/doc/Metadatable.cxx b/sfx2/source/doc/Metadatable.cxx index dc61fb03b768..2faca055d72d 100644 --- a/sfx2/source/doc/Metadatable.cxx +++ b/sfx2/source/doc/Metadatable.cxx @@ -471,8 +471,7 @@ struct XmlIdRegistryDocument::XmlIdRegistry_Impl const OUString & i_rIdref) { return const_cast( - const_cast(this) - ->LookupElementVector(i_rStreamName, i_rIdref)); + std::as_const(*this).LookupElementVector(i_rStreamName, i_rIdref)); } XmlIdMap_t m_XmlIdMap; diff --git a/svx/source/svdraw/svdlayer.cxx b/svx/source/svdraw/svdlayer.cxx index db1ea1042a2d..ae2d957c623f 100644 --- a/svx/source/svdraw/svdlayer.cxx +++ b/svx/source/svdraw/svdlayer.cxx @@ -204,7 +204,7 @@ sal_uInt16 SdrLayerAdmin::GetLayerPos(const SdrLayer* pLayer) const SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName) { - return const_cast(const_cast(this)->GetLayer(rName)); + return const_cast(std::as_const(*this).GetLayer(rName)); } const SdrLayer* SdrLayerAdmin::GetLayer(const OUString& rName) const diff --git a/sw/inc/txatbase.hxx b/sw/inc/txatbase.hxx index 9069e2a6952b..3ed842e14813 100644 --- a/sw/inc/txatbase.hxx +++ b/sw/inc/txatbase.hxx @@ -34,6 +34,8 @@ #include "tox.hxx" #include "ndhints.hxx" +#include + /** * A wrapper around SfxPoolItem to store the start position of (usually) a text portion, with an * optional end. @@ -176,8 +178,7 @@ inline const SfxPoolItem& SwTextAttr::GetAttr() const inline SfxPoolItem& SwTextAttr::GetAttr() { - return const_cast( - const_cast(this)->GetAttr()); + return const_cast(std::as_const(*this).GetAttr()); } inline void SwTextAttr::SetDontExpand( bool bDontExpand ) diff --git a/sw/source/core/doc/doccorr.cxx b/sw/source/core/doc/doccorr.cxx index 731086e09e22..8e68e1736f28 100644 --- a/sw/source/core/doc/doccorr.cxx +++ b/sw/source/core/doc/doccorr.cxx @@ -28,6 +28,8 @@ #include #include +#include + namespace { /// find the relevant section in which the SwUnoCursor may wander. @@ -371,7 +373,7 @@ SwEditShell const * SwDoc::GetEditShell() const SwEditShell* SwDoc::GetEditShell() { - return const_cast( const_cast( this )->GetEditShell() ); + return const_cast( std::as_const( *this ).GetEditShell() ); } ::sw::IShellCursorSupplier * SwDoc::GetIShellCursorSupplier() diff --git a/sw/source/core/draw/dcontact.cxx b/sw/source/core/draw/dcontact.cxx index f99a474bb5b9..1f1566453ce4 100644 --- a/sw/source/core/draw/dcontact.cxx +++ b/sw/source/core/draw/dcontact.cxx @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -570,7 +571,7 @@ const SwAnchoredObject* SwFlyDrawContact::GetAnchoredObj(const SdrObject* pSdrOb SwAnchoredObject* SwFlyDrawContact::GetAnchoredObj(SdrObject *const pSdrObj) { - return const_cast(const_cast(this)->GetAnchoredObj(pSdrObj)); + return const_cast(std::as_const(*this).GetAnchoredObj(pSdrObj)); } SdrObject* SwFlyDrawContact::GetMaster() @@ -786,7 +787,7 @@ const SwAnchoredObject* SwDrawContact::GetAnchoredObj(const SdrObject* pSdrObj ) SwAnchoredObject* SwDrawContact::GetAnchoredObj(SdrObject *const pSdrObj) { - return const_cast(const_cast(this)->GetAnchoredObj(pSdrObj)); + return const_cast(std::as_const(*this).GetAnchoredObj(pSdrObj)); } SdrObject* SwDrawContact::GetMaster() @@ -817,7 +818,7 @@ const SwFrame* SwDrawContact::GetAnchorFrame( const SdrObject* _pDrawObj ) const SwFrame* SwDrawContact::GetAnchorFrame(SdrObject const *const pDrawObj) { - return const_cast(const_cast(this)->GetAnchorFrame(pDrawObj)); + return const_cast(std::as_const(*this).GetAnchorFrame(pDrawObj)); } /** add a 'virtual' drawing object to drawing page. diff --git a/sw/source/core/edit/edredln.cxx b/sw/source/core/edit/edredln.cxx index 74d1a8d3def1..45cd8ad14793 100644 --- a/sw/source/core/edit/edredln.cxx +++ b/sw/source/core/edit/edredln.cxx @@ -29,6 +29,8 @@ #include #include +#include + RedlineFlags SwEditShell::GetRedlineFlags() const { return GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags(this); @@ -71,7 +73,7 @@ const SwRangeRedline& SwEditShell::GetRedline( SwRedlineTable::size_type nPos ) SwRangeRedline& SwEditShell::GetRedline(SwRedlineTable::size_type nPos) { - return const_cast(const_cast(this)->GetRedline(nPos)); + return const_cast(std::as_const(*this).GetRedline(nPos)); } static void lcl_InvalidateAll( SwViewShell* pSh ) diff --git a/sw/source/core/inc/txtfrm.hxx b/sw/source/core/inc/txtfrm.hxx index 89751c779acd..c43f78e199a2 100644 --- a/sw/source/core/inc/txtfrm.hxx +++ b/sw/source/core/inc/txtfrm.hxx @@ -478,10 +478,9 @@ public: SwTextNode const* GetTextNodeForParaProps() const; SwTextNode const* GetTextNodeForFirstText() const; SwTextNode * GetTextNodeFirst() - { return const_cast(const_cast(this)->GetTextNodeFirst()); }; + { return const_cast(std::as_const(*this).GetTextNodeFirst()); }; SwTextNode const* GetTextNodeFirst() const; - SwDoc & GetDoc() - { return const_cast(const_cast(this)->GetDoc()); } + SwDoc & GetDoc() { return const_cast(std::as_const(*this).GetDoc()); } SwDoc const& GetDoc() const; SwTextFrame(SwTextNode * const, SwFrame*, sw::FrameMode eMode); diff --git a/sw/source/filter/html/htmltab.cxx b/sw/source/filter/html/htmltab.cxx index 3e97c4f323bc..2ec7e5ff3abd 100644 --- a/sw/source/filter/html/htmltab.cxx +++ b/sw/source/filter/html/htmltab.cxx @@ -314,7 +314,7 @@ public: const HTMLTableCell& GetCell(sal_uInt16 nCell) const; HTMLTableCell& GetCell(sal_uInt16 nCell) { - return const_cast(const_cast(*this).GetCell(nCell)); + return const_cast(std::as_const(*this).GetCell(nCell)); } void SetAdjust( SvxAdjust eAdj ) { m_eAdjust = eAdj; } @@ -550,7 +550,7 @@ public: const HTMLTableCell& GetCell(sal_uInt16 nRow, sal_uInt16 nCell) const; HTMLTableCell& GetCell(sal_uInt16 nRow, sal_uInt16 nCell) { - return const_cast(const_cast(*this).GetCell(nRow, nCell)); + return const_cast(std::as_const(*this).GetCell(nRow, nCell)); } // set/determine caption diff --git a/unoxml/source/events/eventdispatcher.cxx b/unoxml/source/events/eventdispatcher.cxx index e990040ae17a..d629a9dfb1ce 100644 --- a/unoxml/source/events/eventdispatcher.cxx +++ b/unoxml/source/events/eventdispatcher.cxx @@ -28,6 +28,8 @@ #include +#include + using namespace css::uno; using namespace css::xml::dom; using namespace css::xml::dom::events; @@ -213,15 +215,13 @@ namespace DOM::events { // then bubbeling phase listeners are called in target to root // order // start at the root - NodeVector_t::const_reverse_iterator rinode = - const_cast(captureVector).rbegin(); - if (rinode == const_cast(captureVector).rend()) + auto rinode = std::as_const(captureVector).rbegin(); + if (rinode == std::as_const(captureVector).rend()) return; // capturing phase: pEvent->m_phase = PhaseType_CAPTURING_PHASE; - while (rinode != - const_cast(captureVector).rend()) + while (rinode != std::as_const(captureVector).rend()) { pEvent->m_currentTarget = rinode->first; callListeners(captureListeners, rinode->second, aType, xEvent); diff --git a/vcl/source/bitmap/bmpfast.cxx b/vcl/source/bitmap/bmpfast.cxx index 33661c7bc52f..88428c4b55d1 100644 --- a/vcl/source/bitmap/bmpfast.cxx +++ b/vcl/source/bitmap/bmpfast.cxx @@ -24,6 +24,8 @@ #include +#include + typedef unsigned char PIXBYTE; namespace { @@ -417,7 +419,7 @@ static inline ConstScanline ImplGetScanline( const BitmapBuffer& rBuf, tools::Lo static inline Scanline ImplGetScanline( BitmapBuffer& rBuf, tools::Long nY ) { - return const_cast(ImplGetScanline( const_cast(rBuf), nY )); + return const_cast(ImplGetScanline( std::as_const(rBuf), nY )); } template diff --git a/vcl/source/treelist/treelist.cxx b/vcl/source/treelist/treelist.cxx index ffd8c8185be6..0cf3b0c53f40 100644 --- a/vcl/source/treelist/treelist.cxx +++ b/vcl/source/treelist/treelist.cxx @@ -26,6 +26,7 @@ #include #include #include +#include typedef std::unordered_map> SvDataTable; @@ -1352,7 +1353,7 @@ const SvViewDataEntry* SvListView::GetViewData( const SvTreeListEntry* pEntry ) SvViewDataEntry* SvListView::GetViewData( SvTreeListEntry* pEntry ) { - return const_cast(const_cast(this)->GetViewData(pEntry)); + return const_cast(std::as_const(*this).GetViewData(pEntry)); } sal_Int32 SvTreeList::Compare(const SvTreeListEntry* pLeft, const SvTreeListEntry* pRight) const diff --git a/vcl/source/window/layout.cxx b/vcl/source/window/layout.cxx index 899498ee25bf..aea117d60bea 100644 --- a/vcl/source/window/layout.cxx +++ b/vcl/source/window/layout.cxx @@ -1464,7 +1464,7 @@ const vcl::Window *VclBin::get_child() const vcl::Window *VclBin::get_child() { - return const_cast(const_cast(this)->get_child()); + return const_cast(std::as_const(*this).get_child()); } Size VclBin::calculateRequisition() const @@ -1569,7 +1569,7 @@ const vcl::Window *VclFrame::get_label_widget() const vcl::Window *VclFrame::get_label_widget() { - return const_cast(const_cast(this)->get_label_widget()); + return const_cast(std::as_const(*this).get_label_widget()); } const vcl::Window *VclFrame::get_child() const @@ -1586,7 +1586,7 @@ const vcl::Window *VclFrame::get_child() const vcl::Window *VclFrame::get_child() { - return const_cast(const_cast(this)->get_child()); + return const_cast(std::as_const(*this).get_child()); } void VclFrame::set_label(const OUString &rLabel) @@ -1741,7 +1741,7 @@ const vcl::Window *VclExpander::get_child() const vcl::Window *VclExpander::get_child() { - return const_cast(const_cast(this)->get_child()); + return const_cast(std::as_const(*this).get_child()); } Size VclExpander::calculateRequisition() const @@ -1854,7 +1854,7 @@ const vcl::Window *VclExpander::get_label_widget() const vcl::Window *VclExpander::get_label_widget() { - return const_cast(const_cast(this)->get_label_widget()); + return const_cast(std::as_const(*this).get_label_widget()); } void VclExpander::DumpAsPropertyTree(tools::JsonWriter& rJsonWriter) @@ -1952,7 +1952,7 @@ const vcl::Window *VclScrolledWindow::get_child() const vcl::Window *VclScrolledWindow::get_child() { - return const_cast(const_cast(this)->get_child()); + return const_cast(std::as_const(*this).get_child()); } Size VclScrolledWindow::calculateRequisition() const @@ -2276,7 +2276,7 @@ const vcl::Window *VclEventBox::get_child() const vcl::Window *VclEventBox::get_child() { - return const_cast(const_cast(this)->get_child()); + return const_cast(std::as_const(*this).get_child()); } void VclEventBox::setAllocation(const Size& rAllocation)