cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TSpan.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24#ifndef CPPCORE_COMMON_TSPAN_H
25#define CPPCORE_COMMON_TSPAN_H
26
28
29namespace cppcore {
30
31//-------------------------------------------------------------------------------------------------
36//-------------------------------------------------------------------------------------------------
37template<class T>
38class TSpan {
39public:
41 using Iterator = const T*;
42
44 TSpan() = default;
45
49 TSpan(const T *ptr, size_t size);
50
52 ~TSpan() = default;
53
56 const T* data() const;
57
60 size_t size() const;
61
64 bool empty() const;
65
69
72 Iterator end();
73
77 T operator[] (size_t index);
78
82 const T operator[](size_t index) const;
83
84private:
85 const T *mData{nullptr};
86 size_t mSize{0u};
87};
88
89template<class T>
90inline TSpan<T>::TSpan(const T *ptr, size_t size) : mData(ptr), mSize(size) {
91 // empty
92}
93
94template<class T>
95inline const T *TSpan<T>::data() const {
96 return mData;
97}
98
99template<class T>
100inline size_t TSpan<T>::size() const {
101 return mSize;
102}
103
104template<class T>
105inline bool TSpan<T>::empty() const {
106 return mSize == 0;
107}
108
109template<class T>
111 return &mData[0];
112}
113
114template<class T>
116 return mData + mSize;
117}
118
119template<class T>
120inline T TSpan<T>::operator[](size_t index) {
121 if (index >= mSize) {
122 return std::numeric_limits<T>::max();
123 }
124 return mData[index];
125}
126
127template<class T>
128inline const T TSpan<T>::operator[](size_t index) const {
129 return mData[index];
130}
131
132} // namespace cppcore
133
134#endif // CPPCORE_COMMON_TSPAN_H
Iterator end()
Will return the end iterator.
Definition TSpan.h:115
~TSpan()=default
The class destructor.
size_t size() const
Will return the number of items in the view.
Definition TSpan.h:100
const T * Iterator
The iterator type.
Definition TSpan.h:41
bool empty() const
Will return true, if the span view is empty.
Definition TSpan.h:105
T operator[](size_t index)
Will return the item at the given index.
Definition TSpan.h:120
const T * data() const
Will return the data pointer showing to the first entry.
Definition TSpan.h:95
Iterator begin()
Will return the begin iterator.
Definition TSpan.h:110
TSpan()=default
The default class constructor.