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
21namespace o3tl { using std::span; }
22
23#else
24
25#include <cassert>
26#include <cstddef>
27#include <iterator>
28#include <type_traits>
29#include <vector>
30
31namespace o3tl {
32
35template<typename T>
36class span {
37public:
38 using value_type = T;
40 using const_pointer = value_type const *;
42 using const_reference = value_type const &;
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;
49
50 constexpr span() noexcept : data_(nullptr), size_(0) {}
51
52 template<std::size_t N>
53 constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {}
54
55 constexpr span (T *a, size_type len) noexcept
56 : data_(a), size_(len)
57 {
58 // not terribly sure about this, might need to strengthen it
59 assert(a != nullptr || len == 0);
60 }
61
62 template<typename T2>
63 constexpr span(const std::vector<T2>& vec)
64 : data_(vec.data()), size_(vec.size()) {}
65
67 constexpr span (const span<typename std::remove_const<T>::type>& other) noexcept
68 : data_(other.data()), size_(other.size()) {}
69
70 constexpr bool empty() const noexcept { return size_ == 0; }
71
72 constexpr iterator begin() const noexcept { return data_; }
73 constexpr iterator end() const noexcept { return begin() + size(); }
74
75 reverse_iterator rbegin() const noexcept
76 { return reverse_iterator(end()); }
77 reverse_iterator rend() const noexcept
78 { return reverse_iterator(begin()); }
79
80 constexpr size_type size() const noexcept { return size_; }
81
82 constexpr reference operator [](size_type pos) const {
83 assert(pos < size());
84 return data_[pos];
85 }
86
87 constexpr pointer data() const noexcept { return data_; }
88
89private:
92};
93
94} // namespace o3tl
95
96#endif
97
98#endif
99
100/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
A barebones approximation of C++20 .
Definition: span.hxx:36
constexpr iterator begin() const noexcept
Definition: span.hxx:72
value_type const * const_pointer
Definition: span.hxx:40
constexpr span(const std::vector< T2 > &vec)
Definition: span.hxx:63
value_type & reference
Definition: span.hxx:41
reverse_iterator rbegin() const noexcept
Definition: span.hxx:75
const_pointer const_iterator
Definition: span.hxx:43
constexpr iterator end() const noexcept
Definition: span.hxx:73
constexpr span() noexcept
Definition: span.hxx:50
value_type * pointer
Definition: span.hxx:39
constexpr size_type size() const noexcept
Definition: span.hxx:80
pointer data_
Definition: span.hxx:90
std::ptrdiff_t difference_type
Definition: span.hxx:48
constexpr span(T(&a)[N]) noexcept
Definition: span.hxx:53
constexpr pointer data() const noexcept
Definition: span.hxx:87
constexpr span(T *a, size_type len) noexcept
Definition: span.hxx:55
reverse_iterator rend() const noexcept
Definition: span.hxx:77
size_type size_
Definition: span.hxx:91
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: span.hxx:45
constexpr bool empty() const noexcept
Definition: span.hxx:70
value_type const & const_reference
Definition: span.hxx:42
constexpr reference operator[](size_type pos) const
Definition: span.hxx:82
constexpr span(const span< typename std::remove_const< T >::type > &other) noexcept
for assigning from span<T> to span<const T>
Definition: span.hxx:67
T value_type
Definition: span.hxx:38
std::reverse_iterator< iterator > reverse_iterator
Definition: span.hxx:46
std::size_t size_type
Definition: span.hxx:47
pointer iterator
Definition: span.hxx:44
uno_Any a
size_t pos