Cosmetics.
[LibreOffice.git] / include / o3tl / span.hxx
blob1618b86df897df721e78f5354b087de79ac0e78b
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 #if __has_include(<span>)
17 #include <span>
19 namespace o3tl { using std::span; }
21 #else
23 #include <cassert>
24 #include <cstddef>
25 #include <iterator>
27 namespace o3tl {
29 /** A barebones approximation of C++20 <span>.
31 template<typename T>
32 class span {
33 public:
34 using value_type = T;
35 using pointer = value_type *;
36 using const_pointer = value_type const *;
37 using reference = value_type &;
38 using const_reference = value_type const &;
39 using const_iterator = const_pointer;
40 using iterator = pointer;
41 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
42 using reverse_iterator = std::reverse_iterator<iterator>;
43 using index_type = std::size_t;
44 using difference_type = std::ptrdiff_t;
46 constexpr span() noexcept : data_(nullptr), size_(0) {}
48 template<std::size_t N>
49 constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {}
51 constexpr span (T *a, index_type len) noexcept
52 : data_(a), size_(len)
54 // not terribly sure about this, might need to strengthen it
55 assert(a != nullptr || len == 0);
58 constexpr bool empty() const noexcept { return size_ == 0; }
60 constexpr iterator begin() const noexcept { return data_; }
61 constexpr iterator end() const noexcept { return begin() + size(); }
63 constexpr const_iterator cbegin() const noexcept { return begin(); }
64 constexpr const_iterator cend() const noexcept { return end(); }
66 reverse_iterator rbegin() const noexcept
67 { return reverse_iterator(end()); }
68 reverse_iterator rend() const noexcept
69 { return reverse_iterator(begin()); }
71 constexpr const_reverse_iterator crbegin() const noexcept
72 { return rbegin(); }
73 constexpr const_reverse_iterator crend() const noexcept { return rend(); }
75 constexpr index_type size() const noexcept { return size_; }
77 constexpr reference operator [](index_type pos) const {
78 assert(pos < size());
79 return data_[pos];
82 constexpr pointer data() const noexcept { return data_; }
84 private:
85 pointer data_;
86 index_type size_;
89 } // namespace o3tl
91 #endif
93 #endif
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */