diff options
-rw-r--r-- | include/o3tl/array_view.hxx | 163 | ||||
-rw-r--r-- | include/o3tl/span.hxx | 95 | ||||
-rw-r--r-- | include/sfx2/dispatch.hxx | 4 | ||||
-rw-r--r-- | o3tl/CppunitTest_o3tl_tests.mk | 2 | ||||
-rw-r--r-- | o3tl/qa/test-array_view.cxx | 85 | ||||
-rw-r--r-- | o3tl/qa/test-span.cxx | 65 | ||||
-rw-r--r-- | sd/source/ui/inc/DrawDocShell.hxx | 6 | ||||
-rw-r--r-- | sfx2/source/control/dispatch.cxx | 7 | ||||
-rw-r--r-- | solenv/clang-format/blacklist | 4 |
9 files changed, 172 insertions, 259 deletions
diff --git a/include/o3tl/array_view.hxx b/include/o3tl/array_view.hxx deleted file mode 100644 index dd6ca7fa94c0..000000000000 --- a/include/o3tl/array_view.hxx +++ /dev/null @@ -1,163 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#ifndef INCLUDED_O3TL_ARRAY_VIEW_HXX -#define INCLUDED_O3TL_ARRAY_VIEW_HXX - -#include <sal/config.h> - -#include <algorithm> -#include <cstddef> -#include <ios> -#include <iterator> -#include <ostream> -#include <stdexcept> -#include <string> -#include <type_traits> -#include <utility> - -#include <rtl/string.hxx> -#include <rtl/ustring.hxx> -#include <sal/types.h> - -namespace o3tl { - -#if defined _MSC_VER -#pragma warning(push) -#pragma warning(disable: 4522) // multiple assignment operators specified -#endif - -/** A barebones approximation of C++17(?) <array_view>. - Haven't bothered with more than single-dimensional arrays. -*/ -template<typename T> -class array_view { - friend class array_view<T const>; -public: - using value_type = T; - using pointer = value_type *; - using const_pointer = value_type const *; - using reference = value_type &; - using const_reference = value_type const &; - using const_iterator = const_pointer; - using iterator = pointer; - using const_reverse_iterator = std::reverse_iterator<const_iterator>; - using reverse_iterator = std::reverse_iterator<iterator>; - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - - static constexpr size_type npos = size_type(-1); - - constexpr array_view() noexcept : data_(nullptr), size_(0) {} - - template<size_type N> - constexpr array_view (T (&a)[N]) noexcept : data_(a), size_(N) {} - - constexpr array_view (T *a, size_type len) noexcept - : data_(a), size_(len) - { - // not terribly sure about this, might need to relax it - assert((a == nullptr && len == 0) || (a != nullptr && len > 0)); - } - - /// Allow for assigning array_view<T> to array_view<T const> i.e. - /// array_view<T> a; - /// array_view<T const> b = a; - template<typename = std::enable_if< std::is_const<value_type>::value > > - array_view& operator=(array_view<typename std::remove_const<value_type>::type> const & other) - { - data_ = other.data_; - size_ = other.size_; - return *this; - } - - constexpr bool empty() const noexcept { return size_ == 0; } - - constexpr iterator begin() const noexcept { return data_; } - constexpr iterator end() const noexcept { return begin() + size(); } - - constexpr const_iterator cbegin() const noexcept { return begin(); } - constexpr const_iterator cend() const noexcept { return end(); } - - reverse_iterator rbegin() const noexcept - { return reverse_iterator(end()); } - reverse_iterator rend() const noexcept - { return reverse_iterator(begin()); } - - constexpr const_reverse_iterator crbegin() const noexcept - { return rbegin(); } - constexpr const_reverse_iterator crend() const noexcept { return rend(); } - - constexpr size_type size() const noexcept { return size_; } - constexpr size_type length() const noexcept { return size(); } - - constexpr size_type max_size() const noexcept { - (void) this; // silence loplugin:staticmethods - return npos - 1; - } - - constexpr reference operator [](size_type pos) const { - assert(pos < size()); - return data_[pos]; - } - - constexpr reference at(size_type pos) const { - if (pos >= size()) { - throw std::out_of_range("o3tl::array_view::at"); - } - return operator [](pos); - } - - constexpr reference front() const { - assert(!empty()); - return operator [](0); - } - - constexpr reference back() const { - assert(!empty()); - return operator [](size() - 1); - } - - constexpr pointer data() const noexcept { return data_; } - - constexpr void swap(array_view & s) noexcept { - std::swap(data_, s.data_); - std::swap(size_, s.size_); - } - - /// so we can use it in associative containers - constexpr bool operator<(array_view const & other) const noexcept { - return data_ < other.data_ && size_ < other.size_; - } - -private: - pointer data_; - size_type size_; -}; - - -#if defined _MSC_VER -#pragma warning(pop) -#endif - -} // namespace o3tl - -namespace std { - -template<typename T> -struct hash<o3tl::array_view<T>> { - std::size_t operator()(o3tl::array_view<T> s) const - { return hash<T[]>()(s.data(), s.size()); } -}; - -} // namespace std - -#endif - -/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/o3tl/span.hxx b/include/o3tl/span.hxx new file mode 100644 index 000000000000..acb31bab728d --- /dev/null +++ b/include/o3tl/span.hxx @@ -0,0 +1,95 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef INCLUDED_O3TL_SPAN_HXX +#define INCLUDED_O3TL_SPAN_HXX + +#include <sal/config.h> + +#if __has_include(<span>) + +#include <span> + +namespace o3tl { using std::span; } + +#else + +#include <cassert> +#include <cstddef> +#include <iterator> + +namespace o3tl { + +/** A barebones approximation of C++20 <span>. +*/ +template<typename T> +class span { +public: + using value_type = T; + using pointer = value_type *; + using const_pointer = value_type const *; + using reference = value_type &; + using const_reference = value_type const &; + using const_iterator = const_pointer; + using iterator = pointer; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + using reverse_iterator = std::reverse_iterator<iterator>; + using index_type = std::ptrdiff_t; + using difference_type = std::ptrdiff_t; + + constexpr span() noexcept : data_(nullptr), size_(0) {} + + template<std::size_t N> + constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {} + + constexpr span (T *a, index_type len) noexcept + : data_(a), size_(len) + { + // not terribly sure about this, might need to strengthen it + assert((a == nullptr && len == 0) || (a != nullptr && len >= 0)); + } + + constexpr bool empty() const noexcept { return size_ == 0; } + + constexpr iterator begin() const noexcept { return data_; } + constexpr iterator end() const noexcept { return begin() + size(); } + + constexpr const_iterator cbegin() const noexcept { return begin(); } + constexpr const_iterator cend() const noexcept { return end(); } + + reverse_iterator rbegin() const noexcept + { return reverse_iterator(end()); } + reverse_iterator rend() const noexcept + { return reverse_iterator(begin()); } + + constexpr const_reverse_iterator crbegin() const noexcept + { return rbegin(); } + constexpr const_reverse_iterator crend() const noexcept { return rend(); } + + constexpr index_type size() const noexcept { return size_; } + + constexpr reference operator [](index_type pos) const { + assert(0 <= pos && pos < size()); + return data_[pos]; + } + + constexpr pointer data() const noexcept { return data_; } + +private: + pointer data_; + index_type size_; +}; + +} // namespace o3tl + +#endif + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/include/sfx2/dispatch.hxx b/include/sfx2/dispatch.hxx index 9e2f0a0379a2..899957133024 100644 --- a/include/sfx2/dispatch.hxx +++ b/include/sfx2/dispatch.hxx @@ -29,7 +29,7 @@ #include <sfx2/viewfrm.hxx> #include <vcl/menu.hxx> #include <o3tl/typed_flags_set.hxx> -#include <o3tl/array_view.hxx> +#include <o3tl/span.hxx> #include <initializer_list> @@ -161,7 +161,7 @@ public: void Lock( bool bLock ); bool IsLocked() const; void SetSlotFilter( SfxSlotFilterState nEnable = SfxSlotFilterState::DISABLED, - o3tl::array_view<sal_uInt16 const> pSIDs = o3tl::array_view<sal_uInt16 const>()); + o3tl::span<sal_uInt16 const> pSIDs = o3tl::span<sal_uInt16 const>()); void HideUI( bool bHide = true ); ToolbarId GetObjectBarId( sal_uInt16 nPos ) const; diff --git a/o3tl/CppunitTest_o3tl_tests.mk b/o3tl/CppunitTest_o3tl_tests.mk index 1dbfebdf798a..152fa48c72ca 100644 --- a/o3tl/CppunitTest_o3tl_tests.mk +++ b/o3tl/CppunitTest_o3tl_tests.mk @@ -27,12 +27,12 @@ $(eval $(call gb_CppunitTest_use_libraries,o3tl_tests,\ $(eval $(call gb_CppunitTest_add_exception_objects,o3tl_tests,\ o3tl/qa/cow_wrapper_clients \ - o3tl/qa/test-array_view \ o3tl/qa/test-cow_wrapper \ o3tl/qa/test-enumarray \ o3tl/qa/test-lru_map \ o3tl/qa/test-safeint \ o3tl/qa/test-sorted_vector \ + o3tl/qa/test-span \ o3tl/qa/test-typed_flags \ o3tl/qa/test-vector_pool \ )) diff --git a/o3tl/qa/test-array_view.cxx b/o3tl/qa/test-array_view.cxx deleted file mode 100644 index b82aa8cd1bf4..000000000000 --- a/o3tl/qa/test-array_view.cxx +++ /dev/null @@ -1,85 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ - -#include <sal/config.h> - -#include <stdexcept> - -#include <cppunit/TestAssert.h> -#include <cppunit/TestFixture.h> -#include <cppunit/extensions/HelperMacros.h> - -#include <o3tl/array_view.hxx> - -namespace { - -class Test: public CppUnit::TestFixture { -private: - CPPUNIT_TEST_SUITE(Test); - CPPUNIT_TEST(testOperations); - CPPUNIT_TEST_SUITE_END(); - - - void testOperations() { - int const some_data[] { 1, 2, 3 }; - o3tl::array_view<int const> v(some_data); - - CPPUNIT_ASSERT_EQUAL(1, *v.begin()); - CPPUNIT_ASSERT_EQUAL( - o3tl::array_view<int>::difference_type(3), v.end() - v.begin()); - CPPUNIT_ASSERT_EQUAL(1, *v.cbegin()); - CPPUNIT_ASSERT_EQUAL( - o3tl::array_view<int>::difference_type(3), v.cend() - v.cbegin()); - CPPUNIT_ASSERT_EQUAL(3, *v.rbegin()); - CPPUNIT_ASSERT_EQUAL( - o3tl::array_view<int>::difference_type(3), v.rend() - v.rbegin()); - CPPUNIT_ASSERT_EQUAL(3, *v.crbegin()); - CPPUNIT_ASSERT_EQUAL( - o3tl::array_view<int>::difference_type(3), v.crend() - v.crbegin()); - CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(3), v.size()); - CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(3), v.length()); - CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::npos - 1, v.max_size()); - CPPUNIT_ASSERT(!v.empty()); - CPPUNIT_ASSERT_EQUAL(2, v[1]); - try { - v.at(o3tl::array_view<int>::npos); - CPPUNIT_FAIL("missing exception"); - } catch (std::out_of_range &) {} - CPPUNIT_ASSERT_EQUAL(1, v.at(0)); - CPPUNIT_ASSERT_EQUAL(3, v.at(2)); - try { - v.at(3); - CPPUNIT_FAIL("missing exception"); - } catch (std::out_of_range &) {} - CPPUNIT_ASSERT_EQUAL(1, v.front()); - CPPUNIT_ASSERT_EQUAL(3, v.back()); - CPPUNIT_ASSERT_EQUAL(1, *v.data()); - { - int const d1[] { 1, 2 }; - int const d2[] { 3, 4, 5, 6 }; - o3tl::array_view<int const> v1( d1 ); - o3tl::array_view<int const> v2( d2 ); - v1.swap(v2); - CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(4), v1.size()); - CPPUNIT_ASSERT_EQUAL(o3tl::array_view<int>::size_type(2), v2.size()); - } - { - int d1[] { 1, 2, 3 }; - o3tl::array_view<int> v1(d1); - o3tl::array_view<int const> v2; - v2 = v1; // the special operator= - } - } -}; - -CPPUNIT_TEST_SUITE_REGISTRATION(Test); - -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/o3tl/qa/test-span.cxx b/o3tl/qa/test-span.cxx new file mode 100644 index 000000000000..7ec67fa7fd91 --- /dev/null +++ b/o3tl/qa/test-span.cxx @@ -0,0 +1,65 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <utility> + +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> + +#include <o3tl/span.hxx> + +namespace { + +class Test: public CppUnit::TestFixture { +private: + CPPUNIT_TEST_SUITE(Test); + CPPUNIT_TEST(testOperations); + CPPUNIT_TEST_SUITE_END(); + + + void testOperations() { + int const some_data[] { 1, 2, 3 }; + o3tl::span<int const> v(some_data); + + CPPUNIT_ASSERT_EQUAL(1, *v.begin()); + CPPUNIT_ASSERT_EQUAL( + o3tl::span<int>::difference_type(3), v.end() - v.begin()); + CPPUNIT_ASSERT_EQUAL(1, *v.cbegin()); + CPPUNIT_ASSERT_EQUAL( + o3tl::span<int>::difference_type(3), v.cend() - v.cbegin()); + CPPUNIT_ASSERT_EQUAL(3, *v.rbegin()); + CPPUNIT_ASSERT_EQUAL( + o3tl::span<int>::difference_type(3), v.rend() - v.rbegin()); + CPPUNIT_ASSERT_EQUAL(3, *v.crbegin()); + CPPUNIT_ASSERT_EQUAL( + o3tl::span<int>::difference_type(3), v.crend() - v.crbegin()); + CPPUNIT_ASSERT_EQUAL(o3tl::span<int>::index_type(3), v.size()); + CPPUNIT_ASSERT(!v.empty()); + CPPUNIT_ASSERT_EQUAL(2, v[1]); + CPPUNIT_ASSERT_EQUAL(1, *v.data()); + { + int const d1[] { 1, 2 }; + int const d2[] { 3, 4, 5, 6 }; + o3tl::span<int const> v1( d1 ); + o3tl::span<int const> v2( d2 ); + std::swap(v1, v2); + CPPUNIT_ASSERT_EQUAL(o3tl::span<int>::index_type(4), v1.size()); + CPPUNIT_ASSERT_EQUAL(o3tl::span<int>::index_type(2), v2.size()); + } + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Test); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sd/source/ui/inc/DrawDocShell.hxx b/sd/source/ui/inc/DrawDocShell.hxx index 84d6d277e40a..3d400c889974 100644 --- a/sd/source/ui/inc/DrawDocShell.hxx +++ b/sd/source/ui/inc/DrawDocShell.hxx @@ -20,7 +20,7 @@ #ifndef INCLUDED_SD_SOURCE_UI_INC_DRAWDOCSHELL_HXX #define INCLUDED_SD_SOURCE_UI_INC_DRAWDOCSHELL_HXX -#include <o3tl/array_view.hxx> +#include <o3tl/span.hxx> #include <sfx2/docfac.hxx> #include <sfx2/objsh.hxx> @@ -146,7 +146,7 @@ public: */ bool CheckPageName(weld::Window* pWin, OUString& rName ); - void SetSlotFilter(bool bEnable = false, o3tl::array_view<sal_uInt16 const> pSIDs = o3tl::array_view<sal_uInt16 const>()) { mbFilterEnable = bEnable; mpFilterSIDs = pSIDs; } + void SetSlotFilter(bool bEnable = false, o3tl::span<sal_uInt16 const> pSIDs = o3tl::span<sal_uInt16 const>()) { mbFilterEnable = bEnable; mpFilterSIDs = pSIDs; } void ApplySlotFilter() const; SfxStyleFamily GetStyleFamily() const { return mnStyleFamily; } @@ -212,7 +212,7 @@ protected: rtl::Reference<FuPoor> mxDocShellFunction; DocumentType const meDocType; SfxStyleFamily mnStyleFamily; - o3tl::array_view<sal_uInt16 const> + o3tl::span<sal_uInt16 const> mpFilterSIDs; bool mbFilterEnable; bool const mbSdDataObj; diff --git a/sfx2/source/control/dispatch.cxx b/sfx2/source/control/dispatch.cxx index 7a0d8a267d84..40c7652c1f42 100644 --- a/sfx2/source/control/dispatch.cxx +++ b/sfx2/source/control/dispatch.cxx @@ -20,6 +20,7 @@ #include <config_features.h> #include <algorithm> +#include <cstddef> #include <deque> #include <vector> @@ -132,7 +133,7 @@ struct SfxDispatcher_Impl SfxSlotFilterState nFilterEnabling; // 1==filter enabled slots, // 2==ReadOnlyDoc overturned - o3tl::array_view<sal_uInt16 const> + o3tl::span<sal_uInt16 const> pFilterSIDs; // sorted Array of SIDs SfxDisableFlags nDisableFlags; bool bFlushed; @@ -1541,11 +1542,11 @@ void SfxDispatcher::FlushImpl() pDisp->SetSlotFilter(); */ void SfxDispatcher::SetSlotFilter(SfxSlotFilterState nEnable, - o3tl::array_view<sal_uInt16 const> pSIDs) + o3tl::span<sal_uInt16 const> pSIDs) { #ifdef DBG_UTIL // Check Array - for ( size_t n = 1; n < pSIDs.size(); ++n ) + for ( std::ptrdiff_t n = 1; n < pSIDs.size(); ++n ) DBG_ASSERT( pSIDs[n] > pSIDs[n-1], "SetSlotFilter: SIDs not sorted" ); #endif diff --git a/solenv/clang-format/blacklist b/solenv/clang-format/blacklist index 0287ee50af5c..87f88d633832 100644 --- a/solenv/clang-format/blacklist +++ b/solenv/clang-format/blacklist @@ -6489,7 +6489,6 @@ include/linguistic/lngprops.hxx include/linguistic/misc.hxx include/linguistic/spelldta.hxx include/o3tl/any.hxx -include/o3tl/array_view.hxx include/o3tl/char16_t2wchar_t.hxx include/o3tl/cow_wrapper.hxx include/o3tl/deleter.hxx @@ -6504,6 +6503,7 @@ include/o3tl/numeric.hxx include/o3tl/runtimetooustring.hxx include/o3tl/safeint.hxx include/o3tl/sorted_vector.hxx +include/o3tl/span.hxx include/o3tl/strong_int.hxx include/o3tl/typed_flags_set.hxx include/o3tl/vector_pool.hxx @@ -8777,12 +8777,12 @@ mysqlc/source/mysqlc_types.cxx mysqlc/source/mysqlc_types.hxx o3tl/qa/cow_wrapper_clients.cxx o3tl/qa/cow_wrapper_clients.hxx -o3tl/qa/test-array_view.cxx o3tl/qa/test-cow_wrapper.cxx o3tl/qa/test-enumarray.cxx o3tl/qa/test-lru_map.cxx o3tl/qa/test-safeint.cxx o3tl/qa/test-sorted_vector.cxx +o3tl/qa/test-span.cxx o3tl/qa/test-string_view.cxx o3tl/qa/test-typed_flags.cxx o3tl/qa/test-vector_pool.cxx |