ptr_container/doc/examples.html
Christos Stratopoulos 1ae651ac66 Delete extra space
2018-04-07 00:39:01 -04:00

901 lines
46 KiB
HTML

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
<title>Boost Pointer Container Library</title>
<style type="text/css">
/*
:Author: David Goodger (goodger@python.org)
:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
.subscript {
vertical-align: sub;
font-size: smaller }
.superscript {
vertical-align: super;
font-size: smaller }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
overflow: hidden;
}
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title, .code .error {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em }
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin: 0 0 0.5em 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left, .figure.align-left, object.align-left, table.align-left {
clear: left ;
float: left ;
margin-right: 1em }
img.align-right, .figure.align-right, object.align-right, table.align-right {
clear: right ;
float: right ;
margin-left: 1em }
img.align-center, .figure.align-center, object.align-center {
display: block;
margin-left: auto;
margin-right: auto;
}
table.align-center {
margin-left: auto;
margin-right: auto;
}
.align-left {
text-align: left }
.align-center {
clear: both ;
text-align: center }
.align-right {
text-align: right }
/* reset inner alignment in figures */
div.align-right {
text-align: inherit }
/* div.align-center * { */
/* text-align: left } */
.align-top {
vertical-align: top }
.align-middle {
vertical-align: middle }
.align-bottom {
vertical-align: bottom }
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font: inherit }
pre.literal-block, pre.doctest-block, pre.math, pre.code {
margin-left: 2em ;
margin-right: 2em }
pre.code .ln { color: grey; } /* line numbers */
pre.code, code { background-color: #eeeeee }
pre.code .comment, code .comment { color: #5C6576 }
pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
pre.code .literal.string, code .literal.string { color: #0C5404 }
pre.code .name.builtin, code .name.builtin { color: #352B84 }
pre.code .deleted, code .deleted { background-color: #DEB0A1}
pre.code .inserted, code .inserted { background-color: #A3D289}
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
/* "booktabs" style (no vertical lines) */
table.docutils.booktabs {
border: 0px;
border-top: 2px solid;
border-bottom: 2px solid;
border-collapse: collapse;
}
table.docutils.booktabs * {
border: 0px;
}
table.docutils.booktabs th {
border-bottom: thin solid;
text-align: left;
}
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
ul.auto-toc {
list-style-type: none }
</style>
</head>
<body>
<div class="document" id="boost-pointer-container-library">
<h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
<h2 class="subtitle" id="examples">Examples</h2>
<p>Some examples are given here and in the accompanying test files:</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#null-pointers-cannot-be-stored-in-the-containers" id="id2">1. Null pointers cannot be stored in the containers</a></li>
<li><a class="reference internal" href="#iterators-and-other-operations-return-indirected-values" id="id3">2. Iterators and other operations return indirected values</a></li>
<li><a class="reference internal" href="#copy-semantics-of-pointer-containers" id="id4">3. Copy-semantics of pointer containers</a></li>
<li><a class="reference internal" href="#making-a-non-copyable-type-cloneable" id="id5">4. Making a non-copyable type Cloneable</a></li>
<li><a class="reference internal" href="#objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container" id="id6">5. Objects are cloned before insertion, inserted pointers are owned by the container</a></li>
<li><a class="reference internal" href="#transferring-ownership-of-a-single-element" id="id7">6. Transferring ownership of a single element</a></li>
<li><a class="reference internal" href="#transferring-ownership-of-pointers-between-different-pointer-containers" id="id8">7. Transferring ownership of pointers between different pointer containers</a></li>
<li><a class="reference internal" href="#selected-test-files" id="id9">8. Selected test files</a></li>
<li><a class="reference internal" href="#a-large-example" id="id10">9. A large example</a></li>
</ul>
</div>
<div class="section" id="null-pointers-cannot-be-stored-in-the-containers">
<span id="example-1"></span><h1><a class="toc-backref" href="#id2">1. Null pointers cannot be stored in the containers</a></h1>
<pre class="literal-block">
my_container.push_back( 0 ); // throws bad_ptr
my_container.replace( an_iterator, 0 ); // throws bad_ptr
my_container.insert( an_iterator, 0 ); // throws bad_ptr
std::auto_ptr&lt;T&gt; p( 0 );
my_container.push_back( p ); // throws bad_ptr
</pre>
</div>
<div class="section" id="iterators-and-other-operations-return-indirected-values">
<span id="example-2"></span><h1><a class="toc-backref" href="#id3">2. Iterators and other operations return indirected values</a></h1>
<pre class="literal-block">
ptr_vector&lt;X&gt; pvec;
std::vector&lt;X*&gt; vec;
*vec.begin() = new X; // fine, memory leak
*pvec.begin() = new X; // compile time error
( *vec.begin() )-&gt;foo(); // call X::foo(), a bit clumsy
pvec.begin()-&gt;foo(); // no indirection needed
*vec.front() = X(); // overwrite first element
pvec.front() = X(); // no indirection needed
</pre>
</div>
<div class="section" id="copy-semantics-of-pointer-containers">
<span id="example-3"></span><h1><a class="toc-backref" href="#id4">3. Copy-semantics of pointer containers</a></h1>
<pre class="literal-block">
ptr_vector&lt;T&gt; vec1;
...
ptr_vector&lt;T&gt; vec2( vec1.clone() ); // deep copy objects of 'vec1' and use them to construct 'vec2', could be very expensive
vec2 = vec1.release(); // give up ownership of pointers in 'vec1' and pass the ownership to 'vec2', rather cheap
vec2.release(); // give up ownership; the objects will be deallocated if not assigned to another container
vec1 = vec2; // deep copy objects of 'vec2' and assign them to 'vec1', could be very expensive
ptr_vector&lt;T&gt; vec3( vec1 ); // deep copy objects of 'vec1', could be very expensive
</pre>
</div>
<div class="section" id="making-a-non-copyable-type-cloneable">
<span id="example-4"></span><h1><a class="toc-backref" href="#id5">4. Making a non-copyable type Cloneable</a></h1>
<pre class="literal-block">
// a class that has no normal copy semantics
class X : boost::noncopyable { public: X* clone() const; ... };
// this will be found by the library by argument dependent lookup (ADL)
X* new_clone( const X&amp; x )
{ return x.clone(); }
// we can now use the interface that requires cloneability
ptr_vector&lt;X&gt; vec1, vec2;
...
vec2 = vec1.clone(); // 'clone()' requires cloning &lt;g&gt;
vec2.insert( vec2.end(), vec1.begin(), vec1.end() ); // inserting always means inserting clones
</pre>
</div>
<div class="section" id="objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container">
<span id="example-5"></span><h1><a class="toc-backref" href="#id6">5. Objects are cloned before insertion, inserted pointers are owned by the container</a></h1>
<pre class="literal-block">
class X { ... }; // assume 'X' is Cloneable
X x; // and 'X' can be stack-allocated
ptr_list&lt;X&gt; list;
list.push_back( new_clone( x ) ); // insert a clone
list.push_back( new X ); // always give the pointer directly to the container to avoid leaks
list.push_back( &amp;x ); // don't do this!!!
std::auto_ptr&lt;X&gt; p( new X );
list.push_back( p ); // give up ownership
BOOST_ASSERT( p.get() == 0 );
</pre>
</div>
<div class="section" id="transferring-ownership-of-a-single-element">
<span id="example-6"></span><h1><a class="toc-backref" href="#id7">6. Transferring ownership of a single element</a></h1>
<pre class="literal-block">
ptr_deque&lt;T&gt; deq;
typedef ptr_deque&lt;T&gt;::auto_type auto_type;
// ... fill the container somehow
auto_type ptr = deq.pop_back(); // remove back element from container and give up ownership
auto_type ptr2 = deq.release( deq.begin() + 2 ); // use an iterator to determine the element to release
ptr = deq.pop_front(); // supported for 'ptr_list' and 'ptr_deque'
deq.push_back( ptr.release() ); // give ownership back to the container
</pre>
</div>
<div class="section" id="transferring-ownership-of-pointers-between-different-pointer-containers">
<span id="example-7"></span><h1><a class="toc-backref" href="#id8">7. Transferring ownership of pointers between different pointer containers</a></h1>
<pre class="literal-block">
ptr_list&lt;X&gt; list; ptr_vector&lt;X&gt; vec;
...
//
// note: no cloning happens in these examples
//
list.transfer( list.begin(), vec.begin(), vec ); // make the first element of 'vec' the first element of 'list'
vec.transfer( vec.end(), list.begin(), list.end(), list ); // put all the lists element into the vector
</pre>
<p>We can also transfer objects from <tt class="docutils literal">ptr_container&lt;Derived&gt;</tt> to <tt class="docutils literal">ptr_container&lt;Base&gt;</tt> without any problems.</p>
</div>
<div class="section" id="selected-test-files">
<span id="example-8"></span><h1><a class="toc-backref" href="#id9">8. Selected test files</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name" colspan="2"><a class="reference external" href="../test/incomplete_type_test.cpp">incomplete_type_test.cpp</a>:</th></tr>
<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how to implement the Composite pattern.</td>
</tr>
<tr class="field"><th class="field-name" colspan="2"><a class="reference external" href="../test/simple_test.cpp">simple_test.cpp</a>:</th></tr>
<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how the usage of pointer container compares with a
container of smart pointers</td>
</tr>
<tr class="field"><th class="field-name" colspan="2"><a class="reference external" href="../test/view_example.cpp">view_example.cpp</a>:</th></tr>
<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how to use a pointer container as a view into other container</td>
</tr>
<tr class="field"><th class="field-name"><a class="reference external" href="../test/tree_test.cpp">tree_test.cpp</a>:</th><td class="field-body">Shows how to make a tree-structure</td>
</tr>
<tr class="field"><th class="field-name"><a class="reference external" href="../test/ptr_array.cpp">array_test.cpp</a>:</th><td class="field-body">Shows how to make an n-ary tree</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="a-large-example">
<h1><a class="toc-backref" href="#id10">9. A large example</a></h1>
<p>This example shows many of the most common
features at work. The example provide lots of comments.
The source code can also be found <a class="reference external" href="../test/tut1.cpp">here</a>.</p>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> </title>
<link rel="stylesheet" href="default.css" type="text/css">
</head>
<body>
<pre><span class=comment>//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2003-2005. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
//
// This example is intended to get you started.
// Notice how the smart container
//
// 1. takes ownership of objects
// 2. transfers ownership
// 3. applies indirection to iterators
// 4. clones objects from other smart containers
//
//
// First we select which container to use.
//</span>
<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>ptr_container</span><span class=special>/</span><span class=identifier>ptr_deque</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=comment>//
// we need these later in the example
//</span>
<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>boost</span><span class=special>/</span><span class=identifier>assert</span><span class=special>.</span><span class=identifier>hpp</span><span class=special>&gt;</span>
<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>string</span><span class=special>&gt;</span>
<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>exception</span><span class=special>&gt;</span>
<span class=comment>//
// Then we define a small polymorphic class
// hierarchy.
//</span>
<span class=keyword>class</span> <span class=identifier>animal</span> <span class=special>:</span> <span class=identifier>boost</span><span class=special>::</span><span class=identifier>noncopyable</span>
<span class=special>{</span>
<span class=keyword>virtual</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>do_speak</span><span class=special>()</span> <span class=keyword>const</span> <span class=special>=</span> <span class=number>0</span><span class=special>;</span>
<span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>name_</span><span class=special>;</span>
<span class=keyword>protected</span><span class=special>:</span>
<span class=comment>//
// Animals cannot be copied...
//</span>
<span class=identifier>animal</span><span class=special>(</span> <span class=keyword>const</span> <span class=identifier>animal</span><span class=special>&amp;</span> <span class=identifier>r</span> <span class=special>)</span> <span class=special>:</span> <span class=identifier>name_</span><span class=special>(</span> <span class=identifier>r</span><span class=special>.</span><span class=identifier>name_</span> <span class=special>)</span> <span class=special>{</span> <span class=special>}</span>
<span class=keyword>void</span> <span class=keyword>operator</span><span class=special>=(</span> <span class=keyword>const</span> <span class=identifier>animal</span><span class=special>&amp;</span> <span class=special>);</span>
<span class=keyword>private</span><span class=special>:</span>
<span class=comment>//
// ...but due to advances in genetics, we can clone them!
//</span>
<span class=keyword>virtual</span> <span class=identifier>animal</span><span class=special>*</span> <span class=identifier>do_clone</span><span class=special>()</span> <span class=keyword>const</span> <span class=special>=</span> <span class=number>0</span><span class=special>;</span>
<span class=keyword>public</span><span class=special>:</span>
<span class=identifier>animal</span><span class=special>(</span> <span class=keyword>const</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>&amp;</span> <span class=identifier>name</span> <span class=special>)</span> <span class=special>:</span> <span class=identifier>name_</span><span class=special>(</span><span class=identifier>name</span><span class=special>)</span> <span class=special>{</span> <span class=special>}</span>
<span class=keyword>virtual</span> <span class=special>~</span><span class=identifier>animal</span><span class=special>()</span> <span class=keyword>throw</span><span class=special>()</span> <span class=special>{</span> <span class=special>}</span>
<span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>speak</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>do_speak</span><span class=special>();</span>
<span class=special>}</span>
<span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>name</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>name_</span><span class=special>;</span>
<span class=special>}</span>
<span class=identifier>animal</span><span class=special>*</span> <span class=identifier>clone</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>do_clone</span><span class=special>();</span>
<span class=special>}</span>
<span class=special>};</span>
<span class=comment>//
// An animal is still not Clonable. We need this last hook.
//
// Notice that we pass the animal by const reference
// and return by pointer.
//</span>
<span class=identifier>animal</span><span class=special>*</span> <span class=identifier>new_clone</span><span class=special>(</span> <span class=keyword>const</span> <span class=identifier>animal</span><span class=special>&amp;</span> <span class=identifier>a</span> <span class=special>)</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>a</span><span class=special>.</span><span class=identifier>clone</span><span class=special>();</span>
<span class=special>}</span>
<span class=comment>//
// We do not need to define 'delete_clone()' since
// since the default is to call the default 'operator delete()'.
//</span>
<span class=keyword>const</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>muuuh</span> <span class=special>=</span> <span class=string>&quot;Muuuh!&quot;</span><span class=special>;</span>
<span class=keyword>const</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>oiink</span> <span class=special>=</span> <span class=string>&quot;Oiiink&quot;</span><span class=special>;</span>
<span class=keyword>class</span> <span class=identifier>cow</span> <span class=special>:</span> <span class=keyword>public</span> <span class=identifier>animal</span>
<span class=special>{</span>
<span class=keyword>virtual</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>do_speak</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>muuuh</span><span class=special>;</span>
<span class=special>}</span>
<span class=keyword>virtual</span> <span class=identifier>animal</span><span class=special>*</span> <span class=identifier>do_clone</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=keyword>new</span> <span class=identifier>cow</span><span class=special>(</span> <span class=special>*</span><span class=keyword>this</span> <span class=special>);</span>
<span class=special>}</span>
<span class=keyword>public</span><span class=special>:</span>
<span class=identifier>cow</span><span class=special>(</span> <span class=keyword>const</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>&amp;</span> <span class=identifier>name</span> <span class=special>)</span> <span class=special>:</span> <span class=identifier>animal</span><span class=special>(</span><span class=identifier>name</span><span class=special>)</span> <span class=special>{</span> <span class=special>}</span>
<span class=special>};</span>
<span class=keyword>class</span> <span class=identifier>pig</span> <span class=special>:</span> <span class=keyword>public</span> <span class=identifier>animal</span>
<span class=special>{</span>
<span class=keyword>virtual</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>do_speak</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>oiink</span><span class=special>;</span>
<span class=special>}</span>
<span class=keyword>virtual</span> <span class=identifier>animal</span><span class=special>*</span> <span class=identifier>do_clone</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=keyword>new</span> <span class=identifier>pig</span><span class=special>(</span> <span class=special>*</span><span class=keyword>this</span> <span class=special>);</span>
<span class=special>}</span>
<span class=keyword>public</span><span class=special>:</span>
<span class=identifier>pig</span><span class=special>(</span> <span class=keyword>const</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span><span class=special>&amp;</span> <span class=identifier>name</span> <span class=special>)</span> <span class=special>:</span> <span class=identifier>animal</span><span class=special>(</span><span class=identifier>name</span><span class=special>)</span> <span class=special>{</span> <span class=special>}</span>
<span class=special>};</span>
<span class=comment>//
// Then we, of course, need a place to put all
// those animals.
//</span>
<span class=keyword>class</span> <span class=identifier>farm</span>
<span class=special>{</span>
<span class=comment>//
// This is where the smart containers are handy
//</span>
<span class=keyword>typedef</span> <span class=identifier>boost</span><span class=special>::</span><span class=identifier>ptr_deque</span><span class=special>&lt;</span><span class=identifier>animal</span><span class=special>&gt;</span> <span class=identifier>barn_type</span><span class=special>;</span>
<span class=identifier>barn_type</span> <span class=identifier>barn</span><span class=special>;</span>
<span class=comment>//
// A convenience typedef for the compiler-appropriate
// smart pointer used to manage barns
//</span>
<span class=keyword>typedef</span> <span class=identifier><a href="compatible_smart_ptr.html"><em>compatible-smart-ptr</em></a></span><span class=special>&lt;</span><span class=identifier>barn_type</span><span class=special>&gt;</span> <span class=identifier>raii_ptr</span><span class=special>;</span>
<span class=comment>//
// An error type
//</span>
<span class=keyword>struct</span> <span class=identifier>farm_trouble</span> <span class=special>:</span> <span class=keyword>public</span> <span class=identifier>std</span><span class=special>::</span><span class=identifier>exception</span> <span class=special>{</span> <span class=special>};</span>
<span class=keyword>public</span><span class=special>:</span>
<span class=comment>//
// We would like to make it possible to
// iterate over the animals in the farm
//</span>
<span class=keyword>typedef</span> <span class=identifier>barn_type</span><span class=special>::</span><span class=identifier>iterator</span> <span class=identifier>animal_iterator</span><span class=special>;</span>
<span class=comment>//
// We also need to count the farm's size...
//</span>
<span class=keyword>typedef</span> <span class=identifier>barn_type</span><span class=special>::</span><span class=identifier>size_type</span> <span class=identifier>size_type</span><span class=special>;</span>
<span class=comment>//
// And we also want to transfer an animal
// safely around. The easiest way to think
// about '::auto_type' is to imagine a simplified
// 'std::auto_ptr&lt;T&gt;' ... this means you can expect
//
// T* operator-&gt;()
// T* release()
// deleting destructor
//
// but not more.
//</span>
<span class=keyword>typedef</span> <span class=identifier>barn_type</span><span class=special>::</span><span class=identifier>auto_type</span> <span class=identifier>animal_transport</span><span class=special>;</span>
<span class=comment>//
// Create an empty farm.
//</span>
<span class=identifier>farm</span><span class=special>()</span> <span class=special>{</span> <span class=special>}</span>
<span class=comment>//
// We need a constructor that can make a new
// farm by cloning a range of animals.
//</span>
<span class=identifier>farm</span><span class=special>(</span> <span class=identifier>animal_iterator</span> <span class=identifier>begin</span><span class=special>,</span> <span class=identifier>animal_iterator</span> <span class=identifier>end</span> <span class=special>)</span>
<span class=special>:</span>
<span class=comment>//
// Objects are always cloned before insertion
// unless we explicitly add a pointer or
// use 'release()'. Therefore we actually
// clone all animals in the range
//</span>
<span class=identifier>barn</span><span class=special>(</span> <span class=identifier>begin</span><span class=special>,</span> <span class=identifier>end</span> <span class=special>)</span> <span class=special>{</span> <span class=special>}</span>
<span class=comment>//
// ... so we need some other function too
//</span>
<span class=identifier>animal_iterator</span> <span class=identifier>begin</span><span class=special>()</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>begin</span><span class=special>();</span>
<span class=special>}</span>
<span class=identifier>animal_iterator</span> <span class=identifier>end</span><span class=special>()</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>end</span><span class=special>();</span>
<span class=special>}</span>
<span class=comment>//
// Here it is quite ok to have an 'animal*' argument.
// The smart container will handle all ownership
// issues.
//</span>
<span class=keyword>void</span> <span class=identifier>buy_animal</span><span class=special>(</span> <span class=identifier>animal</span><span class=special>*</span> <span class=identifier>a</span> <span class=special>)</span>
<span class=special>{</span>
<span class=identifier>barn</span><span class=special>.</span><span class=identifier>push_back</span><span class=special>(</span> <span class=identifier>a</span> <span class=special>);</span>
<span class=special>}</span>
<span class=comment>//
// The farm can also be in economical trouble and
// therefore be in the need to sell animals.
//</span>
<span class=identifier>animal_transport</span> <span class=identifier>sell_animal</span><span class=special>(</span> <span class=identifier>animal_iterator</span> <span class=identifier>to_sell</span> <span class=special>)</span>
<span class=special>{</span>
<span class=keyword>if</span><span class=special>(</span> <span class=identifier>to_sell</span> <span class=special>==</span> <span class=identifier>end</span><span class=special>()</span> <span class=special>)</span>
<span class=keyword>throw</span> <span class=identifier>farm_trouble</span><span class=special>();</span>
<span class=comment>//
// Here we remove the animal from the barn,
// but the animal is not deleted yet...it's
// up to the buyer to decide what
// to do with it.
//</span>
<span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>release</span><span class=special>(</span> <span class=identifier>to_sell</span> <span class=special>);</span>
<span class=special>}</span>
<span class=comment>//
// How big a farm do we have?
//</span>
<span class=identifier>size_type</span> <span class=identifier>size</span><span class=special>()</span> <span class=keyword>const</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>size</span><span class=special>();</span>
<span class=special>}</span>
<span class=comment>//
// If things are bad, we might choose to sell all animals :-(
//</span>
<span class=identifier>raii_ptr</span> <span class=identifier>sell_farm</span><span class=special>()</span>
<span class=special>{</span>
<span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>release</span><span class=special>();</span>
<span class=special>}</span>
<span class=comment>//
// However, if things are good, we might buy somebody
// else's farm :-)
//</span>
<span class=keyword>void</span> <span class=identifier>buy_farm</span><span class=special>(</span> <span class=identifier>raii_ptr</span> <span class=identifier>other</span> <span class=special>)</span>
<span class=special>{</span>
<span class=comment>//
// This line inserts all the animals from 'other'
// and is guaranteed either to succeed or to have no
// effect
//</span>
<span class=identifier>barn</span><span class=special>.</span><span class=identifier>transfer</span><span class=special>(</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>end</span><span class=special>(),</span> <span class=comment>// insert new animals at the end</span>
<span class=special>*</span><span class=identifier>other</span> <span class=special>);</span> <span class=comment>// we want to transfer all animals,
// so we use the whole container as argument
//
// You might think you would have to do
//
// other.release();
//
// but '*other' is empty and can go out of scope as it wants
//</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>other</span><span class=special>-&gt;</span><span class=identifier>empty</span><span class=special>()</span> <span class=special>);</span>
<span class=special>}</span>
<span class=special>};</span> <span class=comment>// class 'farm'.</span>
<span class=keyword>int</span> <span class=identifier>main</span><span class=special>()</span>
<span class=special>{</span>
<span class=comment>//
// First we make a farm
//</span>
<span class=identifier>farm</span> <span class=identifier>animal_farm</span><span class=special>;</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>0u</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>pig</span><span class=special>(</span><span class=string>&quot;Betty&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>pig</span><span class=special>(</span><span class=string>&quot;Benny&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>pig</span><span class=special>(</span><span class=string>&quot;Jeltzin&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>cow</span><span class=special>(</span><span class=string>&quot;Hanz&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>cow</span><span class=special>(</span><span class=string>&quot;Mary&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=keyword>new</span> <span class=identifier>cow</span><span class=special>(</span><span class=string>&quot;Frederik&quot;</span><span class=special>)</span> <span class=special>);</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>6u</span> <span class=special>);</span>
<span class=comment>//
// Then we make another farm...it will actually contain
// a clone of the other farm.
//</span>
<span class=identifier>farm</span> <span class=identifier>new_farm</span><span class=special>(</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(),</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>end</span><span class=special>()</span> <span class=special>);</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>6u</span> <span class=special>);</span>
<span class=comment>//
// Is it really clones in the new farm?
//</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>begin</span><span class=special>()-&gt;</span><span class=identifier>name</span><span class=special>()</span> <span class=special>==</span> <span class=string>&quot;Betty&quot;</span> <span class=special>);</span>
<span class=comment>//
// Then we search for an animal, Mary (the Crown Princess of Denmark),
// because we would like to buy her ...
//</span>
<span class=keyword>typedef</span> <span class=identifier>farm</span><span class=special>::</span><span class=identifier>animal_iterator</span> <span class=identifier>iterator</span><span class=special>;</span>
<span class=identifier>iterator</span> <span class=identifier>to_sell</span><span class=special>;</span>
<span class=keyword>for</span><span class=special>(</span> <span class=identifier>iterator</span> <span class=identifier>i</span> <span class=special>=</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>begin</span><span class=special>(),</span>
<span class=identifier>end</span> <span class=special>=</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>end</span><span class=special>();</span>
<span class=identifier>i</span> <span class=special>!=</span> <span class=identifier>end</span><span class=special>;</span> <span class=special>++</span><span class=identifier>i</span> <span class=special>)</span>
<span class=special>{</span>
<span class=keyword>if</span><span class=special>(</span> <span class=identifier>i</span><span class=special>-&gt;</span><span class=identifier>name</span><span class=special>()</span> <span class=special>==</span> <span class=string>&quot;Mary&quot;</span> <span class=special>)</span>
<span class=special>{</span>
<span class=identifier>to_sell</span> <span class=special>=</span> <span class=identifier>i</span><span class=special>;</span>
<span class=keyword>break</span><span class=special>;</span>
<span class=special>}</span>
<span class=special>}</span>
<span class=identifier>farm</span><span class=special>::</span><span class=identifier>animal_transport</span> <span class=identifier>mary</span> <span class=special>=</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>sell_animal</span><span class=special>(</span> <span class=identifier>to_sell</span> <span class=special>);</span>
<span class=keyword>if</span><span class=special>(</span> <span class=identifier>mary</span><span class=special>-&gt;</span><span class=identifier>speak</span><span class=special>()</span> <span class=special>==</span> <span class=identifier>muuuh</span> <span class=special>)</span>
<span class=comment>//
// Great, Mary is a cow, and she may live longer
//</span>
<span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>buy_animal</span><span class=special>(</span> <span class=identifier>mary</span><span class=special>.</span><span class=identifier>release</span><span class=special>()</span> <span class=special>);</span>
<span class=keyword>else</span>
<span class=comment>//
// Then the animal would be destroyed (!)
// when we go out of scope.
//</span>
<span class=special>;</span>
<span class=comment>//
// Now we can observe some changes to the two farms...
//</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>5u</span> <span class=special>);</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>7u</span> <span class=special>);</span>
<span class=comment>//
// The new farm has however underestimated how much
// it cost to feed Mary and its owner is forced to sell the farm...
//</span>
<span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>buy_farm</span><span class=special>(</span> <span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>sell_farm</span><span class=special>()</span> <span class=special>);</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>new_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>0u</span> <span class=special>);</span>
<span class=identifier>BOOST_ASSERT</span><span class=special>(</span> <span class=identifier>animal_farm</span><span class=special>.</span><span class=identifier>size</span><span class=special>()</span> <span class=special>==</span> <span class=number>12u</span> <span class=special>);</span>
<span class=special>}</span>
</pre>
</body>
</html>
<!-- 10. Changing the Clone Allocator
++++++++++++++++++++++++++++++++
This example shows how we can change
the Clone Allocator to use the pointer containers
as view into other containers:
.. raw:: html
:file: tut2.html -->
<hr><p><strong>Navigate:</strong></p>
<ul class="simple">
<li><a class="reference external" href="ptr_container.html">home</a></li>
<li><a class="reference external" href="reference.html">reference</a></li>
</ul>
<hr><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt</a>).</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>