diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-09-06 10:25:54 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2017-09-06 15:18:33 +0200 |
commit | 4d830ab33c75dc07d77796d422d909c235c2c127 (patch) | |
tree | 40c3fcb363617246dbb5d92c0b94bc5e3e40acdf | |
parent | bc3e0121b47cc601575b0a49f6ba4959130cf96e (diff) |
const correctness in o3tl::array_view
Change-Id: I44c1ace97ae44069c5a0c6a247aa8a0b49896ad3
Reviewed-on: https://gerrit.libreoffice.org/41985
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | include/o3tl/array_view.hxx | 65 | ||||
-rw-r--r-- | include/sfx2/dispatch.hxx | 2 | ||||
-rw-r--r-- | o3tl/qa/test-array_view.cxx | 12 | ||||
-rw-r--r-- | sd/source/ui/inc/DrawDocShell.hxx | 4 | ||||
-rw-r--r-- | sfx2/source/control/dispatch.cxx | 4 |
5 files changed, 59 insertions, 28 deletions
diff --git a/include/o3tl/array_view.hxx b/include/o3tl/array_view.hxx index 9da86866680e..bb4818a4c8a0 100644 --- a/include/o3tl/array_view.hxx +++ b/include/o3tl/array_view.hxx @@ -27,8 +27,6 @@ #include <rtl/ustring.hxx> #include <sal/types.h> -// A barebones approximation of C++17(?) <array_view>, haven't bothered with more than single-dimensional arrays - #if HAVE_CXX14_CONSTEXPR #define CONSTEXPR constexpr #else @@ -42,8 +40,12 @@ namespace o3tl { #pragma warning(disable: 4814) // in C++14 'constexpr' will not imply 'const' #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 *; @@ -51,9 +53,9 @@ public: using reference = value_type &; using const_reference = value_type const &; using const_iterator = const_pointer; - using iterator = const_iterator; + using iterator = pointer; using const_reverse_iterator = std::reverse_iterator<const_iterator>; - using reverse_iterator = const_reverse_iterator; + using reverse_iterator = std::reverse_iterator<iterator>; using size_type = std::size_t; using difference_type = std::ptrdiff_t; @@ -62,9 +64,9 @@ public: constexpr array_view() noexcept : data_(nullptr), size_(0) {} template<size_type N> - CONSTEXPR array_view (T const (&a)[N]) noexcept : data_(a), size_(N) {} + CONSTEXPR array_view (T (&a)[N]) noexcept : data_(a), size_(N) {} - CONSTEXPR array_view (T const *a, size_type len) noexcept + CONSTEXPR array_view (T *a, size_type len) noexcept : data_(a), size_(len) { #if HAVE_CXX14_CONSTEXPR @@ -73,18 +75,36 @@ public: #endif } - constexpr bool empty() const noexcept { return size_ == 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 const_iterator begin() const noexcept { return data_; } - constexpr const_iterator end() const noexcept { return begin() + size(); } + array_view& operator=(array_view 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(); } - constexpr const_reverse_iterator rbegin() const noexcept - { return const_reverse_iterator(end()); } - constexpr const_reverse_iterator rend() const noexcept - { return const_reverse_iterator(begin()); } + 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(); } @@ -103,7 +123,7 @@ public: return npos - 1; } - constexpr const_reference operator [](size_type pos) const { + constexpr reference operator [](size_type pos) const { #if HAVE_CXX14_CONSTEXPR assert(pos < size()); #endif @@ -111,37 +131,42 @@ public: } CONSTEXPR - const_reference at(size_type pos) const { + reference at(size_type pos) const { if (pos >= size()) { throw std::out_of_range("o3tl::array_view::at"); } return operator [](pos); } - constexpr const_reference front() const { + constexpr reference front() const { #if HAVE_CXX14_CONSTEXPR assert(!empty()); #endif return operator [](0); } - constexpr const_reference back() const { + constexpr reference back() const { #if HAVE_CXX14_CONSTEXPR assert(!empty()); #endif return operator [](size() - 1); } - constexpr const_pointer data() const noexcept { return data_; } + 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: - const_pointer data_; - size_type size_; + pointer data_; + size_type size_; }; diff --git a/include/sfx2/dispatch.hxx b/include/sfx2/dispatch.hxx index 43776a86b659..2c41b9ae98bb 100644 --- a/include/sfx2/dispatch.hxx +++ b/include/sfx2/dispatch.hxx @@ -160,7 +160,7 @@ public: void Lock( bool bLock ); bool IsLocked() const; void SetSlotFilter( SfxSlotFilterState nEnable = SfxSlotFilterState::DISABLED, - o3tl::array_view<sal_uInt16> pSIDs = o3tl::array_view<sal_uInt16>()); + o3tl::array_view<sal_uInt16 const> pSIDs = o3tl::array_view<sal_uInt16 const>()); void HideUI( bool bHide = true ); ToolbarId GetObjectBarId( sal_uInt16 nPos ) const; diff --git a/o3tl/qa/test-array_view.cxx b/o3tl/qa/test-array_view.cxx index ab97d7de49c5..b82aa8cd1bf4 100644 --- a/o3tl/qa/test-array_view.cxx +++ b/o3tl/qa/test-array_view.cxx @@ -28,7 +28,7 @@ private: void testOperations() { int const some_data[] { 1, 2, 3 }; - o3tl::array_view<int> v(some_data); + o3tl::array_view<int const> v(some_data); CPPUNIT_ASSERT_EQUAL(1, *v.begin()); CPPUNIT_ASSERT_EQUAL( @@ -63,12 +63,18 @@ private: { int const d1[] { 1, 2 }; int const d2[] { 3, 4, 5, 6 }; - o3tl::array_view<int> v1( d1 ); - o3tl::array_view<int> v2( d2 ); + 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= + } } }; diff --git a/sd/source/ui/inc/DrawDocShell.hxx b/sd/source/ui/inc/DrawDocShell.hxx index ecfb545be2fd..3c38e0847af2 100644 --- a/sd/source/ui/inc/DrawDocShell.hxx +++ b/sd/source/ui/inc/DrawDocShell.hxx @@ -150,7 +150,7 @@ public: */ bool CheckPageName(vcl::Window* pWin, OUString& rName ); - void SetSlotFilter(bool bEnable = false, o3tl::array_view<sal_uInt16> pSIDs = o3tl::array_view<sal_uInt16>()) { mbFilterEnable = bEnable; mpFilterSIDs = pSIDs; } + void SetSlotFilter(bool bEnable = false, o3tl::array_view<sal_uInt16 const> pSIDs = o3tl::array_view<sal_uInt16 const>()) { mbFilterEnable = bEnable; mpFilterSIDs = pSIDs; } void ApplySlotFilter() const; SfxStyleFamily GetStyleFamily() const { return mnStyleFamily; } @@ -216,7 +216,7 @@ protected: rtl::Reference<FuPoor> mxDocShellFunction; DocumentType meDocType; SfxStyleFamily mnStyleFamily; - o3tl::array_view<sal_uInt16> + o3tl::array_view<sal_uInt16 const> mpFilterSIDs; bool mbFilterEnable; bool mbSdDataObj; diff --git a/sfx2/source/control/dispatch.cxx b/sfx2/source/control/dispatch.cxx index 1cafc6d351d0..20f4de86914e 100644 --- a/sfx2/source/control/dispatch.cxx +++ b/sfx2/source/control/dispatch.cxx @@ -139,7 +139,7 @@ struct SfxDispatcher_Impl SfxSlotFilterState nFilterEnabling; // 1==filter enabled slots, // 2==ReadOnlyDoc overturned - o3tl::array_view<sal_uInt16> + o3tl::array_view<sal_uInt16 const> pFilterSIDs; // sorted Array of SIDs SfxDisableFlags nDisableFlags; bool bFlushed; @@ -1614,7 +1614,7 @@ void SfxDispatcher::FlushImpl() pDisp->SetSlotFilter(); */ void SfxDispatcher::SetSlotFilter(SfxSlotFilterState nEnable, - o3tl::array_view<sal_uInt16> pSIDs) + o3tl::array_view<sal_uInt16 const> pSIDs) { #ifdef DBG_UTIL // Check Array |