24 #ifndef STATICLIB_SUPPORT_SPAN_HPP
25 #define STATICLIB_SUPPORT_SPAN_HPP
33 #include "staticlib/config.hpp"
34 #include "staticlib/support.hpp"
56 sl::support::exception(msg) { }
69 T* first_ptr =
nullptr;
73 T* last_ptr =
nullptr;
84 template<
typename IntType>
85 span(T* buffer, IntType length) :
88 if (
nullptr != buffer) {
90 if ( sl::support::is_sizet(length) && sl::support::is_streamsize(length)) {
91 last_ptr +=
static_cast<size_t> (length);
94 " length: [" + sl::support::to_string(length) +
"]");
96 }
else if (0 != length) {
98 " length: [" + sl::support::to_string(length) +
"]");
107 template<
typename Convertible,
size_t Length>
108 span(std::array<Convertible, Length>& buffer) :
124 template<
typename Convertible>
125 span(std::vector<Convertible>& buffer) :
134 span(buffer.length() > 0 ? std::addressof(buffer.front()) : nullptr, buffer.length()) { }
141 span(
const std::string& buffer) :
142 span(buffer.length() > 0 ? buffer.
data() : nullptr, buffer.length()) { }
149 template<
size_t Size>
150 span(
const char (&buffer)[Size]) :
151 span(Size > 1 ? static_cast<const char*>(buffer) : nullptr, Size - 1) { }
159 first_ptr(other.first_ptr),
160 last_ptr(other.last_ptr) { }
168 first_ptr = other.first_ptr;
169 last_ptr = other.last_ptr;
181 if (index <
size()) {
182 return *(first_ptr + index);
185 " span size: [" + sl::support::to_string(
size()) +
"],"
186 " index: [" + sl::support::to_string(index) +
"]");
195 T*
data() const STATICLIB_NOEXCEPT {
204 bool empty() const STATICLIB_NOEXCEPT {
223 return nullptr == first_ptr;
231 size_t size() const STATICLIB_NOEXCEPT {
232 return nullptr == first_ptr ? 0 : last_ptr - first_ptr;
241 return static_cast<std::streamsize
>(
size());
250 return static_cast<int>(
size());
259 return (
size()) *
sizeof(T);
267 T*
begin() const STATICLIB_NOEXCEPT {
276 T*
end() const STATICLIB_NOEXCEPT {
289 template<
typename T,
typename IntType>
291 return span<T>(buffer, length);
300 template<
typename T,
size_t Length>
302 return span<T>(buffer.data(), buffer.size());
313 return span<T>(buffer.data(), buffer.size());