Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (c) 2015 Microsoft Corporation. All rights reserved. |
| 4 | // |
| 5 | // This code is licensed under the MIT License (MIT). |
| 6 | // |
| 7 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 8 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 10 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 11 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 12 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 13 | // THE SOFTWARE. |
| 14 | // |
| 15 | /////////////////////////////////////////////////////////////////////////////// |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #ifndef GSL_SPAN_H |
| 20 | #define GSL_SPAN_H |
| 21 | |
| 22 | #include "gsl_assert.h" |
| 23 | #include "gsl_util.h" |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 24 | #include <array> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 25 | #include <limits> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 26 | #include <iterator> |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 27 | #include <stdexcept> |
| 28 | #include <type_traits> |
| 29 | #include <utility> |
| 30 | |
| 31 | #ifdef _MSC_VER |
| 32 | |
| 33 | // turn off some warnings that are noisy about our Expects statements |
| 34 | #pragma warning(push) |
| 35 | #pragma warning(disable : 4127) // conditional expression is constant |
| 36 | |
| 37 | // No MSVC does constexpr fully yet |
| 38 | #pragma push_macro("constexpr") |
| 39 | #define constexpr |
| 40 | |
| 41 | // VS 2013 workarounds |
| 42 | #if _MSC_VER <= 1800 |
| 43 | |
| 44 | #define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 45 | #define GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT |
| 46 | |
| 47 | // noexcept is not understood |
| 48 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 49 | #pragma push_macro("noexcept") |
| 50 | #define noexcept /* nothing */ |
| 51 | #endif |
| 52 | |
| 53 | // turn off some misguided warnings |
| 54 | #pragma warning(push) |
| 55 | #pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
| 56 | #pragma warning(disable : 4512) // warns that assignment op could not be generated |
| 57 | |
| 58 | #endif // _MSC_VER <= 1800 |
| 59 | |
| 60 | #endif // _MSC_VER |
| 61 | |
| 62 | #ifdef GSL_THROW_ON_CONTRACT_VIOLATION |
| 63 | |
| 64 | #ifdef _MSC_VER |
| 65 | #pragma push_macro("noexcept") |
| 66 | #endif |
| 67 | |
| 68 | #define noexcept /* nothing */ |
| 69 | |
| 70 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 71 | |
| 72 | namespace gsl |
| 73 | { |
| 74 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 75 | template <class ElementType, std::ptrdiff_t Extent = dynamic_extent> |
| 76 | class span; |
| 77 | |
| 78 | |
| 79 | namespace details |
| 80 | { |
| 81 | template <class T> |
| 82 | struct is_span_oracle : std::false_type |
| 83 | { |
| 84 | }; |
| 85 | |
| 86 | template <class ElementType, std::ptrdiff_t Extent> |
| 87 | struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type |
| 88 | { |
| 89 | }; |
| 90 | |
| 91 | template <class T> |
| 92 | struct is_span : is_span_oracle<std::remove_cv_t<T>> |
| 93 | { |
| 94 | }; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 95 | |
| 96 | template <class From, class To> |
| 97 | struct is_allowed_pointer_conversion |
| 98 | : std::integral_constant<bool, |
| 99 | std::is_pointer<From>::value && |
| 100 | std::is_pointer<To>::value && |
| 101 | std::is_convertible<From, To>::value |
| 102 | > |
| 103 | { |
| 104 | }; |
| 105 | |
| 106 | template <class From, class To> |
| 107 | struct is_allowed_integral_conversion |
| 108 | : std::integral_constant<bool, |
| 109 | std::is_integral<From>::value && |
| 110 | std::is_integral<To>::value && |
| 111 | sizeof(From) == sizeof(To) && |
| 112 | alignof(From) == alignof(To) && |
| 113 | std::is_convertible<From, To>::value |
| 114 | > |
| 115 | { |
| 116 | }; |
| 117 | |
| 118 | template <class From, class To> |
| 119 | struct is_allowed_element_type_conversion |
| 120 | : std::integral_constant<bool, |
| 121 | std::is_same<From, std::remove_cv_t<To>>::value || |
| 122 | is_allowed_pointer_conversion<From, To>::value || |
| 123 | is_allowed_integral_conversion<From, To>::value |
| 124 | > |
| 125 | { |
| 126 | }; |
| 127 | |
| 128 | template <class From> |
| 129 | struct is_allowed_element_type_conversion<From, char> |
| 130 | : std::integral_constant<bool, !std::is_const<From>::value> |
| 131 | { |
| 132 | }; |
| 133 | |
| 134 | template <class From> |
| 135 | struct is_allowed_element_type_conversion<From, const char> |
| 136 | : std::integral_constant<bool, true> |
| 137 | { |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 142 | } // namespace details |
| 143 | |
| 144 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 145 | // [views.constants], constants |
| 146 | constexpr const std::ptrdiff_t dynamic_extent = -1; |
| 147 | |
| 148 | |
| 149 | // [span], class template span |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 150 | template <class ElementType, std::ptrdiff_t Extent> |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 151 | class span { |
| 152 | public: |
| 153 | // constants and types |
| 154 | using element_type = ElementType; |
| 155 | using index_type = std::ptrdiff_t; |
| 156 | using pointer = element_type*; |
| 157 | using reference = element_type&; |
| 158 | #if 0 // TODO |
| 159 | using iterator = /*implementation-defined */; |
| 160 | using const_iterator = /* implementation-defined */; |
| 161 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 162 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 163 | #endif |
| 164 | constexpr static const index_type extent = Extent; |
| 165 | |
| 166 | // [span.cons], span constructors, copy, assignment, and destructor |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 167 | constexpr span() noexcept : storage_(nullptr, extent_type<0>()) |
| 168 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 169 | |
| 170 | constexpr span(nullptr_t) noexcept : span() |
| 171 | {} |
| 172 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 173 | constexpr span(pointer ptr, index_type count) : storage_(ptr, count) |
| 174 | { Expects(((!ptr && count == 0) || (ptr && count >= 0))); } |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 175 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 176 | constexpr span(pointer firstElem, pointer lastElem) |
| 177 | : storage_(firstElem, std::distance(firstElem, lastElem)) |
| 178 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 179 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 180 | template <size_t N> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 181 | constexpr span(element_type(&arr)[N]) |
| 182 | : storage_(&arr[0], extent_type<N>()) |
| 183 | {} |
| 184 | |
| 185 | template <size_t N> |
| 186 | constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) |
| 187 | : storage_(&arr[0], extent_type<N>()) |
| 188 | {} |
| 189 | |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame^] | 190 | template <size_t N, class = std::enable_if_t<is_const<element_type>::value>> |
Neil MacIntosh | f61a9bb | 2016-02-29 13:16:48 -0800 | [diff] [blame] | 191 | constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) |
| 192 | : storage_(&arr[0], extent_type<N>()) |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 193 | {} |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 194 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 195 | // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement |
| 196 | // on Container to be a contiguous sequence container. |
| 197 | template <class Container, |
| 198 | class = std::enable_if_t<!details::is_span<Container>::value && |
| 199 | std::is_convertible<Container::pointer, pointer>::value && |
| 200 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 201 | > |
| 202 | constexpr span(Container& cont) : span(cont.data(), cont.size()) {} |
| 203 | |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 204 | template <class Container, |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame^] | 205 | class = std::enable_if_t<std::is_const<element_type>::value && |
| 206 | !details::is_span<Container>::value && |
Neil MacIntosh | c40094a | 2016-03-01 12:11:41 -0800 | [diff] [blame] | 207 | std::is_convertible<Container::pointer, pointer>::value && |
| 208 | std::is_convertible<Container::pointer, decltype(std::declval<Container>().data())>::value> |
| 209 | > |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame^] | 210 | constexpr span(const Container& cont) : span(cont.data(), cont.size()) {} |
| 211 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 212 | constexpr span(const span& other) noexcept = default; |
| 213 | constexpr span(span&& other) noexcept = default; |
Neil MacIntosh | 717a2e3 | 2016-03-16 19:39:55 -0700 | [diff] [blame] | 214 | |
| 215 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
| 216 | class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value && |
| 217 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 218 | > |
| 219 | > |
| 220 | constexpr span(const span<OtherElementType, OtherExtent>& other) |
| 221 | : storage_(reinterpret_cast<pointer>(other.data()), other.length()) |
| 222 | {} |
| 223 | |
| 224 | template <class OtherElementType, std::ptrdiff_t OtherExtent, |
| 225 | class = std::enable_if_t<!std::is_same<element_type, OtherElementType>::value && |
| 226 | details::is_allowed_element_type_conversion<OtherElementType, element_type>::value |
| 227 | > |
| 228 | > |
| 229 | constexpr span(span<OtherElementType, OtherExtent>&& other) |
| 230 | : storage_(reinterpret_cast<pointer>(other.data()), other.length()) |
| 231 | { |
| 232 | } |
| 233 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 234 | ~span() noexcept = default; |
| 235 | constexpr span& operator=(const span& other) noexcept = default; |
| 236 | constexpr span& operator=(span&& other) noexcept = default; |
Neil MacIntosh | 3d4c349 | 2016-03-17 17:20:33 -0700 | [diff] [blame^] | 237 | #if 0 // TODO |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 238 | |
| 239 | // [span.sub], span subviews |
| 240 | template <ptrdiff_t Count> |
| 241 | constexpr span<element_type, Count> first() const; |
| 242 | template <ptrdiff_t Count> |
| 243 | constexpr span<element_type, Count> last() const; |
| 244 | template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent> |
| 245 | constexpr span<element_type, Count> subspan() const; |
| 246 | constexpr span<element_type, dynamic_extent> first(index_type count) const; |
| 247 | constexpr span<element_type, dynamic_extent> last(index_type count) const; |
| 248 | constexpr span<element_type, dynamic_extent> subspan(index_type offset, index_type count = dynamic_extent) const; |
| 249 | #endif |
| 250 | // [span.obs], span observers |
| 251 | constexpr index_type length() const noexcept { return size(); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 252 | constexpr index_type size() const noexcept { return storage_.size(); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 253 | constexpr index_type length_bytes() const noexcept { return size_bytes(); } |
| 254 | constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); } |
| 255 | constexpr bool empty() const noexcept { return size() == 0; } |
| 256 | |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 257 | // [span.elem], span element access |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 258 | constexpr reference operator[](index_type idx) const |
| 259 | { |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 260 | Expects(idx >= 0 && idx < storage_.size()); |
| 261 | return storage_.data()[idx]; |
Neil MacIntosh | cc22f2b | 2016-02-25 11:42:26 -0800 | [diff] [blame] | 262 | } |
| 263 | constexpr reference operator()(index_type idx) const { return this->operator[](idx); } |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 264 | constexpr pointer data() const noexcept { return storage_.data(); } |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 265 | #if 0 // TODO |
| 266 | // [span.iter], span iterator support |
| 267 | iterator begin() const noexcept; |
| 268 | iterator end() const noexcept; |
| 269 | |
| 270 | const_iterator cbegin() const noexcept; |
| 271 | const_iterator cend() const noexcept; |
| 272 | |
| 273 | reverse_iterator rbegin() const noexcept; |
| 274 | reverse_iterator rend() const noexcept; |
| 275 | |
| 276 | const_reverse_iterator crbegin() const noexcept; |
| 277 | const_reverse_iterator crend() const noexcept; |
| 278 | #endif |
| 279 | private: |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 280 | template <index_type Extent> |
| 281 | class extent_type; |
| 282 | |
| 283 | template <index_type Extent> |
| 284 | class extent_type |
| 285 | { |
| 286 | public: |
| 287 | static_assert(Extent >= 0, "A fixed-size span must be >= 0 in size."); |
| 288 | |
| 289 | constexpr extent_type() noexcept {} |
| 290 | |
| 291 | template <index_type Other> |
| 292 | constexpr extent_type(extent_type<Other>) noexcept |
| 293 | { |
| 294 | static_assert(Other == Extent, |
| 295 | "Mismatch between fixed-size extent and size of initializing data."); |
| 296 | } |
| 297 | |
| 298 | constexpr extent_type(index_type size) |
| 299 | { Expects(size == Extent); } |
| 300 | |
| 301 | constexpr inline index_type size() const noexcept { return Extent; } |
| 302 | }; |
| 303 | |
| 304 | template <> |
| 305 | class extent_type<dynamic_extent> |
| 306 | { |
| 307 | public: |
| 308 | template <index_type Other> |
| 309 | explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) |
| 310 | {} |
| 311 | |
| 312 | explicit constexpr extent_type(index_type size) : size_(size) |
| 313 | { Expects(size >= 0); } |
| 314 | |
| 315 | constexpr inline index_type size() const noexcept |
| 316 | { return size_; } |
| 317 | |
| 318 | private: |
| 319 | index_type size_; |
| 320 | }; |
| 321 | |
| 322 | // this implementation detail class lets us take advantage of the |
| 323 | // empty base class optimization to pay for only storage of a single |
| 324 | // pointer in the case of fixed-size spans |
| 325 | template <class ExtentType> |
| 326 | class storage_type : public ExtentType |
| 327 | { |
| 328 | public: |
| 329 | template <class OtherExtentType> |
| 330 | storage_type(pointer data, OtherExtentType ext) |
| 331 | : ExtentType(ext), data_(data) {} |
| 332 | |
Neil MacIntosh | 502cd66 | 2016-02-28 00:50:53 -0800 | [diff] [blame] | 333 | constexpr inline pointer data() const noexcept |
| 334 | { return data_; } |
| 335 | |
| 336 | private: |
| 337 | pointer data_; |
| 338 | }; |
| 339 | |
| 340 | storage_type<extent_type<Extent>> storage_; |
Neil MacIntosh | d3929c5 | 2016-02-24 16:11:33 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | |
| 344 | #if 0 // TODO |
| 345 | // [span.comparison], span comparison operators |
| 346 | template <class ElementType, ptrdiff_t Extent> |
| 347 | constexpr bool operator==(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 348 | |
| 349 | template <class ElementType, ptrdiff_t Extent> |
| 350 | constexpr bool operator!=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 351 | |
| 352 | template <class ElementType, ptrdiff_t Extent> |
| 353 | constexpr bool operator<(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 354 | |
| 355 | template <class ElementType, ptrdiff_t Extent> |
| 356 | constexpr bool operator<=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 357 | |
| 358 | template <class ElementType, ptrdiff_t Extent> |
| 359 | constexpr bool operator>(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 360 | |
| 361 | template <class ElementType, ptrdiff_t Extent> |
| 362 | constexpr bool operator>=(const span<ElementType, Extent>& l, const span<ElementType, Extent>& r) const noexcept; |
| 363 | #endif |
| 364 | |
| 365 | |
| 366 | #if 0 // TODO |
| 367 | // [span.objectrep], views of object representation |
| 368 | template <class ElementType, ptrdiff_t Extent> |
| 369 | constexpr span<const char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept; |
| 370 | |
| 371 | template <class ElementType, ptrdiff_t Extent> |
| 372 | constexpr span<char, ((Extent == dynamic_extent) ? dynamic_extent : (sizeof(ElementType) * Extent))> as_writeable_bytes(span<ElementType, Extent>) noexcept; |
| 373 | #endif |
Neil MacIntosh | cec26a2 | 2016-02-24 11:26:28 -0800 | [diff] [blame] | 374 | |
| 375 | } // namespace gsl |
| 376 | |
| 377 | #ifdef _MSC_VER |
| 378 | |
| 379 | #undef constexpr |
| 380 | #pragma pop_macro("constexpr") |
| 381 | |
| 382 | #if _MSC_VER <= 1800 |
| 383 | #pragma warning(pop) |
| 384 | |
| 385 | #ifndef GSL_THROW_ON_CONTRACT_VIOLATION |
| 386 | #undef noexcept |
| 387 | #pragma pop_macro("noexcept") |
| 388 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 389 | |
| 390 | #undef GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
| 391 | |
| 392 | #endif // _MSC_VER <= 1800 |
| 393 | |
| 394 | #endif // _MSC_VER |
| 395 | |
| 396 | #if defined(GSL_THROW_ON_CONTRACT_VIOLATION) |
| 397 | |
| 398 | #undef noexcept |
| 399 | |
| 400 | #ifdef _MSC_VER |
| 401 | #pragma warning(pop) |
| 402 | #pragma pop_macro("noexcept") |
| 403 | #endif |
| 404 | |
| 405 | #endif // GSL_THROW_ON_CONTRACT_VIOLATION |
| 406 | |
| 407 | #endif // GSL_SPAN_H |