From a3ad04f12fa6cdc0cbe928524789af631081380f Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 21 May 2022 17:41:21 +0300 Subject: [PATCH] message: make it easier to create heap-allocated messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We need this for guile to coƶperate with its garbage collector. --- lib/meson.build | 2 +- lib/message/mu-contact.hh | 2 -- lib/message/mu-document.hh | 7 +++---- lib/message/mu-message.cc | 10 +++++----- lib/message/mu-message.hh | 28 +++++++++++----------------- 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/lib/meson.build b/lib/meson.build index 00920195..77f218dc 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -49,7 +49,7 @@ lib_mu=static_library( ], install: false) -# some of the libme headers include xapian + lib_mu_dep = declare_dependency( link_with: lib_mu, dependencies: [ lib_mu_message_dep ], diff --git a/lib/message/mu-contact.hh b/lib/message/mu-contact.hh index 6b305f3b..2c22bf92 100644 --- a/lib/message/mu-contact.hh +++ b/lib/message/mu-contact.hh @@ -181,10 +181,8 @@ contact_type_from_field_id(Field::Id id) noexcept { } } - using Contacts = std::vector; - /** * Get contacts as a comma-separated list. * diff --git a/lib/message/mu-document.hh b/lib/message/mu-document.hh index ffef95a6..9c923a19 100644 --- a/lib/message/mu-document.hh +++ b/lib/message/mu-document.hh @@ -97,10 +97,9 @@ public: void add(Field::Id id, const Contacts& contacts); /** - * Addd some extra contacts with the given propname; - * this is useful for ":reply-to" and ":list-post" which don't - * have a Field::Id and are only present in the sexp, - * not in the terms/values + * Add some extra contacts with the given propname; this is useful for + * ":reply-to" and ":list-post" which don't have a Field::Id and are + * only present in the sexp, not in the terms/values * * @param propname property name (e.g.,. ":reply-to") * @param contacts contacts for this property. diff --git a/lib/message/mu-message.cc b/lib/message/mu-message.cc index facfc726..c7119a34 100644 --- a/lib/message/mu-message.cc +++ b/lib/message/mu-message.cc @@ -46,6 +46,8 @@ using namespace Mu; struct Message::Private { Private(Message::Options options): opts{options} {} + Private(Message::Options options, Xapian::Document&& xdoc): + opts{options}, doc{std::move(xdoc)} {} Message::Options opts; Document doc; @@ -154,11 +156,9 @@ Message::operator=(Message&& other) noexcept return *this; } -Message::Message(Document&& doc): - priv_{std::make_unique(Message::Options::None)} -{ - priv_->doc = std::move(doc); -} +Message::Message(Xapian::Document&& doc): + priv_{std::make_unique(Message::Options::None, std::move(doc))} +{} Message::~Message() = default; diff --git a/lib/message/mu-message.hh b/lib/message/mu-message.hh index 650a53e1..dad7026a 100644 --- a/lib/message/mu-message.hh +++ b/lib/message/mu-message.hh @@ -82,13 +82,13 @@ public: } /** - * Construct a message based on a Message::Document + * Construct a message based on a Xapian::Document * * @param doc a Mu Document * * @return a message or an error */ - static Result make_from_document(Mu::Document&& doc) try { + static Result make_from_document(Xapian::Document&& doc) try { return Ok(Message{std::move(doc)}); } catch (Error& err) { return Err(err); @@ -96,17 +96,6 @@ public: return Err(Mu::Error(Error::Code::Message, "failed to create message")); } - /** - * Construct a message based on a Xapian::Document - * - * @param doc a xapian document - * - * @return a message or an error - */ - static Result make_from_document(Xapian::Document&& doc) noexcept { - return make_from_document(Mu::Document{std::move(doc)}); - } - /** * Construct a message from a string. This is mostly useful for testing. @@ -436,13 +425,18 @@ public: struct Private; - Message(Document&& doc); // XXX: make private + /* + * Usually the make_ builders are better to create a message, but in + * some special cases, we need a heap-allocated message... */ + + Message(Xapian::Document&& xdoc); + Message(const std::string& path, Options opts); private: - Message(const std::string& path, Options opts); - Message(const std::string& str, const std::string& path, Options opt); + Message(const std::string& str, const std::string& path, Options opt); + + std::unique_ptr priv_; - std::unique_ptr priv_; }; // Message MU_ENABLE_BITOPS(Message::Options);