Skip to content

Commit

Permalink
const-correct kiwix::Library
Browse files Browse the repository at this point in the history
- Made most methods of kiwix::Library const.
- Also added const versions of getBookById() and getBookByPath()
  methods.
  • Loading branch information
veloman-yunkan committed Apr 28, 2021
1 parent 7336dca commit 3879b82
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
28 changes: 15 additions & 13 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Reader> getReaderById(const std::string& id);

Expand All @@ -211,15 +213,15 @@ 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.
*
* @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.
Expand All @@ -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<std::string> getBooksLanguages();
std::vector<std::string> getBooksLanguages() const;

/**
* Get all book creators of the books in the library.
*
* @return A list of book creators.
*/
std::vector<std::string> getBooksCreators();
std::vector<std::string> getBooksCreators() const;

/**
* Get all book publishers of the books in the library.
*
* @return A list of book publishers.
*/
std::vector<std::string> getBooksPublishers();
std::vector<std::string> getBooksPublishers() const;

/**
* Get all bookmarks.
*
* @return A list of bookmarks
*/
const std::vector<kiwix::Bookmark> getBookmarks(bool onlyValidBookmarks = true);
const std::vector<kiwix::Bookmark> 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.
Expand All @@ -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;


/**
Expand All @@ -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;


/**
Expand All @@ -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.
Expand Down Expand Up @@ -324,13 +326,13 @@ class Library
const std::string& creator = "",
const std::string& publisher = "",
const std::vector<std::string>& 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);
};

Expand Down
6 changes: 3 additions & 3 deletions include/libxml_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LibXMLDumper
{
public:
LibXMLDumper() = default;
LibXMLDumper(Library* library);
LibXMLDumper(const Library* library);
~LibXMLDumper();

/**
Expand Down Expand Up @@ -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);
Expand Down
48 changes: 30 additions & 18 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Book&>(const_self.getBookById(id));
}

const Book& Library::getBookByPath(const std::string& path) const
{
for(auto& it: m_books) {
auto& book = it.second;
Expand All @@ -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<Book&>(const_self.getBookByPath(path));
}

std::shared_ptr<Reader> Library::getReaderById(const std::string& id)
{
try {
Expand All @@ -143,7 +155,7 @@ std::shared_ptr<Reader> 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) {
Expand All @@ -156,21 +168,21 @@ 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);
dumper.setBaseDir(baseDir);
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<std::string> Library::getBooksLanguages()
std::vector<std::string> Library::getBooksLanguages() const
{
std::vector<std::string> booksLanguages;
std::map<std::string, bool> booksLanguagesMap;
Expand All @@ -189,7 +201,7 @@ std::vector<std::string> Library::getBooksLanguages()
return booksLanguages;
}

std::vector<std::string> Library::getBooksCreators()
std::vector<std::string> Library::getBooksCreators() const
{
std::vector<std::string> booksCreators;
std::map<std::string, bool> booksCreatorsMap;
Expand All @@ -208,7 +220,7 @@ std::vector<std::string> Library::getBooksCreators()
return booksCreators;
}

std::vector<std::string> Library::getBooksPublishers()
std::vector<std::string> Library::getBooksPublishers() const
{
std::vector<std::string> booksPublishers;
std::map<std::string, bool> booksPublishersMap;
Expand All @@ -227,7 +239,7 @@ std::vector<std::string> Library::getBooksPublishers()
return booksPublishers;
}

const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks)
const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks) const
{
if (!onlyValidBookmarks) {
return m_bookmarks;
Expand All @@ -242,7 +254,7 @@ const std::vector<kiwix::Bookmark> Library::getBookmarks(bool onlyValidBookmarks
return validBookmarks;
}

Library::BookIdCollection Library::getBooksIds()
Library::BookIdCollection Library::getBooksIds() const
{
BookIdCollection bookIds;

Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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)) {
Expand All @@ -467,13 +479,13 @@ struct KEY_TYPE<SIZE> {
template<supportedListSortBy sort>
class Comparator {
private:
Library* lib;
bool ascending;
const Library* const lib;
const bool ascending;

inline typename KEY_TYPE<sort>::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);
Expand Down Expand Up @@ -513,7 +525,7 @@ std::string Comparator<PUBLISHER>::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:
Expand Down Expand Up @@ -545,7 +557,7 @@ Library::BookIdCollection Library::listBooksIds(
const std::string& creator,
const std::string& publisher,
const std::vector<std::string>& tags,
size_t maxSize) {
size_t maxSize) const {

Filter _filter;
if (mode & LOCAL)
Expand Down
2 changes: 1 addition & 1 deletion src/libxml_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace kiwix
{
/* Constructor */
LibXMLDumper::LibXMLDumper(Library* library)
LibXMLDumper::LibXMLDumper(const Library* library)
: library(library)
{
}
Expand Down

0 comments on commit 3879b82

Please sign in to comment.