From f9913fa28a27f23fde2d4956c62cbb2fb2bc2ee8 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Thu, 21 Nov 2013 22:16:57 +0100 Subject: Boost/C++11 shared pointers. --- doc/book/msg.tex | 88 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 44 deletions(-) (limited to 'doc/book/msg.tex') diff --git a/doc/book/msg.tex b/doc/book/msg.tex index 384aab74..50344b62 100644 --- a/doc/book/msg.tex +++ b/doc/book/msg.tex @@ -36,22 +36,22 @@ vmime::utility::outputStreamStringAdapter os(data); vmime::utility::bufferedStreamCopy(is, os); // Actually parse the message -vmime::ref msg = vmime::create (); +vmime::shared_ptr msg = vmime::make_shared (); msg->parse(data); -vmime::ref hdr = msg->getHeader(); -vmime::ref bdy = msg->getBody(); +vmime::shared_ptr hdr = msg->getHeader(); +vmime::shared_ptr bdy = msg->getBody(); // Now, you can extract some of its components vmime::charset ch(vmime::charsets::UTF_8); std::cout << "The subject of the message is: " - << hdr->Subject()->getValue().dynamicCast ()->getConvertedText(ch) + << hdr->Subject()->getValue ()->getConvertedText(ch) << std::endl << "It was sent by: " - << hdr->From()->getValue().dynamicCast ()->getName().getConvertedText(ch) - << " (email: " << hdr->From()->getValue().dynamicCast()->getEmail() << ")" + << hdr->From()->getValue ()->getName().getConvertedText(ch) + << " (email: " << hdr->From()->getValue ()->getEmail() << ")" << std::endl; \end{lstlisting} @@ -84,7 +84,7 @@ vmime::utility::outputStreamStringAdapter os(data); vmime::utility::bufferedStreamCopy(is, os); // Actually parse the message -vmime::ref msg = vmime::create (); +vmime::shared_ptr msg = vmime::make_shared (); msg->parse(data); // Here start the differences with the previous example @@ -96,7 +96,7 @@ std::cout << "Message has " << mp.getAttachmentCount() for (int i = 0 ; i < mp.getAttachmentCount() ; ++i) { - vmime::ref att = mp.getAttachmentAt(i); + vmime::shared_ptr att = mp.getAttachmentAt(i); std::cout << " - " << att->getType().generate() << std::endl; } @@ -106,13 +106,13 @@ std::cout << "Message has " << mp.getTextPartCount() for (int i = 0 ; i < mp.getTextPartCount() ; ++i) { - vmime::ref tp = mp.getTextPartAt(i); + vmime::shared_ptr tp = mp.getTextPartAt(i); // text/html if (tp->getType().getSubType() == vmime::mediaTypes::TEXT_HTML) { - vmime::ref htp = - tp.dynamicCast (); + vmime::shared_ptr htp = + vmime::dynamicCast (tp); // HTML text is in tp->getText() // Plain text is in tp->getPlainText() @@ -120,7 +120,7 @@ for (int i = 0 ; i < mp.getTextPartCount() ; ++i) // Enumerate embedded objects for (int j = 0 ; j < htp->getObjectCount() ; ++j) { - vmime::ref obj = + vmime::shared_ptr obj = htp->getObjectAt(j); // Identifier (Content-Id or Content-Location) is obj->getId() @@ -146,51 +146,51 @@ objects that compose it (parts, fields, etc.). The following is an example of how to achieve it: \begin{lstlisting}[caption={Building a simple message from scratch}] -vmime::ref msg = vmime::create (); +vmime::shared_ptr msg = vmime::make_shared (); -vmime::ref hdr = msg->getHeader(); -vmime::ref bdy = msg->getBody(); +vmime::shared_ptr hdr = msg->getHeader(); +vmime::shared_ptr bdy = msg->getBody(); -vmime::headerFieldFactory* hfFactory = +vmime::shared_ptr hfFactory = vmime::headerFieldFactory::getInstance(); // Append a 'Date:' field -vmime::ref dateField = +vmime::shared_ptr dateField = hfFactory->create(vmime::fields::DATE); dateField->setValue(vmime::datetime::now()); hdr->appendField(dateField); // Append a 'Subject:' field -vmime::ref subjectField = +vmime::shared_ptr subjectField = hfFactory->create(vmime::fields::SUBJECT); subjectField->setValue(vmime::text("Message subject")); hdr->appendField(subjectField); // Append a 'From:' field -vmime::ref fromField = +vmime::shared_ptr fromField = hfFactory->create(vmime::fields::FROM); fromField->setValue - (vmime::create ("me@vmime.org")); + (vmime::make_shared ("me@vmime.org")); hdr->appendField(fromField); // Append a 'To:' field -vmime::ref toField = +vmime::shared_ptr toField = hfFactory->create(vmime::fields::TO); -vmime::ref recipients = - vmime::create (); +vmime::shared_ptr recipients = + vmime::make_shared (); recipients->appendMailbox - (vmime::create ("you@vmime.org")); + (vmime::make_shared ("you@vmime.org")); toField->setValue(recipients); hdr->appendField(toField); // Set the body contents -bdy->setContents(vmime::create +bdy->setContents(vmime::make_shared ("This is the text of your message...")); // Output raw message data to standard output @@ -215,14 +215,14 @@ try mb.setSubject(vmime::text("Message subject")); mb.setExpeditor(vmime::mailbox("me@vmime.org")); mb.getRecipients().appendAddress - (vmime::create ("you@vmime.org")); + (vmime::make_shared ("you@vmime.org")); mb.getTextPart()->setCharset(vmime::charsets::ISO8859_15); - mb.getTextPart()->setText(vmime::create + mb.getTextPart()->setText(vmime::make_shared ("This is the text of your message...")); // Message construction - vmime::ref msg = mb.construct(); + vmime::shared_ptr msg = mb.construct(); // Output raw message data to standard output vmime::utility::outputStreamAdapter out(std::cout); @@ -249,8 +249,8 @@ previous example to attach a file to the message: \begin{lstlisting}[caption={Building a message with an attachment using {\vcode vmime::messageBuilder}}] // Create an attachment -vmime::ref att = - vmime::create +vmime::shared_ptr att = + vmime::make_shared ( /* full path to file */ "/home/vincent/paris.jpg", /* content type */ vmime::mediaType("image/jpeg), @@ -284,7 +284,7 @@ using the {\vcode vmime::messageBuilder}}] mb.setSubject(vmime::text("An HTML message")); mb.setExpeditor(vmime::mailbox("me@vmime.org")); mb.getRecipients().appendAddress - (vmime::create ("you@vmime.org")); + (vmime::make_shared ("you@vmime.org")); // Set the content-type to "text/html": a text part factory must be // available for the type you are using. The following code will make @@ -294,8 +294,8 @@ mb.constructTextPart(vmime::mediaType // Set contents of the text parts; the message is available in two formats: // HTML and plain text. The HTML format also includes an embedded image. -vmime::ref textPart = - mb.getTextPart().dynamicCast (); +vmime::shared_ptr textPart = + vmime::dynamicCast (mb.getTextPart()); // -- Add the JPEG image (the returned identifier is used to identify the // -- embedded object in the HTML text, the famous "CID", or "Content-Id"). @@ -306,11 +306,11 @@ const vmime::string id = textPart->addObject("<...image data...>", // -- Set the text textPart->setCharset(vmime::charsets::ISO8859_15); -textPart->setText(vmime::create +textPart->setText(vmime::make_shared ("This is the HTML text, and the image:
" "")); -textPart->setPlainText(vmime::create +textPart->setPlainText(vmime::make_shared ("This is the plain text.")); \end{lstlisting} @@ -332,11 +332,11 @@ input stream, then add an embedded object: vmime::utility::fileSystemFactory* fs = vmime::platform::getHandler()->getFileSystemFactory(); -vmime::ref imageFile = +vmime::shared_ptr imageFile = fs->create(fs->stringToPath("/path/to/image.jpg")); -vmime::ref imageCts = - vmime::create +vmime::shared_ptr imageCts = + vmime::make_shared (imageFile->getFileReader()->getInputStream(), imageFile->getLength()); const vmime::string cid = textPart.addObject(imageCts, @@ -359,12 +359,12 @@ The following code snippet tests if a body part is an attachment, and if so, extract its contents to the standard output: \begin{lstlisting}[caption={Testing if a body part is an attachment}] -vmime::ref part; // suppose we have a body part +vmime::shared_ptr part; // suppose we have a body part if (vmime::attachmentHelper::isBodyPartAnAttachment(part)) { // The body part contains an attachment, get it - vmime::ref attach = + vmime::shared_ptr attach = attachmentHelper::getBodyPartAttachment(part); // Extract attachment data to standard output @@ -376,7 +376,7 @@ if (vmime::attachmentHelper::isBodyPartAnAttachment(part)) You can also easily extract all attachments from a message: \begin{lstlisting}[caption={Extracting all attachments from a message}] -vmime::ref msg; // suppose we have a message +vmime::shared_ptr msg; // suppose we have a message const std::vector > atts = attachmentHelper::findAttachmentsInMessage(msg); @@ -390,11 +390,11 @@ exists in the message). Simply call the {\vcode addAttachment} function: \begin{lstlisting}[caption={Adding an attachment to an existing message}] -vmime::ref msg; // suppose we have a message +vmime::shared_ptr msg; // suppose we have a message // Create an attachment -vmime::ref att = - vmime::create +vmime::shared_ptr att = + vmime::make_shared ( /* full path to file */ "/home/vincent/paris.jpg", /* content type */ vmime::mediaType("image/jpeg), -- cgit v1.2.3