Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ set(SOURCES
src/CreaDispositivo.cpp
src/Dispositivo.cpp
src/RicercaDispositivo.cpp
src/main.cpp
src/mainNode.cpp
src/LinkedList.cpp
)

set(HEADERS
include/CreaDispositivo.h
include/Dispositivo.h
include/ListaDispositivi.h
include/RicercaDispositivo.h
include/LinkedList.h
Comment on lines 21 to +24
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: La documentazione per LinkedList sarebbe utile.

Sarebbe utile aggiungere documentazione che spieghi lo scopo e l'utilizzo della classe LinkedList. Questo aiuterà altri sviluppatori a capire come si inserisce nel progetto complessivo.

Implementazione suggerita:

/**
 * @file LinkedList.h
 * @brief Un'implementazione generica di lista concatenata per la gestione di raccolte di elementi
 *
 * Questa classe fornisce un'implementazione standard di una struttura dati di lista concatenata che può
 * essere utilizzata per memorizzare e gestire sequenze di elementi. Supporta operazioni di base
 * come inserimento, eliminazione e attraversamento degli elementi.
 *
 * Esempio di utilizzo:
 * @code
 * LinkedList<int> list;
 * list.add(1);
 * list.add(2);
 * list.remove(1);
 * @endcode
 */

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

La modifica precedente aggiunge la documentazione a livello di file. Dovresti inoltre aggiungere documentazione per:

  1. La classe LinkedList stessa utilizzando commenti in stile Doxygen simili prima della dichiarazione della classe
  2. Singoli metodi nella classe LinkedList
  3. Eventuali variabili membro importanti

La documentazione esatta dipenderà dai dettagli di implementazione effettivi della tua classe LinkedList, che non posso vedere nel frammento di codice fornito.

Esempio di documentazione a livello di classe da aggiungere:

/**
 * @class LinkedList
 * @brief Una classe contenitore di lista concatenata con template
 *
 * @tparam T Il tipo di elementi memorizzati nella lista concatenata
 *
 * Questa classe implementa una lista concatenata singola/doppia che fornisce
 * operazioni standard di lista. Mantiene puntatori interni alla testa e alla coda
 * della lista per operazioni efficienti.
 */
Original comment in English

suggestion: Documentation for LinkedList would be helpful.

It would be beneficial to add documentation explaining the purpose and usage of the LinkedList class. This will help other developers understand how it fits into the overall project.

Suggested implementation:

/**
 * @file LinkedList.h
 * @brief A generic linked list implementation for managing collections of elements
 *
 * This class provides a standard linked list data structure implementation that can
 * be used to store and manage sequences of elements. It supports basic operations
 * like insertion, deletion, and traversal of elements.
 *
 * Usage example:
 * @code
 * LinkedList<int> list;
 * list.add(1);
 * list.add(2);
 * list.remove(1);
 * @endcode
 */

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

The above change adds file-level documentation. You should also add documentation for:

  1. The LinkedList class itself using similar Doxygen-style comments before the class declaration
  2. Individual methods in the LinkedList class
  3. Any important member variables

The exact documentation will depend on the actual implementation details of your LinkedList class, which I cannot see in the provided code snippet.

Example of class-level documentation to add:

/**
 * @class LinkedList
 * @brief A templated linked list container class
 *
 * @tparam T The type of elements stored in the linked list
 *
 * This class implements a singly/doubly linked list that provides
 * standard list operations. It maintains internal pointers to the
 * head and tail of the list for efficient operations.
 */

)

# Crea l'eseguibile
Expand Down
13 changes: 0 additions & 13 deletions include/DispositivoNonTrovatoException.h

This file was deleted.

17 changes: 6 additions & 11 deletions include/LinkedList.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include "CreaDispositivo.h"
#include "DispositivoNonTrovatoException.h"
#include "ListaVuotaException.h"
#include <iostream>
#include <ostream>

#include <exception>

class LinkedList
{
Expand All @@ -18,22 +16,18 @@ class LinkedList
Dispositivo* disp;
Node* prev;
Node* next;

Node(Dispositivo& data);

//copy constructor
Node(const Node& data);

//copy assignment
Node& operator=(const Node& data);

~Node();
};

Node* head;
Node* tail;

//Funzioni private
Node* searchDispositivo(const std::string nome) const; //cerca un dispositivo nella lista
Node* searchDispositivoName(const std::string nome) const; //cerca un dispositivo nella lista by NAME
Node* searchDispositivoId(const int id) const; //cerca un dispositivo nella lista by ID
void connectBefore(Node* p, Node* q); //connette prima di p il nodo q

public:
Expand All @@ -43,7 +37,8 @@ class LinkedList

//Funzioni membro utili
void insert(Dispositivo& dispositivo); //inserisce un dispositivo in coda
Dispositivo* removeDispositivo(const std::string nome); //rimuove un dispositivo dalla lista accettando il nome del dispositivo
Dispositivo* removeDispositivoName(const std::string nome); //rimuove un dispositivo dalla lista accettando il nome del dispositivo by NAME
Dispositivo* removeDispositivoId(const int id); //rimuove un dispositivo dalla lista accettando il nome del dispositivo by ID
std::vector<Dispositivo*> removeAllDispositiviOff(const int currentTime); //rimuove tutti i dispositivi spenti (la cui ora e' prima dell'orario indicato)
void removeTimer(const std::string nome); //rimuove il timer di un dispositivo
void removeAllTimers(); //rimuove tutti i timer
Expand Down
12 changes: 0 additions & 12 deletions include/ListaVuotaException.h

This file was deleted.

111 changes: 74 additions & 37 deletions src/LinkedList.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
#include "../include/LinkedList.h"
using namespace std;

LinkedList::Node::Node(Dispositivo& data): prev{nullptr}, next{nullptr}, disp{&data}
{ }

LinkedList::Node::Node(const Node& data): prev{data.prev}, next{data.next}, disp{data.disp}
LinkedList::LinkedList(): head{nullptr}, tail{nullptr}
{ }

LinkedList::Node& LinkedList::Node::operator=(const Node& data)
{
prev = data.prev;
next = data.next;
disp = data.disp;
return *this;
}

LinkedList::Node::~Node()
{
delete disp;
prev = nullptr;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Il distruttore di Node non elimina più il puntatore disp - per favore chiarisci la semantica di proprietà

Se LinkedList dovrebbe possedere gli oggetti Dispositivo, questo potrebbe causare memory leak. Se non è così, l'implementazione precedente era scorretta. Per favore documenta il modello di proprietà previsto.

Original comment in English

issue (bug_risk): The Node destructor no longer deletes the disp pointer - please clarify ownership semantics

If LinkedList is supposed to own the Dispositivo objects, this could cause memory leaks. If not, the previous implementation was incorrect. Please document the intended ownership model.

next = nullptr;
}

LinkedList::LinkedList(): head{nullptr}, tail{nullptr}
{ }

LinkedList::LinkedList(Dispositivo& dispositivo)
{
head = tail = new Node(dispositivo);
Expand Down Expand Up @@ -62,71 +51,98 @@ void LinkedList::insert(Dispositivo& dispositivo)
}
}

Dispositivo* LinkedList::removeDispositivo(const string nome)
Dispositivo* LinkedList::removeDispositivoName(const std::string nome)
{
if(isEmpty())
{
throw std::out_of_range("Lista vuota!");
}

Node* current = searchDispositivoName(nome);

if(current == head)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Il caso di un singolo elemento non è gestito nelle funzioni di rimozione

Quando head == tail (la lista ha un solo elemento), il codice corrente causerà una dereferenziazione di puntatore nullo. Aggiungi un caso speciale per le liste con un singolo elemento.

Original comment in English

issue: Single element case not handled in remove functions

When head == tail (list has one element), the current code will cause null pointer dereference. Add a special case for single-element lists.

{
head = head->next;
head->prev = nullptr;
}
else if(current == tail)
{
tail = tail->prev;
tail->next = nullptr;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
}

Dispositivo* disp = current->disp;
delete current;
return disp;
}

Dispositivo* LinkedList::removeDispositivoId(const int id)
{
if(isEmpty())
{
throw ListaVuotaException();
throw std::out_of_range("Lista vuota!");
}

Node* current = searchDispositivo(nome);
Node* current = searchDispositivoId(id);

if(current == head)
{
head = head->next;
head->prev = nullptr;
return current->disp;
}
else if(current == tail)
{
tail = tail->prev;
tail->next = nullptr;
return current->disp;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
return current->disp;
}

Dispositivo* disp = current->disp;
delete current;
return disp;
}

vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
std::vector<Dispositivo*> LinkedList::removeAllDispositiviOff(const int currentTime)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Modificare la lista durante l'iterazione può portare a un comportamento indefinito

L'implementazione corrente modifica la struttura della lista durante l'iterazione. Considera di raccogliere prima i nodi da rimuovere, quindi rimuoverli in un passaggio separato.

Original comment in English

issue (bug_risk): Modifying list while iterating can lead to undefined behavior

The current implementation modifies the list structure while iterating through it. Consider collecting nodes to remove first, then removing them in a separate pass.

{
if(isEmpty())
{
throw ListaVuotaException();
throw std::out_of_range("Lista vuota!");
}

vector<Dispositivo*> dispositiviSpenti;
std::vector<Dispositivo*> dispositiviSpenti;
Node* current = head;
while(current)
{
if(current->disp->getOrarioSpegnimento() <= currentTime)
{
dispositiviSpenti.push_back(current->disp);
Node* temp = current;
current = current->next;
removeDispositivo(temp->disp->getNome());
}
else
{
current = current->next;
removeDispositivoName(temp->disp->getNome());
}
current = current->next;
}

return dispositiviSpenti;
}


void LinkedList::removeTimer(const string nome)
void LinkedList::removeTimer(const std::string nome)
{
if(isEmpty())
{
throw ListaVuotaException();
throw std::out_of_range("Lista vuota!");;
}

Node* current = searchDispositivo(nome);
Node* current = searchDispositivoName(nome);

current->disp->setTimerOff();
}
Expand All @@ -135,7 +151,7 @@ void LinkedList::removeAllTimers()
{
if(isEmpty())
{
throw ListaVuotaException();
throw std::out_of_range("Lista vuota!");;
}

Node* current = head;
Expand All @@ -151,7 +167,7 @@ bool LinkedList::isEmpty() const
return (head == nullptr);
}

ostream& operator<<(ostream& os, const LinkedList& list)
std::ostream& operator<<(std::ostream& os, const LinkedList& list)
{
if(list.isEmpty())
{
Expand Down Expand Up @@ -180,11 +196,11 @@ void LinkedList::connectBefore(Node* p, Node* q)
p->prev = q;
}

LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const
LinkedList::Node* LinkedList::searchDispositivoName(const std::string nome) const
{
if(isEmpty())
{
throw ListaVuotaException();
throw std::out_of_range("Lista vuota!");;
}

Node* current = head;
Expand All @@ -195,7 +211,28 @@ LinkedList::Node* LinkedList::searchDispositivo(const std::string nome) const

if(current == nullptr)
{
throw DispositivoNonTrovatoException();
throw std::invalid_argument("Dispositivo non trovato!");;
}

return current;
}

LinkedList::Node* LinkedList::searchDispositivoId(const int id) const
{
if(isEmpty())
{
throw std::out_of_range("Lista vuota!");;
}

Node* current = head;
while(current && current->disp->getId() != id)
{
current = current->next;
}

if(current == nullptr)
{
throw std::invalid_argument("Dispositivo non trovato!");;
}

return current;
Expand Down
84 changes: 44 additions & 40 deletions src/mainNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,48 @@ using namespace std;

int main()
{
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale
Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min
Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min
Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min

LinkedList list = LinkedList();

list.insert(Frigorifero);
cout << list << endl;

list.insert(Lavatrice);
cout << list << endl;

list.insert(ScaldaBagno);
cout << list << endl;

list.insert(Televisore);
cout << list << endl;

list.insert(FornoMicroonde);
cout << list << endl;

list.insert(Asciugatrice);
cout << list << endl;

list.removeDispositivo("Scaldabagno");
cout << list << endl;

cout << Televisore.getOrarioSpegnimento() << endl;

list.removeAllDispositiviOff(5);
cout << list << endl;

list.removeTimer("Asciugatrice");
cout << list << endl;
cout << Asciugatrice.getOrarioSpegnimento() << endl;


try {
Dispositivo Frigorifero = CreaDispositivo::creaDispositivo("Frigrifero", 76); //Manuale
Dispositivo Lavatrice = CreaDispositivo::creaDispositivo("Lavtrice", 8); //110 min
Dispositivo ScaldaBagno = CreaDispositivo::creaDispositivo("Scalda bagno", 9); //Manuale
Dispositivo Televisore = CreaDispositivo::creaDispositivo("Televisre", 4); //60 min
Dispositivo Asciugatrice = CreaDispositivo::creaDispositivo("Asciugatrice", 2); //60 min
Dispositivo FornoMicroonde = CreaDispositivo::creaDispositivo("Forno a microonde", 1); //2 min

LinkedList list = LinkedList();

list.insert(Frigorifero);
cout << list << endl;

list.insert(Lavatrice);
cout << list << endl;

list.insert(ScaldaBagno);
cout << list << endl;

list.insert(Televisore);
cout << list << endl;

list.insert(FornoMicroonde);
cout << list << endl;

list.insert(Asciugatrice);
cout << list << endl;

list.removeDispositivoName("Scaldabagno");
cout << list << endl;

cout << Televisore.getOrarioSpegnimento() << endl;

list.removeAllDispositiviOff(5);
cout << list << endl;

list.removeTimer("Asciugatrice");
cout << list << endl;
cout << Asciugatrice.getOrarioSpegnimento() << endl;
}
catch (exception& e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Il gestore delle eccezioni dovrebbe registrare i dettagli dell'errore

Considera di registrare i dettagli dell'eccezione utilizzando e.what() prima di restituire, per aiutare nel debug.

Implementazione suggerita:

    catch (exception& e) {
        cerr << "Errore: " << e.what() << endl;
        return 1;
    }

Se la base di codice utilizza un framework di registrazione specifico invece dell'output diretto su cerr, dovresti sostituire cerr con il meccanismo di registrazione appropriato per essere coerente con il resto della base di codice.

Original comment in English

suggestion (bug_risk): Exception handler should log error details

Consider logging the exception details using e.what() before returning, to help with debugging.

Suggested implementation:

    catch (exception& e) {
        cerr << "Error: " << e.what() << endl;
        return 1;
    }

If the codebase uses a specific logging framework instead of direct cerr output, you should replace cerr with the appropriate logging mechanism for consistency with the rest of the codebase.

return 1;
}
return 0;
}
Loading