diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2021-10-14 09:25:24 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2021-10-15 10:36:36 +0200 |
commit | 2484de6728bd11bb7949003d112f1ece2223c7a1 (patch) | |
tree | 1296534e396da284b38d2c478dcd2b31c4714179 /include | |
parent | 88375fd36899d21d3309cf8333712e02a87d3a91 (diff) |
Remove non-const Sequence::begin()/end() in internal code
... to avoid hidden cost of multiple COW checks, because they
call getArray() internally.
This obsoletes [loplugin:sequenceloop].
Also rename toNonConstRange to asNonConstRange, to reflect that
the result is a view of the sequence, not an independent object.
TODO: also drop non-const operator[], but introduce operator[]
in SequenceRange.
Change-Id: Idd5fd7a3400fe65274d2a6343025e2ef8911635d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123518
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include')
-rw-r--r-- | include/com/sun/star/uno/Sequence.h | 4 | ||||
-rw-r--r-- | include/com/sun/star/uno/Sequence.hxx | 8 | ||||
-rw-r--r-- | include/comphelper/basicio.hxx | 2 | ||||
-rw-r--r-- | include/comphelper/propertysequence.hxx | 4 | ||||
-rw-r--r-- | include/comphelper/sequence.hxx | 2 |
5 files changed, 14 insertions, 6 deletions
diff --git a/include/com/sun/star/uno/Sequence.h b/include/com/sun/star/uno/Sequence.h index 39caf8ff768f..cc2c515f1322 100644 --- a/include/com/sun/star/uno/Sequence.h +++ b/include/com/sun/star/uno/Sequence.h @@ -185,12 +185,14 @@ public: */ inline E * SAL_CALL getArray(); +#if !defined LIBO_INTERNAL_ONLY /** This function allows to use Sequence in standard algorithms, like std::find and others. @since LibreOffice 4.2 */ inline E * begin(); +#endif /** This function allows to use Sequence in standard algorithms, like std::find and others. @@ -199,12 +201,14 @@ public: */ inline E const * begin() const; +#if !defined LIBO_INTERNAL_ONLY /** This function allows to use Sequence in standard algorithms, like std::find and others. @since LibreOffice 4.2 */ inline E * end(); +#endif /** This function allows to use Sequence in standard algorithms, like std::find and others. diff --git a/include/com/sun/star/uno/Sequence.hxx b/include/com/sun/star/uno/Sequence.hxx index 08a74d73789f..5356872b1378 100644 --- a/include/com/sun/star/uno/Sequence.hxx +++ b/include/com/sun/star/uno/Sequence.hxx @@ -166,12 +166,16 @@ inline E * Sequence< E >::getArray() return reinterpret_cast< E * >( _pSequence->elements ); } +#if !defined LIBO_INTERNAL_ONLY template<class E> E * Sequence<E>::begin() { return getArray(); } +#endif template<class E> E const * Sequence<E>::begin() const { return getConstArray(); } +#if !defined LIBO_INTERNAL_ONLY template<class E> E * Sequence<E>::end() { return begin() + getLength(); } +#endif template<class E> E const * Sequence<E>::end() const { return begin() + getLength(); } @@ -279,7 +283,7 @@ inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, t return os; } -template <class E> inline auto toNonConstRange(css::uno::Sequence<E>& s) +template <class E> inline auto asNonConstRange(css::uno::Sequence<E>& s) { // Two iterators [begin, end] representing the non-const range of the Sequence. // It only calls Sequence::getArray once, to avoid the second COW overhead when @@ -292,7 +296,7 @@ template <class E> inline auto toNonConstRange(css::uno::Sequence<E>& s) E* begin() { return std::pair<E*, E*>::first; } E* end() { return std::pair<E*, E*>::second; } }; - return SequenceRange(s.begin(), s.getLength()); + return SequenceRange(s.getArray(), s.getLength()); }; /// @endcond diff --git a/include/comphelper/basicio.hxx b/include/comphelper/basicio.hxx index 96e185d78838..62260e3da360 100644 --- a/include/comphelper/basicio.hxx +++ b/include/comphelper/basicio.hxx @@ -65,7 +65,7 @@ template <class ELEMENT> const css::uno::Reference<css::io::XObjectInputStream>& operator >> (const css::uno::Reference<css::io::XObjectInputStream>& _rxInStream, css::uno::Sequence<ELEMENT>& _rSeq) { _rSeq.realloc(_rxInStream->readLong()); - for (ELEMENT& rElement : _rSeq) + for (ELEMENT& rElement : asNonConstRange(_rSeq)) _rxInStream >> rElement; return _rxInStream; } diff --git a/include/comphelper/propertysequence.hxx b/include/comphelper/propertysequence.hxx index 3f9838f9ab8f..2c143aa9ed7a 100644 --- a/include/comphelper/propertysequence.hxx +++ b/include/comphelper/propertysequence.hxx @@ -24,7 +24,7 @@ namespace comphelper ::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit) { css::uno::Sequence< css::beans::PropertyValue> vResult{static_cast<sal_Int32>(vInit.size())}; - std::transform(vInit.begin(), vInit.end(), vResult.begin(), + std::transform(vInit.begin(), vInit.end(), vResult.getArray(), [](const std::pair<OUString, css::uno::Any>& rInit) { return css::beans::PropertyValue(rInit.first, -1, rInit.second, css::beans::PropertyState_DIRECT_VALUE); @@ -40,7 +40,7 @@ namespace comphelper ::std::initializer_list< ::std::pair< OUString, css::uno::Any > > vInit) { css::uno::Sequence<css::uno::Any> vResult{static_cast<sal_Int32>(vInit.size())}; - std::transform(vInit.begin(), vInit.end(), vResult.begin(), + std::transform(vInit.begin(), vInit.end(), vResult.getArray(), [](const std::pair<OUString, css::uno::Any>& rInit) { return css::uno::Any( css::beans::PropertyValue(rInit.first, -1, rInit.second, diff --git a/include/comphelper/sequence.hxx b/include/comphelper/sequence.hxx index 18e269173627..7c999ec25059 100644 --- a/include/comphelper/sequence.hxx +++ b/include/comphelper/sequence.hxx @@ -50,7 +50,7 @@ namespace comphelper { // unary fold to disallow empty parameter pack: at least have one sequence in rSn css::uno::Sequence<T> aReturn(std::size(rS1) + (... + std::size(rSn))); - T* pReturn = std::copy(std::begin(rS1), std::end(rS1), aReturn.begin()); + T* pReturn = std::copy(std::begin(rS1), std::end(rS1), aReturn.getArray()); (..., (pReturn = std::copy(std::begin(rSn), std::end(rSn), pReturn))); return aReturn; } |