diff --git a/include/manager.h b/include/manager.h index d0addad70..9b13da39c 100644 --- a/include/manager.h +++ b/include/manager.h @@ -37,10 +37,10 @@ namespace kiwix class LibraryManipulator { public: // functions - explicit LibraryManipulator(Library* library); + explicit LibraryManipulator(std::shared_ptr library); virtual ~LibraryManipulator(); - Library& getLibrary() const { return library; } + std::shared_ptr getLibrary() const { return library; } bool addBookToLibrary(const Book& book); void addBookmarkToLibrary(const Bookmark& bookmark); @@ -52,7 +52,7 @@ class LibraryManipulator virtual void booksWereRemovedFromLibrary(); private: // data - kiwix::Library& library; + std::shared_ptr library; }; /** @@ -64,8 +64,8 @@ class Manager typedef std::vector Paths; public: // functions - explicit Manager(LibraryManipulator* manipulator); - explicit Manager(Library* library); + explicit Manager(LibraryManipulator manipulator); + explicit Manager(std::shared_ptr library); /** * Read a `library.xml` and add book in the file to the library. @@ -163,7 +163,7 @@ class Manager uint64_t m_itemsPerPage = 0; protected: - std::shared_ptr manipulator; + kiwix::LibraryManipulator manipulator; bool readBookFromPath(const std::string& path, Book* book); bool parseXmlDom(const pugi::xml_document& doc, diff --git a/src/manager.cpp b/src/manager.cpp index 55d9eff70..9a4f9b28c 100644 --- a/src/manager.cpp +++ b/src/manager.cpp @@ -27,22 +27,12 @@ namespace kiwix { -namespace -{ - -struct NoDelete -{ - template void operator()(T*) {} -}; - -} // unnamed namespace - //////////////////////////////////////////////////////////////////////////////// // LibraryManipulator //////////////////////////////////////////////////////////////////////////////// -LibraryManipulator::LibraryManipulator(Library* library) - : library(*library) +LibraryManipulator::LibraryManipulator(std::shared_ptr library) + : library(library) {} LibraryManipulator::~LibraryManipulator() @@ -50,7 +40,7 @@ LibraryManipulator::~LibraryManipulator() bool LibraryManipulator::addBookToLibrary(const Book& book) { - const auto ret = library.addBook(book); + const auto ret = library->addBook(book); if ( ret ) { bookWasAddedToLibrary(book); } @@ -59,13 +49,13 @@ bool LibraryManipulator::addBookToLibrary(const Book& book) void LibraryManipulator::addBookmarkToLibrary(const Bookmark& bookmark) { - library.addBookmark(bookmark); + library->addBookmark(bookmark); bookmarkWasAddedToLibrary(bookmark); } uint32_t LibraryManipulator::removeBooksNotUpdatedSince(Library::Revision rev) { - const auto n = library.removeBooksNotUpdatedSince(rev); + const auto n = library->removeBooksNotUpdatedSince(rev); if ( n != 0 ) { booksWereRemovedFromLibrary(); } @@ -89,15 +79,15 @@ void LibraryManipulator::booksWereRemovedFromLibrary() //////////////////////////////////////////////////////////////////////////////// /* Constructor */ -Manager::Manager(LibraryManipulator* manipulator): +Manager::Manager(LibraryManipulator manipulator): writableLibraryPath(""), - manipulator(manipulator, NoDelete()) + manipulator(manipulator) { } -Manager::Manager(Library* library) : +Manager::Manager(std::shared_ptr library) : writableLibraryPath(""), - manipulator(new LibraryManipulator(library)) + manipulator(LibraryManipulator(library)) { } @@ -121,7 +111,7 @@ bool Manager::parseXmlDom(const pugi::xml_document& doc, if (!trustLibrary && !book.getPath().empty()) { this->readBookFromPath(book.getPath(), &book); } - manipulator->addBookToLibrary(book); + manipulator.addBookToLibrary(book); } return true; @@ -166,7 +156,7 @@ bool Manager::parseOpdsDom(const pugi::xml_document& doc, const std::string& url book.updateFromOpds(entryNode, urlHost); /* Update the book properties with the new importer */ - manipulator->addBookToLibrary(book); + manipulator.addBookToLibrary(book); } return true; @@ -241,7 +231,7 @@ std::string Manager::addBookFromPathAndGetId(const std::string& pathToOpen, || (!book.getTitle().empty() && !book.getLanguages().empty() && !book.getDate().empty())) { book.setUrl(url); - manipulator->addBookToLibrary(book); + manipulator.addBookToLibrary(book); return book.getId(); } } @@ -296,7 +286,7 @@ bool Manager::readBookmarkFile(const std::string& path) bookmark.updateFromXml(node); - manipulator->addBookmarkToLibrary(bookmark); + manipulator.addBookmarkToLibrary(bookmark); } return true; @@ -304,7 +294,7 @@ bool Manager::readBookmarkFile(const std::string& path) void Manager::reload(const Paths& paths) { - const auto libRevision = manipulator->getLibrary().getRevision(); + const auto libRevision = manipulator.getLibrary()->getRevision(); for (std::string path : paths) { if (!path.empty()) { if ( kiwix::isRelativePath(path) ) @@ -316,7 +306,7 @@ void Manager::reload(const Paths& paths) } } - manipulator->removeBooksNotUpdatedSince(libRevision); + manipulator.removeBooksNotUpdatedSince(libRevision); } } diff --git a/test/library.cpp b/test/library.cpp index aa75eb80a..30a77fcff 100644 --- a/test/library.cpp +++ b/test/library.cpp @@ -239,7 +239,7 @@ typedef std::vector Langs; TEST(LibraryOpdsImportTest, allInOne) { auto lib = kiwix::Library::create(); - kiwix::Manager manager(lib.get()); + kiwix::Manager manager(lib); manager.readOpds(sampleOpdsStream, "library-opds-import.unittests.dev"); EXPECT_EQ(10U, lib->getBookCount(true, true)); @@ -304,7 +304,7 @@ class LibraryTest : public ::testing::Test { LibraryTest(): lib(kiwix::Library::create()) {} void SetUp() override { - kiwix::Manager manager(lib.get()); + kiwix::Manager manager(lib); manager.readOpds(sampleOpdsStream, "foo.urlHost"); manager.readXml(sampleLibraryXML, false, "./test/library.xml", true); } diff --git a/test/manager.cpp b/test/manager.cpp index 21a492005..47f1edf86 100644 --- a/test/manager.cpp +++ b/test/manager.cpp @@ -9,7 +9,7 @@ TEST(ManagerTest, addBookFromPathAndGetIdTest) { auto lib = kiwix::Library::create(); - kiwix::Manager manager = kiwix::Manager(lib.get()); + kiwix::Manager manager = kiwix::Manager(lib); auto bookId = manager.addBookFromPathAndGetId("./test/example.zim"); ASSERT_NE(bookId, ""); @@ -49,7 +49,7 @@ const char sampleLibraryXML[] = R"( TEST(ManagerTest, readXml) { auto lib = kiwix::Library::create(); - kiwix::Manager manager = kiwix::Manager(lib.get()); + kiwix::Manager manager = kiwix::Manager(lib); EXPECT_EQ(true, manager.readXml(sampleLibraryXML, true, "/data/lib.xml", true)); kiwix::Book book = lib->getBookById("0d0bcd57-d3f6-cb22-44cc-a723ccb4e1b2"); @@ -71,7 +71,7 @@ TEST(ManagerTest, readXml) TEST(Manager, reload) { auto lib = kiwix::Library::create(); - kiwix::Manager manager(lib.get()); + kiwix::Manager manager(lib); manager.reload({ "./test/library.xml" }); EXPECT_EQ(lib->getBooksIds(), (kiwix::Library::BookIdCollection{ diff --git a/test/name_mapper.cpp b/test/name_mapper.cpp index cb7af2686..208b81240 100644 --- a/test/name_mapper.cpp +++ b/test/name_mapper.cpp @@ -22,7 +22,7 @@ class NameMapperTest : public ::testing::Test { NameMapperTest(): lib(kiwix::Library::create()) {} protected: void SetUp() override { - kiwix::Manager manager(lib.get()); + kiwix::Manager manager(lib); manager.readXml(libraryXML, false, "./library.xml", true); for ( const std::string& id : lib->getBooksIds() ) { kiwix::Book bookCopy = lib->getBookById(id); diff --git a/test/server_testing_tools.h b/test/server_testing_tools.h index 1974a0132..6a797e345 100644 --- a/test/server_testing_tools.h +++ b/test/server_testing_tools.h @@ -108,7 +108,7 @@ private: // data ZimFileServer::ZimFileServer(int serverPort, Cfg _cfg, std::string libraryFilePath) : library(kiwix::Library::create()) -, manager(this->library.get()) +, manager(this->library) , cfg(_cfg) { if ( kiwix::isRelativePath(libraryFilePath) ) @@ -122,7 +122,7 @@ ZimFileServer::ZimFileServer(int serverPort, const FilePathCollection& zimpaths, std::string indexTemplateString) : library(kiwix::Library::create()) -, manager(this->library.get()) +, manager(this->library) , cfg(_cfg) { for ( const auto& zimpath : zimpaths ) {