Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/ptr_container/doc/examples.html @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 9.7 KB
Line 
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.3.9: http://docutils.sourceforge.net/" />
7<title>Boost Pointer Container Library</title>
8<link rel="stylesheet" href="default.css" type="text/css" />
9</head>
10<body>
11<div class="document" id="boost-pointer-container-library">
12<h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
13<h2 class="subtitle" id="examples">Examples</h2>
14<p>Some examples are given here and in the accompanying test files:</p>
15<div class="contents topic" id="contents">
16<ul class="simple">
17<li><a class="reference" href="#null-pointers-cannot-be-stored-in-the-containers" id="id1" name="id1">1. Null pointers cannot be stored in the containers</a></li>
18<li><a class="reference" href="#iterators-and-other-operations-return-indirected-values" id="id2" name="id2">2. Iterators and other operations return indirected values</a></li>
19<li><a class="reference" href="#copy-semantics-of-pointer-containers" id="id3" name="id3">3. Copy-semantics of pointer containers</a></li>
20<li><a class="reference" href="#making-a-non-copyable-type-clonable" id="id4" name="id4">4. Making a non-copyable type Clonable</a></li>
21<li><a class="reference" href="#objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container" id="id5" name="id5">5. Objects are cloned before insertion, inserted pointers are owned by the container</a></li>
22<li><a class="reference" href="#transferring-ownership-of-a-single-element" id="id6" name="id6">6. Transferring ownership of a single element</a></li>
23<li><a class="reference" href="#transferring-ownership-of-pointers-between-different-pointer-containers" id="id7" name="id7">7. Transferring ownership of pointers between different pointer containers</a></li>
24<li><a class="reference" href="#selected-test-files" id="id8" name="id8">8. Selected test files</a></li>
25</ul>
26</div>
27<span id="example-1"></span><div class="section" id="null-pointers-cannot-be-stored-in-the-containers">
28<h1><a class="toc-backref" href="#id1" name="null-pointers-cannot-be-stored-in-the-containers">1. Null pointers cannot be stored in the containers</a></h1>
29<pre class="literal-block">
30my_container.push_back( 0 );            // throws bad_ptr
31my_container.replace( an_iterator, 0 ); // throws bad_ptr
32my_container.insert( an_iterator, 0 );  // throws bad_ptr                                                                 
33</pre>
34</div>
35<span id="example-2"></span><div class="section" id="iterators-and-other-operations-return-indirected-values">
36<h1><a class="toc-backref" href="#id2" name="iterators-and-other-operations-return-indirected-values">2. Iterators and other operations return indirected values</a></h1>
37<pre class="literal-block">
38ptr_vector&lt;X&gt; pvec;
39std::vector&lt;X*&gt; vec;
40*vec.begin()  = new X;   // fine, memory leak
41*pvec.begin() = new X;   // compile time error
42( *vec.begin() )-&gt;foo(); // call X::foo(), a bit clumsy
43pvec.begin()-&gt;foo();     // no indirection needed
44*vec.front()  = X();     // overwrite first element
45pvec.front()  = X();     // no indirection needed
46</pre>
47</div>
48<span id="example-3"></span><div class="section" id="copy-semantics-of-pointer-containers">
49<h1><a class="toc-backref" href="#id3" name="copy-semantics-of-pointer-containers">3. Copy-semantics of pointer containers</a></h1>
50<pre class="literal-block">
51ptr_vector&lt;T&gt; vec1;
52...
53ptr_vector&lt;T&gt; vec2( vec1.clone() ); // deep copy objects of 'vec1' and use them to construct 'vec2', could be very expensive
54vec2 = vec1.release();              // give up ownership of pointers in 'vec1' and pass the ownership to 'vec2', rather cheap
55vec2.release();                     // give up ownership; the objects will be deallocated if not assigned to another container
56vec1 = vec2;                        // compile time error: 'operator=()' not defined
57ptr_vector&lt;T&gt; vec3( vec1 );         // compile time error: copy-constructor not defined
58</pre>
59</div>
60<span id="example-4"></span><div class="section" id="making-a-non-copyable-type-clonable">
61<h1><a class="toc-backref" href="#id4" name="making-a-non-copyable-type-clonable">4. Making a non-copyable type Clonable</a></h1>
62<pre class="literal-block">
63 // a class that has no normal copy semantics
64class X : boost::noncopyable { public: X* clone() const; ... };
65                                                                   
66// this will be found by the library by argument dependent lookup                                                                   
67X* new_clone( const X&amp; x )
68{ return x.clone(); }
69                                                                   
70// we can now use the interface that requires clonability
71ptr_vector&lt;X&gt; vec1, vec2;
72...
73vec2 = vec1.clone();                                 // 'clone()' requires cloning &lt;g&gt; 
74vec2.insert( vec2.end(), vec1.begin(), vec1.end() ); // inserting always means inserting clones
75</pre>
76</div>
77<span id="example-5"></span><div class="section" id="objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container">
78<h1><a class="toc-backref" href="#id5" name="objects-are-cloned-before-insertion-inserted-pointers-are-owned-by-the-container">5. Objects are cloned before insertion, inserted pointers are owned by the container</a></h1>
79<pre class="literal-block">
80class X { ... };                     // assume 'X' is Clonable
81X x;                                 // and 'X' can be stack-allocated
82ptr_list&lt;X&gt; list;
83list.push_back( x );                 // clone 'x' and then insert the resulting pointer
84list.push_back( new_clone( x );      // do it manually
85list.push_back( new X );             // always give the pointer directly to the container to avoid leaks
86list.push_back( &amp;x );                // don't do this!!!
87</pre>
88</div>
89<span id="example-6"></span><div class="section" id="transferring-ownership-of-a-single-element">
90<h1><a class="toc-backref" href="#id6" name="transferring-ownership-of-a-single-element">6. Transferring ownership of a single element</a></h1>
91<pre class="literal-block">
92ptr_deque&lt;T&gt;                    deq;
93typedef ptr_deque&lt;T&gt;::auto_type auto_type;
94
95// ... fill the container somehow
96
97auto_type ptr  = deq.release_back();             // remove back element from container and give up ownership
98auto_type ptr2 = deq.release( deq.begin() + 2 ); // use an iterator to determine the element to release
99ptr            = deq.release_front();            // supported for 'ptr_list' and 'ptr_deque'
100</pre>
101</div>
102<span id="example-7"></span><div class="section" id="transferring-ownership-of-pointers-between-different-pointer-containers">
103<h1><a class="toc-backref" href="#id7" name="transferring-ownership-of-pointers-between-different-pointer-containers">7. Transferring ownership of pointers between different pointer containers</a></h1>
104<pre class="literal-block">
105ptr_list&lt;X&gt; list; ptr_vector&lt;X&gt; vec;
106...
107//
108// note: no cloning happens in these examples                               
109//
110list.transfer( list.begin(), vec.begin(), vec );           // make the first element of 'vec' the first element of 'list'
111vec.transfer( vec.end(), list.begin(), list.end(), list ); // put all the lists element into the vector                                 
112</pre>
113</div>
114<span id="example-8"></span><div class="section" id="selected-test-files">
115<h1><a class="toc-backref" href="#id8" name="selected-test-files">8. Selected test files</a></h1>
116<table class="docutils field-list" frame="void" rules="none">
117<col class="field-name" />
118<col class="field-body" />
119<tbody valign="top">
120<tr class="field"><th class="field-name" colspan="2"><a class="reference" href="../test/incomplete_type_test.cpp">incomplete_type_test.cpp</a>:</th></tr>
121<tr><td>&nbsp;</td><td class="field-body">Shows how to implement the Composite pattern.</td>
122</tr>
123<tr class="field"><th class="field-name" colspan="2"><a class="reference" href="../test/simple_test.cpp">simple_test.cpp</a>:</th></tr>
124<tr><td>&nbsp;</td><td class="field-body">Shows how the usage of pointer container compares with a
125container of pointer pointers</td>
126</tr>
127<tr class="field"><th class="field-name" colspan="2"><a class="reference" href="../test/view_example.cpp">view_example.cpp</a>:</th></tr>
128<tr><td>&nbsp;</td><td class="field-body">Shows how to use a pointer container as a view into other container</td>
129</tr>
130<tr class="field"><th class="field-name"><a class="reference" href="../test/tree_test.cpp">tree_test.cpp</a>:</th><td class="field-body">Shows how to make a tree-structure</td>
131</tr>
132<tr class="field"><th class="field-name"><a class="reference" href="../test/ptr_array.cpp">array_test.cpp</a>:</th><td class="field-body">Shows how to make an n-ary tree</td>
133</tr>
134</tbody>
135</table>
136<!-- 9. A large example
137++++++++++++++++++
138
139This examples shows many of the most common
140features at work.
141
142.. raw:: html
143        :file: tut1.html
144
14510. Changing the Clone Allocator
146++++++++++++++++++++++++++++++++
147
148This example shows how we can change
149the Clone Allocator to use the pointer containers
150as view into other containers:
151
152.. raw:: html
153        :file: tut2.html -->
154<p><strong>Navigate:</strong></p>
155<ul class="simple">
156<li><a class="reference" href="ptr_container.html">home</a></li>
157<li><a class="reference" href="reference.html">reference</a></li>
158</ul>
159<table class="docutils field-list" frame="void" rules="none">
160<col class="field-name" />
161<col class="field-body" />
162<tbody valign="top">
163<tr class="field"><th class="field-name">copyright:</th><td class="field-body">Thorsten Ottosen 2004-2005.</td>
164</tr>
165</tbody>
166</table>
167</div>
168</div>
169</body>
170</html>
Note: See TracBrowser for help on using the repository browser.