Skip to content

Commit

Permalink
add dynamicFifo back in (I need it for LSM library). Update README an…
Browse files Browse the repository at this point in the history
…d example.cpp
  • Loading branch information
robosam2003 committed Aug 15, 2022
1 parent 546faf3 commit 19f4344
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Fifo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef Fifo_H
#define Fifo_H
#ifndef FIFO_H
#define FIFO_H

#include <iostream>

Expand Down Expand Up @@ -167,4 +167,4 @@ class Fifo { /// essentially a circular fifo
#include "Fifo.tpp" // implementation file


#endif //Fifo_H
#endif //FIFO_H
36 changes: 27 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# FIFO
A FIFO class.
A FIFO class that comes in both static and dynamic forms.

---

Expand All @@ -18,6 +18,7 @@ The `push()` function returns a status code of type `Fifo_STATUS`.
if (exampleFifo.push(item) == Fifo_STATUS::Fifo_FULL) {
// Handle the fact that the Fifo is full
}
// otherwise, the push was successful, continue.
```

Popping from a Fifo should be done after a check of the Fifo status to avoid unexpected behaviour.
Expand All @@ -34,14 +35,31 @@ See `example.cpp` for a more examples.
### Usage with Vector
Fifo also works well with my [Vector](https://github.com/robosam2003/Vector) class.
```cpp
#include "FIFO.h"
#include "Fifo.h"
#include "Vector.h"
Fifo<Vector<float, 3>, 64> e1;
e1.push(v);

// C-style casts for both Vector and Fifo in one cast.
Fifo<Vector<double, 6>, 256> e2 = (Fifo<Vector<double, 6>, 256>) e1;

(e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : e2.pop();
Fifo<Vector<float, 3>, 64> e1;
e1.push(v);

// C-style casts for both Vector and Fifo in one cast.
Fifo<Vector<double, 6>, 256> e2 = (Fifo<Vector<double, 6>, 256>) e1;

(e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : e2.pop();
```

### Dynamic Fifo
`dynamicFifo` is an identical to `Fifo` in functionality, but uses dynamic memory allocation, not a template.
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))

```cpp
#include "dynamicFifo.h"
// dynamicFifo is initialised with a template type and a parameter template
Fifo<float> e1 (256);
e1.push(3.14159);

// C-style casts. - does not resize the fifo.
Fifo<double> e2 = (Fifo<double>) e1;

(e2.fifo_status() == Fifo_STATUS::Fifo_EMPTY) ? /* Handle the fact that it's empty. */ : e2.pop();
std::cout << "e2 Free space: " << e2.free_space() << std::endl;
```
169 changes: 169 additions & 0 deletions dynamicFifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#ifndef DYNAMIC_FIFO_H
#define DYNAMIC_FIFO_H

#include <iostream>


enum class Fifo_STATUS {
Fifo_FULL,
Fifo_EMPTY,
Fifo_GOOD
};

template<class T>
class Fifo { /// essentially a circular fifo
private:
T* elem;
int nextFree;
int endPointer;
unsigned int sz;
public:
/**
* @brief Construct a new Fifo object
* @param s
*/
explicit Fifo(int s);

/* *//**
* @brief Construct a new Fifo object from an initializer list
* @param lst
*//*
Fifo(std::initializer_list<T> lst);*/


/**
* @brief Copy constructor for Fifo
* @param a
*/
Fifo(const Fifo& a); // copy constructor

/**
* @brief Copy assignment constructor for Fifo
* @param a
* @return Fifo&
*/
Fifo& operator=(const Fifo& a); // copy assignment

/**
* @brief Move constructor for Fifo
* @param a
*/
Fifo(Fifo&& a) noexcept ; // move constructor

/**
* @brief Move assignment constructor for Fifo
* @param a
* @return Fifo&
*/
Fifo& operator=(Fifo&& a) noexcept; // move assignment


/**
* @brief C-style cast operator for dynamicFifo. Does not resize the fifo. Usage: e.g.
* @example Fifo\<double\> g;\n Fifo\<float\> f = (Fifo\<float\>) g;
* @return Fifo_STATUS
*/
template <typename D>
explicit operator Fifo<D>() const;

/**
* @brief operator []
* @param i
* @return T&
*/
T& operator[](int i);

/**
* @brief operator [] const
* @param i
* @return const T&
*/
T& operator[](int i) const;

/**
* @brief Get the T at index i of the Fifo
* @param i
* @return The T at index i of the Fifo (not elem)
*/
T& atFifoIndex(int i);

/**
* @brief Get the T at index i of the Fifo
* @param i
* @return The T at index i of the Fifo (not elem)
*/
T& atFifoIndex(int i) const;

/**
* @brief Push an item to the Fifo
* @param item
* @return Fifo Status enum class of state of push (FULL, GOOD, etc)
*/
Fifo_STATUS push(const T& item);

/**
* @brief Pop an item off the Fifo
* @return the item popped off the Fifo
*/
T pop();

// end
/**
* @brief Peek at the back of the Fifo (the next thing to be popped)
* @return the next thing to be popped
*/
T peekBack() const;

/**
* @brief Peek at the back of the Fifo i times in
* @param i
* @return The item i times from the back of the Fifo
*/
T peekBack(int i) const;

// front
/**
* @brief Peek at the front of the Fifo (the thing you just pushed)
* @return The item at the front of the fifo (the thing you just pushed)
*/
T peekFront() const;

/**
* @brief Peek at the front of the Fifo i times in.
* @param i
* @return The item i times in from the Front of the Fifo.
*/
T peekFront(int i) const;

/**
* @brief Get the status of the Fifo, (GOOD, EMPTY, FULL)
* @return The status of the Fifo (of type: Fifo_STATUS)
*/
Fifo_STATUS fifo_status() const;

/**
* @brief Get the size of the Fifo
* @return The size of the Fifo
*/
int size() const;

/**
* @brief Get the free space left in the Fifo
* @return The free space in the Fifo
*/
int free_space() const;

/**
* @brief Get the used space in the Fifo
* @return The used space in the Fifo
*/
int used_space() const;


~Fifo() { delete[] elem; } // destructor
};

#include "dynamicFifo.tpp" // implementation file


#endif //DYNAMIC_FIFO_H
Loading

0 comments on commit 19f4344

Please sign in to comment.