1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*******************************************************************************
* Copyright (c) 2016 protos software gmbh (http://www.protos.de).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* CONTRIBUTORS:
* Thomas Schuetz (initial contribution)
*
*******************************************************************************/
#ifndef STATICARRAY_H_
#define STATICARRAY_H_
#include "etDatatypes.h"
#include <cstring>
namespace etRuntime {
template<class Type, int Size>
class StaticArray {
public:
/**
* default constructor
*/
StaticArray() {
}
/**
* copy constructor forwards to deepCopy()
*/
StaticArray(const StaticArray<Type, Size> &rhs) {
this->deepCopy(rhs);
}
/**
* initializes each array element to t
*/
StaticArray(const Type& t) {
for (int i = 0; i < Size; i++) {
data[i] = t;
}
}
/**
* virtual destructor
*/
virtual ~StaticArray(void) {
}
/**
* returns the size of this array
*/
int getSize(void) const {
return Size;
}
/**
* returns a pointer to the data of this array (as a C array)
*/
Type* getData(void) {
return this->data;
}
/**
* returns a const pointer to the data of this array (as a C array)
*/
const Type* getData(void) const {
return this->data;
}
/**
* indexed access without range check
*/
Type& operator[](int index) {
return data[index];
}
/**
* copy assignment makes a deep copy
*/
StaticArray<Type, Size>& operator=(const StaticArray<Type, Size> &rhs) {
if (&rhs!=this) {
this->deepCopy(rhs);
}
return *this;
}
/**
* copy assignment from c++ array
*/
StaticArray<Type, Size>& operator=(const Type data[Size]) {
memcpy(this->data, data, sizeof(this->data));
return *this;
}
/**
* comparison operator based on memcmp()
*/
bool operator==(const StaticArray<Type, Size> &rhs) const {
return std::memcmp(this->data, rhs.data, sizeof(data)) == 0;
}
protected:
/**
* the C array holding the data
*/
Type data[Size];
/**
* makes a copy of the array including all elements
*/
void deepCopy(const StaticArray<Type, Size> &rhs) {
std::memcpy(this->data, rhs.data, sizeof(data));
}
// TODO: use this to copy because it uses T::operator=() ?
void deepCopy2(const StaticArray<Type, Size> &rhs) {
for (int i=0; i<Size; ++i) {
data[i] = rhs.data[i];
}
}
};
} /* namespace etRuntime */
#endif /* STATICARRAY_H_ */
|