Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / o3tl / span.hxx
blobcfe4c1ffa3a37f1cd6c31245d7a6890dcbd66c74
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_O3TL_SPAN_HXX
11 #define INCLUDED_O3TL_SPAN_HXX
13 #include <sal/config.h>
15 #include <config_global.h>
17 #if HAVE_CPP_SPAN
19 #include <span>
21 namespace o3tl { using std::span; }
23 #else
25 #include <cassert>
26 #include <cstddef>
27 #include <iterator>
28 #include <type_traits>
29 #include <vector>
31 namespace o3tl {
33 /** A barebones approximation of C++20 <span>.
35 template<typename T>
36 class span {
37 public:
38 using value_type = T;
39 using pointer = value_type *;
40 using const_pointer = value_type const *;
41 using reference = value_type &;
42 using const_reference = value_type const &;
43 using const_iterator = const_pointer;
44 using iterator = pointer;
45 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
46 using reverse_iterator = std::reverse_iterator<iterator>;
47 using size_type = std::size_t;
48 using difference_type = std::ptrdiff_t;
50 constexpr span() noexcept : data_(nullptr), size_(0) {}
52 template<std::size_t N>
53 constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {}
55 constexpr span (T *a, size_type len) noexcept
56 : data_(a), size_(len)
58 // not terribly sure about this, might need to strengthen it
59 assert(a != nullptr || len == 0);
62 template<typename T2>
63 constexpr span(const std::vector<T2>& vec)
64 : data_(vec.data()), size_(vec.size()) {}
66 /** for assigning from span<T> to span<const T> */
67 constexpr span (const span<typename std::remove_const<T>::type>& other) noexcept
68 : data_(other.data()), size_(other.size()) {}
70 constexpr bool empty() const noexcept { return size_ == 0; }
72 constexpr iterator begin() const noexcept { return data_; }
73 constexpr iterator end() const noexcept { return begin() + size(); }
75 reverse_iterator rbegin() const noexcept
76 { return reverse_iterator(end()); }
77 reverse_iterator rend() const noexcept
78 { return reverse_iterator(begin()); }
80 constexpr size_type size() const noexcept { return size_; }
82 constexpr reference operator [](size_type pos) const {
83 assert(pos < size());
84 return data_[pos];
87 constexpr pointer data() const noexcept { return data_; }
89 private:
90 pointer data_;
91 size_type size_;
94 } // namespace o3tl
96 #endif
98 #endif
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */