Skip to content

Commit

Permalink
Introduce LibraryPtr and ConstLibraryPtr.
Browse files Browse the repository at this point in the history
As we enforce the use of Library through a shared_ptr, let's simplify
user life (and code) with new "type".
  • Loading branch information
mgautierfr committed Aug 24, 2023
1 parent 5af183a commit ba825d4
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion include/html_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace kiwix
class HTMLDumper : public LibraryDumper
{
public:
HTMLDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> NameMapper);
HTMLDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> NameMapper);
~HTMLDumper();


Expand Down
8 changes: 6 additions & 2 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ class ConcurrentCache;
template<typename, typename>
class MultiKeyCache;

using LibraryPtr = std::shared_ptr<Library>;
using ConstLibraryPtr = std::shared_ptr<const Library>;

/**
* A Library store several books.
*/
Expand All @@ -198,8 +201,8 @@ class Library: public std::enable_shared_from_this<Library>
Library();

public:
[[nodiscard]] static std::shared_ptr<Library> create() {
return std::shared_ptr<Library>(new Library());
[[nodiscard]] static LibraryPtr create() {
return LibraryPtr(new Library());
}
~Library();

Expand Down Expand Up @@ -405,6 +408,7 @@ class Library: public std::enable_shared_from_this<Library>
std::unique_ptr<Xapian::WritableDatabase> m_bookDB;
};


}

#endif
2 changes: 1 addition & 1 deletion include/library_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace kiwix
class LibraryDumper
{
public:
LibraryDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> NameMapper);
LibraryDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> NameMapper);
~LibraryDumper();

void setLibraryId(const std::string& id) { this->libraryId = id;}
Expand Down
4 changes: 2 additions & 2 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(std::shared_ptr<const Library> library);
LibXMLDumper(ConstLibraryPtr library);
~LibXMLDumper();

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ class LibXMLDumper
void setLibrary(std::shared_ptr<const Library> library) { this->library = library; }

protected:
std::shared_ptr<const kiwix::Library> library;
ConstLibraryPtr library;
std::string baseDir;
private:
void handleBook(Book book, pugi::xml_node root_node);
Expand Down
8 changes: 4 additions & 4 deletions include/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ namespace kiwix
class LibraryManipulator
{
public: // functions
explicit LibraryManipulator(std::shared_ptr<Library> library);
explicit LibraryManipulator(LibraryPtr library);
virtual ~LibraryManipulator();

std::shared_ptr<Library> getLibrary() const { return library; }
LibraryPtr getLibrary() const { return library; }

bool addBookToLibrary(const Book& book);
void addBookmarkToLibrary(const Bookmark& bookmark);
Expand All @@ -52,7 +52,7 @@ class LibraryManipulator
virtual void booksWereRemovedFromLibrary();

private: // data
std::shared_ptr<kiwix::Library> library;
LibraryPtr library;
};

/**
Expand All @@ -65,7 +65,7 @@ class Manager

public: // functions
explicit Manager(LibraryManipulator manipulator);
explicit Manager(std::shared_ptr<Library> library);
explicit Manager(LibraryPtr library);

/**
* Read a `library.xml` and add book in the file to the library.
Expand Down
2 changes: 1 addition & 1 deletion include/opds_dumper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace kiwix
class OPDSDumper : public LibraryDumper
{
public:
OPDSDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> NameMapper);
OPDSDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> NameMapper);
~OPDSDumper();

/**
Expand Down
4 changes: 2 additions & 2 deletions include/search_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SearchRenderer
* @param start The start offset used for the srs.
* @param estimatedResultCount The estimatedResultCount of the whole search
*/
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<const NameMapper> mapper, std::shared_ptr<const Library> library,
SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<const NameMapper> mapper, ConstLibraryPtr library,
unsigned int start, unsigned int estimatedResultCount);

~SearchRenderer();
Expand Down Expand Up @@ -107,7 +107,7 @@ class SearchRenderer
std::string beautifyInteger(const unsigned int number);
zim::SearchResultSet m_srs;
std::shared_ptr<const NameMapper> mp_nameMapper;
std::shared_ptr<const Library> mp_library;
ConstLibraryPtr mp_library;
std::string searchBookQuery;
std::string searchPattern;
std::string protocolPrefix;
Expand Down
2 changes: 1 addition & 1 deletion src/html_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace kiwix
{

/* Constructor */
HTMLDumper::HTMLDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> nameMapper)
HTMLDumper::HTMLDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> nameMapper)
: LibraryDumper(library, nameMapper)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/library_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace kiwix
{
/* Constructor */
LibraryDumper::LibraryDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> nameMapper)
LibraryDumper::LibraryDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> nameMapper)
: library(library),
nameMapper(nameMapper)
{
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(std::shared_ptr<const Library> library)
LibXMLDumper::LibXMLDumper(ConstLibraryPtr library)

Check warning on line 31 in src/libxml_dumper.cpp

View check run for this annotation

Codecov / codecov/patch

src/libxml_dumper.cpp#L31

Added line #L31 was not covered by tests
: library(library)
{
}
Expand Down
4 changes: 2 additions & 2 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace kiwix
// LibraryManipulator
////////////////////////////////////////////////////////////////////////////////

LibraryManipulator::LibraryManipulator(std::shared_ptr<Library> library)
LibraryManipulator::LibraryManipulator(LibraryPtr library)
: library(library)
{}

Expand Down Expand Up @@ -87,7 +87,7 @@ Manager::Manager(LibraryManipulator manipulator):
{
}

Manager::Manager(std::shared_ptr<Library> library) :
Manager::Manager(LibraryPtr library) :
writableLibraryPath(""),
manipulator(LibraryManipulator(library))
{
Expand Down
4 changes: 2 additions & 2 deletions src/name_mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace kiwix {

HumanReadableNameMapper::HumanReadableNameMapper(std::shared_ptr<const kiwix::Library> library, bool withAlias) {
HumanReadableNameMapper::HumanReadableNameMapper(ConstLibraryPtr library, bool withAlias) {
for (auto& bookId: library->filter(kiwix::Filter().local(true).valid(true))) {
auto& currentBook = library->getBookById(bookId);
auto bookName = currentBook.getHumanReadableIdFromPath();
Expand Down Expand Up @@ -63,7 +63,7 @@ std::string HumanReadableNameMapper::getIdForName(const std::string& name) const
// UpdatableNameMapper
////////////////////////////////////////////////////////////////////////////////

UpdatableNameMapper::UpdatableNameMapper(std::shared_ptr<Library> lib, bool withAlias)
UpdatableNameMapper::UpdatableNameMapper(LibraryPtr lib, bool withAlias)
: library(lib)
, withAlias(withAlias)
{
Expand Down
2 changes: 1 addition & 1 deletion src/opds_dumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace kiwix
{

/* Constructor */
OPDSDumper::OPDSDumper(std::shared_ptr<const Library> library, std::shared_ptr<const NameMapper> nameMapper)
OPDSDumper::OPDSDumper(ConstLibraryPtr library, std::shared_ptr<const NameMapper> nameMapper)
: LibraryDumper(library, nameMapper)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/search_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<const N
: SearchRenderer(srs, mapper, nullptr, start, estimatedResultCount)
{}

SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<const NameMapper> mapper, std::shared_ptr<const Library> library,
SearchRenderer::SearchRenderer(zim::SearchResultSet srs, std::shared_ptr<const NameMapper> mapper, ConstLibraryPtr library,
unsigned int start, unsigned int estimatedResultCount)
: m_srs(srs),
mp_nameMapper(mapper),
Expand Down
2 changes: 1 addition & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace kiwix {

Server::Server(std::shared_ptr<Library> library, std::shared_ptr<const NameMapper> nameMapper) :
Server::Server(LibraryPtr library, std::shared_ptr<const NameMapper> nameMapper) :
mp_library(library),
mp_nameMapper(nameMapper),
mp_server(nullptr)
Expand Down
2 changes: 1 addition & 1 deletion src/server/internalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class InternalServer::CustomizedResources : public std::map<std::string, Customi
};


InternalServer::InternalServer(std::shared_ptr<Library> library,
InternalServer::InternalServer(LibraryPtr library,
std::shared_ptr<const NameMapper> nameMapper,
std::string addr,
int port,
Expand Down
4 changes: 2 additions & 2 deletions src/server/internalServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class OPDSDumper;

class InternalServer {
public:
InternalServer(std::shared_ptr<Library> library,
InternalServer(LibraryPtr library,
std::shared_ptr<const NameMapper> nameMapper,
std::string addr,
int port,
Expand Down Expand Up @@ -178,7 +178,7 @@ class InternalServer {
int m_ipConnectionLimit;
struct MHD_Daemon* mp_daemon;

std::shared_ptr<Library> mp_library;
LibraryPtr mp_library;
std::shared_ptr<const NameMapper> mp_nameMapper;

SearchCache searchCache;
Expand Down

0 comments on commit ba825d4

Please sign in to comment.