Introduce LibraryPtr and ConstLibraryPtr.

As we enforce the use of Library through a shared_ptr, let's simplify
user life (and code) with new "type".
This commit is contained in:
Matthieu Gautier 2023-08-23 11:03:16 +02:00
parent 5292f06fff
commit 1dc9705597
7 changed files with 17 additions and 13 deletions

View File

@ -183,6 +183,9 @@ class ConcurrentCache;
template<typename, typename> template<typename, typename>
class MultiKeyCache; class MultiKeyCache;
using LibraryPtr = std::shared_ptr<Library>;
using ConstLibraryPtr = std::shared_ptr<const Library>;
/** /**
* A Library store several books. * A Library store several books.
*/ */
@ -198,8 +201,8 @@ class Library: public std::enable_shared_from_this<Library>
Library(); Library();
public: public:
[[nodiscard]] static std::shared_ptr<Library> create() { [[nodiscard]] static LibraryPtr create() {
return std::shared_ptr<Library>(new Library()); return LibraryPtr(new Library());
} }
~Library(); ~Library();
@ -405,6 +408,7 @@ private: //data
std::unique_ptr<Xapian::WritableDatabase> m_bookDB; std::unique_ptr<Xapian::WritableDatabase> m_bookDB;
}; };
} }
#endif #endif

View File

@ -37,10 +37,10 @@ namespace kiwix
class LibraryManipulator class LibraryManipulator
{ {
public: // functions public: // functions
explicit LibraryManipulator(std::shared_ptr<Library> library); explicit LibraryManipulator(LibraryPtr library);
virtual ~LibraryManipulator(); virtual ~LibraryManipulator();
std::shared_ptr<Library> getLibrary() const { return library; } LibraryPtr getLibrary() const { return library; }
bool addBookToLibrary(const Book& book); bool addBookToLibrary(const Book& book);
void addBookmarkToLibrary(const Bookmark& bookmark); void addBookmarkToLibrary(const Bookmark& bookmark);
@ -52,7 +52,7 @@ class LibraryManipulator
virtual void booksWereRemovedFromLibrary(); virtual void booksWereRemovedFromLibrary();
private: // data private: // data
std::shared_ptr<kiwix::Library> library; LibraryPtr library;
}; };
/** /**
@ -65,7 +65,7 @@ class Manager
public: // functions public: // functions
explicit Manager(LibraryManipulator manipulator); 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. * Read a `library.xml` and add book in the file to the library.

View File

@ -31,7 +31,7 @@ namespace kiwix
// LibraryManipulator // LibraryManipulator
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
LibraryManipulator::LibraryManipulator(std::shared_ptr<Library> library) LibraryManipulator::LibraryManipulator(LibraryPtr library)
: library(library) : library(library)
{} {}
@ -85,7 +85,7 @@ Manager::Manager(LibraryManipulator manipulator):
{ {
} }
Manager::Manager(std::shared_ptr<Library> library) : Manager::Manager(LibraryPtr library) :
writableLibraryPath(""), writableLibraryPath(""),
manipulator(LibraryManipulator(library)) manipulator(LibraryManipulator(library))
{ {

View File

@ -63,7 +63,7 @@ std::string HumanReadableNameMapper::getIdForName(const std::string& name) const
// UpdatableNameMapper // UpdatableNameMapper
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
UpdatableNameMapper::UpdatableNameMapper(std::shared_ptr<Library> lib, bool withAlias) UpdatableNameMapper::UpdatableNameMapper(LibraryPtr lib, bool withAlias)
: library(lib) : library(lib)
, withAlias(withAlias) , withAlias(withAlias)
{ {

View File

@ -29,7 +29,7 @@
namespace kiwix { namespace kiwix {
Server::Server(std::shared_ptr<Library> library, std::shared_ptr<NameMapper> nameMapper) : Server::Server(LibraryPtr library, std::shared_ptr<NameMapper> nameMapper) :
mp_library(library), mp_library(library),
mp_nameMapper(nameMapper), mp_nameMapper(nameMapper),
mp_server(nullptr) mp_server(nullptr)

View File

@ -411,7 +411,7 @@ public:
}; };
InternalServer::InternalServer(std::shared_ptr<Library> library, InternalServer::InternalServer(LibraryPtr library,
std::shared_ptr<NameMapper> nameMapper, std::shared_ptr<NameMapper> nameMapper,
std::string addr, std::string addr,
int port, int port,

View File

@ -92,7 +92,7 @@ class OPDSDumper;
class InternalServer { class InternalServer {
public: public:
InternalServer(std::shared_ptr<Library> library, InternalServer(LibraryPtr library,
std::shared_ptr<NameMapper> nameMapper, std::shared_ptr<NameMapper> nameMapper,
std::string addr, std::string addr,
int port, int port,
@ -178,7 +178,7 @@ class InternalServer {
int m_ipConnectionLimit; int m_ipConnectionLimit;
struct MHD_Daemon* mp_daemon; struct MHD_Daemon* mp_daemon;
std::shared_ptr<Library> mp_library; LibraryPtr mp_library;
std::shared_ptr<NameMapper> mp_nameMapper; std::shared_ptr<NameMapper> mp_nameMapper;
SearchCache searchCache; SearchCache searchCache;