From a3c6e748690dcdd115661394b0d0278d11d8f975 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Thu, 11 Feb 2021 18:54:25 +0200 Subject: [PATCH] lib: replace some #define with constexpr minor cleanup. --- lib/mu-msg-file.cc | 2 +- lib/mu-server.cc | 29 +++++++++++++---------------- lib/mu-store.cc | 12 ++++++------ lib/mu-store.hh | 11 ++++------- 4 files changed, 24 insertions(+), 30 deletions(-) diff --git a/lib/mu-msg-file.cc b/lib/mu-msg-file.cc index 069935d4..eed3f18d 100644 --- a/lib/mu-msg-file.cc +++ b/lib/mu-msg-file.cc @@ -683,7 +683,7 @@ static gchar* get_msgid (MuMsgFile *self, gboolean *do_free) { const char *msgid{g_mime_message_get_message_id (self->_mime_msg)}; - if (msgid && strlen(msgid) < MU_STORE_MAX_TERM_LENGTH) { + if (msgid && strlen(msgid) < Store::MaxTermLength) { *do_free = FALSE; return (char*)msgid; } diff --git a/lib/mu-server.cc b/lib/mu-server.cc index 50edbe6b..203b3b6f 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -50,9 +50,6 @@ using namespace Mu; using namespace Command; -using DocId = unsigned; - - /// @brief object to manage the server-context for all commands. struct Server::Private { Private(Store& store, Output output): @@ -116,9 +113,9 @@ private: const Option qm, MuMsgOptions opts); - Sexp::List move_docid (DocId docid, const std::string& flagstr, + Sexp::List move_docid (Store::Id docid, const std::string& flagstr, bool new_name, bool no_view); - Sexp::List perform_move (DocId docid, MuMsg *msg, + Sexp::List perform_move (Store::Id docid, MuMsg *msg, const std::string& maildirarg, MuFlags flags, bool new_name, bool no_view); @@ -654,10 +651,10 @@ Server::Private::extract_handler (const Parameters& params) /* get a *list* of all messages with the given message id */ -static std::vector +static std::vector docids_for_msgid (const Query& q, const std::string& msgid, size_t max=100) { - if (msgid.size() > MU_STORE_MAX_TERM_LENGTH - 1) { + if (msgid.size() > Store::MaxTermLength) { throw Error(Error::Code::InvalidArgument, "invalid message-id '%s'", msgid.c_str()); } @@ -677,7 +674,7 @@ docids_for_msgid (const Query& q, const std::string& msgid, size_t max=100) throw Error(Error::Code::NotFound, "could not find message(s) for msgid %s", msgid.c_str()); - std::vector docids{}; + std::vector docids{}; for(auto&& mi: *res) docids.emplace_back(mi.doc_id()); @@ -709,7 +706,7 @@ path_from_docid (const Store& store, unsigned docid) } -static std::vector +static std::vector determine_docids (const Query& q, const Parameters& params) { auto docid{get_int_or(params, ":docid", 0)}; @@ -911,7 +908,7 @@ get_flags (const std::string& path, const std::string& flagstr) } Sexp::List -Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maildirarg, +Server::Private::perform_move (Store::Id docid, MuMsg *msg, const std::string& maildirarg, MuFlags flags, bool new_name, bool no_view) { bool different_mdir{}; @@ -944,10 +941,10 @@ Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maild } Sexp::List -Server::Private::move_docid (DocId docid, const std::string& flagstr, +Server::Private::move_docid (Store::Id docid, const std::string& flagstr, bool new_name, bool no_view) { - if (docid == MU_STORE_INVALID_DOCID) + if (docid == Store::InvalidId) throw Error{Error::Code::InvalidArgument, "invalid docid"}; auto msg{store_.find_message(docid)}; @@ -1115,7 +1112,7 @@ Server::Private::sent_handler (const Parameters& params) { const auto path{get_string_or(params, ":path")}; const auto docid{store().add_message(path)}; - if (docid == MU_STORE_INVALID_DOCID) + if (docid == Store::InvalidId) throw Error{Error::Code::Store, "failed to add path"}; Sexp::List lst; @@ -1127,11 +1124,11 @@ Server::Private::sent_handler (const Parameters& params) } static bool -maybe_mark_as_read (Mu::Store& store, MuMsg *msg, DocId docid) +maybe_mark_as_read (Mu::Store& store, MuMsg *msg, Store::Id docid) { if (!msg) throw Error{Error::Code::Store, "missing message"}; - if (docid == MU_STORE_INVALID_DOCID) + if (docid == Store::InvalidId) throw Error{Error::Code::Store, "invalid docid"}; const auto oldflags{mu_msg_get_flags (msg)}; @@ -1160,7 +1157,7 @@ maybe_mark_as_read (Mu::Store& store, MuMsg *msg, DocId docid) void Server::Private::view_handler (const Parameters& params) { - DocId docid{MU_STORE_INVALID_DOCID}; + Store::Id docid{Store::InvalidId}; const auto path{get_string_or(params, ":path")}; const auto mark_as_read{get_bool_or(params, ":mark-as-read")}; diff --git a/lib/mu-store.cc b/lib/mu-store.cc index 2fa03c2e..c99bceaa 100644 --- a/lib/mu-store.cc +++ b/lib/mu-store.cc @@ -385,7 +385,7 @@ Store::add_message (const std::string& path) const auto docid{priv_->add_or_update_msg (0, msg, &gerr)}; mu_msg_unref (msg); - if (G_UNLIKELY(docid == MU_STORE_INVALID_DOCID)) + if (G_UNLIKELY(docid == InvalidId)) throw Error{Error::Code::Message, "failed to add message: %s", gerr ? gerr->message : "something went wrong"}; @@ -669,10 +669,10 @@ prio_val (MuMsgPrio prio) static void // add term, truncate if needed. add_term (Xapian::Document& doc, const std::string& term) { - if (term.length() < MU_STORE_MAX_TERM_LENGTH) + if (term.length() < Store::MaxTermLength) doc.add_term(term); else - doc.add_term(term.substr(0, MU_STORE_MAX_TERM_LENGTH)); + doc.add_term(term.substr(0, Store::MaxTermLength)); } @@ -800,7 +800,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartData *pdata) /* save the mime type of any part */ if (part->type) { - char ctype[MU_STORE_MAX_TERM_LENGTH + 1]; + char ctype[Store::MaxTermLength + 1]; g_snprintf(ctype, sizeof(ctype), "%s/%s", part->type, part->subtype); add_term(pdata->_doc, mime + ctype); } @@ -1044,7 +1044,7 @@ update_threading_info (MuMsg *msg, Xapian::Document& doc) Xapian::docid Store::Private::add_or_update_msg (unsigned docid, MuMsg *msg, GError **err) { - g_return_val_if_fail (msg, MU_STORE_INVALID_DOCID); + g_return_val_if_fail (msg, InvalidId); try { Xapian::Document doc (new_doc_from_message(msg)); @@ -1065,5 +1065,5 @@ Store::Private::add_or_update_msg (unsigned docid, MuMsg *msg, GError **err) } MU_XAPIAN_CATCH_BLOCK_G_ERROR (err, MU_ERROR_XAPIAN_STORE_FAILED); - return MU_STORE_INVALID_DOCID; + return InvalidId; } diff --git a/lib/mu-store.hh b/lib/mu-store.hh index d1d05673..508ed41e 100644 --- a/lib/mu-store.hh +++ b/lib/mu-store.hh @@ -35,17 +35,14 @@ #include namespace Mu { - -/* http://article.gmane.org/gmane.comp.search.xapian.general/3656 */ -#define MU_STORE_MAX_TERM_LENGTH (240) -#define MU_STORE_INVALID_DOCID 0 - class Store { public: - using Id = unsigned; /**< Id for a message in the store (internally, - * corresponds to a Xapian document-id) */ + using Id = Xapian::docid; /**< Id for a message in the store */ static constexpr Id InvalidId = 0; /**< Invalid store id */ + static constexpr size_t MaxTermLength = 240; /**< Maximum length of a term, + http://article.gmane.org/gmane.comp.search.xapian.general/3656 */ + /** * Construct a store for an existing document database *