LibreOffice Module o3tl (master)  1
span.hxx
Go to the documentation of this file.
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.
4  *
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  */
9 
10 #ifndef INCLUDED_O3TL_SPAN_HXX
11 #define INCLUDED_O3TL_SPAN_HXX
12 
13 #include <sal/config.h>
14 
15 #include <config_global.h>
16 
17 #if HAVE_CPP_SPAN
18 
19 #include <span>
20 
21 namespace o3tl { using std::span; }
22 
23 #else
24 
25 #include <cassert>
26 #include <cstddef>
27 #include <iterator>
28 
29 namespace o3tl {
30 
33 template<typename T>
34 class span {
35 public:
36  using value_type = T;
37  using pointer = value_type *;
38  using const_pointer = value_type const *;
39  using reference = value_type &;
40  using const_reference = value_type const &;
42  using iterator = pointer;
43  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
44  using reverse_iterator = std::reverse_iterator<iterator>;
45  using size_type = std::size_t;
46  using difference_type = std::ptrdiff_t;
47 
48  constexpr span() noexcept : data_(nullptr), size_(0) {}
49 
50  template<std::size_t N>
51  constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {}
52 
53  constexpr span (T *a, size_type len) noexcept
54  : data_(a), size_(len)
55  {
56  // not terribly sure about this, might need to strengthen it
57  assert(a != nullptr || len == 0);
58  }
59 
60  constexpr bool empty() const noexcept { return size_ == 0; }
61 
62  constexpr iterator begin() const noexcept { return data_; }
63  constexpr iterator end() const noexcept { return begin() + size(); }
64 
65  reverse_iterator rbegin() const noexcept
66  { return reverse_iterator(end()); }
67  reverse_iterator rend() const noexcept
68  { return reverse_iterator(begin()); }
69 
70  constexpr size_type size() const noexcept { return size_; }
71 
72  constexpr reference operator [](size_type pos) const {
73  assert(pos < size());
74  return data_[pos];
75  }
76 
77  constexpr pointer data() const noexcept { return data_; }
78 
79 private:
82 };
83 
84 } // namespace o3tl
85 
86 #endif
87 
88 #endif
89 
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
constexpr bool empty() const noexcept
Definition: span.hxx:60
constexpr span() noexcept
Definition: span.hxx:48
const_pointer const_iterator
Definition: span.hxx:41
value_type const & const_reference
Definition: span.hxx:40
constexpr size_type size() const noexcept
Definition: span.hxx:70
size_type size_
Definition: span.hxx:81
std::reverse_iterator< iterator > reverse_iterator
Definition: span.hxx:44
T value_type
Definition: span.hxx:36
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: span.hxx:43
constexpr iterator begin() const noexcept
Definition: span.hxx:62
uno_Any a
reverse_iterator rend() const noexcept
Definition: span.hxx:67
A barebones approximation of C++20 .
Definition: span.hxx:34
reverse_iterator rbegin() const noexcept
Definition: span.hxx:65
value_type const * const_pointer
Definition: span.hxx:38
constexpr iterator end() const noexcept
Definition: span.hxx:63
pointer data_
Definition: span.hxx:80
constexpr span(T *a, size_type len) noexcept
Definition: span.hxx:53
constexpr pointer data() const noexcept
Definition: span.hxx:77
constexpr reference operator[](size_type pos) const
Definition: span.hxx:72
constexpr span(T(&a)[N]) noexcept
Definition: span.hxx:51
std::ptrdiff_t difference_type
Definition: span.hxx:46