VLCB
Toggle main menu visibility
Loading...
Searching...
No Matches
ArrayHolder.h
Go to the documentation of this file.
1
// Copyright (C) Sven Rosvall (sven@rosvall.ie)
2
// This file is part of VLCB-Arduino project on https://github.com/SvenRosvall/VLCB-Arduino
3
// Licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
4
// The full licence can be found at: http://creativecommons.org/licenses/by-nc-sa/4.0
5
//
6
7
#pragma once
8
9
#include <stddef.h>
10
#include "
initializer_list.h
"
11
12
namespace
VLCB
13
{
14
15
template
<
typename
E>
16
class
ArrayHolder
17
{
18
public
:
19
ArrayHolder
();
20
ArrayHolder
(
const
std::initializer_list<E>
& il);
21
~ArrayHolder
();
22
23
ArrayHolder
&
operator=
(
const
std::initializer_list<E>
& il);
24
25
// Number of elements.
26
constexpr
size_t
27
size
() const noexcept {
return
len; }
28
29
// First element.
30
constexpr
const
E*
31
begin
() const noexcept {
return
array; }
32
33
// One past the last element.
34
constexpr
const
E*
35
end
() const noexcept {
return
begin
() +
size
(); }
36
37
constexpr
const
E
38
operator[]
(
size_t
index)
const
noexcept
{
return
array[index]; }
39
40
private
:
41
static
E* copyArray(
const
E * a,
size_t
len);
42
void
freeArray();
43
44
const
E* array;
45
size_t
len;
46
};
47
48
template
<
typename
E>
49
ArrayHolder<E>::ArrayHolder
()
50
: array(nullptr)
51
, len(0)
52
{ }
53
54
template
<
typename
E>
55
ArrayHolder<E>::ArrayHolder
(
const
std::initializer_list<E>
&il)
56
: array(copyArray(il.
begin
(), il.
size
()))
57
, len(il.
size
())
58
{ }
59
60
template
<
typename
E>
61
ArrayHolder<E>::~ArrayHolder
()
62
{
63
freeArray();
64
}
65
66
template
<
typename
E>
67
ArrayHolder<E>
&
ArrayHolder<E>::operator=
(
const
std::initializer_list<E>
& il)
68
{
69
freeArray();
70
71
array = copyArray(il.
begin
(), il.
size
());
72
len = il.
size
();
73
return
*
this
;
74
}
75
76
template
<
typename
E>
77
E* ArrayHolder<E>::copyArray(
const
E * a,
size_t
len)
78
{
79
E * array =
new
E[len];
80
for
(
size_t
i = 0 ; i < len ; ++i)
81
{
82
array[i] = a[i];
83
}
84
return
array;
85
}
86
87
template
<
typename
E>
88
void
ArrayHolder<E>::freeArray()
89
{
90
if
(this->array !=
nullptr
)
91
{
92
delete
[] this->array;
93
}
94
}
95
96
}
VLCB::ArrayHolder::ArrayHolder
ArrayHolder()
Definition
ArrayHolder.h:49
VLCB::ArrayHolder::~ArrayHolder
~ArrayHolder()
Definition
ArrayHolder.h:61
VLCB::ArrayHolder::operator[]
constexpr const E operator[](size_t index) const noexcept
Definition
ArrayHolder.h:38
VLCB::ArrayHolder::end
constexpr const E * end() const noexcept
Definition
ArrayHolder.h:35
VLCB::ArrayHolder::begin
constexpr const E * begin() const noexcept
Definition
ArrayHolder.h:31
VLCB::ArrayHolder::size
constexpr size_t size() const noexcept
Definition
ArrayHolder.h:27
VLCB::ArrayHolder::operator=
ArrayHolder & operator=(const std::initializer_list< E > &il)
Definition
ArrayHolder.h:67
std::initializer_list
Convenience class for use with initializer lists, i.e. bracketed list of items: { a,...
Definition
initializer_list.h:19
std::initializer_list::begin
constexpr const E * begin() const noexcept
Definition
initializer_list.h:30
std::initializer_list::size
constexpr size_t size() const noexcept
Definition
initializer_list.h:26
initializer_list.h
VLCB
Definition
AbstractEventTeachingService.cpp:13
src
ArrayHolder.h
Generated by
1.18.0