1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
// Try to use either `std::span` or `gsl::span`
#if defined(OPENTELEMETRY_STL_VERSION)
# if OPENTELEMETRY_STL_VERSION >= 2020
# include <array>
# include <cstddef>
# include <iterator>
# include <type_traits>
/**
* @brief Clang 14.0.0 with libc++ do not support implicitly construct a span
* for a range. We just use our fallback version.
*
*/
# if !defined(OPENTELEMETRY_OPTION_USE_STD_SPAN) && defined(_LIBCPP_VERSION)
# if _LIBCPP_VERSION <= 14000
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 0
# endif
# endif
# ifndef OPENTELEMETRY_OPTION_USE_STD_SPAN
# define OPENTELEMETRY_OPTION_USE_STD_SPAN 1
# endif
# if OPENTELEMETRY_OPTION_USE_STD_SPAN
# include "opentelemetry/std/span.h"
# endif
# endif /* OPENTELEMETRY_STL_VERSION >= 2020 */
#endif /* OPENTELEMETRY_STL_VERSION */
// Fallback to `nostd::span` if necessary
#if !defined(OPENTELEMETRY_HAVE_SPAN)
# include <array>
# include <cassert>
# include <cstddef>
# include <exception>
# include <iterator>
# include <type_traits>
# include "opentelemetry/nostd/utility.h"
# include "opentelemetry/version.h"
OPENTELEMETRY_BEGIN_NAMESPACE
namespace nostd
{
constexpr size_t dynamic_extent = static_cast<size_t>(-1);
template <class T, size_t Extent = dynamic_extent>
class span;
namespace detail
{
/**
* Helper class to resolve overloaded constructors
*/
template <class T>
struct is_specialized_span_convertible : std::false_type
{};
template <class T, size_t N>
struct is_specialized_span_convertible<std::array<T, N>> : std::true_type
{};
template <class T, size_t N>
struct is_specialized_span_convertible<T[N]> : std::true_type
{};
template <class T, size_t Extent>
struct is_specialized_span_convertible<span<T, Extent>> : std::true_type
{};
} // namespace detail
/**
* Back port of std::span.
*
* See https://en.cppreference.com/w/cpp/container/span for interface documentation.
*
* Note: This provides a subset of the methods available on std::span.
*
* Note: The std::span API specifies error cases to have undefined behavior, so this implementation
* chooses to terminate or assert rather than throw exceptions.
*/
template <class T, size_t Extent>
class span
{
public:
static constexpr size_t extent = Extent;
// This arcane code is how we make default-construction result in an SFINAE error
// with C++11 when Extent != 0 as specified by the std::span API.
//
// See https://stackoverflow.com/a/10309720/4447365
template <bool B = Extent == 0, typename std::enable_if<B>::type * = nullptr>
span() noexcept : data_{nullptr}
{}
span(T *data, size_t count) noexcept : data_{data}
{
if (count != Extent)
{
std::terminate();
}
}
span(T *first, T *last) noexcept : data_{first}
{
if (std::distance(first, last) != Extent)
{
std::terminate();
}
}
template <size_t N, typename std::enable_if<Extent == N>::type * = nullptr>
span(T (&array)[N]) noexcept : data_{array}
{}
template <size_t N, typename std::enable_if<Extent == N>::type * = nullptr>
span(std::array<T, N> &array) noexcept : data_{array.data()}
{}
template <size_t N, typename std::enable_if<Extent == N>::type * = nullptr>
span(const std::array<T, N> &array) noexcept : data_{array.data()}
{}
template <
class C,
typename std::enable_if<!detail::is_specialized_span_convertible<C>::value &&
std::is_convertible<typename std::remove_pointer<decltype(nostd::data(
std::declval<C &>()))>::type (*)[],
T (*)[]>::value &&
std::is_convertible<decltype(nostd::size(std::declval<const C &>())),
size_t>::value>::type * = nullptr>
span(C &c) noexcept(noexcept(nostd::data(c), nostd::size(c))) : data_{nostd::data(c)}
{
if (nostd::size(c) != Extent)
{
std::terminate();
}
}
template <
class C,
typename std::enable_if<!detail::is_specialized_span_convertible<C>::value &&
std::is_convertible<typename std::remove_pointer<decltype(nostd::data(
std::declval<const C &>()))>::type (*)[],
T (*)[]>::value &&
std::is_convertible<decltype(nostd::size(std::declval<const C &>())),
size_t>::value>::type * = nullptr>
span(const C &c) noexcept(noexcept(nostd::data(c), nostd::size(c))) : data_{nostd::data(c)}
{
if (nostd::size(c) != Extent)
{
std::terminate();
}
}
template <class U,
size_t N,
typename std::enable_if<N == Extent &&
std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
span(const span<U, N> &other) noexcept : data_{other.data()}
{}
span(const span &) noexcept = default;
span &operator=(const span &) noexcept = default;
bool empty() const noexcept { return Extent == 0; }
T *data() const noexcept { return data_; }
size_t size() const noexcept { return Extent; }
T &operator[](size_t index) const noexcept
{
assert(index < Extent);
return data_[index];
}
T *begin() const noexcept { return data_; }
T *end() const noexcept { return data_ + Extent; }
private:
T *data_;
};
template <class T>
class span<T, dynamic_extent>
{
public:
static constexpr size_t extent = dynamic_extent;
span() noexcept : extent_{0}, data_{nullptr} {}
span(T *data, size_t count) noexcept : extent_{count}, data_{data} {}
span(T *first, T *last) noexcept
: extent_{static_cast<size_t>(std::distance(first, last))}, data_{first}
{
assert(first <= last);
}
template <size_t N>
span(T (&array)[N]) noexcept : extent_{N}, data_{array}
{}
template <size_t N>
span(std::array<T, N> &array) noexcept : extent_{N}, data_{array.data()}
{}
template <size_t N>
span(const std::array<T, N> &array) noexcept : extent_{N}, data_{array.data()}
{}
template <
class C,
typename std::enable_if<!detail::is_specialized_span_convertible<C>::value &&
std::is_convertible<typename std::remove_pointer<decltype(nostd::data(
std::declval<C &>()))>::type (*)[],
T (*)[]>::value &&
std::is_convertible<decltype(nostd::size(std::declval<const C &>())),
size_t>::value>::type * = nullptr>
span(C &c) noexcept(noexcept(nostd::data(c), nostd::size(c)))
: extent_{nostd::size(c)}, data_{nostd::data(c)}
{}
template <
class C,
typename std::enable_if<!detail::is_specialized_span_convertible<C>::value &&
std::is_convertible<typename std::remove_pointer<decltype(nostd::data(
std::declval<const C &>()))>::type (*)[],
T (*)[]>::value &&
std::is_convertible<decltype(nostd::size(std::declval<const C &>())),
size_t>::value>::type * = nullptr>
span(const C &c) noexcept(noexcept(nostd::data(c), nostd::size(c)))
: extent_{nostd::size(c)}, data_{nostd::data(c)}
{}
template <class U,
size_t N,
typename std::enable_if<std::is_convertible<U (*)[], T (*)[]>::value>::type * = nullptr>
span(const span<U, N> &other) noexcept : extent_{other.size()}, data_{other.data()}
{}
span(const span &) noexcept = default;
span &operator=(const span &) noexcept = default;
bool empty() const noexcept { return extent_ == 0; }
T *data() const noexcept { return data_; }
size_t size() const noexcept { return extent_; }
T &operator[](size_t index) const noexcept
{
assert(index < extent_);
return data_[index];
}
T *begin() const noexcept { return data_; }
T *end() const noexcept { return data_ + extent_; }
private:
size_t extent_;
T *data_;
};
} // namespace nostd
OPENTELEMETRY_END_NAMESPACE
#endif
|