diff --git a/src/Media/Album.cpp b/src/Media/Album.cpp index d19e638..ef163d7 100644 --- a/src/Media/Album.cpp +++ b/src/Media/Album.cpp @@ -26,64 +26,57 @@ bool Album::operator==(const Media &other) const { return false; } -std::vector> Album::filter(const std::vector>& input) const { - std::vector> result; +std::unique_ptr Album::clone() const { + return std::make_unique(*this); +} - // Riutilizzo filtro base di Media - std::vector> baseInput(input.begin(), input.end()); - std::vector> filteredBase = Media::filter(baseInput); - // Filtro specifico per Album - for (const auto& mediaPtr : filteredBase) { - auto albumPtr = std::dynamic_pointer_cast(mediaPtr); - if (!albumPtr) continue; +bool Album::filter(const Media& album) const { + // Riutilizzo filtro base di Media + if (!Media::filter(album)) + return false; - bool match = true; + // Cast to Album to access Album-specific members + const Album* albumPtr = dynamic_cast(&album); + if (!albumPtr) + return false; - // Band - if (!band_.empty() && !stringContainsIgnoreCase(albumPtr->getBand(), band_)) - match = false; + // Band + if (!band_.empty() && !stringContainsIgnoreCase(albumPtr->getBand(), band_)) + return false; - // Band members (ogni membro richiesto deve matchare almeno uno esistente) - if (!band_members_.empty()) { - for (const auto& memberFilter : band_members_) { - bool found = false; - for (const auto& m : albumPtr->getBandMembers()) { - if (stringContainsIgnoreCase(m, memberFilter)) { - found = true; - break; - } - } - if (!found) { - match = false; + // Band members (ogni membro richiesto deve matchare almeno uno esistente) + if (!band_members_.empty()) { + for (const auto& memberFilter : band_members_) { + bool found = false; + for (const auto& m : albumPtr->getBandMembers()) { + if (stringContainsIgnoreCase(m, memberFilter)) { + found = true; break; } } + if (!found) { + return false; + } } + } - // Songs (ogni canzone richiesta deve matchare almeno una esistente) - if (!songs_.empty()) { - for (const auto& songFilter : songs_) { - bool found = false; - for (const auto& s : albumPtr->getSongs()) { - if (stringContainsIgnoreCase(s, songFilter)) { - found = true; - break; - } - } - if (!found) { - match = false; + // Songs (ogni canzone richiesta deve matchare almeno una esistente) + if (!songs_.empty()) { + for (const auto& songFilter : songs_) { + bool found = false; + for (const auto& s : albumPtr->getSongs()) { + if (stringContainsIgnoreCase(s, songFilter)) { + found = true; break; } } + if (!found) { + return false; + } } - - if (match) - result.push_back(albumPtr); } - - return result; + return true; } - } // namespace media diff --git a/src/Media/Album.h b/src/Media/Album.h index da0fd7c..6efe82a 100644 --- a/src/Media/Album.h +++ b/src/Media/Album.h @@ -20,8 +20,11 @@ class Album : public Media { const std::vector &getSongs() const; bool operator==(const Media &other) const override; - std::vector> filter(const std::vector>& input) const; + std::unique_ptr clone() const override; + + bool filter(const Media& album) const override; }; } // namespace media + #endif diff --git a/src/Media/AudioBook.cpp b/src/Media/AudioBook.cpp index 89f43ca..f9d91f8 100644 --- a/src/Media/AudioBook.cpp +++ b/src/Media/AudioBook.cpp @@ -21,6 +21,10 @@ bool AudioBook::operator==(const Media& other) const { return false; } +std::unique_ptr AudioBook::clone() const { + return std::make_unique(*this); +} + std::string AudioBook::getNarrator() const { return narrator_; } @@ -37,33 +41,21 @@ void AudioBook::setStreamingService(const std::string& service) { streamingService_ = service; } -std::vector> AudioBook::filter(const std::vector>& input) const { - std::vector> result; - - // Riutilizza filtro base di Novel (che include filtro di Media) - std::vector> novels(input.begin(), input.end()); - std::vector> filteredNovels = Novel::filter(novels); +bool AudioBook::filter(const Media& input) const { + if (!Novel::filter(input)) + return false; + const AudioBook* audiobookPtr = dynamic_cast(&input); + if (!audiobookPtr) + return false; // Protegge da cast fallito // Filtro specifico AudioBook - for (const auto& novelPtr : filteredNovels) { - auto audiobookPtr = std::dynamic_pointer_cast(novelPtr); - if (!audiobookPtr) continue; - - bool match = true; + if (!narrator_.empty() && !stringContainsIgnoreCase(audiobookPtr->getNarrator(), narrator_)) + return false; - // File size - if (narrator_ != "" && audiobookPtr->getNarrator()!= narrator_) - match = false; - - // DRM - if (streamingService_ != "" && audiobookPtr->getStreamingService() != streamingService_) - match = false; - - if (match) - result.push_back(audiobookPtr); - } + if (!streamingService_.empty() && !stringContainsIgnoreCase(audiobookPtr->getStreamingService(), streamingService_)) + return false; - return result; + return true; } } \ No newline at end of file diff --git a/src/Media/AudioBook.h b/src/Media/AudioBook.h index 38d77fa..2012e99 100644 --- a/src/Media/AudioBook.h +++ b/src/Media/AudioBook.h @@ -26,7 +26,9 @@ class AudioBook : public Novel { void setNarrator(const std::string& narrator); void setStreamingService(const std::string& service); - std::vector> filter(const std::vector>& input) const; + std::unique_ptr clone() const override; + + bool filter(const Media& audiobook) const override; }; } diff --git a/src/Media/Ebook.cpp b/src/Media/Ebook.cpp index 60a10c4..cb4d562 100644 --- a/src/Media/Ebook.cpp +++ b/src/Media/Ebook.cpp @@ -36,34 +36,28 @@ void Ebook::setDrm(bool drm) { drm_ = drm; } -std::vector> Ebook::filter(const std::vector>& input) const { - std::vector> result; - - // Riutilizza filtro base di Novel (che include filtro di Media) - std::vector> novels(input.begin(), input.end()); - std::vector> filteredNovels = Novel::filter(novels); - - // Filtro specifico Ebook - for (const auto& novelPtr : filteredNovels) { - auto ebookPtr = std::dynamic_pointer_cast(novelPtr); - if (!ebookPtr) continue; - - bool match = true; +std::unique_ptr Ebook::clone() const { + return std::make_unique(*this); +} - // File size - if (fileSizeBytes_ > 0 && ebookPtr->getFileSizeBytes() != fileSizeBytes_) - match = false; +bool Ebook::filter(const Media& input) const { + // Riutilizzo filtro base di Novel + if (!Novel::filter(input)) + return false; + // Cast to Ebook to access Ebook-specific members + const Ebook* ebookPtr = dynamic_cast(&input); + if (!ebookPtr) + return false; - // DRM - if (drm_ && ebookPtr->hasDrm() != drm_) - match = false; + // File size filter + if (fileSizeBytes_ > 0 && ebookPtr->getFileSizeBytes() != fileSizeBytes_) + return false; - if (match) - result.push_back(ebookPtr); - } + // DRM filter + if (drm_ && ebookPtr->hasDrm() != drm_) + return false; - return result; + return true; } - } \ No newline at end of file diff --git a/src/Media/Ebook.h b/src/Media/Ebook.h index d2ba138..e277746 100644 --- a/src/Media/Ebook.h +++ b/src/Media/Ebook.h @@ -25,7 +25,9 @@ class Ebook : public Novel { void setFileSizeBytes(unsigned int size); void setDrm(bool drm); - std::vector> filter(const std::vector>& input) const; + std::unique_ptr clone() const override; + + bool filter(const Media& ebook) const override; }; diff --git a/src/Media/IMedia.h b/src/Media/IMedia.h index d2ffa8b..677381b 100644 --- a/src/Media/IMedia.h +++ b/src/Media/IMedia.h @@ -8,6 +8,7 @@ namespace media { class IMedia { virtual void accept(IConstMediaVisitor &) const = 0; virtual bool open() = 0; + virtual std::unique_ptr clone() const = 0; }; } diff --git a/src/Media/Media.cpp b/src/Media/Media.cpp index 2fcdcae..3e7b8dc 100644 --- a/src/Media/Media.cpp +++ b/src/Media/Media.cpp @@ -18,6 +18,11 @@ Media::Media(const std::string &title, int release, const std::string &language, void Media::accept(IConstMediaVisitor &v) const {} +std::unique_ptr media::Media::clone() const { + return std::make_unique(*this); +} + + bool Media::operator==(const Media &other) const { return title_ == other.title_ && release_ == other.release_ && language_ == other.language_ && favourite_ == other.favourite_ && @@ -31,55 +36,42 @@ bool Media::open() { return false; } -std::vector> Media::filter(const std::vector>& input) const { - std::vector> result; - - for (const auto& mediaPtr : input) { - if (!mediaPtr) continue; - - const Media& media = *mediaPtr; - bool match = true; - +bool Media::filter(const Media& media) const { // Title (substring, case-insensitive) - if (!getTitle().empty() && !stringContainsIgnoreCase(media.getTitle(), getTitle())) - match = false; - - // Release (confronto stretto) - if (getRelease() != std::numeric_limits::min() && - media.getRelease() != getRelease()) - match = false; - - // Language (substring, case-insensitive) - if (!getLanguage().empty() && media.getLanguage() != getLanguage()) - match = false; - - // Favourite (confronto booleano) - if (isFavourite() && media.isFavourite() != isFavourite()) - match = false; - - // Generi (match parziale case-insensitive su ogni genere richiesto) - if (!getGenres().empty()) { - const auto& mediaGenres = media.getGenres(); - for (const auto& genreFilter : getGenres()) { - bool found = false; - for (const auto& g : mediaGenres) { - if (stringContainsIgnoreCase(g, genreFilter)) { - found = true; - break; - } - } - if (!found) { - match = false; + if (!getTitle().empty() && !stringContainsIgnoreCase(media.getTitle(), getTitle())) + return false; + + // Release (confronto stretto) + if (getRelease() != std::numeric_limits::min() && + media.getRelease() != getRelease()) + return false; + + // Language (substring, case-insensitive) + if (!getLanguage().empty() && media.getLanguage() != getLanguage()) + return false; + + // Favourite (confronto booleano) + if (isFavourite() && media.isFavourite() != isFavourite()) + return false; + + // Generi (match parziale case-insensitive su ogni genere richiesto) + if (!getGenres().empty()) { + const auto& mediaGenres = media.getGenres(); + for (const auto& genreFilter : getGenres()) { + bool found = false; + for (const auto& g : mediaGenres) { + if (stringContainsIgnoreCase(g, genreFilter)) { + found = true; break; } } + if (!found) { + return false; + } } + } - if (match) - result.push_back(mediaPtr); - } - - return result; + return true; } diff --git a/src/Media/Media.h b/src/Media/Media.h index 0d39960..7d607c4 100644 --- a/src/Media/Media.h +++ b/src/Media/Media.h @@ -44,7 +44,10 @@ class Media : IMedia{ const std::string &getImgPath() const; const std::string &getNotes() const; - std::vector> filter(const std::vector> &media) const; + virtual std::unique_ptr clone() const; + + + virtual bool filter(const Media &media) const; }; } // namespace media #endif diff --git a/src/Media/Movie.cpp b/src/Media/Movie.cpp index 6d83d6b..59652ab 100644 --- a/src/Media/Movie.cpp +++ b/src/Media/Movie.cpp @@ -18,55 +18,53 @@ bool Movie::operator==(const Media &other) const { } return false; } + +std::unique_ptr Movie::clone() const { + return std::make_unique(*this); +} + const std::vector &Movie::getCast() const { return cast_; } unsigned int Movie::getLength() const { return length_; } const std::string &Movie::getUniverse() const { return universe_; } -std::vector> Movie::filter(const std::vector>& input) const { - std::vector> result; - - // Riutilizza filtro base di Media (poiché Movie deriva da Media) - std::vector> baseInput(input.begin(), input.end()); - std::vector> filteredBase = Media::filter(baseInput); +bool Movie::filter(const Media& movie) const { - for (const auto& mediaPtr : filteredBase) { - auto moviePtr = std::dynamic_pointer_cast(mediaPtr); - if (!moviePtr) continue; + if (!Media::filter(movie)) + return false; - bool match = true; - - // Cast (match parziale, case-insensitive) - if (!cast_.empty()) { - const auto& movieCast = moviePtr->getCast(); - for (const auto& filterCast : cast_) { - bool found = false; - for (const auto& member : movieCast) { - if (stringContainsIgnoreCase(member, filterCast)) { - found = true; - break; - } - } - if (!found) { - match = false; + // Cast (match parziale, case-insensitive) + const Movie* moviePtr = dynamic_cast(&movie); + if (!moviePtr) { + return false; + } + if (!cast_.empty()) { + const auto& movieCast = moviePtr->getCast(); + for (const auto& filterCast : cast_) { + bool found = false; + for (const auto& member : movieCast) { + if (stringContainsIgnoreCase(member, filterCast)) { + found = true; break; } } + if (!found) { + return false; + } } - - // Length (confronto stretto) - if (length_ != std::numeric_limits::max() && moviePtr->getLength() != length_) - match = false; - - // Universe (match parziale, case-insensitive) - if (!universe_.empty() && !stringContainsIgnoreCase(moviePtr->getUniverse(), universe_)) - match = false; - - if (match) - result.push_back(moviePtr); } - return result; + // Length (confronto stretto) + if (length_ != std::numeric_limits::max() && moviePtr->getLength() != length_) + return false; + + // Universe (match parziale, case-insensitive) + if (!universe_.empty() && !stringContainsIgnoreCase(moviePtr->getUniverse(), universe_)) + return false; + + return true; } } // namespace media + + diff --git a/src/Media/Movie.h b/src/Media/Movie.h index c37d134..19a50d7 100644 --- a/src/Media/Movie.h +++ b/src/Media/Movie.h @@ -24,7 +24,10 @@ class Movie : public Media { unsigned int getLength() const; const std::string &getUniverse() const; - std::vector> filter(const std::vector>& input) const; + std::unique_ptr clone() const override; + + bool filter(const Media& movie) const override; }; } // namespace media #endif + diff --git a/src/Media/Novel.cpp b/src/Media/Novel.cpp index 88f9195..361585c 100644 --- a/src/Media/Novel.cpp +++ b/src/Media/Novel.cpp @@ -19,6 +19,10 @@ bool Novel::operator==(const Media& other) const { return false; } +std::unique_ptr Novel::clone() const { + return std::make_unique(*this); +} + // Getters std::string Novel::getAuthor() const { return author_; } std::string Novel::getPublisher() const { return publisher_; } @@ -34,45 +38,37 @@ void Novel::setSeries(const std::string& series) { series_ = series; } void Novel::setIsbn(const std::string& isbn) { isbn_ = isbn; } -std::vector> Novel::filter(const std::vector>& input) const { - std::vector> result; - - // Riutilizzo del filtro base di Media - std::vector> baseInput(input.begin(), input.end()); - std::vector> filteredBase = Media::filter(baseInput); - - // Filtro specifico per Novel - for (const auto& mediaPtr : filteredBase) { - auto novelPtr = std::dynamic_pointer_cast(mediaPtr); - if (!novelPtr) continue; +bool Novel::filter(const Media& input) const { + // Riutilizzo filtro base di Media + if (!Media::filter(input)) + return false; + // Cast to Novel to access Novel-specific members + const Novel* novelPtr = dynamic_cast(&input); + if (!novelPtr) + return false; - bool match = true; + // Match fields + // Autore + if (!author_.empty() && !stringContainsIgnoreCase(novelPtr->getAuthor(), author_)) + return false; - // Autore - if (!author_.empty() && !stringContainsIgnoreCase(novelPtr->getAuthor(), author_)) - match = false; + // Editore + if (!publisher_.empty() && !stringContainsIgnoreCase(novelPtr->getPublisher(), publisher_)) + return false; - // Editore - if (!publisher_.empty() && !stringContainsIgnoreCase(novelPtr->getPublisher(), publisher_)) - match = false; + // Serie + if (!series_.empty() && !stringContainsIgnoreCase(novelPtr->getSeries(), series_)) + return false; - // Serie - if (!series_.empty() && !stringContainsIgnoreCase(novelPtr->getSeries(), series_)) - match = false; + // ISBN + if (!isbn_.empty() && !stringContainsIgnoreCase(novelPtr->getIsbn(), isbn_)) + return false; - // ISBN - if (!isbn_.empty() && !stringContainsIgnoreCase(novelPtr->getIsbn(), isbn_)) - match = false; - - // Pagine (confronto stretto) - if (pages_ > 0 && novelPtr->getPages() != pages_) - match = false; - - if (match) - result.push_back(novelPtr); - } + // Pagine (confronto stretto) + if (pages_ != std::numeric_limits::max() && novelPtr->getPages() != pages_) + return false; - return result; + return true; } } \ No newline at end of file diff --git a/src/Media/Novel.h b/src/Media/Novel.h index 64f91c9..6fa0ad5 100644 --- a/src/Media/Novel.h +++ b/src/Media/Novel.h @@ -35,7 +35,9 @@ class Novel : public Media { void setSeries(const std::string& series); void setIsbn(const std::string& isbn); - std::vector> filter(const std::vector> &novel) const; + std::unique_ptr clone() const override; + + bool filter(const Media &novel) const override; }; } diff --git a/src/Media/Series.cpp b/src/Media/Series.cpp index 3dd0a1f..f0fa1a9 100644 --- a/src/Media/Series.cpp +++ b/src/Media/Series.cpp @@ -21,40 +21,36 @@ bool Series::operator==(const Media &other) const { } return false; } + +std::unique_ptr Series::clone() const { + return std::make_unique(*this); +} + unsigned int Series::getEpisodes() const { return episodes_; } unsigned int Series::getSeasons() const { return seasons_; } bool Series::isEnded() const { return ended_; } -std::vector> Series::filter(const std::vector>& input) const { - std::vector> result; - - // Riutilizzo filtro base di Movie (che a sua volta chiama Media::filter) - std::vector> baseInput(input.begin(), input.end()); - std::vector> filteredBase = Movie::filter(baseInput); +bool Series::filter(const Media& input) const { + if (!Movie::filter(input)) + return false; - for (const auto& mediaPtr : filteredBase) { - auto seriesPtr = std::dynamic_pointer_cast(mediaPtr); - if (!seriesPtr) continue; + const Series* seriesPtr = dynamic_cast(&input); + if (!seriesPtr) + return false; // Protegge da cast fallito - bool match = true; + // Episodes (confronto stretto) + if (episodes_ != std::numeric_limits::max() && seriesPtr->getEpisodes() != episodes_) + return false; - // Episodes (confronto stretto) - if (episodes_ != std::numeric_limits::max() && seriesPtr->getEpisodes() != episodes_) - match = false; + // Seasons (confronto stretto) + if (seasons_ != std::numeric_limits::max() && seriesPtr->getSeasons() != seasons_) + return false; - // Seasons (confronto stretto) - if (seasons_ != std::numeric_limits::max() && seriesPtr->getSeasons() != seasons_) - match = false; - - // Ended (confronto booleano) - if (ended_ && ended_ != seriesPtr->isEnded()) - match = false; - - if (match) - result.push_back(seriesPtr); - } + // Ended (confronto booleano) + if (ended_ && ended_ != seriesPtr->isEnded()) + return false; - return result; + return true; } } // namespace media diff --git a/src/Media/Series.h b/src/Media/Series.h index 42118bc..73fa611 100644 --- a/src/Media/Series.h +++ b/src/Media/Series.h @@ -20,8 +20,10 @@ class Series : public Movie { unsigned int getEpisodes() const; unsigned int getSeasons() const; bool isEnded() const; + + std::unique_ptr clone() const override; - std::vector> filter(const std::vector>& input) const; + bool filter(const Media& input) const override; }; } #endif // MEDIA_SERIES_H diff --git a/src/Memory/Database.cpp b/src/Memory/Database.cpp deleted file mode 100644 index 4744e0b..0000000 --- a/src/Memory/Database.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "Database.h" - -namespace memory { -DataBase::~DataBase() { - // può darsi che se ne occupi il distruttore di QFile - // comportamento di default: per ora metto che non vengono salvati i - // cambiamenti - close(false); -} -int DataBase::open(const QString &path) { - // QFile.close() non lancia errori se il file non è stato aperto - // file_.commit(); - file_.setFileName(path); - if (!file_.open(QIODevice::ReadOnly | QIODevice::Text)) { - // TODO(alessandro): possiamo creare una qualche enum per i codici di errore - return 1; - } - return 0; -} -int DataBase::close(bool save_on_exit) { - if (save_on_exit) { - // ritorna false se c'è stato un errore. Possiamo voler cambiare il tipo di - // ritorno del metodo. - file_.commit(); - } else { - // void cancelWriting() - file_.cancelWriting(); - } - return 0; -} - -int DataBase::save() { - // ritorna false in caso di fallimento - if (!file_.commit()) { - // errore in scrittura - return -1; - } - // riapri il file dopo averlo chiuso - // ritorna false in caso di fallimento - if (!file_.open(QIODevice::ReadOnly | QIODevice::Text)) { - return 1; - } - return 0; -} - -// qua possiamo volendo utilizzare riferimenti costanti al posto di puntatori -std::vector DataBase::filterMedia( - const media::Media *media_as_filter) { - return media_container_.filter(media_as_filter); -} - -} // namespace memory diff --git a/src/Memory/Database.h b/src/Memory/Database.h deleted file mode 100644 index d327857..0000000 --- a/src/Memory/Database.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MEMORY_DATABASE_H -#define MEMORY_DATABASE_H -#include - -#include "./MediaContainer.h" - -namespace memory { -class DataBase { - private: - MediaContainer media_container_; - QSaveFile file_; - - // possiamo mettere open, close e save privati e poi usare un tipo di observer - // pattern per chiamare le funzioni. - public: - DataBase() = default; - ~DataBase(); - int open(const QString &path); - int close(bool save); - int save(); - // TODO(alessandro): smart pointer - // possiamo cambiarlo a riferimenti costanti - std::vector filterMedia(const media::Media *); -}; -} // namespace memory -#endif diff --git a/src/Memory/Deserializer.h b/src/Memory/Deserializer.h index d44d427..fb074c9 100644 --- a/src/Memory/Deserializer.h +++ b/src/Memory/Deserializer.h @@ -7,11 +7,11 @@ namespace memory { class Deserializer { private: - static std::vector>& XMLtoVec(const QFile &); - static std::vector>& JSONtoVec(const QFile &); + static std::vector>& XMLtoVec(const QFile &){} + static std::vector>& JSONtoVec(const QFile &){} public: - static std::vector>& deserialize(const QFile &); + static std::vector>& deserialize(const QFile &){} }; } // namespace memory #endif diff --git a/src/Memory/MediaContainer.cpp b/src/Memory/MediaContainer.cpp index 7d9c177..1cff655 100644 --- a/src/Memory/MediaContainer.cpp +++ b/src/Memory/MediaContainer.cpp @@ -1,25 +1,39 @@ #include "MediaContainer.h" +#include +#include namespace memory { -void MediaContainer::addMedia(const std::shared_ptr& media) { - if (!media) return; +void MediaContainer::addMedia(const media::Media& media) { + std::unique_ptr clone = media.clone(); + media::Media* rawPtr = clone.get(); - data_[static_cast(MediaType::All)].push_back(media); + // Inserisce rawPtr nel vettore "All" copiando il puntatore (non l’oggetto!) + data_[static_cast(Type::All)].push_back(std::unique_ptr(clone->clone())); - MediaType type = determineType(media); - if (type != MediaType::All) { - data_[static_cast(type)].push_back(media); + // Inserisco anche nel tipo specifico + Type t = detectType(*clone); + if (t != Type::All) { + data_[static_cast(t)].push_back(std::move(clone)); } } -void MediaContainer::removeMedia(const std::shared_ptr& media) { - if (!media) return; +MediaContainer::Type MediaContainer::detectType(const media::Media& media) const { + if (dynamic_cast(&media)) return Type::Series; + if (dynamic_cast(&media)) return Type::AudioBook; + if (dynamic_cast(&media)) return Type::Ebook; + if (dynamic_cast(&media)) return Type::Movie; + if (dynamic_cast(&media)) return Type::Album; + if (dynamic_cast(&media)) return Type::Novel; + return Type::All; +} + +void MediaContainer::removeMedia(const media::Media& media) { for (auto& vec : data_) { vec.erase(std::remove_if(vec.begin(), vec.end(), - [&](const std::shared_ptr& m) { - return typeid(*m) == typeid(*media) && *m == *media; + [&](const std::unique_ptr& m) { + return typeid(*m) == typeid(media) && *m == media; }), vec.end()); } } @@ -30,127 +44,65 @@ void MediaContainer::clear() { } } -const std::vector>& MediaContainer::getAll() const { - return data_[static_cast(MediaType::All)]; +std::vector MediaContainer::getAll() const { + return getByType(Type::All); } -const std::vector>& MediaContainer::getByType(MediaType type) const { - return data_[static_cast(type)]; +std::vector MediaContainer::getByType(Type type) const { + std::vector result; + for (const auto& ptr : data_[static_cast(type)]) { + result.push_back(ptr.get()); + } + return result; } -const std::vector>& MediaContainer::getByTypeAndSubtype(MediaType type) const { - std::vector> result; +std::vector MediaContainer::getByGroup(Type type) const { + std::vector result; + + auto appendGroup = [&](Type t) { + for (const auto& ptr : data_[static_cast(t)]) { + result.push_back(ptr.get()); + } + }; switch (type) { - case MediaType::Novel: - result.insert(result.end(), data_[static_cast(MediaType::Novel)].begin(), data_[static_cast(MediaType::Novel)].end()); - result.insert(result.end(), data_[static_cast(MediaType::EBook)].begin(), data_[static_cast(MediaType::EBook)].end()); - result.insert(result.end(), data_[static_cast(MediaType::AudioBook)].begin(), data_[static_cast(MediaType::AudioBook)].end()); + case Type::Novel: + appendGroup(Type::Novel); + appendGroup(Type::Ebook); + appendGroup(Type::AudioBook); break; - case MediaType::Movie: - result.insert(result.end(), data_[static_cast(MediaType::Movie)].begin(), data_[static_cast(MediaType::Movie)].end()); - result.insert(result.end(), data_[static_cast(MediaType::Series)].begin(), data_[static_cast(MediaType::Series)].end()); + case Type::Movie: + appendGroup(Type::Movie); + appendGroup(Type::Series); break; - case MediaType::All: - result = data_[0]; + case Type::All: + appendGroup(Type::All); break; default: - result = data_[static_cast(type)]; + appendGroup(type); break; } return result; } -MediaType MediaContainer::determineType(const std::shared_ptr& media) const { - if (std::dynamic_pointer_cast(media)) return MediaType::Series; - if (std::dynamic_pointer_cast(media)) return MediaType::Movie; - if (std::dynamic_pointer_cast(media)) return MediaType::AudioBook; - if (std::dynamic_pointer_cast(media)) return MediaType::EBook; - if (std::dynamic_pointer_cast(media)) return MediaType::Novel; - if (std::dynamic_pointer_cast(media)) return MediaType::Album; - return MediaType::All; -} - -std::vector> MediaContainer::filter(const media::Media& media) const { - std::shared_ptr mediaPtr = std::make_shared(media); - - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - if (auto m = std::dynamic_pointer_cast(mediaPtr)) return filters(m); - return filters(mediaPtr); -} - -std::vector> MediaContainer::filters(const std::shared_ptr& media) const { - auto allMedia = getAll(); - return media->filter(allMedia); -} - -std::vector> MediaContainer::filters(const std::shared_ptr& novel) const { - auto media = getByTypeAndSubtype(MediaType::Novel); - std::vector> allNovel; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allNovel.push_back(ptr); - std::vector> result; - for (const auto& m : novel->filter(allNovel)) result.push_back(m); - return result; -} - -std::vector> MediaContainer::filters(const std::shared_ptr& album) const { - auto media = getByType(MediaType::Album); - std::vector> allAlbum; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allAlbum.push_back(ptr); - std::vector> result; - for (const auto& m : album->filter(allAlbum)) result.push_back(m); - return result; -} - -std::vector> MediaContainer::filters(const std::shared_ptr& movie) const { - auto media = getByTypeAndSubtype(MediaType::Movie); - std::vector> allMovie; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allMovie.push_back(ptr); - std::vector> result; - for (const auto& m : movie->filter(allMovie)) result.push_back(m); - return result; -} - -std::vector> MediaContainer::filters(const std::shared_ptr& ebook) const { - auto media = getByType(MediaType::EBook); - std::vector> allEbook; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allEbook.push_back(ptr); - std::vector> result; - for (const auto& m : ebook->filter(allEbook)) result.push_back(m); - return result; -} - -std::vector> MediaContainer::filters(const std::shared_ptr& audiobook) const { - auto media = getByType(MediaType::AudioBook); - std::vector> allAudio; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allAudio.push_back(ptr); - std::vector> result; - for (const auto& m : audiobook->filter(allAudio)) result.push_back(m); - return result; -} - -std::vector> MediaContainer::filters(const std::shared_ptr& series) const { - auto media = getByType(MediaType::Series); - std::vector> allSeries; - for (const auto& m : media) - if (auto ptr = std::dynamic_pointer_cast(m)) allSeries.push_back(ptr); - std::vector> result; - for (const auto& m : series->filter(allSeries)) result.push_back(m); - return result; +std::vector MediaContainer::filter(const media::Media& media) const { + std::vector results; + Type t = detectType(media); + for (const media::Media* m : getByGroup(t)) { + if (media.filter(*m)) { + results.push_back(m); + } + } + return results; } int MediaContainer::serialize(QSaveFile& file) const { - return Serializer::Serialize(getAll(), file); + std::vector rawAll; + for (const auto& ptr : data_[static_cast(Type::All)]) { + rawAll.push_back(ptr.get()); + } + return Serializer::Serialize(rawAll, file); } } // namespace memory diff --git a/src/Memory/MediaContainer.h b/src/Memory/MediaContainer.h index c4d5938..7b26273 100644 --- a/src/Memory/MediaContainer.h +++ b/src/Memory/MediaContainer.h @@ -4,57 +4,47 @@ #include #include #include -#include -#include #include -#include "Media.h" -#include "Novel.h" -#include "Album.h" -#include "Movie.h" -#include "Ebook.h" -#include "AudioBook.h" -#include "Series.h" +#include "../Media/Media.h" +#include "../Media/Novel.h" +#include "../Media/Album.h" +#include "../Media/Movie.h" +#include "../Media/Ebook.h" +#include "../Media/AudioBook.h" +#include "../Media/Series.h" #include "Serializer.h" namespace memory { -enum class MediaType { - All = 0, - Novel, - Album, - Movie, - EBook, - AudioBook, - Series, - Count -}; - class MediaContainer { private: - std::array>, static_cast(MediaType::Count)> data_; + enum class Type { + All = 0, + Novel, + Album, + Movie, + Ebook, + AudioBook, + Series, + TypeCount + }; - MediaType determineType(const std::shared_ptr& media) const; + std::array>, static_cast(Type::TypeCount)> data_; - std::vector> filters(const std::shared_ptr& media) const; - std::vector> filters(const std::shared_ptr& novel) const; - std::vector> filters(const std::shared_ptr& album) const; - std::vector> filters(const std::shared_ptr& movie) const; - std::vector> filters(const std::shared_ptr& ebook) const; - std::vector> filters(const std::shared_ptr& audiobook) const; - std::vector> filters(const std::shared_ptr& series) const; + Type detectType(const media::Media& media) const; - const std::vector>& getAll() const; - const std::vector>& getByType(MediaType type) const; - const std::vector>& getByTypeAndSubtype(MediaType type) const; + std::vector getByType(Type type) const; + std::vector getByGroup(Type type) const; public: - void addMedia(const std::shared_ptr& media); - void removeMedia(const std::shared_ptr& media); + void addMedia(const media::Media& media); + void removeMedia(const media::Media& media); void clear(); - - // Filtri - std::vector> filter(const media::Media& media) const; + + std::vector filter(const media::Media& media) const; + + std::vector getAll() const; int serialize(QSaveFile& file) const; }; diff --git a/src/Memory/Serializer.h b/src/Memory/Serializer.h index 34b5fa9..cecc7d7 100644 --- a/src/Memory/Serializer.h +++ b/src/Memory/Serializer.h @@ -5,12 +5,13 @@ namespace memory { class Serializer { + private: - static int vecToJSON(const std::vector> &, QSaveFile &); - static int vecToXML(const std::vector> &, QSaveFile &); + static int vecToJSON(const media::Media &, QSaveFile &){} + static int vecToXML(const media::Media&, QSaveFile &){} public: - static int Serialize(const std::vector> &, QSaveFile &); + static int Serialize(std::vector, QSaveFile &){} }; } // namespace memory #endif diff --git a/src/main.cpp b/src/main.cpp index 55a15bc..4a96007 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,48 +5,6 @@ #include "Media/Media.h" // Include il tuo header int main() { - // Crea alcuni media - auto media1 = std::make_shared( - "House of Cards", - 2013, - "English", - true, - std::vector{"Drama", "Politics"} - ); - - auto media2 = std::make_shared( - "The Crown", - 2016, - "English", - false, - std::vector{"History", "Drama"} - ); - - auto media3 = std::make_shared( - "Breaking Bad", - 2008, - "English", - false, - std::vector{"Crime", "Thriller"} - ); - - // Inserisci i media in un vettore - std::vector> inputList = {media1, media2, media3}; - - // Crea un oggetto Media come filtro - auto filterMedia = std::make_shared( - "Ho", - std::numeric_limits::min() - ); - - // Filtra - std::vector> filtered = filterMedia->filter(inputList); - - //Stampa i risultati - std::cout << "Media che passano il filtro:\n"; - for (const auto& m : filtered) { - std::cout << "- " << m->getTitle() << "\n"; - } - + std::cout << "Aggiornato" << std::endl; return 0; }