Skip to content

Commit f11633c

Browse files
committed
rename DynamicFifo to DynamicFifo and make enums public.
1 parent 341e29a commit f11633c

File tree

6 files changed

+75
-74
lines changed

6 files changed

+75
-74
lines changed

dynamicFifo.h renamed to DynamicFifo.h

+20-22
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@
44
#include <iostream>
55

66

7-
8-
97
template<class T>
10-
class Fifo { /// essentially a circular fifo
8+
class DynamicFifo { /// essentially a circular fifo
119
private:
1210
T* elem;
1311
int nextFree;
1412
int endPointer;
1513
unsigned int sz;
1614

15+
16+
public:
1717
enum Fifo_STATUS {
1818
Fifo_FULL,
1919
Fifo_EMPTY,
2020
Fifo_GOOD
2121
};
22-
23-
public:
2422
/**
2523
* @brief Construct a new Fifo object
2624
* @param s
2725
*/
28-
explicit Fifo(int s);
26+
explicit DynamicFifo(int s);
2927

3028
/* *//**
3129
* @brief Construct a new Fifo object from an initializer list
@@ -35,39 +33,39 @@ class Fifo { /// essentially a circular fifo
3533

3634

3735
/**
38-
* @brief Copy constructor for Fifo
36+
* @brief Copy constructor for DynamicFifo
3937
* @param a
4038
*/
41-
Fifo(const Fifo& a); // copy constructor
39+
DynamicFifo(const DynamicFifo& a); // copy constructor
4240

4341
/**
44-
* @brief Copy assignment constructor for Fifo
42+
* @brief Copy assignment constructor for DynamicFifo
4543
* @param a
46-
* @return Fifo&
44+
* @return DynamicFifo&
4745
*/
48-
Fifo& operator=(const Fifo& a); // copy assignment
46+
DynamicFifo& operator=(const DynamicFifo& a); // copy assignment
4947

5048
/**
51-
* @brief Move constructor for Fifo
49+
* @brief Move constructor for DynamicFifo
5250
* @param a
5351
*/
54-
Fifo(Fifo&& a) noexcept ; // move constructor
52+
DynamicFifo(DynamicFifo&& a) noexcept ; // move constructor
5553

5654
/**
57-
* @brief Move assignment constructor for Fifo
55+
* @brief Move assignment constructor for DynamicFifo
5856
* @param a
59-
* @return Fifo&
57+
* @return DynamicFifo&
6058
*/
61-
Fifo& operator=(Fifo&& a) noexcept; // move assignment
59+
DynamicFifo& operator=(DynamicFifo&& a) noexcept; // move assignment
6260

6361

6462
/**
65-
* @brief C-style cast operator for dynamicFifo. Does not resize the fifo. Usage: e.g.
66-
* @example Fifo\<double\> g;\n Fifo\<float\> f = (Fifo\<float\>) g;
63+
* @brief C-style cast operator for DynamicFifo. Does not resize the fifo. Usage: e.g.
64+
* @example DynamicFifo\<double\> g;\n DynamicFifo\<float\> f = (DynamicFifo\<float\>) g;
6765
* @return Fifo_STATUS
6866
*/
6967
template <typename D>
70-
explicit operator Fifo<D>() const;
68+
explicit operator DynamicFifo<D>() const;
7169

7270
/**
7371
* @brief operator []
@@ -84,9 +82,9 @@ class Fifo { /// essentially a circular fifo
8482
T& operator[](int i) const;
8583

8684
/**
87-
* @brief Get the T at index i of the Fifo
85+
* @brief Get the T at index i of the DynamicFifo
8886
* @param i
89-
* @return The T at index i of the Fifo (not elem)
87+
* @return The T at index i of the DynamicFifo (not elem)
9088
*/
9189
T& atFifoIndex(int i);
9290

@@ -163,7 +161,7 @@ class Fifo { /// essentially a circular fifo
163161
int used_space() const;
164162

165163

166-
~Fifo() { delete[] elem; } // destructor
164+
~DynamicFifo() { delete[] elem; } // destructor
167165
};
168166

169167
#include "dynamicFifo.tpp" // implementation file

Fifo.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ class Fifo { /// essentially a circular fifo
1313
int nextFree;
1414
int endPointer;
1515

16-
enum class Fifo_STATUS {
16+
17+
18+
public:
19+
enum Fifo_STATUS {
1720
Fifo_FULL,
1821
Fifo_EMPTY,
1922
Fifo_GOOD
2023
};
21-
22-
public:
2324
/**
2425
* @brief Construct a new Fifo object
2526
* @param s

Fifo.tpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ T& Fifo<T, sz>::atFifoIndex(int i) const{
9696
}
9797

9898
template<class T, unsigned int sz>
99-
Fifo_STATUS Fifo<T, sz>::push(const T& item) { // returns the status of the fifo
99+
typename Fifo<T, sz>::Fifo_STATUS Fifo<T, sz>::push(const T& item) { // returns the status of the fifo
100100
if (fifo_status()==Fifo_STATUS::Fifo_FULL) {
101101
// throw std::length_error("NA"); // throw does not work with arduino :(
102102
return Fifo_STATUS::Fifo_FULL; // status code
@@ -166,7 +166,7 @@ T Fifo<T, sz>::peekFront(int i) const {
166166
}
167167

168168
template<class T, unsigned int sz>
169-
Fifo_STATUS Fifo<T, sz>::fifo_status() const {
169+
typename Fifo<T, sz>::Fifo_STATUS Fifo<T, sz>::fifo_status() const {
170170
if (nextFree==endPointer) {
171171
return Fifo_STATUS::Fifo_EMPTY; // fifo empty
172172
}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ Fifo<Vector<double, 6>, 256> e2 = (Fifo<Vector<double, 6>, 256>) e1;
4747
```
4848

4949
### Dynamic Fifo
50-
`dynamicFifo` is an identical to `Fifo` in functionality, but uses dynamic memory allocation, not a template.
50+
`DynamicFifo` is an identical to `Fifo` in functionality, but uses dynamic memory allocation, not a template.
5151
It is useful when the size of the Fifo is not known at compile time, or when writing libraries etc. (e.g. [LSM6DSO32](https://github.com/TeamSunride/Arduino-LSM6DSO32))
5252

5353
```cpp
54-
#include "dynamicFifo.h"
55-
// dynamicFifo is initialised with a template type and size as a parameter.
54+
#include "DynamicFifo.h"
55+
// DynamicFifo is initialised with a template type and size as a parameter.
5656
Fifo<float> e1 (256);
5757
e1.push(3.14159);
5858

dynamicFifo.tpp

+22-22
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// Created by robos on 30/06/2022.
33
//
44

5-
#include "dynamicFifo.h"
5+
#include "DynamicFifo.h"
66
#include "Vector.h"
77

88

99

1010
template<class T>
11-
Fifo<T>::Fifo(int s) {
11+
DynamicFifo<T>::DynamicFifo(int s) {
1212
elem = new T[s]; // allocates on the free store (the heap)
1313
sz = s;
1414
nextFree=0;
@@ -28,28 +28,28 @@ Fifo<T>::Fifo(std::initializer_list<T> lst) {
2828
*/
2929

3030
template<class T>
31-
T &Fifo<T>::operator[](int i) {
31+
T &DynamicFifo<T>::operator[](int i) {
3232
return elem[i%sz]; // if the index is larger than the sz, it wraps around;
3333
}
3434

3535
template<class T>
36-
T &Fifo<T>::operator[](int i) const {
36+
T &DynamicFifo<T>::operator[](int i) const {
3737
return elem[i%sz]; // if the index is larger than the sz, it wraps around;
3838
}
3939

4040
template<class T>
41-
T& Fifo<T>::atFifoIndex(int i) {
41+
T& DynamicFifo<T>::atFifoIndex(int i) {
4242
return elem[(endPointer+i) % sz];
4343
}
4444

4545
template<class T>
46-
T& Fifo<T>::atFifoIndex(int i) const{
46+
T& DynamicFifo<T>::atFifoIndex(int i) const{
4747
return elem[(endPointer+i) % sz];
4848
}
4949

5050

5151
template<class T>
52-
Fifo_STATUS Fifo<T>::push(const T& item) {
52+
typename DynamicFifo<T>::Fifo_STATUS DynamicFifo<T>::push(const T& item) {
5353
if (fifo_status()==Fifo_STATUS::Fifo_FULL) {
5454
// throw std::length_error("NA"); // throw does not work with arduino :(
5555
return Fifo_STATUS::Fifo_FULL; // status code
@@ -67,7 +67,7 @@ Fifo_STATUS Fifo<T>::push(const T& item) {
6767
}
6868

6969
template<class T>
70-
T Fifo<T>::pop() { /// Note: You should check the status of the fifo before calling this function
70+
T DynamicFifo<T>::pop() { /// Note: You should check the status of the fifo before calling this function
7171
if (fifo_status()==Fifo_STATUS::Fifo_EMPTY) {
7272
return {0}; // return 0
7373
}
@@ -83,7 +83,7 @@ T Fifo<T>::pop() { /// Note: You should check the status of the fifo before call
8383

8484

8585
template<class T>
86-
T Fifo<T>::peekBack() const { // GitHub copilot for the win
86+
T DynamicFifo<T>::peekBack() const { // GitHub copilot for the win
8787
if (fifo_status()==Fifo_STATUS::Fifo_EMPTY) {
8888
return {0}; // return 0
8989
}
@@ -92,7 +92,7 @@ T Fifo<T>::peekBack() const { // GitHub copilot for the win
9292
}
9393

9494
template<class T>
95-
T Fifo<T>::peekBack(int i) const {
95+
T DynamicFifo<T>::peekBack(int i) const {
9696
if (fifo_status()==Fifo_STATUS::Fifo_EMPTY) {
9797
return {0}; // return 0
9898
}
@@ -103,7 +103,7 @@ T Fifo<T>::peekBack(int i) const {
103103

104104

105105
template<class T>
106-
T Fifo<T>::peekFront() const {
106+
T DynamicFifo<T>::peekFront() const {
107107
if (fifo_status()==Fifo_STATUS::Fifo_EMPTY) {
108108
return {0}; // return 0
109109
}
@@ -112,7 +112,7 @@ T Fifo<T>::peekFront() const {
112112
}
113113

114114
template<class T>
115-
T Fifo<T>::peekFront(int i) const {
115+
T DynamicFifo<T>::peekFront(int i) const {
116116
if (fifo_status()==Fifo_STATUS::Fifo_EMPTY) {
117117
return {0}; // return 0
118118
}
@@ -121,7 +121,7 @@ T Fifo<T>::peekFront(int i) const {
121121
}
122122

123123
template<class T>
124-
Fifo_STATUS Fifo<T>::fifo_status() const {
124+
typename DynamicFifo<T>::Fifo_STATUS DynamicFifo<T>::fifo_status() const {
125125
if (nextFree==endPointer) {
126126
return Fifo_STATUS::Fifo_EMPTY; // fifo empty
127127
}
@@ -133,12 +133,12 @@ Fifo_STATUS Fifo<T>::fifo_status() const {
133133
}
134134

135135
template<class T>
136-
int Fifo<T>::size() const{
136+
int DynamicFifo<T>::size() const{
137137
return sz;
138138
}
139139

140140
template<class T>
141-
int Fifo<T>::free_space() const{
141+
int DynamicFifo<T>::free_space() const{
142142
if (fifo_status()==Fifo_STATUS::Fifo_FULL){
143143
return 0;
144144
}
@@ -152,8 +152,8 @@ int Fifo<T>::free_space() const{
152152

153153
template<class T>
154154
template<typename D>
155-
Fifo<T>::operator Fifo<D>() const {
156-
Fifo<D> r(sz);
155+
DynamicFifo<T>::operator DynamicFifo<D>() const {
156+
DynamicFifo<D> r(sz);
157157
for (int i=0;i<used_space();i++) {
158158
D item = static_cast<D>(elem[i]);
159159
r.push(item);
@@ -163,7 +163,7 @@ Fifo<T>::operator Fifo<D>() const {
163163

164164
// copy constructor
165165
template<class T>
166-
Fifo<T>::Fifo(const Fifo &a) {
166+
DynamicFifo<T>::DynamicFifo(const DynamicFifo &a) {
167167
elem = new T[a.sz];
168168
sz = a.sz;
169169
nextFree = a.nextFree;
@@ -175,7 +175,7 @@ Fifo<T>::Fifo(const Fifo &a) {
175175

176176
// copy assignment operator
177177
template<class T>
178-
Fifo<T> &Fifo<T>::operator=(const Fifo &a) {
178+
DynamicFifo<T> &DynamicFifo<T>::operator=(const DynamicFifo &a) {
179179
T* p = new T[a.sz];
180180
for (int i=0;i<a.sz;i++) {
181181
p[i] = a.elem[i];
@@ -190,7 +190,7 @@ Fifo<T> &Fifo<T>::operator=(const Fifo &a) {
190190

191191
// move constructor
192192
template<class T>
193-
Fifo<T>::Fifo(Fifo &&a) noexcept {
193+
DynamicFifo<T>::DynamicFifo(DynamicFifo &&a) noexcept {
194194
elem = a.elem;
195195
sz = a.sz;
196196
nextFree = a.nextFree;
@@ -200,7 +200,7 @@ Fifo<T>::Fifo(Fifo &&a) noexcept {
200200

201201
// move assignment operator
202202
template<class T>
203-
Fifo<T> &Fifo<T>::operator=(Fifo &&a) noexcept {
203+
DynamicFifo<T> &DynamicFifo<T>::operator=(DynamicFifo &&a) noexcept {
204204
if (this != &a) {
205205
delete[] elem;
206206
elem = a.elem;
@@ -213,6 +213,6 @@ Fifo<T> &Fifo<T>::operator=(Fifo &&a) noexcept {
213213
}
214214

215215
template<class T>
216-
int Fifo<T>::used_space() const {
216+
int DynamicFifo<T>::used_space() const {
217217
return sz-free_space();
218218
}

0 commit comments

Comments
 (0)