slight changes ,size t replacement

This commit is contained in:
Robin Müller 2020-09-29 16:10:35 +02:00
parent cdadf48f38
commit 17d5de15c9

View File

@ -1,15 +1,15 @@
#ifndef ARRAYLIST_H_ #ifndef FSFW_CONTAINER_ARRAYLIST_H_
#define ARRAYLIST_H_ #define FSFW_CONTAINER_ARRAYLIST_H_
#include "../returnvalues/HasReturnvaluesIF.h" #include "../returnvalues/HasReturnvaluesIF.h"
#include "../serialize/SerializeAdapter.h" #include "../serialize/SerializeAdapter.h"
#include "../serialize/SerializeIF.h" #include "../serialize/SerializeIF.h"
/** /**
* A List that stores its values in an array. * @brief A List that stores its values in an array.
* * @details
* The backend is an array that can be allocated by the class itself or supplied via ctor. * The underlying storage is an array that can be allocated by the class
* * itself or supplied via ctor.
* *
* @ingroup container * @ingroup container
*/ */
@ -21,94 +21,8 @@ public:
static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01); static const ReturnValue_t FULL = MAKE_RETURN_CODE(0x01);
/** /**
* An Iterator to go trough an ArrayList * This is the allocating constructor.
*
* It stores a pointer to an element and increments the
* pointer when incremented itself.
*/
class Iterator {
public:
/**
* Empty ctor, points to NULL
*/
Iterator() :
value(0) {
}
/**
* Initializes the Iterator to point to an element
*
* @param initialize
*/
Iterator(T *initialize) {
value = initialize;
}
/**
* The current element the iterator points to
*/
T *value;
Iterator& operator++() {
value++;
return *this;
}
Iterator operator++(int) {
Iterator tmp(*this);
operator++();
return tmp;
}
Iterator& operator--() {
value--;
return *this;
}
Iterator operator--(int) {
Iterator tmp(*this);
operator--();
return tmp;
}
T& operator*(){
return *value;
}
const T& operator*() const{
return *value;
}
T *operator->(){
return value;
}
const T *operator->() const{
return value;
}
//SHOULDDO this should be implemented as non-member
bool operator==(const typename ArrayList<T, count_t>::Iterator& other) const{
return (value == other.value);
}
//SHOULDDO this should be implemented as non-member
bool operator!=(const typename ArrayList<T, count_t>::Iterator& other) const {
return !(*this == other);
}
};
/**
* Number of Elements stored in this List
*/
count_t size;
/**
* This is the allocating constructor;
*
* It allocates an array of the specified size. * It allocates an array of the specified size.
*
* @param maxSize * @param maxSize
*/ */
ArrayList(count_t maxSize) : ArrayList(count_t maxSize) :
@ -119,7 +33,8 @@ public:
/** /**
* This is the non-allocating constructor * This is the non-allocating constructor
* *
* It expects a pointer to an array of a certain size and initializes itself to it. * It expects a pointer to an array of a certain size and initializes
* itself to it.
* *
* @param storage the array to use as backend * @param storage the array to use as backend
* @param maxSize size of storage * @param maxSize size of storage
@ -129,6 +44,20 @@ public:
size(size), entries(storage), maxSize_(maxSize), allocated(false) { size(size), entries(storage), maxSize_(maxSize), allocated(false) {
} }
/**
* Copying is forbiden by declaring copy ctor and copy assignment deleted
* It is too ambigous in this case.
* (Allocate a new backend? Use the same? What to do in an modifying call?)
*/
ArrayList(const ArrayList& other) = delete;
const ArrayList& operator=(const ArrayList& other) = delete;
/**
* Number of Elements stored in this List
*/
count_t size;
/** /**
* Destructor, if the allocating constructor was used, it deletes the array. * Destructor, if the allocating constructor was used, it deletes the array.
*/ */
@ -138,6 +67,85 @@ public:
} }
} }
/**
* An Iterator to go trough an ArrayList
*
* It stores a pointer to an element and increments the
* pointer when incremented itself.
*/
class Iterator {
public:
/**
* Empty ctor, points to NULL
*/
Iterator(): value(0) {}
/**
* Initializes the Iterator to point to an element
*
* @param initialize
*/
Iterator(T *initialize) {
value = initialize;
}
/**
* The current element the iterator points to
*/
T *value;
Iterator& operator++() {
value++;
return *this;
}
Iterator operator++(int) {
Iterator tmp(*this);
operator++();
return tmp;
}
Iterator& operator--() {
value--;
return *this;
}
Iterator operator--(int) {
Iterator tmp(*this);
operator--();
return tmp;
}
T& operator*() {
return *value;
}
const T& operator*() const {
return *value;
}
T *operator->() {
return value;
}
const T *operator->() const {
return value;
}
//SHOULDDO this should be implemented as non-member
bool operator==(const typename
ArrayList<T, count_t>::Iterator& other) const {
return (value == other.value);
}
//SHOULDDO this should be implemented as non-member
bool operator!=(const typename
ArrayList<T, count_t>::Iterator& other) const {
return !(*this == other);
}
};
/** /**
* Iterator pointing to the first stored elmement * Iterator pointing to the first stored elmement
* *
@ -191,7 +199,7 @@ public:
* *
* @return maximum number of elements * @return maximum number of elements
*/ */
uint32_t maxSize() const { size_t maxSize() const {
return this->maxSize_; return this->maxSize_;
} }
@ -226,19 +234,7 @@ public:
count_t remaining() { count_t remaining() {
return (maxSize_ - size); return (maxSize_ - size);
} }
private:
/**
* This is the copy constructor
*
* It is private, as copying is too ambigous in this case. (Allocate a new backend? Use the same?
* What to do in an modifying call?)
*
* @param other
*/
ArrayList(const ArrayList& other) :
size(other.size), entries(other.entries), maxSize_(other.maxSize_), allocated(
false) {
}
protected: protected:
/** /**
* pointer to the array in which the entries are stored * pointer to the array in which the entries are stored
@ -247,12 +243,12 @@ protected:
/** /**
* remembering the maximum size * remembering the maximum size
*/ */
uint32_t maxSize_; size_t maxSize_;
/** /**
* true if the array was allocated and needs to be deleted in the destructor. * true if the array was allocated and needs to be deleted in the destructor.
*/ */
bool allocated; bool allocated;
}; };
#endif /* ARRAYLIST_H_ */
#endif /* FSFW_CONTAINER_ARRAYLIST_H_ */