message: make it easier to create heap-allocated messages

We need this for guile to coöperate with its garbage collector.
This commit is contained in:
Dirk-Jan C. Binnema 2022-05-21 17:41:21 +03:00
parent 4305f47b86
commit a3ad04f12f
5 changed files with 20 additions and 29 deletions

View File

@ -49,7 +49,7 @@ lib_mu=static_library(
], ],
install: false) install: false)
# some of the libme headers include xapian
lib_mu_dep = declare_dependency( lib_mu_dep = declare_dependency(
link_with: lib_mu, link_with: lib_mu,
dependencies: [ lib_mu_message_dep ], dependencies: [ lib_mu_message_dep ],

View File

@ -181,10 +181,8 @@ contact_type_from_field_id(Field::Id id) noexcept {
} }
} }
using Contacts = std::vector<Contact>; using Contacts = std::vector<Contact>;
/** /**
* Get contacts as a comma-separated list. * Get contacts as a comma-separated list.
* *

View File

@ -97,10 +97,9 @@ public:
void add(Field::Id id, const Contacts& contacts); void add(Field::Id id, const Contacts& contacts);
/** /**
* Addd some extra contacts with the given propname; * Add some extra contacts with the given propname; this is useful for
* this is useful for ":reply-to" and ":list-post" which don't * ":reply-to" and ":list-post" which don't have a Field::Id and are
* have a Field::Id and are only present in the sexp, * only present in the sexp, not in the terms/values
* not in the terms/values
* *
* @param propname property name (e.g.,. ":reply-to") * @param propname property name (e.g.,. ":reply-to")
* @param contacts contacts for this property. * @param contacts contacts for this property.

View File

@ -46,6 +46,8 @@ using namespace Mu;
struct Message::Private { struct Message::Private {
Private(Message::Options options): opts{options} {} Private(Message::Options options): opts{options} {}
Private(Message::Options options, Xapian::Document&& xdoc):
opts{options}, doc{std::move(xdoc)} {}
Message::Options opts; Message::Options opts;
Document doc; Document doc;
@ -154,11 +156,9 @@ Message::operator=(Message&& other) noexcept
return *this; return *this;
} }
Message::Message(Document&& doc): Message::Message(Xapian::Document&& doc):
priv_{std::make_unique<Private>(Message::Options::None)} priv_{std::make_unique<Private>(Message::Options::None, std::move(doc))}
{ {}
priv_->doc = std::move(doc);
}
Message::~Message() = default; Message::~Message() = default;

View File

@ -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 * @param doc a Mu Document
* *
* @return a message or an error * @return a message or an error
*/ */
static Result<Message> make_from_document(Mu::Document&& doc) try { static Result<Message> make_from_document(Xapian::Document&& doc) try {
return Ok(Message{std::move(doc)}); return Ok(Message{std::move(doc)});
} catch (Error& err) { } catch (Error& err) {
return Err(err); return Err(err);
@ -96,17 +96,6 @@ public:
return Err(Mu::Error(Error::Code::Message, "failed to create message")); 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<Message> 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. * Construct a message from a string. This is mostly useful for testing.
@ -436,13 +425,18 @@ public:
struct Private; 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: 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<Private> priv_;
std::unique_ptr<Private> priv_;
}; // Message }; // Message
MU_ENABLE_BITOPS(Message::Options); MU_ENABLE_BITOPS(Message::Options);