Kaydet (Commit) 73df1aea authored tarafından Stephan Bergmann's avatar Stephan Bergmann

array_view was changed to span in upcoming C++20

...see <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0122r7.pdf>
"span: bounds-safe views for sequences of objects".  o3tl::span is still an
incomplete approximation of std::span; removed those o3tl::array_view members
that are not present in std::span (and were not used in the code).

Relies on C++17 __has_include to use standard <span> where available (e.g., in
LLVM 7 libc++).

Change-Id: I82a7e246b61b2456fa6183025d25eec4121ad3c9
Reviewed-on: https://gerrit.libreoffice.org/66215
Tested-by: Jenkins
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst c3e9d393
......@@ -7,38 +7,29 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_O3TL_ARRAY_VIEW_HXX
#define INCLUDED_O3TL_ARRAY_VIEW_HXX
#ifndef INCLUDED_O3TL_SPAN_HXX
#define INCLUDED_O3TL_SPAN_HXX
#include <sal/config.h>
#include <algorithm>
#if __has_include(<span>)
#include <span>
namespace o3tl { using std::span; }
#else
#include <cassert>
#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.
/** A barebones approximation of C++20 <span>.
*/
template<typename T>
class array_view {
friend class array_view<T const>;
class span {
public:
using value_type = T;
using pointer = value_type *;
......@@ -49,32 +40,19 @@ public:
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 index_type = std::ptrdiff_t;
using difference_type = std::ptrdiff_t;
static constexpr size_type npos = size_type(-1);
constexpr array_view() noexcept : data_(nullptr), size_(0) {}
constexpr span() noexcept : data_(nullptr), size_(0) {}
template<size_type N>
constexpr array_view (T (&a)[N]) noexcept : data_(a), size_(N) {}
template<std::size_t N>
constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {}
constexpr array_view (T *a, size_type len) noexcept
constexpr span (T *a, index_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;
// 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; }
......@@ -94,69 +72,23 @@ public:
{ 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 index_type size() const noexcept { return size_; }
constexpr reference operator [](size_type pos) const {
assert(pos < size());
constexpr reference operator [](index_type pos) const {
assert(0 <= pos && 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_;
index_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
#endif
......
......@@ -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;
......
......@@ -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 \
))
......
......@@ -9,13 +9,13 @@
#include <sal/config.h>
#include <stdexcept>
#include <utility>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <o3tl/array_view.hxx>
#include <o3tl/span.hxx>
namespace {
......@@ -28,52 +28,32 @@ private:
void testOperations() {
int const some_data[] { 1, 2, 3 };
o3tl::array_view<int const> v(some_data);
o3tl::span<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());
o3tl::span<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());
o3tl::span<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());
o3tl::span<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());
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]);
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=
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());
}
}
};
......
......@@ -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;
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment