• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
7<title>Boost Pointer Container Library</title>
8<style type="text/css">
9
10/*
11:Author: David Goodger (goodger@python.org)
12:Id: $Id: html4css1.css 7952 2016-07-26 18:15:59Z milde $
13:Copyright: This stylesheet has been placed in the public domain.
14
15Default cascading style sheet for the HTML output of Docutils.
16
17See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
18customize this style sheet.
19*/
20
21/* used to remove borders from tables and images */
22.borderless, table.borderless td, table.borderless th {
23  border: 0 }
24
25table.borderless td, table.borderless th {
26  /* Override padding for "table.docutils td" with "! important".
27     The right padding separates the table cells. */
28  padding: 0 0.5em 0 0 ! important }
29
30.first {
31  /* Override more specific margin styles with "! important". */
32  margin-top: 0 ! important }
33
34.last, .with-subtitle {
35  margin-bottom: 0 ! important }
36
37.hidden {
38  display: none }
39
40.subscript {
41  vertical-align: sub;
42  font-size: smaller }
43
44.superscript {
45  vertical-align: super;
46  font-size: smaller }
47
48a.toc-backref {
49  text-decoration: none ;
50  color: black }
51
52blockquote.epigraph {
53  margin: 2em 5em ; }
54
55dl.docutils dd {
56  margin-bottom: 0.5em }
57
58object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
59  overflow: hidden;
60}
61
62/* Uncomment (and remove this text!) to get bold-faced definition list terms
63dl.docutils dt {
64  font-weight: bold }
65*/
66
67div.abstract {
68  margin: 2em 5em }
69
70div.abstract p.topic-title {
71  font-weight: bold ;
72  text-align: center }
73
74div.admonition, div.attention, div.caution, div.danger, div.error,
75div.hint, div.important, div.note, div.tip, div.warning {
76  margin: 2em ;
77  border: medium outset ;
78  padding: 1em }
79
80div.admonition p.admonition-title, div.hint p.admonition-title,
81div.important p.admonition-title, div.note p.admonition-title,
82div.tip p.admonition-title {
83  font-weight: bold ;
84  font-family: sans-serif }
85
86div.attention p.admonition-title, div.caution p.admonition-title,
87div.danger p.admonition-title, div.error p.admonition-title,
88div.warning p.admonition-title, .code .error {
89  color: red ;
90  font-weight: bold ;
91  font-family: sans-serif }
92
93/* Uncomment (and remove this text!) to get reduced vertical space in
94   compound paragraphs.
95div.compound .compound-first, div.compound .compound-middle {
96  margin-bottom: 0.5em }
97
98div.compound .compound-last, div.compound .compound-middle {
99  margin-top: 0.5em }
100*/
101
102div.dedication {
103  margin: 2em 5em ;
104  text-align: center ;
105  font-style: italic }
106
107div.dedication p.topic-title {
108  font-weight: bold ;
109  font-style: normal }
110
111div.figure {
112  margin-left: 2em ;
113  margin-right: 2em }
114
115div.footer, div.header {
116  clear: both;
117  font-size: smaller }
118
119div.line-block {
120  display: block ;
121  margin-top: 1em ;
122  margin-bottom: 1em }
123
124div.line-block div.line-block {
125  margin-top: 0 ;
126  margin-bottom: 0 ;
127  margin-left: 1.5em }
128
129div.sidebar {
130  margin: 0 0 0.5em 1em ;
131  border: medium outset ;
132  padding: 1em ;
133  background-color: #ffffee ;
134  width: 40% ;
135  float: right ;
136  clear: right }
137
138div.sidebar p.rubric {
139  font-family: sans-serif ;
140  font-size: medium }
141
142div.system-messages {
143  margin: 5em }
144
145div.system-messages h1 {
146  color: red }
147
148div.system-message {
149  border: medium outset ;
150  padding: 1em }
151
152div.system-message p.system-message-title {
153  color: red ;
154  font-weight: bold }
155
156div.topic {
157  margin: 2em }
158
159h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
160h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
161  margin-top: 0.4em }
162
163h1.title {
164  text-align: center }
165
166h2.subtitle {
167  text-align: center }
168
169hr.docutils {
170  width: 75% }
171
172img.align-left, .figure.align-left, object.align-left, table.align-left {
173  clear: left ;
174  float: left ;
175  margin-right: 1em }
176
177img.align-right, .figure.align-right, object.align-right, table.align-right {
178  clear: right ;
179  float: right ;
180  margin-left: 1em }
181
182img.align-center, .figure.align-center, object.align-center {
183  display: block;
184  margin-left: auto;
185  margin-right: auto;
186}
187
188table.align-center {
189  margin-left: auto;
190  margin-right: auto;
191}
192
193.align-left {
194  text-align: left }
195
196.align-center {
197  clear: both ;
198  text-align: center }
199
200.align-right {
201  text-align: right }
202
203/* reset inner alignment in figures */
204div.align-right {
205  text-align: inherit }
206
207/* div.align-center * { */
208/*   text-align: left } */
209
210.align-top    {
211  vertical-align: top }
212
213.align-middle {
214  vertical-align: middle }
215
216.align-bottom {
217  vertical-align: bottom }
218
219ol.simple, ul.simple {
220  margin-bottom: 1em }
221
222ol.arabic {
223  list-style: decimal }
224
225ol.loweralpha {
226  list-style: lower-alpha }
227
228ol.upperalpha {
229  list-style: upper-alpha }
230
231ol.lowerroman {
232  list-style: lower-roman }
233
234ol.upperroman {
235  list-style: upper-roman }
236
237p.attribution {
238  text-align: right ;
239  margin-left: 50% }
240
241p.caption {
242  font-style: italic }
243
244p.credits {
245  font-style: italic ;
246  font-size: smaller }
247
248p.label {
249  white-space: nowrap }
250
251p.rubric {
252  font-weight: bold ;
253  font-size: larger ;
254  color: maroon ;
255  text-align: center }
256
257p.sidebar-title {
258  font-family: sans-serif ;
259  font-weight: bold ;
260  font-size: larger }
261
262p.sidebar-subtitle {
263  font-family: sans-serif ;
264  font-weight: bold }
265
266p.topic-title {
267  font-weight: bold }
268
269pre.address {
270  margin-bottom: 0 ;
271  margin-top: 0 ;
272  font: inherit }
273
274pre.literal-block, pre.doctest-block, pre.math, pre.code {
275  margin-left: 2em ;
276  margin-right: 2em }
277
278pre.code .ln { color: grey; } /* line numbers */
279pre.code, code { background-color: #eeeeee }
280pre.code .comment, code .comment { color: #5C6576 }
281pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
282pre.code .literal.string, code .literal.string { color: #0C5404 }
283pre.code .name.builtin, code .name.builtin { color: #352B84 }
284pre.code .deleted, code .deleted { background-color: #DEB0A1}
285pre.code .inserted, code .inserted { background-color: #A3D289}
286
287span.classifier {
288  font-family: sans-serif ;
289  font-style: oblique }
290
291span.classifier-delimiter {
292  font-family: sans-serif ;
293  font-weight: bold }
294
295span.interpreted {
296  font-family: sans-serif }
297
298span.option {
299  white-space: nowrap }
300
301span.pre {
302  white-space: pre }
303
304span.problematic {
305  color: red }
306
307span.section-subtitle {
308  /* font-size relative to parent (h1..h6 element) */
309  font-size: 80% }
310
311table.citation {
312  border-left: solid 1px gray;
313  margin-left: 1px }
314
315table.docinfo {
316  margin: 2em 4em }
317
318table.docutils {
319  margin-top: 0.5em ;
320  margin-bottom: 0.5em }
321
322table.footnote {
323  border-left: solid 1px black;
324  margin-left: 1px }
325
326table.docutils td, table.docutils th,
327table.docinfo td, table.docinfo th {
328  padding-left: 0.5em ;
329  padding-right: 0.5em ;
330  vertical-align: top }
331
332table.docutils th.field-name, table.docinfo th.docinfo-name {
333  font-weight: bold ;
334  text-align: left ;
335  white-space: nowrap ;
336  padding-left: 0 }
337
338/* "booktabs" style (no vertical lines) */
339table.docutils.booktabs {
340  border: 0px;
341  border-top: 2px solid;
342  border-bottom: 2px solid;
343  border-collapse: collapse;
344}
345table.docutils.booktabs * {
346  border: 0px;
347}
348table.docutils.booktabs th {
349  border-bottom: thin solid;
350  text-align: left;
351}
352
353h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
354h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
355  font-size: 100% }
356
357ul.auto-toc {
358  list-style-type: none }
359
360</style>
361</head>
362<body>
363<div class="document" id="boost-pointer-container-library">
364<h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
365<h2 class="subtitle" id="examples">Examples</h2>
366
367<p>Some examples are given here and in the accompanying test files:</p>
368<div class="contents local topic" id="contents">
369<ul class="simple">
370<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>
371<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>
372<li><a class="reference internal" href="#copy-semantics-of-pointer-containers" id="id4">3. Copy-semantics of pointer containers</a></li>
373<li><a class="reference internal" href="#making-a-non-copyable-type-cloneable" id="id5">4. Making a non-copyable type Cloneable</a></li>
374<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>
375<li><a class="reference internal" href="#transferring-ownership-of-a-single-element" id="id7">6. Transferring ownership of a single element</a></li>
376<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>
377<li><a class="reference internal" href="#selected-test-files" id="id9">8. Selected test files</a></li>
378<li><a class="reference internal" href="#a-large-example" id="id10">9. A large example</a></li>
379</ul>
380</div>
381<div class="section" id="null-pointers-cannot-be-stored-in-the-containers">
382<span id="example-1"></span><h1><a class="toc-backref" href="#id2">1. Null pointers cannot be stored in the containers</a></h1>
383<pre class="literal-block">
384my_container.push_back( 0 );            // throws bad_ptr
385my_container.replace( an_iterator, 0 ); // throws bad_ptr
386my_container.insert( an_iterator, 0 );  // throws bad_ptr
387std::auto_ptr&lt;T&gt; p( 0 );
388my_container.push_back( p );            // throws bad_ptr
389</pre>
390</div>
391<div class="section" id="iterators-and-other-operations-return-indirected-values">
392<span id="example-2"></span><h1><a class="toc-backref" href="#id3">2. Iterators and other operations return indirected values</a></h1>
393<pre class="literal-block">
394ptr_vector&lt;X&gt; pvec;
395std::vector&lt;X*&gt; vec;
396*vec.begin()  = new X;   // fine, memory leak
397*pvec.begin() = new X;   // compile time error
398( *vec.begin() )-&gt;foo(); // call X::foo(), a bit clumsy
399pvec.begin()-&gt;foo();     // no indirection needed
400*vec.front()  = X();     // overwrite first element
401pvec.front()  = X();     // no indirection needed
402</pre>
403</div>
404<div class="section" id="copy-semantics-of-pointer-containers">
405<span id="example-3"></span><h1><a class="toc-backref" href="#id4">3. Copy-semantics of pointer containers</a></h1>
406<pre class="literal-block">
407ptr_vector&lt;T&gt; vec1;
408...
409ptr_vector&lt;T&gt; vec2( vec1.clone() ); // deep copy objects of 'vec1' and use them to construct 'vec2', could be very expensive
410vec2 = vec1.release();              // give up ownership of pointers in 'vec1' and pass the ownership to 'vec2', rather cheap
411vec2.release();                     // give up ownership; the objects will be deallocated if not assigned to another container
412vec1 = vec2;                        // deep copy objects of 'vec2' and assign them to 'vec1', could be very expensive
413ptr_vector&lt;T&gt; vec3( vec1 );         // deep copy objects of 'vec1', could be very expensive
414</pre>
415</div>
416<div class="section" id="making-a-non-copyable-type-cloneable">
417<span id="example-4"></span><h1><a class="toc-backref" href="#id5">4. Making a non-copyable type Cloneable</a></h1>
418<pre class="literal-block">
419 // a class that has no normal copy semantics
420class X : boost::noncopyable { public: X* clone() const; ... };
421
422// this will be found by the library by argument dependent lookup (ADL)
423X* new_clone( const X&amp; x )
424{ return x.clone(); }
425
426// we can now use the interface that requires cloneability
427ptr_vector&lt;X&gt; vec1, vec2;
428...
429vec2 = vec1.clone();                                 // 'clone()' requires cloning &lt;g&gt;
430vec2.insert( vec2.end(), vec1.begin(), vec1.end() ); // inserting always means inserting clones
431</pre>
432</div>
433<div class="section" id="objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container">
434<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>
435<pre class="literal-block">
436class X { ... };                     // assume 'X' is Cloneable
437X x;                                 // and 'X' can be stack-allocated
438ptr_list&lt;X&gt; list;
439list.push_back( new_clone( x ) );    // insert a clone
440list.push_back( new X );             // always give the pointer directly to the container to avoid leaks
441list.push_back( &amp;x );                // don't do this!!!
442std::auto_ptr&lt;X&gt; p( new X );
443list.push_back( p );                 // give up ownership
444BOOST_ASSERT( p.get() == 0 );
445</pre>
446</div>
447<div class="section" id="transferring-ownership-of-a-single-element">
448<span id="example-6"></span><h1><a class="toc-backref" href="#id7">6. Transferring ownership of a single element</a></h1>
449<pre class="literal-block">
450ptr_deque&lt;T&gt;                    deq;
451typedef ptr_deque&lt;T&gt;::auto_type auto_type;
452
453// ... fill the container somehow
454
455auto_type ptr  = deq.pop_back();                 // remove back element from container and give up ownership
456auto_type ptr2 = deq.release( deq.begin() + 2 ); // use an iterator to determine the element to release
457ptr            = deq.pop_front();                // supported for 'ptr_list' and 'ptr_deque'
458
459deq.push_back( ptr.release() );                  // give ownership back to the container
460</pre>
461</div>
462<div class="section" id="transferring-ownership-of-pointers-between-different-pointer-containers">
463<span id="example-7"></span><h1><a class="toc-backref" href="#id8">7. Transferring ownership of pointers between different pointer containers</a></h1>
464<pre class="literal-block">
465ptr_list&lt;X&gt; list; ptr_vector&lt;X&gt; vec;
466...
467//
468// note: no cloning happens in these examples
469//
470list.transfer( list.begin(), vec.begin(), vec );           // make the first element of 'vec' the first element of 'list'
471vec.transfer( vec.end(), list.begin(), list.end(), list ); // put all the lists element into the vector
472</pre>
473<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>
474</div>
475<div class="section" id="selected-test-files">
476<span id="example-8"></span><h1><a class="toc-backref" href="#id9">8. Selected test files</a></h1>
477<table class="docutils field-list" frame="void" rules="none">
478<col class="field-name" />
479<col class="field-body" />
480<tbody valign="top">
481<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>
482<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how to implement the Composite pattern.</td>
483</tr>
484<tr class="field"><th class="field-name" colspan="2"><a class="reference external" href="../test/simple_test.cpp">simple_test.cpp</a>:</th></tr>
485<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how the usage of pointer container compares with a
486container of smart pointers</td>
487</tr>
488<tr class="field"><th class="field-name" colspan="2"><a class="reference external" href="../test/view_example.cpp">view_example.cpp</a>:</th></tr>
489<tr class="field"><td>&nbsp;</td><td class="field-body">Shows how to use a pointer container as a view into other container</td>
490</tr>
491<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>
492</tr>
493<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>
494</tr>
495</tbody>
496</table>
497</div>
498<div class="section" id="a-large-example">
499<h1><a class="toc-backref" href="#id10">9. A large example</a></h1>
500<p>This example shows many of the most common
501features at work. The example provide lots of comments.
502The source code can also be found <a class="reference external" href="../test/tut1.cpp">here</a>.</p>
503<html>
504<head>
505<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
506<title> </title>
507<link rel="stylesheet" href="default.css" type="text/css">
508</head>
509
510<body>
511    <pre><span class=comment>//
512// Boost.Pointer Container
513//
514//  Copyright Thorsten Ottosen 2003-2005. Use, modification and
515//  distribution is subject to the Boost Software License, Version
516//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
517//  http://www.boost.org/LICENSE_1_0.txt)
518//
519// For more information, see http://www.boost.org/libs/ptr_container/
520//
521
522//
523// This example is intended to get you started.
524// Notice how the smart container
525//
526// 1. takes ownership of objects
527// 2. transfers ownership
528// 3. applies indirection to iterators
529// 4. clones objects from other smart containers
530//
531
532//
533// First we select which container to use.
534//</span>
535<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>
536
537<span class=comment>//
538// we need these later in the example
539//</span>
540<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>
541<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>string</span><span class=special>&gt;</span>
542<span class=preprocessor>#include</span> <span class=special>&lt;</span><span class=identifier>exception</span><span class=special>&gt;</span>
543
544
545<span class=comment>//
546// Then we define a small polymorphic class
547// hierarchy.
548//</span>
549
550<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>
551<span class=special>{</span>
552    <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>
553    <span class=identifier>std</span><span class=special>::</span><span class=identifier>string</span> <span class=identifier>name_</span><span class=special>;</span>
554
555<span class=keyword>protected</span><span class=special>:</span>
556    <span class=comment>//
557    // Animals cannot be copied...
558    //</span>
559    <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>
560    <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>
561
562<span class=keyword>private</span><span class=special>:</span>
563    <span class=comment>//
564    // ...but due to advances in genetics, we can clone them!
565    //</span>
566
567    <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>
568
569<span class=keyword>public</span><span class=special>:</span>
570    <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>
571    <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>
572
573    <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>
574    <span class=special>{</span>
575        <span class=keyword>return</span> <span class=identifier>do_speak</span><span class=special>();</span>
576    <span class=special>}</span>
577
578    <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>
579    <span class=special>{</span>
580        <span class=keyword>return</span> <span class=identifier>name_</span><span class=special>;</span>
581    <span class=special>}</span>
582
583    <span class=identifier>animal</span><span class=special>*</span> <span class=identifier>clone</span><span class=special>()</span> <span class=keyword>const</span>
584    <span class=special>{</span>
585        <span class=keyword>return</span> <span class=identifier>do_clone</span><span class=special>();</span>
586    <span class=special>}</span>
587<span class=special>};</span>
588
589<span class=comment>//
590// An animal is still not Clonable. We need this last hook.
591//
592// Notice that we pass the animal by const reference
593// and return by pointer.
594//</span>
595
596<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>
597<span class=special>{</span>
598    <span class=keyword>return</span> <span class=identifier>a</span><span class=special>.</span><span class=identifier>clone</span><span class=special>();</span>
599<span class=special>}</span>
600
601<span class=comment>//
602// We do not need to define 'delete_clone()' since
603// since the default is to call the default 'operator delete()'.
604//</span>
605
606<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>
607<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>
608
609<span class=keyword>class</span> <span class=identifier>cow</span> <span class=special>:</span> <span class=keyword>public</span> <span class=identifier>animal</span>
610<span class=special>{</span>
611    <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>
612    <span class=special>{</span>
613        <span class=keyword>return</span> <span class=identifier>muuuh</span><span class=special>;</span>
614    <span class=special>}</span>
615
616    <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>
617    <span class=special>{</span>
618        <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>
619    <span class=special>}</span>
620
621<span class=keyword>public</span><span class=special>:</span>
622    <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>
623<span class=special>};</span>
624
625<span class=keyword>class</span> <span class=identifier>pig</span> <span class=special>:</span> <span class=keyword>public</span> <span class=identifier>animal</span>
626<span class=special>{</span>
627    <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>
628    <span class=special>{</span>
629        <span class=keyword>return</span> <span class=identifier>oiink</span><span class=special>;</span>
630    <span class=special>}</span>
631
632    <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>
633    <span class=special>{</span>
634        <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>
635    <span class=special>}</span>
636
637<span class=keyword>public</span><span class=special>:</span>
638    <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>
639<span class=special>};</span>
640
641<span class=comment>//
642// Then we, of course, need a place to put all
643// those animals.
644//</span>
645
646<span class=keyword>class</span> <span class=identifier>farm</span>
647<span class=special>{</span>
648    <span class=comment>//
649    // This is where the smart containers are handy
650    //</span>
651    <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>
652    <span class=identifier>barn_type</span>                        <span class=identifier>barn</span><span class=special>;</span>
653
654    <span class=comment>//
655    // A convenience typedef for the compiler-appropriate
656    // smart pointer used to manage barns
657    //</span>
658    <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>
659
660    <span class=comment>//
661    // An error type
662    //</span>
663    <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>
664
665<span class=keyword>public</span><span class=special>:</span>
666    <span class=comment>//
667    // We would like to make it possible to
668    // iterate over the animals in the farm
669    //</span>
670    <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>
671
672    <span class=comment>//
673    // We also need to count the farm's size...
674    //</span>
675    <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>
676
677    <span class=comment>//
678    // And we also want to transfer an animal
679    // safely around. The easiest way to think
680    // about '::auto_type' is to imagine a simplified
681    // 'std::auto_ptr&lt;T&gt;' ... this means you can expect
682    //
683    //   T* operator-&gt;()
684    //   T* release()
685    //   deleting destructor
686    //
687    // but not more.
688    //</span>
689    <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>
690
691    <span class=comment>//
692    // Create an empty farm.
693    //</span>
694    <span class=identifier>farm</span><span class=special>()</span>                                                 <span class=special>{</span> <span class=special>}</span>
695
696    <span class=comment>//
697    // We need a constructor that can make a new
698    // farm by cloning a range of animals.
699    //</span>
700    <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>
701     <span class=special>:</span>
702        <span class=comment>//
703        // Objects are always cloned before insertion
704        // unless we explicitly add a pointer or
705        // use 'release()'. Therefore we actually
706        // clone all animals in the range
707        //</span>
708        <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>
709
710    <span class=comment>//
711    // ... so we need some other function too
712    //</span>
713
714    <span class=identifier>animal_iterator</span> <span class=identifier>begin</span><span class=special>()</span>
715    <span class=special>{</span>
716        <span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>begin</span><span class=special>();</span>
717    <span class=special>}</span>
718
719    <span class=identifier>animal_iterator</span> <span class=identifier>end</span><span class=special>()</span>
720    <span class=special>{</span>
721        <span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>end</span><span class=special>();</span>
722    <span class=special>}</span>
723
724    <span class=comment>//
725    // Here it is quite ok to have an 'animal*' argument.
726    // The smart container will handle all ownership
727    // issues.
728    //</span>
729    <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>
730    <span class=special>{</span>
731        <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>
732    <span class=special>}</span>
733
734    <span class=comment>//
735    // The farm can also be in economical trouble and
736    // therefore be in the need to sell animals.
737    //</span>
738    <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>
739    <span class=special>{</span>
740        <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>
741            <span class=keyword>throw</span> <span class=identifier>farm_trouble</span><span class=special>();</span>
742
743        <span class=comment>//
744        // Here we remove the animal from the barn,
745        // but the animal is not deleted yet...it's
746        // up to the buyer to decide what
747        // to do with it.
748        //</span>
749        <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>
750    <span class=special>}</span>
751
752    <span class=comment>//
753    // How big a farm do we have?
754    //</span>
755    <span class=identifier>size_type</span> <span class=identifier>size</span><span class=special>()</span> <span class=keyword>const</span>
756    <span class=special>{</span>
757        <span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>size</span><span class=special>();</span>
758    <span class=special>}</span>
759
760    <span class=comment>//
761    // If things are bad, we might choose to sell all animals :-(
762      //</span>
763
764    <span class=identifier>raii_ptr</span> <span class=identifier>sell_farm</span><span class=special>()</span>
765    <span class=special>{</span>
766        <span class=keyword>return</span> <span class=identifier>barn</span><span class=special>.</span><span class=identifier>release</span><span class=special>();</span>
767    <span class=special>}</span>
768
769    <span class=comment>//
770    // However, if things are good, we might buy somebody
771    // else's farm :-)
772    //</span>
773
774    <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>
775    <span class=special>{</span>
776        <span class=comment>//
777        // This line inserts all the animals from 'other'
778        // and is guaranteed either to succeed or to have no
779        // effect
780        //</span>
781        <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>
782                         <span class=special>*</span><span class=identifier>other</span> <span class=special>);</span>     <span class=comment>// we want to transfer all animals,
783                                       // so we use the whole container as argument
784        //
785        // You might think you would have to do
786        //
787        // other.release();
788        //
789        // but '*other' is empty and can go out of scope as it wants
790        //</span>
791        <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>
792    <span class=special>}</span>
793
794<span class=special>};</span> <span class=comment>// class 'farm'.</span>
795
796<span class=keyword>int</span> <span class=identifier>main</span><span class=special>()</span>
797<span class=special>{</span>
798    <span class=comment>//
799    // First we make a farm
800    //</span>
801    <span class=identifier>farm</span> <span class=identifier>animal_farm</span><span class=special>;</span>
802    <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>
803
804    <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>
805    <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>
806    <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>
807    <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>
808    <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>
809    <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>
810    <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>
811
812    <span class=comment>//
813    // Then we make another farm...it will actually contain
814    // a clone of the other farm.
815    //</span>
816    <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>
817    <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>
818
819    <span class=comment>//
820    // Is it really clones in the new farm?
821    //</span>
822    <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>
823
824    <span class=comment>//
825    // Then we search for an animal, Mary (the Crown Princess of Denmark),
826    // because we would like to buy her ...
827    //</span>
828    <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>
829    <span class=identifier>iterator</span> <span class=identifier>to_sell</span><span class=special>;</span>
830    <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>
831                  <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>
832         <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>
833    <span class=special>{</span>
834        <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>
835        <span class=special>{</span>
836            <span class=identifier>to_sell</span> <span class=special>=</span> <span class=identifier>i</span><span class=special>;</span>
837            <span class=keyword>break</span><span class=special>;</span>
838        <span class=special>}</span>
839    <span class=special>}</span>
840
841    <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>
842
843
844    <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>
845        <span class=comment>//
846        // Great, Mary is a cow, and she may live longer
847        //</span>
848        <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>
849    <span class=keyword>else</span>
850        <span class=comment>//
851        // Then the animal would be destroyed (!)
852        // when we go out of scope.
853        //</span>
854        <span class=special>;</span>
855
856    <span class=comment>//
857    // Now we can observe some changes to the two farms...
858    //</span>
859    <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>
860    <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>
861
862    <span class=comment>//
863    // The new farm has however underestimated how much
864    // it cost to feed Mary and its owner is forced to sell the farm...
865    //</span>
866    <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>
867
868    <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>
869    <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>
870<span class=special>}</span>
871</pre>
872</body>
873
874</html>
875<!-- 10. Changing the Clone Allocator
876++++++++++++++++++++++++++++++++
877
878This example shows how we can change
879the Clone Allocator to use the pointer containers
880as view into other containers:
881
882.. raw:: html
883        :file: tut2.html -->
884<hr><p><strong>Navigate:</strong></p>
885<ul class="simple">
886<li><a class="reference external" href="ptr_container.html">home</a></li>
887<li><a class="reference external" href="reference.html">reference</a></li>
888</ul>
889<hr><table class="docutils field-list" frame="void" rules="none">
890<col class="field-name" />
891<col class="field-body" />
892<tbody valign="top">
893<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>
894</tr>
895</tbody>
896</table>
897</div>
898</div>
899</body>
900</html>
901