From 8c8a58b7fa3e99d9c8de493ed67688293f8c89cd Mon Sep 17 00:00:00 2001 From: randomdan Date: Mon, 23 Apr 2012 22:32:56 +0000 Subject: Tidy up some error handling and mutables --- project2/mail/sendmailTask.cpp | 20 +++++++------------- project2/mail/sendmailTask.h | 7 ++----- project2/processes/processStream.cpp | 9 ++++++--- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/project2/mail/sendmailTask.cpp b/project2/mail/sendmailTask.cpp index f70fe64..3cc3a92 100644 --- a/project2/mail/sendmailTask.cpp +++ b/project2/mail/sendmailTask.cpp @@ -61,12 +61,6 @@ SendMailTask::~SendMailTask() { } -const char * -SendMailTask::writeMailWrapper(void ** buf, int * len, void * arg) -{ - return static_cast(arg)->writeMail(buf, len); -} - SendMailTask::MailPart::MailPart(uint8_t s, uint8_t i, uint8_t p) : sec(s), ind(i), @@ -205,7 +199,7 @@ class EmailViewHost : public ViewHost { void SendMailTask::execute() const { - parts = new Parts(); + boost::intrusive_ptr parts = new Parts(); MailPart::mimeIdx = 0; parts->parts.insert(new Header("To", to)); parts->parts.insert(new Header("From", from)); @@ -218,7 +212,7 @@ SendMailTask::execute() const ViewHostPtr vsp = new EmailViewHost(parts, Environment::getCurrent()->resolveScript("emails", present(), false)->root()); vsp->executeViews(); vsp->doTransforms(); - part = parts->parts.begin(); + parts->part = parts->parts.begin(); // Write email smtp_session_t session = smtp_create_session(); @@ -226,21 +220,21 @@ SendMailTask::execute() const smtp_set_server(session, server()); smtp_set_header(message, "To", NULL, NULL); smtp_add_recipient(message, to()); - smtp_set_messagecb(message, writeMailWrapper, (SendMailTask*)this); + smtp_set_messagecb(message, writeMail, parts.get()); if (!smtp_start_session(session)) { char buf[BUFSIZ]; smtp_strerror(smtp_errno(), buf, sizeof buf); throw SendEmailFailed(buf); } - parts.reset(); } const char * -SendMailTask::writeMail(void ** buf, int * len) const +SendMailTask::writeMail(void ** buf, int * len, void * arg) { - if (len == NULL || part == parts->parts.end()) { + Parts * parts = static_cast(arg); + if (len == NULL || parts->part == parts->parts.end()) { return NULL; } - return (*part++)->write((char**)buf, len); + return (*parts->part++)->write((char**)buf, len); } diff --git a/project2/mail/sendmailTask.h b/project2/mail/sendmailTask.h index 3703203..75d98f6 100644 --- a/project2/mail/sendmailTask.h +++ b/project2/mail/sendmailTask.h @@ -28,6 +28,7 @@ class SendMailTask : public Task { class Parts : public TransformChainLink { public: PartList parts; + PartList::iterator part; }; SendMailTask(ScriptNodePtr p); @@ -42,13 +43,9 @@ class SendMailTask : public Task { const Variable present; private: - static const char * writeMailWrapper(void ** buf, int * len, void * arg); - const char * writeMail(void ** buf, int * len) const; + static const char * writeMail(void ** buf, int * len, void * arg); PresenterPtr createDefaultPresenter(ScriptNodePtr n) const; - mutable boost::intrusive_ptr parts; - mutable PartList::iterator part; - // Configurables friend class CustomSendMailTaskLoader; friend class TransformWritableContentToEmail; diff --git a/project2/processes/processStream.cpp b/project2/processes/processStream.cpp index 13c2555..1a853e4 100644 --- a/project2/processes/processStream.cpp +++ b/project2/processes/processStream.cpp @@ -1,5 +1,6 @@ #include "iHaveParameters.h" #include "scriptLoader.h" +#include "scopeObject.h" #include "scripts.h" #include "stream.h" #include @@ -29,8 +30,13 @@ class ProcessStream : public Stream, IHaveParameters { callProc[pidx++] = p.second(); } callProc[pidx] = NULL; + int fds[2]; popenrw(callProc, fds); + ScopeObject doClose([&] { + close(fds[0]); + close(fds[1]); + }); char buf[BUFSIZ]; while (ssize_t r = read(fds[1], buf, BUFSIZ) != 0) { if (r < 0) { @@ -39,8 +45,6 @@ class ProcessStream : public Stream, IHaveParameters { sink(buf, r); } - close(fds[0]); - close(fds[1]); int status; wait(&status); // ignore any error if the application is still running, @@ -51,7 +55,6 @@ class ProcessStream : public Stream, IHaveParameters { } } protected: - mutable int fds[2]; const Variable path; }; -- cgit v1.2.3