cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TStaticArray.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
25#include <string.h>
26#include <cassert>
27
28namespace cppcore {
29
30//-------------------------------------------------------------------------------------------------
38//-------------------------------------------------------------------------------------------------
39template <class T, size_t len>
41public:
44
47 explicit TStaticArray(T initValue);
48
52
54 ~TStaticArray() = default;
55
58 size_t size() const;
59
63 void set(size_t index, T value);
64
67 void memset(T value);
68
70 T operator[](size_t index) const;
71
73 T &operator[](size_t index);
74 bool operator==(const TStaticArray<T, len> &rhs) const;
76
77private:
78 T mArray[len];
79 size_t mLen{0l};
80};
81
82template <class T, size_t len>
84 // empty
85}
86
87template <class T, size_t len>
88inline TStaticArray<T, len>::TStaticArray(T initValue) : mLen(len) {
89 memset(initValue);
90}
91
92template <class T, size_t len>
93inline TStaticArray<T, len>::TStaticArray(const TStaticArray<T, len> &rhs) : mLen(rhs.mLen) {
94 for (size_t i = 0; i < mLen; ++i) {
95 mArray[i] = rhs.mArray[i];
96 }
97}
98
99template <class T, size_t len>
100inline void TStaticArray<T, len>::set(size_t index, T value) {
101 assert(index < mLen);
102
103 mArray[index] = value;
104}
105
106template <class T, size_t len>
107inline void TStaticArray<T, len>::memset(T value) {
108 for (size_t i = 0; i < mLen; ++i) {
109 mArray[i] = value;
110 }
111}
112
113template <class T, size_t len>
114inline size_t TStaticArray<T, len>::size() const {
115 return mLen;
116}
117
118template <class T, size_t len>
119inline T TStaticArray<T, len>::operator[](size_t index) const {
120 assert(index < mLen);
121
122 return mArray[index];
123}
124
125template <class T, size_t len>
126inline T &TStaticArray<T, len>::operator[](size_t index) {
127 return mArray[index];
128}
129
130template <class T, size_t len>
132 for (size_t i = 0; i < mLen; ++i) {
133 if (mArray[i] != rhs.mArray[i]) {
134 return false;
135 }
136 }
137
138 return true;
139}
140
141template <class T, size_t len>
143 if (*this == rhs) {
144 return *this;
145 }
146
147 for (unsigned int i = 0; i < mLen; ++i) {
148 mArray[i] = rhs.mArray[i];
149 }
150
151 return *this;
152}
153
154} // namespace cppcore
~TStaticArray()=default
The class destructor.
size_t size() const
Returns the number of items in the array.
bool operator==(const TStaticArray< T, len > &rhs) const
TStaticArray< T, len > & operator=(const TStaticArray< T, len > &rhs)
void memset(T value)
Will set all values to the same value.
T operator[](size_t index) const
The index op.
void set(size_t index, T value)
Will set the item at the given index.
TStaticArray()
The default class constructor.