1.2. Array
Array
is a final
class template defined in
Fw/DataStructures
.
It represents an array with internal storage.
It maintains the backing memory M as a member variable.
1. Template Parameters
Array
has the following template parameters.
Kind | Name | Purpose |
---|---|---|
typename |
T |
The type of an array element |
FwSizeType |
S |
The array size in elements |
Array
statically asserts the following:
T
is default constructible.S > 0
.
2. Types
Array
defines the type Elements
.
It is an alias of T[S]
.
3. Private Member Variables
Array
has the following private member variables.
Name | Type | Purpose | Default Value |
---|---|---|---|
m_elements |
Elements |
The array elements | C++ default initialization |
4. Public Constructors and Destructors
4.1. Zero-Argument Constructor
Defined as = default
.
Example:
4.2. Initializer List Constructor
-
Assert that
il.size() == S
. -
Initialize
m_elements
fromil
.
Examples:
4.3. Primitive Array Constructor
-
Statically assert that
S1 == S
. -
Set
*this = elements
.
Example:
4.4. Single-Element Constructor
Initialize each element of m_elements
with element
.
Example:
// Explicit call to constructor in variable declaration
Array<U32, 3> a(1);
// Explicit call to constructor in assignment
a = Array<U32, 3>(2);
4.5. Copy Constructor
Initialize the elements of m_elements
with the
elements of a.m_elements
.
Example:
// Call the single-item constructor
Array<U32, 3> a1(3);
// Call the copy constructor
Array<U32, 3> a2(a1);
4.6. Destructor
Defined as = default
.
5. Public Member Functions
5.1. operator[]
-
Assert that
i < S
. -
Return
m_elements[i]
.
Example:
constexpr FwSizeType size = 3;
Array<U32, size> a;
// Constant access
ASSERT_EQ(a[0], 0);
// Mutable access
a[0]++;
ASSERT_EQ(a[0], 1);
// Out-of-bounds access
ASSERT_DEATH(a[size], "Assert");
5.2. operator= (Initializer List)
-
Assert that
il.size() == S
. -
Copy each element of
il
intom_elements
. -
Return
*this
.
Example:
5.3. operator= (Primitive Array)
-
Copy each element of
elements
intom_elements
. -
Return
*this
.
Example:
5.4. operator= (Single Element)
-
Copy
element
into each element ofm_elements
. -
Return
*this
.
Example:
5.5. operator= (Copy Assignment)
-
If
&a != this
, overwrite each element ofm_elements
with the corresponding element ofa
. -
Return
*this
.
Example:
5.6. getElements
Return m_elements
.
Example:
constexpr FwSizeType size = 3;
Array<U32, size> a;
// Mutable reference
auto& elements1 = a.getElements();
ASSERT_EQ(elements1[0], 0);
elements1[0] = 1;
// Constant reference
const auto& elements2 = a.getElements();
ASSERT_EQ(elements2[0], 1);
5.7. asExternalArray
Return ExternalArray<T>(m_elements, S)
Example: