From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- include/o3tl/span.hxx | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 include/o3tl/span.hxx (limited to 'include/o3tl/span.hxx') diff --git a/include/o3tl/span.hxx b/include/o3tl/span.hxx new file mode 100644 index 000000000..cfe4c1ffa --- /dev/null +++ b/include/o3tl/span.hxx @@ -0,0 +1,100 @@ +/* -*- 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 + +#include + +#if HAVE_CPP_SPAN + +#include + +namespace o3tl { using std::span; } + +#else + +#include +#include +#include +#include +#include + +namespace o3tl { + +/** A barebones approximation of C++20 . +*/ +template +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; + using reverse_iterator = std::reverse_iterator; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + + constexpr span() noexcept : data_(nullptr), size_(0) {} + + template + constexpr span (T (&a)[N]) noexcept : data_(a), size_(N) {} + + constexpr span (T *a, size_type len) noexcept + : data_(a), size_(len) + { + // not terribly sure about this, might need to strengthen it + assert(a != nullptr || len == 0); + } + + template + constexpr span(const std::vector& vec) + : data_(vec.data()), size_(vec.size()) {} + + /** for assigning from span to span */ + constexpr span (const span::type>& other) noexcept + : data_(other.data()), size_(other.size()) {} + + constexpr bool empty() const noexcept { return size_ == 0; } + + constexpr iterator begin() const noexcept { return data_; } + constexpr iterator end() const noexcept { return begin() + size(); } + + reverse_iterator rbegin() const noexcept + { return reverse_iterator(end()); } + reverse_iterator rend() const noexcept + { return reverse_iterator(begin()); } + + constexpr size_type size() const noexcept { return size_; } + + constexpr reference operator [](size_type pos) const { + assert(pos < size()); + return data_[pos]; + } + + constexpr pointer data() const noexcept { return data_; } + +private: + pointer data_; + size_type size_; +}; + +} // namespace o3tl + +#endif + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit v1.2.3