// This file is a part of Framsticks SDK. http://www.framsticks.com/ // Copyright (C) 2019-2024 Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. #ifndef _NONSTD_SPAN_H_ #define _NONSTD_SPAN_H_ #include #include // Remove this and use std::span when available (c++20) template class span { public: constexpr span() noexcept { data_ = NULL; size_ = 0; } constexpr span(ElementType* first, size_t count) noexcept { data_ = first; size_ = count; } constexpr span(ElementType* first, ElementType* end) noexcept { data_ = first; size_ = end - first; } template constexpr span(ElementType(&arr)[N]) noexcept { data_ = arr; size_ = N; } size_t size() const { return size_; } size_t size_bytes() const { return size_ * sizeof(ElementType); } ElementType& operator[](size_t idx) const { return data_[idx]; } ElementType* begin() const { return data_; } ElementType* end() const { return data_ + size_; } protected: ElementType* data_; size_t size_; }; inline std::string stringFromSpan(const span& s) { return std::string(s.begin(), s.end()); } inline std::string stringFromSpan(const span& s) { return std::string(s.begin(), s.end()); } // in both lines below, in the last argument, &*v.end() was replaced by front+size, because the debugging+extensive checking version of the STL library with std::vector complained when calling &*v.end(). inline span spanFromVector(std::vector& v) { return span(&v.front(), ((uint8_t*)&v.front())+v.size()); } inline span spanFromVector(std::vector& v) { return span((uint8_t*)&v.front(), ((uint8_t*)&v.front())+v.size()); } #endif