lib: replace some #define with constexpr

minor cleanup.
This commit is contained in:
Dirk-Jan C. Binnema
2021-02-11 18:54:25 +02:00
parent 2967a2e251
commit a3c6e74869
4 changed files with 24 additions and 30 deletions

View File

@ -683,7 +683,7 @@ static gchar*
get_msgid (MuMsgFile *self, gboolean *do_free) get_msgid (MuMsgFile *self, gboolean *do_free)
{ {
const char *msgid{g_mime_message_get_message_id (self->_mime_msg)}; 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; *do_free = FALSE;
return (char*)msgid; return (char*)msgid;
} }

View File

@ -50,9 +50,6 @@
using namespace Mu; using namespace Mu;
using namespace Command; using namespace Command;
using DocId = unsigned;
/// @brief object to manage the server-context for all commands. /// @brief object to manage the server-context for all commands.
struct Server::Private { struct Server::Private {
Private(Store& store, Output output): Private(Store& store, Output output):
@ -116,9 +113,9 @@ private:
const Option<QueryMatch&> qm, const Option<QueryMatch&> qm,
MuMsgOptions opts); 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); 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, const std::string& maildirarg,
MuFlags flags, bool new_name, bool no_view); 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 */ /* get a *list* of all messages with the given message id */
static std::vector<DocId> static std::vector<Store::Id>
docids_for_msgid (const Query& q, const std::string& msgid, size_t max=100) 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, throw Error(Error::Code::InvalidArgument,
"invalid message-id '%s'", msgid.c_str()); "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, throw Error(Error::Code::NotFound,
"could not find message(s) for msgid %s", msgid.c_str()); "could not find message(s) for msgid %s", msgid.c_str());
std::vector<DocId> docids{}; std::vector<Store::Id> docids{};
for(auto&& mi: *res) for(auto&& mi: *res)
docids.emplace_back(mi.doc_id()); docids.emplace_back(mi.doc_id());
@ -709,7 +706,7 @@ path_from_docid (const Store& store, unsigned docid)
} }
static std::vector<DocId> static std::vector<Store::Id>
determine_docids (const Query& q, const Parameters& params) determine_docids (const Query& q, const Parameters& params)
{ {
auto docid{get_int_or(params, ":docid", 0)}; auto docid{get_int_or(params, ":docid", 0)};
@ -911,7 +908,7 @@ get_flags (const std::string& path, const std::string& flagstr)
} }
Sexp::List 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) MuFlags flags, bool new_name, bool no_view)
{ {
bool different_mdir{}; bool different_mdir{};
@ -944,10 +941,10 @@ Server::Private::perform_move (DocId docid, MuMsg *msg, const std::string& maild
} }
Sexp::List 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) bool new_name, bool no_view)
{ {
if (docid == MU_STORE_INVALID_DOCID) if (docid == Store::InvalidId)
throw Error{Error::Code::InvalidArgument, "invalid docid"}; throw Error{Error::Code::InvalidArgument, "invalid docid"};
auto msg{store_.find_message(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 path{get_string_or(params, ":path")};
const auto docid{store().add_message(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"}; throw Error{Error::Code::Store, "failed to add path"};
Sexp::List lst; Sexp::List lst;
@ -1127,11 +1124,11 @@ Server::Private::sent_handler (const Parameters& params)
} }
static bool 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) if (!msg)
throw Error{Error::Code::Store, "missing message"}; throw Error{Error::Code::Store, "missing message"};
if (docid == MU_STORE_INVALID_DOCID) if (docid == Store::InvalidId)
throw Error{Error::Code::Store, "invalid docid"}; throw Error{Error::Code::Store, "invalid docid"};
const auto oldflags{mu_msg_get_flags (msg)}; const auto oldflags{mu_msg_get_flags (msg)};
@ -1160,7 +1157,7 @@ maybe_mark_as_read (Mu::Store& store, MuMsg *msg, DocId docid)
void void
Server::Private::view_handler (const Parameters& params) 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 path{get_string_or(params, ":path")};
const auto mark_as_read{get_bool_or(params, ":mark-as-read")}; const auto mark_as_read{get_bool_or(params, ":mark-as-read")};

View File

@ -385,7 +385,7 @@ Store::add_message (const std::string& path)
const auto docid{priv_->add_or_update_msg (0, msg, &gerr)}; const auto docid{priv_->add_or_update_msg (0, msg, &gerr)};
mu_msg_unref (msg); 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", throw Error{Error::Code::Message, "failed to add message: %s",
gerr ? gerr->message : "something went wrong"}; gerr ? gerr->message : "something went wrong"};
@ -669,10 +669,10 @@ prio_val (MuMsgPrio prio)
static void // add term, truncate if needed. static void // add term, truncate if needed.
add_term (Xapian::Document& doc, const std::string& term) 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); doc.add_term(term);
else 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 */ /* save the mime type of any part */
if (part->type) { 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); g_snprintf(ctype, sizeof(ctype), "%s/%s", part->type, part->subtype);
add_term(pdata->_doc, mime + ctype); add_term(pdata->_doc, mime + ctype);
} }
@ -1044,7 +1044,7 @@ update_threading_info (MuMsg *msg, Xapian::Document& doc)
Xapian::docid Xapian::docid
Store::Private::add_or_update_msg (unsigned docid, MuMsg *msg, GError **err) 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 { try {
Xapian::Document doc (new_doc_from_message(msg)); 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); } MU_XAPIAN_CATCH_BLOCK_G_ERROR (err, MU_ERROR_XAPIAN_STORE_FAILED);
return MU_STORE_INVALID_DOCID; return InvalidId;
} }

View File

@ -35,17 +35,14 @@
#include <index/mu-indexer.hh> #include <index/mu-indexer.hh>
namespace Mu { 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 { class Store {
public: public:
using Id = unsigned; /**< Id for a message in the store (internally, using Id = Xapian::docid; /**< Id for a message in the store */
* corresponds to a Xapian document-id) */
static constexpr Id InvalidId = 0; /**< Invalid store id */ 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 * Construct a store for an existing document database
* *