Span.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  * Permission is granted to anyone to use this software for any purpose,
8  * including commercial applications, and to alter it and redistribute it
9  * freely, subject to the following restrictions:
10  * 1. The origin of this software must not be misrepresented; you must not
11  * claim that you wrote the original software. If you use this software
12  * in a product, an acknowledgment in the product documentation would be
13  * appreciated but is not required.
14  * 2. Altered source versions must be plainly marked as such, and must not be
15  * misrepresented as being the original software.
16  * 3. This notice may not be removed or altered from any source distribution.
17  */
18 
19 #ifndef PLAYRHO_COMMON_SPAN_HPP
20 #define PLAYRHO_COMMON_SPAN_HPP
21 
22 #include <PlayRho/Defines.hpp>
24 
25 #include <cstddef>
26 #include <cassert>
27 #include <type_traits>
28 #include <iterator>
29 #include <vector>
30 
31 namespace playrho {
32 
38  template <typename T>
39  class Span
40  {
41  public:
42 
44  using data_type = T;
45 
47  using pointer = data_type*;
48 
50  using const_pointer = const data_type *;
51 
53  using size_type = std::size_t;
54 
55  Span() = default;
56 
58  Span(const Span& copy) = default;
59 
61  PLAYRHO_CONSTEXPR inline Span(pointer array, size_type size) noexcept:
62  m_array{array}, m_size{size}
63  {
64  }
65 
67  template <std::size_t SIZE>
68  PLAYRHO_CONSTEXPR inline Span(data_type (&array)[SIZE]) noexcept: m_array{&array[0]}, m_size{SIZE} {}
69 
71  template <typename U, typename = std::enable_if_t< !std::is_array<U>::value > >
72  PLAYRHO_CONSTEXPR inline Span(U& value) noexcept:
73  m_array{detail::Data(value)}, m_size{detail::Size(value)} {}
74 
76  PLAYRHO_CONSTEXPR inline Span(std::initializer_list<T> list) noexcept:
77  m_array{list.begin()}, m_size{list.size()} {}
78 
80  pointer begin() const noexcept { return m_array; }
81 
83  const_pointer cbegin() const noexcept { return m_array; }
84 
86  pointer end() const noexcept { return m_array + m_size; }
87 
89  const_pointer cend() const noexcept { return m_array + m_size; }
90 
92  data_type& operator[](size_type index) noexcept
93  {
94  assert(index < m_size);
95  return m_array[index];
96  }
97 
99  const data_type& operator[](size_type index) const noexcept
100  {
101  assert(index < m_size);
102  return m_array[index];
103  }
104 
106  PLAYRHO_CONSTEXPR size_type size() const noexcept { return m_size; }
107 
109  pointer data() const noexcept { return m_array; }
110 
112  PLAYRHO_CONSTEXPR bool empty() const noexcept { return m_size == 0; }
113 
114  private:
115  pointer m_array = nullptr;
116  size_type m_size = 0;
117  };
118 
119 } // namespace playrho
120 
121 #endif // PLAYRHO_COMMON_SPAN_HPP