VLCB
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
12namespace VLCB
13{
14
15template<typename E>
17{
18public:
22
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
40private:
41 static E* copyArray(const E * a, size_t len);
42 void freeArray();
43
44 const E* array;
45 size_t len;
46};
47
48template<typename E>
50 : array(nullptr)
51 , len(0)
52{ }
53
54template<typename E>
56 : array(copyArray(il.begin(), il.size()))
57 , len(il.size())
58{ }
59
60template<typename E>
62{
63 freeArray();
64}
65
66template<typename E>
68{
69 freeArray();
70
71 array = copyArray(il.begin(), il.size());
72 len = il.size();
73 return *this;
74}
75
76template<typename E>
77E* 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
87template<typename E>
88void ArrayHolder<E>::freeArray()
89{
90 if (this->array != nullptr)
91 {
92 delete[] this->array;
93 }
94}
95
96}
ArrayHolder()
Definition ArrayHolder.h:49
~ArrayHolder()
Definition ArrayHolder.h:61
constexpr const E operator[](size_t index) const noexcept
Definition ArrayHolder.h:38
constexpr const E * end() const noexcept
Definition ArrayHolder.h:35
constexpr const E * begin() const noexcept
Definition ArrayHolder.h:31
constexpr size_t size() const noexcept
Definition ArrayHolder.h:27
ArrayHolder & operator=(const std::initializer_list< E > &il)
Definition ArrayHolder.h:67
Convenience class for use with initializer lists, i.e. bracketed list of items: { a,...
Definition initializer_list.h:19
constexpr const E * begin() const noexcept
Definition initializer_list.h:30
constexpr size_t size() const noexcept
Definition initializer_list.h:26
Definition AbstractEventTeachingService.cpp:13