From 3879b82112696db2c3409ceafa05f93299614c3c Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Wed, 28 Apr 2021 11:41:05 +0400 Subject: [PATCH] const-correct kiwix::Library - Made most methods of kiwix::Library const. - Also added const versions of getBookById() and getBookByPath() methods. --- include/library.h | 28 +++++++++++++----------- include/libxml_dumper.h | 6 +++--- src/library.cpp | 48 +++++++++++++++++++++++++---------------- src/libxml_dumper.cpp | 2 +- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/include/library.h b/include/library.h index d91e3c7da..f358e6746 100644 --- a/include/library.h +++ b/include/library.h @@ -193,7 +193,9 @@ class Library */ bool removeBookmark(const std::string& zimId, const std::string& url); + const Book& getBookById(const std::string& id) const; Book& getBookById(const std::string& id); + const Book& getBookByPath(const std::string& path) const; Book& getBookByPath(const std::string& path); std::shared_ptr getReaderById(const std::string& id); @@ -211,7 +213,7 @@ class Library * @param path the path of the file to write to. * @return True if the library has been correctly saved. */ - bool writeToFile(const std::string& path); + bool writeToFile(const std::string& path) const; /** * Write the library bookmarks to a file. @@ -219,7 +221,7 @@ class Library * @param path the path of the file to write to. * @return True if the library has been correctly saved. */ - bool writeBookmarksToFile(const std::string& path); + bool writeBookmarksToFile(const std::string& path) const; /** * Get the number of book in the library. @@ -228,42 +230,42 @@ class Library * @param remoteBooks If we must count remote books (books with an url) * @return The number of books. */ - unsigned int getBookCount(const bool localBooks, const bool remoteBooks); + unsigned int getBookCount(const bool localBooks, const bool remoteBooks) const; /** * Get all langagues of the books in the library. * * @return A list of languages. */ - std::vector getBooksLanguages(); + std::vector getBooksLanguages() const; /** * Get all book creators of the books in the library. * * @return A list of book creators. */ - std::vector getBooksCreators(); + std::vector getBooksCreators() const; /** * Get all book publishers of the books in the library. * * @return A list of book publishers. */ - std::vector getBooksPublishers(); + std::vector getBooksPublishers() const; /** * Get all bookmarks. * * @return A list of bookmarks */ - const std::vector getBookmarks(bool onlyValidBookmarks = true); + const std::vector getBookmarks(bool onlyValidBookmarks = true) const; /** * Get all book ids of the books in the library. * * @return A list of book ids. */ - BookIdCollection getBooksIds(); + BookIdCollection getBooksIds() const; /** * Filter the library and generate a new one with the keep elements. @@ -273,7 +275,7 @@ class Library * @param search List only books with search in the title or description. * @return The list of bookIds corresponding to the query. */ - DEPRECATED BookIdCollection filter(const std::string& search); + DEPRECATED BookIdCollection filter(const std::string& search) const; /** @@ -282,7 +284,7 @@ class Library * @param filter The filter to use. * @return The list of bookIds corresponding to the filter. */ - BookIdCollection filter(const Filter& filter); + BookIdCollection filter(const Filter& filter) const; /** @@ -292,7 +294,7 @@ class Library * @param comparator how to sort the books * @return The sorted list of books */ - void sort(BookIdCollection& bookIds, supportedListSortBy sortBy, bool ascending); + void sort(BookIdCollection& bookIds, supportedListSortBy sortBy, bool ascending) const; /** * List books in the library. @@ -324,13 +326,13 @@ class Library const std::string& creator = "", const std::string& publisher = "", const std::vector& tags = {}, - size_t maxSize = 0); + size_t maxSize = 0) const; friend class OPDSDumper; friend class libXMLDumper; private: // functions - BookIdCollection filterViaBookDB(const Filter& filter); + BookIdCollection filterViaBookDB(const Filter& filter) const; void updateBookDB(const Book& book); }; diff --git a/include/libxml_dumper.h b/include/libxml_dumper.h index 09cce2d5a..4c4f30b9e 100644 --- a/include/libxml_dumper.h +++ b/include/libxml_dumper.h @@ -38,7 +38,7 @@ class LibXMLDumper { public: LibXMLDumper() = default; - LibXMLDumper(Library* library); + LibXMLDumper(const Library* library); ~LibXMLDumper(); /** @@ -69,10 +69,10 @@ class LibXMLDumper * * @param library The library to dump. */ - void setLibrary(Library* library) { this->library = library; } + void setLibrary(const Library* library) { this->library = library; } protected: - kiwix::Library* library; + const kiwix::Library* library; std::string baseDir; private: void handleBook(Book book, pugi::xml_node root_node); diff --git a/src/library.cpp b/src/library.cpp index bc74c488f..8a49af35e 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -111,12 +111,18 @@ bool Library::removeBookById(const std::string& id) return m_books.erase(id) == 1; } -Book& Library::getBookById(const std::string& id) +const Book& Library::getBookById(const std::string& id) const { return m_books.at(id); } -Book& Library::getBookByPath(const std::string& path) +Book& Library::getBookById(const std::string& id) +{ + const Library& const_self = *this; + return const_cast(const_self.getBookById(id)); +} + +const Book& Library::getBookByPath(const std::string& path) const { for(auto& it: m_books) { auto& book = it.second; @@ -128,6 +134,12 @@ Book& Library::getBookByPath(const std::string& path) throw std::out_of_range(ss.str()); } +Book& Library::getBookByPath(const std::string& path) +{ + const Library& const_self = *this; + return const_cast(const_self.getBookByPath(path)); +} + std::shared_ptr Library::getReaderById(const std::string& id) { try { @@ -143,7 +155,7 @@ std::shared_ptr Library::getReaderById(const std::string& id) } unsigned int Library::getBookCount(const bool localBooks, - const bool remoteBooks) + const bool remoteBooks) const { unsigned int result = 0; for (auto& pair: m_books) { @@ -156,7 +168,7 @@ unsigned int Library::getBookCount(const bool localBooks, return result; } -bool Library::writeToFile(const std::string& path) +bool Library::writeToFile(const std::string& path) const { auto baseDir = removeLastPathElement(path); LibXMLDumper dumper(this); @@ -164,13 +176,13 @@ bool Library::writeToFile(const std::string& path) return writeTextFile(path, dumper.dumpLibXMLContent(getBooksIds())); } -bool Library::writeBookmarksToFile(const std::string& path) +bool Library::writeBookmarksToFile(const std::string& path) const { LibXMLDumper dumper(this); return writeTextFile(path, dumper.dumpLibXMLBookmark()); } -std::vector Library::getBooksLanguages() +std::vector Library::getBooksLanguages() const { std::vector booksLanguages; std::map booksLanguagesMap; @@ -189,7 +201,7 @@ std::vector Library::getBooksLanguages() return booksLanguages; } -std::vector Library::getBooksCreators() +std::vector Library::getBooksCreators() const { std::vector booksCreators; std::map booksCreatorsMap; @@ -208,7 +220,7 @@ std::vector Library::getBooksCreators() return booksCreators; } -std::vector Library::getBooksPublishers() +std::vector Library::getBooksPublishers() const { std::vector booksPublishers; std::map booksPublishersMap; @@ -227,7 +239,7 @@ std::vector Library::getBooksPublishers() return booksPublishers; } -const std::vector Library::getBookmarks(bool onlyValidBookmarks) +const std::vector Library::getBookmarks(bool onlyValidBookmarks) const { if (!onlyValidBookmarks) { return m_bookmarks; @@ -242,7 +254,7 @@ const std::vector Library::getBookmarks(bool onlyValidBookmarks return validBookmarks; } -Library::BookIdCollection Library::getBooksIds() +Library::BookIdCollection Library::getBooksIds() const { BookIdCollection bookIds; @@ -253,7 +265,7 @@ Library::BookIdCollection Library::getBooksIds() return bookIds; } -Library::BookIdCollection Library::filter(const std::string& search) +Library::BookIdCollection Library::filter(const std::string& search) const { if (search.empty()) { return getBooksIds(); @@ -424,7 +436,7 @@ Xapian::Query buildXapianQuery(const Filter& filter) } // unnamed namespace -Library::BookIdCollection Library::filterViaBookDB(const Filter& filter) +Library::BookIdCollection Library::filterViaBookDB(const Filter& filter) const { const auto query = buildXapianQuery(filter); @@ -443,7 +455,7 @@ Library::BookIdCollection Library::filterViaBookDB(const Filter& filter) return bookIds; } -Library::BookIdCollection Library::filter(const Filter& filter) +Library::BookIdCollection Library::filter(const Filter& filter) const { BookIdCollection result; for(auto id : filterViaBookDB(filter)) { @@ -467,13 +479,13 @@ struct KEY_TYPE { template class Comparator { private: - Library* lib; - bool ascending; + const Library* const lib; + const bool ascending; inline typename KEY_TYPE::TYPE get_key(const std::string& id); public: - Comparator(Library* lib, bool ascending) : lib(lib), ascending(ascending) {} + Comparator(const Library* lib, bool ascending) : lib(lib), ascending(ascending) {} inline bool operator() (const std::string& id1, const std::string& id2) { if (ascending) { return get_key(id1) < get_key(id2); @@ -513,7 +525,7 @@ std::string Comparator::get_key(const std::string& id) return lib->getBookById(id).getPublisher(); } -void Library::sort(BookIdCollection& bookIds, supportedListSortBy sort, bool ascending) +void Library::sort(BookIdCollection& bookIds, supportedListSortBy sort, bool ascending) const { switch(sort) { case TITLE: @@ -545,7 +557,7 @@ Library::BookIdCollection Library::listBooksIds( const std::string& creator, const std::string& publisher, const std::vector& tags, - size_t maxSize) { + size_t maxSize) const { Filter _filter; if (mode & LOCAL) diff --git a/src/libxml_dumper.cpp b/src/libxml_dumper.cpp index 3c0cefab9..cb5892767 100644 --- a/src/libxml_dumper.cpp +++ b/src/libxml_dumper.cpp @@ -28,7 +28,7 @@ namespace kiwix { /* Constructor */ -LibXMLDumper::LibXMLDumper(Library* library) +LibXMLDumper::LibXMLDumper(const Library* library) : library(library) { }