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)
# some of the libme headers include xapian
lib_mu_dep = declare_dependency(
link_with: lib_mu,
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>;
/**
* Get contacts as a comma-separated list.
*

View File

@ -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.

View File

@ -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<Private>(Message::Options::None)}
{
priv_->doc = std::move(doc);
}
Message::Message(Xapian::Document&& doc):
priv_{std::make_unique<Private>(Message::Options::None, std::move(doc))}
{}
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
*
* @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)});
} 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<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.
@ -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<Private> priv_;
std::unique_ptr<Private> priv_;
}; // Message
MU_ENABLE_BITOPS(Message::Options);