mirror of
https://github.com/microsoft/GSL.git
synced 2025-02-27 03:38:03 -05:00
Some checks failed
CI_Android / Android (push) Has been cancelled
cmake_find_package / Build macos-latest (push) Has been cancelled
cmake_find_package / Build ubuntu-latest (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Debug, 23, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 14, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 12) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 20, 14) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 13) (push) Has been cancelled
Compiler Integration Tests / gcc (Release, 23, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Debug, 18, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 16, 23) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 17, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 14) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 17) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 20) (push) Has been cancelled
Compiler Integration Tests / clang (Release, 18, 23) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Debug, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 14, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 17, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 20, 15.4) (push) Has been cancelled
Compiler Integration Tests / xcode (Release, 23, 15.4) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Debug, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 14, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 17, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 16 2019, windows-2019) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 20, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, , Visual Studio 17 2022, windows-2022) (push) Has been cancelled
Compiler Integration Tests / VisualStudio (Release, 23, -T ClangCL, Visual Studio 17 2022, windows-2022) (push) Has been cancelled
CI_iOS / iOS (push) Has been cancelled
* deprecated features adopted into C++ 1) Mark the following GSL features as deprecated: - gsl::unique_ptr (always) - gsl::shared_ptr (always) - gsl::byte (since c++17) - gsl::joining_thread (never implemented) 2) Refactor existing deprecations to use the new GSL_DEPRECATED(msg) macro. 3) Create a section in the README for deprecated features in the standard. * do not deprecate gsl::to_integer because we never claim to implement it. * do not use gsl::byte if it is deprecated
215 lines
6.6 KiB
C++
215 lines
6.6 KiB
C++
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
//
|
|
// This code is licensed under the MIT License (MIT).
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef GSL_SPAN_EXT_H
|
|
#define GSL_SPAN_EXT_H
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// File: span_ext
|
|
// Purpose: continue offering features that have been cut from the official
|
|
// implementation of span.
|
|
// While modernizing gsl::span a number of features needed to be removed to
|
|
// be compliant with the design of std::span
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#include "./assert" // GSL_KERNEL_MODE
|
|
#include "./util" // for narrow_cast, narrow
|
|
|
|
#include <cstddef> // for ptrdiff_t, size_t
|
|
#include <utility>
|
|
|
|
#ifndef GSL_KERNEL_MODE
|
|
#include <algorithm> // for lexicographical_compare
|
|
#endif // GSL_KERNEL_MODE
|
|
|
|
namespace gsl
|
|
{
|
|
|
|
// [span.views.constants], constants
|
|
GSL_INLINE constexpr const std::size_t dynamic_extent = narrow_cast<std::size_t>(-1);
|
|
|
|
template <class ElementType, std::size_t Extent = dynamic_extent>
|
|
class span;
|
|
|
|
// std::equal and std::lexicographical_compare are not /kernel compatible
|
|
// so all comparison operators must be removed for kernel mode.
|
|
#ifndef GSL_KERNEL_MODE
|
|
|
|
// [span.comparison], span comparison operators
|
|
template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
|
|
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
|
|
{
|
|
return std::equal(l.begin(), l.end(), r.begin(), r.end());
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
|
{
|
|
return !(l == r);
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
|
{
|
|
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
|
{
|
|
return !(l > r);
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
|
{
|
|
return r < l;
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
|
|
{
|
|
return !(l < r);
|
|
}
|
|
|
|
#endif // GSL_KERNEL_MODE
|
|
|
|
//
|
|
// make_span() - Utility functions for creating spans
|
|
//
|
|
template <class ElementType>
|
|
constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count)
|
|
{
|
|
return span<ElementType>(ptr, count);
|
|
}
|
|
|
|
template <class ElementType>
|
|
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
|
|
{
|
|
return span<ElementType>(firstElem, lastElem);
|
|
}
|
|
|
|
template <class ElementType, std::size_t N>
|
|
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
|
|
{
|
|
return span<ElementType, N>(arr);
|
|
}
|
|
|
|
template <class Container>
|
|
constexpr span<typename Container::value_type> make_span(Container& cont)
|
|
{
|
|
return span<typename Container::value_type>(cont);
|
|
}
|
|
|
|
template <class Container>
|
|
constexpr span<const typename Container::value_type> make_span(const Container& cont)
|
|
{
|
|
return span<const typename Container::value_type>(cont);
|
|
}
|
|
|
|
template <class Ptr>
|
|
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
|
|
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
|
|
{
|
|
return span<typename Ptr::element_type>(cont, count);
|
|
}
|
|
|
|
template <class Ptr>
|
|
GSL_DEPRECATED("This function is deprecated. See GSL issue #1092.")
|
|
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
|
|
{
|
|
return span<typename Ptr::element_type>(cont);
|
|
}
|
|
|
|
// Specialization of gsl::at for span
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr ElementType& at(span<ElementType, Extent> s, index i)
|
|
{
|
|
// No bounds checking here because it is done in span::operator[] called below
|
|
Ensures(i >= 0);
|
|
return s[narrow_cast<std::size_t>(i)];
|
|
}
|
|
|
|
// [span.obs] Free observer functions
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return static_cast<std::ptrdiff_t>(s.size());
|
|
}
|
|
|
|
// [span.iter] Free functions for begin/end functions
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
begin(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.begin();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent = dynamic_extent>
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
end(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.end();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
rbegin(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.rbegin();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
rend(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.rend();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
cbegin(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.begin();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent = dynamic_extent>
|
|
constexpr typename span<ElementType, Extent>::iterator
|
|
cend(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.end();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
crbegin(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.rbegin();
|
|
}
|
|
|
|
template <class ElementType, std::size_t Extent>
|
|
constexpr typename span<ElementType, Extent>::reverse_iterator
|
|
crend(const span<ElementType, Extent>& s) noexcept
|
|
{
|
|
return s.rend();
|
|
}
|
|
|
|
} // namespace gsl
|
|
|
|
#endif // GSL_SPAN_EXT_H
|