Skip to content
Snippets Groups Projects
Commit fba1b7c5 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Introduce class-oriented list exception management

parent fc4b82fe
No related branches found
No related tags found
No related merge requests found
/*! \file List.h /*! \file List.h
*/ */
#ifndef LIST_OUT_OF_BOUNDS_EXCEPTION #ifndef INCLUDE_LIST_H_
#define LIST_OUT_OF_BOUNDS_EXCEPTION 1 #define INCLUDE_LIST_H_
#endif
#include <exception>
#include <string>
/**
* \brief Exception for out of bounds list requests.
*/
class ListOutOfBoundsException: public std::exception {
protected:
int min_index, max_index, requested_index;
public:
/**
* \brief Exception instance constructor.
*
* \param requested: `int` The index that was requested.
* \param min: `int` The minimum index allowed by the list.
* \param max: `int` The maximum index allowed by the list.
*/
ListOutOfBoundsException(int requested, int min, int max) {
min_index = min;
max_index = max;
requested_index = requested;
}
/**
* \brief Exception message.
*/
virtual const char* what() const throw() {
std::string message = "Error: requested index ";
message += requested_index;
message += " is out of range [";
message += min_index;
message += ", ";
message += (max_index - 1);
message += "]";
return message.c_str();
}
};
/** /**
* \brief A class to represent dynamic lists. * \brief A class to represent dynamic lists.
...@@ -52,7 +89,7 @@ template<class T> class List { ...@@ -52,7 +89,7 @@ template<class T> class List {
size = length; size = length;
first = new element; first = new element;
first->p_prev = NULL; first->p_prev = NULL;
element *current = first; current = first;
element *p_prev = first; element *p_prev = first;
for (int i = 1; i < size; i++) { for (int i = 1; i < size; i++) {
current = new element; current = new element;
...@@ -103,7 +140,7 @@ template<class T> class List { ...@@ -103,7 +140,7 @@ template<class T> class List {
*/ */
T get(int index) { T get(int index) {
if (index < 0 || index > size - 1) { if (index < 0 || index > size - 1) {
throw LIST_OUT_OF_BOUNDS_EXCEPTION; throw ListOutOfBoundsException(index, 0, size - 1);
} }
current = last; current = last;
for (int i = size - 1; i > index; i--) current = current->p_prev; for (int i = size - 1; i > index; i--) current = current->p_prev;
...@@ -131,7 +168,7 @@ template<class T> class List { ...@@ -131,7 +168,7 @@ template<class T> class List {
*/ */
void set(int index, T value) { void set(int index, T value) {
if (index < 0 || index > size - 1) { if (index < 0 || index > size - 1) {
throw LIST_OUT_OF_BOUNDS_EXCEPTION; throw ListOutOfBoundsException(index, 0, size - 1);
} }
current = last; current = last;
for (int i = size - 1; i > index; i--) current = current->p_prev; for (int i = size - 1; i > index; i--) current = current->p_prev;
...@@ -161,3 +198,5 @@ template<class T> class List { ...@@ -161,3 +198,5 @@ template<class T> class List {
return array; return array;
} }
}; };
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment