/usr/share/doc/libvmime-dev/README.refcounting is in libvmime-dev 0.9.1-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | ==============================================
Reference counting and smart pointers in VMime
==============================================
I. Introduction
===============
Since version 0.7.2cvs, VMime has been modified to use smart pointers and
reference counting instead of raw pointers.
This simplifies a lot using VMime objects as you don't have to worry about
freeing memory occupied by objects, or even wondering which of your program
or VMime is responsible for deleting the object.
This is also convenient when a function returns a list of objects. Before,
you wrote:
std::vector <vmime::messaging::folder*> subFolders = folder->getFolders();
...do something with result...
for (std::vector <vmime::messaging::folder*>::iterator
it = subFolders.begin() ; it != subFolders.end() ; ++it)
{
delete *it;
}
Now, you can simply write:
std::vector <vmime::messaging::folder*> subFolders = folder->getFolders();
...do something with result...
and nothing more!
Two new template classes were introduced:
- vmime::ref <> holds a strong reference to an object. When there is no
more strong reference pointing to an object, the object is deleted.
- vmime::weak_ref <> holds a weak reference to an object. A weak reference
automatically points to NULL when the last strong reference is released.
It can be used to bypass the problems with circular references: A holds
a strong reference to B, which holds a strong reference back to A.
II. Creating objects
====================
You should not use 'new' to allocate VMime objects anymore. Instead, you
should use the vmime::create() helper function:
vmime::ref <vmime::mailbox> mbox =
vmime::create <vmime::mailbox>("me@somewhere.com");
III. Casting
============
Like raw C++ pointers, you can cast VMime references. Implicit downcast is
also supported.
To do a dynamic cast, write:
vmime::ref <vmime::component> foo = ...
vmime::ref <vmime::mailbox> mbox = foo.dynamicCast <vmime::mailbox>()
then 'mbox' will be set to null ref if the dynamic cast failed (ie. if dynamic
type of 'foo' is not/is not derived from 'vmime::mailbox').
The same thing is possible with static cast:
vmime::ref <vmime::component> foo = ...
vmime::ref <vmime::mailbox> mbox = foo.staticCast <vmime::mailbox>()
Like in standard C++, if 'foo' is not really a 'vmime::mailbox', the 'mbox'
reference can point to anything (ie. "invalid"), so be careful...
Finally, const cast is also supported:
vmime::ref <const vmime::component> foo_const = ...
vmime::ref <vmime::component> foo = foo_const.constCast();
IV. Upgrading your code from version <= 0.7.1
=============================================
1. vmime::text
--------------
In v0.7.1 and below:
vmime::text t1;
vmime::newFromString("blah blah", vmime::charset(...), &t1);
vmime::text* t2 = vmime::newFromString("foo", vmime::charset(...));
In v0.7.2:
vmime::text t1;
t1.createFromString("blah blah", vmime::charset(...));
vmime::ref <vmime::text> t2 = vmime::newFromString("foo", vmime::charset(...));
|