• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>In-Place Factories</title>
5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Boost.Optional">
8<link rel="up" href="../../optional/tutorial.html" title="Tutorial">
9<link rel="prev" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
10<link rel="next" href="gotchas.html" title="Gotchas">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gotchas.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_optional.tutorial.in_place_factories"></a><a class="link" href="in_place_factories.html" title="In-Place Factories">In-Place
28      Factories</a>
29</h3></div></div></div>
30<p>
31        One of the typical problems with wrappers and containers is that their interfaces
32        usually provide an operation to initialize or assign the contained object
33        as a copy of some other object. This not only requires the underlying type
34        to be <a href="../../../../../utility/CopyConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">CopyConstructible</span></code></a>, but also requires
35        the existence of a fully constructed object, often temporary, just to follow
36        the copy from:
37      </p>
38<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">X</span>
39<span class="special">{</span>
40    <span class="identifier">X</span> <span class="special">(</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">)</span> <span class="special">;</span>
41<span class="special">}</span> <span class="special">;</span>
42
43<span class="keyword">class</span> <span class="identifier">W</span>
44<span class="special">{</span>
45    <span class="identifier">X</span> <span class="identifier">wrapped_</span> <span class="special">;</span>
46
47    <span class="keyword">public</span><span class="special">:</span>
48
49    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">)</span> <span class="special">:</span> <span class="identifier">wrapped_</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
50<span class="special">}</span> <span class="special">;</span>
51
52<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
53<span class="special">{</span>
54    <span class="comment">// Temporary object created.</span>
55    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">X</span><span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="string">"hello"</span><span class="special">)</span> <span class="special">)</span> <span class="special">;</span>
56<span class="special">}</span>
57</pre>
58<p>
59        A solution to this problem is to support direct construction of the contained
60        object right in the container's storage. In this scheme, the user only needs
61        to supply the arguments to the constructor to use in the wrapped object construction.
62      </p>
63<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">W</span>
64<span class="special">{</span>
65    <span class="identifier">X</span> <span class="identifier">wrapped_</span> <span class="special">;</span>
66
67    <span class="keyword">public</span><span class="special">:</span>
68
69    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">)</span> <span class="special">:</span> <span class="identifier">wrapped_</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
70    <span class="identifier">W</span> <span class="special">(</span> <span class="keyword">int</span> <span class="identifier">a0</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">a1</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">wrapped_</span><span class="special">(</span><span class="identifier">a0</span><span class="special">,</span><span class="identifier">a1</span><span class="special">)</span> <span class="special">{}</span>
71<span class="special">}</span> <span class="special">;</span>
72
73<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
74<span class="special">{</span>
75    <span class="comment">// Wrapped object constructed in-place</span>
76    <span class="comment">// No temporary created.</span>
77    <span class="identifier">W</span> <span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="string">"hello"</span><span class="special">)</span> <span class="special">;</span>
78<span class="special">}</span>
79</pre>
80<p>
81        A limitation of this method is that it doesn't scale well to wrapped objects
82        with multiple constructors nor to generic code were the constructor overloads
83        are unknown.
84      </p>
85<p>
86        The solution presented in this library is the family of <span class="bold"><strong>InPlaceFactories</strong></span>
87        and <span class="bold"><strong>TypedInPlaceFactories</strong></span>. These factories
88        are a family of classes which encapsulate an increasing number of arbitrary
89        constructor parameters and supply a method to construct an object of a given
90        type using those parameters at an address specified by the user via placement
91        new.
92      </p>
93<p>
94        For example, one member of this family looks like:
95      </p>
96<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">A0</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">A1</span><span class="special">&gt;</span>
97<span class="keyword">class</span> <span class="identifier">TypedInPlaceFactory2</span>
98<span class="special">{</span>
99    <span class="identifier">A0</span> <span class="identifier">m_a0</span> <span class="special">;</span> <span class="identifier">A1</span> <span class="identifier">m_a1</span> <span class="special">;</span>
100
101    <span class="keyword">public</span><span class="special">:</span>
102
103    <span class="identifier">TypedInPlaceFactory2</span><span class="special">(</span> <span class="identifier">A0</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a0</span><span class="special">,</span> <span class="identifier">A1</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a1</span> <span class="special">)</span> <span class="special">:</span> <span class="identifier">m_a0</span><span class="special">(</span><span class="identifier">a0</span><span class="special">),</span> <span class="identifier">m_a1</span><span class="special">(</span><span class="identifier">a1</span><span class="special">)</span> <span class="special">{}</span>
104
105    <span class="keyword">void</span> <span class="identifier">construct</span> <span class="special">(</span> <span class="keyword">void</span><span class="special">*</span> <span class="identifier">p</span> <span class="special">)</span> <span class="special">{</span> <span class="keyword">new</span> <span class="special">(</span><span class="identifier">p</span><span class="special">)</span> <span class="identifier">T</span><span class="special">(</span><span class="identifier">m_a0</span><span class="special">,</span><span class="identifier">m_a1</span><span class="special">)</span> <span class="special">;</span> <span class="special">}</span>
106 <span class="special">}</span> <span class="special">;</span>
107</pre>
108<p>
109        A wrapper class aware of this can use it as:
110      </p>
111<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">W</span>
112<span class="special">{</span>
113    <span class="identifier">X</span> <span class="identifier">wrapped_</span> <span class="special">;</span>
114
115    <span class="keyword">public</span><span class="special">:</span>
116
117    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">)</span> <span class="special">:</span> <span class="identifier">wrapped_</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
118    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">TypedInPlaceFactory2</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">fac</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">fac</span><span class="special">.</span><span class="identifier">construct</span><span class="special">(&amp;</span><span class="identifier">wrapped_</span><span class="special">)</span> <span class="special">;</span> <span class="special">}</span>
119<span class="special">}</span> <span class="special">;</span>
120
121<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
122<span class="special">{</span>
123    <span class="comment">// Wrapped object constructed in-place via a TypedInPlaceFactory.</span>
124    <span class="comment">// No temporary created.</span>
125    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">TypedInPlaceFactory2</span><span class="special">&lt;</span><span class="identifier">X</span><span class="special">,</span><span class="keyword">int</span><span class="special">,</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="number">123</span><span class="special">,</span><span class="string">"hello"</span><span class="special">))</span> <span class="special">;</span>
126<span class="special">}</span>
127</pre>
128<p>
129        The factories are divided in two groups:
130      </p>
131<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
132<li class="listitem">
133            <span class="underline">TypedInPlaceFactories</span>: those which
134            take the target type as a primary template parameter.
135          </li>
136<li class="listitem">
137            <span class="underline">InPlaceFactories</span>: those with a
138            template <code class="computeroutput"><span class="identifier">construct</span><span class="special">(</span><span class="keyword">void</span><span class="special">*)</span></code>
139            member function taking the target type.
140          </li>
141</ul></div>
142<p>
143        Within each group, all the family members differ only in the number of parameters
144        allowed.
145      </p>
146<p>
147        This library provides an overloaded set of helper template functions to construct
148        these factories without requiring unnecessary template parameters:
149      </p>
150<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">A0</span><span class="special">,...,</span><span class="keyword">class</span> <span class="identifier">AN</span><span class="special">&gt;</span>
151<span class="identifier">InPlaceFactoryN</span> <span class="special">&lt;</span><span class="identifier">A0</span><span class="special">,...,</span><span class="identifier">AN</span><span class="special">&gt;</span> <span class="identifier">in_place</span> <span class="special">(</span> <span class="identifier">A0</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a0</span><span class="special">,</span> <span class="special">...,</span> <span class="identifier">AN</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">aN</span><span class="special">)</span> <span class="special">;</span>
152
153<span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span><span class="keyword">class</span> <span class="identifier">A0</span><span class="special">,...,</span><span class="keyword">class</span> <span class="identifier">AN</span><span class="special">&gt;</span>
154<span class="identifier">TypedInPlaceFactoryN</span> <span class="special">&lt;</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">A0</span><span class="special">,...,</span><span class="identifier">AN</span><span class="special">&gt;</span> <span class="identifier">in_place</span> <span class="special">(</span> <span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a0</span><span class="special">,</span> <span class="identifier">A0</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">a0</span><span class="special">,</span> <span class="special">...,</span> <span class="identifier">AN</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">aN</span><span class="special">)</span> <span class="special">;</span>
155</pre>
156<p>
157        In-place factories can be used generically by the wrapper and user as follows:
158      </p>
159<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">W</span>
160<span class="special">{</span>
161    <span class="identifier">X</span> <span class="identifier">wrapped_</span> <span class="special">;</span>
162
163    <span class="keyword">public</span><span class="special">:</span>
164
165    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">X</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">x</span> <span class="special">)</span> <span class="special">:</span> <span class="identifier">wrapped_</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">{}</span>
166
167    <span class="keyword">template</span><span class="special">&lt;</span> <span class="keyword">class</span> <span class="identifier">InPlaceFactory</span> <span class="special">&gt;</span>
168    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">InPlaceFactory</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">fac</span> <span class="special">)</span> <span class="special">{</span> <span class="identifier">fac</span><span class="special">.</span><span class="keyword">template</span> <span class="special">&lt;</span><span class="identifier">X</span><span class="special">&gt;</span><span class="identifier">construct</span><span class="special">(&amp;</span><span class="identifier">wrapped_</span><span class="special">)</span> <span class="special">;</span> <span class="special">}</span>
169
170<span class="special">}</span> <span class="special">;</span>
171
172<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span>
173<span class="special">{</span>
174    <span class="comment">// Wrapped object constructed in-place via a InPlaceFactory.</span>
175    <span class="comment">// No temporary created.</span>
176    <span class="identifier">W</span> <span class="special">(</span> <span class="identifier">in_place</span><span class="special">(</span><span class="number">123</span><span class="special">,</span><span class="string">"hello"</span><span class="special">)</span> <span class="special">)</span> <span class="special">;</span>
177<span class="special">}</span>
178</pre>
179<p>
180        The factories are implemented in the headers: <a href="../../../../../../boost/utility/in_place_factory.hpp" target="_top">in_place_factory.hpp</a>
181        and <a href="../../../../../../boost/utility/typed_in_place_factory.hpp" target="_top">typed_in_place_factory.hpp</a>
182      </p>
183</div>
184<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
185<td align="left"></td>
186<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2018 Andrzej Krzemieński<p>
187        Distributed under the Boost Software License, Version 1.0. (See accompanying
188        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
189      </p>
190</div></td>
191</tr></table>
192<hr>
193<div class="spirit-nav">
194<a accesskey="p" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="gotchas.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
195</div>
196</body>
197</html>
198