diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-01-12 15:58:00 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-01-13 11:27:30 +0100 |
commit | 73df1aea4f8a779241c053ab54f616f7b3a6dcb9 (patch) | |
tree | 4692637c56cdf2b9e1c2a949503ccd26a0fb3ca5 /include/o3tl | |
parent | c3e9d393ea10da48e8b9ab4742dbc5277b7c4882 (diff) |
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: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/o3tl')
-rw-r--r-- | include/o3tl/array_view.hxx | 163 | ||||
-rw-r--r-- | include/o3tl/span.hxx | 95 |
2 files changed, 95 insertions, 163 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: */ |