Last change
on this file since 1342 was
1340,
checked in by Maciej Komosinski, 13 days ago
|
Added helper functions
|
File size:
1.8 KB
|
Rev | Line | |
---|
[1179] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/
|
---|
[1340] | 2 | // Copyright (C) 2019-2024 Maciej Komosinski and Szymon Ulatowski.
|
---|
[1179] | 3 | // See LICENSE.txt for details.
|
---|
| 4 |
|
---|
| 5 | #ifndef _NONSTD_SPAN_H_
|
---|
| 6 | #define _NONSTD_SPAN_H_
|
---|
| 7 |
|
---|
[1340] | 8 | #include <string>
|
---|
| 9 | #include <vector>
|
---|
| 10 |
|
---|
[1179] | 11 | // Remove this and use std::span when available (c++20)
|
---|
| 12 |
|
---|
| 13 | template<class ElementType> class span
|
---|
| 14 | {
|
---|
| 15 | public:
|
---|
| 16 | constexpr span() noexcept
|
---|
| 17 | {
|
---|
[1183] | 18 | data_ = NULL;
|
---|
| 19 | size_ = 0;
|
---|
[1179] | 20 | }
|
---|
| 21 | constexpr span(ElementType* first, size_t count) noexcept
|
---|
| 22 | {
|
---|
[1183] | 23 | data_ = first;
|
---|
| 24 | size_ = count;
|
---|
[1179] | 25 | }
|
---|
| 26 | constexpr span(ElementType* first, ElementType* end) noexcept
|
---|
| 27 | {
|
---|
[1183] | 28 | data_ = first;
|
---|
| 29 | size_ = end - first;
|
---|
[1179] | 30 | }
|
---|
| 31 | template<size_t N> constexpr span(ElementType(&arr)[N]) noexcept
|
---|
| 32 | {
|
---|
[1183] | 33 | data_ = arr;
|
---|
| 34 | size_ = N;
|
---|
[1179] | 35 | }
|
---|
| 36 |
|
---|
| 37 | size_t size() const { return size_; }
|
---|
| 38 | size_t size_bytes() const { return size_ * sizeof(ElementType); }
|
---|
| 39 | ElementType& operator[](size_t idx) const { return data_[idx]; }
|
---|
| 40 |
|
---|
| 41 | ElementType* begin() const { return data_; }
|
---|
| 42 | ElementType* end() const { return data_ + size_; }
|
---|
| 43 |
|
---|
| 44 | protected:
|
---|
| 45 | ElementType* data_;
|
---|
| 46 | size_t size_;
|
---|
| 47 | };
|
---|
| 48 |
|
---|
[1340] | 49 | inline std::string stringFromSpan(const span<uint8_t>& s) { return std::string(s.begin(), s.end()); }
|
---|
| 50 | inline std::string stringFromSpan(const span<char>& s) { return std::string(s.begin(), s.end()); }
|
---|
| 51 | // 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().
|
---|
| 52 | inline span<uint8_t> spanFromVector(std::vector<uint8_t>& v) { return span<uint8_t>(&v.front(), ((uint8_t*)&v.front())+v.size()); }
|
---|
| 53 | inline span<uint8_t> spanFromVector(std::vector<char>& v) { return span<uint8_t>((uint8_t*)&v.front(), ((uint8_t*)&v.front())+v.size()); }
|
---|
| 54 |
|
---|
[1179] | 55 | #endif
|
---|
Note: See
TracBrowser
for help on using the repository browser.