in_place_factories.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!DOCTYPE HTML PUBLIC "-//SoftQuad Software//DTD HoTMetaL PRO 5.0::19981217::extensions to HTML 4.0//EN" "hmpro5.dtd">
  2. <HTML>
  3. <HEAD>
  4. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
  5. <TITLE>In_place_factory Documentation</TITLE>
  6. </HEAD>
  7. <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
  8. <H2 align="left"><IMG SRC="../../boost.png" WIDTH="276" HEIGHT="86"></H2>
  9. <blockquote>
  10. <blockquote>
  11. <blockquote>
  12. <blockquote>
  13. <blockquote>
  14. <blockquote>
  15. <H2 align="left">Header &lt;<A
  16. HREF="../../boost/utility/in_place_factory.hpp">boost/utility/in_place_factory.hpp</A>&gt; </H2>
  17. <H2 align="left">Header &lt;<A
  18. HREF="../../boost/utility/typed_in_place_factory.hpp">boost/utility/typed_in_place_factory.hpp</A>&gt; </H2>
  19. </blockquote>
  20. </blockquote>
  21. </blockquote>
  22. </blockquote>
  23. </blockquote>
  24. </blockquote>
  25. <p>&nbsp;</p>
  26. <H2>Contents</H2>
  27. <DL CLASS="page-index">
  28. <DT><A HREF="#mot">Motivation</A></DT>
  29. <DT><A HREF="#framework">Framework</A></DT>
  30. <DT><A HREF="#specification">Specification</A></DT>
  31. <DT><A HREF="#container-usage">Container-side Usage</A></DT>
  32. <DT><A HREF="#user-usage">User-side Usage</A></DT>
  33. </DL>
  34. <HR>
  35. <H2><A NAME="mot"></A>Motivation</H2>
  36. <p>Suppose we have a class</p>
  37. <pre>struct X
  38. {
  39. X ( int, std::string ) ;
  40. } ;</pre>
  41. <p>And a container for it which supports an empty state (that is, which can contain zero objects):</p>
  42. <pre>struct C
  43. {
  44. C() : contained_(0) {}
  45. ~C() { delete contained_ ; }
  46. X* contained_ ;
  47. } ;</pre>
  48. <p>A container designed to support an empty state typically doesn't require the contained type to be DefaultConstructible,
  49. but it typically requires it to be CopyConstructible as a mechanism to
  50. initialize the object to store:</p>
  51. <pre>struct C
  52. {
  53. C() : contained_(0) {}
  54. C ( X const& v ) : contained_ ( new X(v) ) {}
  55. ~C() { delete contained_ ; }
  56. X* contained_ ;
  57. } ;</pre>
  58. <p>There is a subtle problem with this: since the mechanism used to initialize the stored object is copy construction,
  59. there must exist a previously constructed source object to copy from. This
  60. object is likely to be temporary and serve no purpose besides being the source</p>
  61. <pre>void foo()
  62. {
  63. // Temporary object created.
  64. C c( X(123,"hello") ) ;
  65. }
  66. </pre>
  67. <p>A solution to this problem is to support direct construction of the contained
  68. object right in the container's storage.<br>
  69. In this scheme, the user supplies the arguments for the X constructor
  70. directly to the container:</p>
  71. <pre>struct C
  72. {
  73. C() : contained_(0) {}
  74. C ( X const& v ) : contained_ ( new X(v) ) {}
  75. C ( int a0, std::string a1 ) : contained_ ( new X(a0,a1) ) {}
  76. ~C() { delete contained_ ; }
  77. X* contained_ ;
  78. } ;</pre>
  79. <pre>void foo()
  80. {
  81. // Wrapped object constructed in-place
  82. // No temporary created.
  83. C c(123,"hello") ;
  84. }
  85. </pre>
  86. <p>Clearly, this solution doesn't scale well since the container must duplicate all the constructor overloads from the contained type
  87. (at least all those which are to be supported directly in the container).</p>
  88. <H2><A NAME="framework"></A>Framework</H2>
  89. <p>
  90. This library proposes a framework to allow some containers to directly contruct contained objects in-place without requiring
  91. the entire set of constructor overloads from the contained type. It also allows the container to remove the CopyConstuctible
  92. requirement from the contained type since objects can be directly constructed in-place without need of a copy.<br>
  93. The only requirement on the container is that it must provide proper storage (that is, correctly aligned and sized).
  94. Naturally, the container will typically support uninitialized storage to avoid the in-place construction to override
  95. a fully-constructed object (as this would defeat the purpose of in-place construction)
  96. </p>
  97. <p>For this purpose, the framework provides two families of classes collectively called: InPlaceFactories and TypedInPlaceFactories.<br>
  98. Essentially, these classes hold a sequence of actual parameters and a method to contruct an object in place using these parameters.
  99. Each member of the family differs only in the number (and type) of the parameter list. The first family
  100. takes the type of the object to construct directly in method provided for that
  101. purpose, whereas the second family incorporates that type in the factory class
  102. itself..</p>
  103. <p>From the container POV, using the framework amounts to calling the factory's method to contruct the object in place.
  104. From the user POV, it amounts to creating the right factory object to hold the parameters and pass it to the container.<br>
  105. The following simplified example shows the basic idea. A complete example follows the formal specification of the framework:</p>
  106. <pre>struct C
  107. {
  108. template&lt;class InPlaceFactory&gt;
  109. C ( InPlaceFactory const& aFactory )
  110. :
  111. contained_ ( uninitialized_storage() )
  112. {
  113. aFactory.template apply&lt;X&gt;(contained_);
  114. }
  115. ~C()
  116. {
  117. contained_ -> X::~X();
  118. delete[] contained_ ;
  119. }
  120. char* uninitialized_storage() { return new char[sizeof(X)] ; }
  121. char* contained_ ;
  122. } ;
  123. void foo()
  124. {
  125. C c( in_place(123,"hello") ) ;
  126. }
  127. </pre>
  128. <HR>
  129. <H2><A NAME="specification">Specification</A></H2>
  130. <p>The following is the first member of the family of 'in_place_factory' classes, along with its corresponding helper template function.
  131. The rest of the family varies only in the number and type of template (and constructor) parameters.</p>
  132. <PRE>namespace boost {
  133. struct in_place_factory_base {} ;
  134. template&lt;class A0&gt;
  135. class in_place_factory : public in_place_factory_base
  136. {
  137. public:</PRE>
  138. <PRE> in_place_factory ( A0 const& a0 ) : m_a0(a0) {}
  139. template&lt; class T &gt;
  140. void apply ( void* address ) const
  141. {
  142. new (address) T(m_a0);
  143. }
  144. private:</PRE>
  145. <PRE> A0 const& m_a0 ;
  146. } ;
  147. template&lt;class A0&gt;
  148. in_place_factory&lt;A0&gt; in_place ( A0 const& a0 )
  149. {
  150. return in_place_factory&lt;A0&gt;(a0);
  151. }
  152. </PRE>
  153. <p>Similarly, the following is the first member of the family of 'typed_in_place_factory' classes, along with its corresponding
  154. helper template function. The rest of the family varies only in the number and type of template (and constructor) parameters.</p>
  155. <PRE>namespace boost {
  156. struct typed_in_place_factory_base {} ;
  157. template&lt;class T, class A0&gt;
  158. class typed_in_place_factory : public typed_in_place_factory_base
  159. {
  160. public:</PRE>
  161. <PRE> typed_in_place_factory ( A0 const& a0 ) : m_a0(a0) {}
  162. void apply ( void* address ) const
  163. {
  164. new (address) T(m_a0);
  165. }
  166. private:</PRE>
  167. <PRE> A0 const& m_a0 ;
  168. } ;
  169. template&lt;class T, class A0&gt;
  170. typed_in_place_factory&lt;A0&gt; in_place ( A0 const& a0 )
  171. {
  172. return typed_in_place_factory&lt;T,A0&gt;(a0);
  173. }</PRE>
  174. <PRE>}
  175. </PRE>
  176. <p>As you can see, the 'in_place_factory' and 'typed_in_place_factory' template classes varies only in the way they specify
  177. the target type: in the first family, the type is given as a template argument to the apply member function while in the
  178. second it is given directly as part of the factory class.<br>
  179. When the container holds a unique non-polymorphic type (such as the case of Boost.Optional), it knows the exact dynamic-type
  180. of the contained object and can pass it to the apply() method of a (non-typed) factory.
  181. In this case, end users can use an 'in_place_factory' instance which can be constructed without the type of the object to construct.<br>
  182. However, if the container holds heterogeneous or polymorphic objects (such as the case of Boost.Variant), the dynamic-type
  183. of the object to be constructed must be known by the factory itslef. In this case, end users must use a 'typed_in_place_factory'
  184. instead.</p>
  185. <HR>
  186. <h2><A NAME="container-usage">Container-side Usage</a></h2>
  187. <p>As shown in the introductory simplified example, the container class must
  188. contain methods that accept an instance of
  189. these factories and pass the object's storage to the factory's apply method.<br>
  190. However, the type of the factory class cannot be completly specified in the container class because that would
  191. defeat the whole purpose of the factories which is to allow the container to accept a variadic argument list
  192. for the constructor of its contained object.<br>
  193. The correct function overload must be based on the only distinctive and common
  194. characteristic of all the classes in each family, the base class.<br>
  195. Depending on the container class, you can use 'enable_if' to generate the right overload, or use the following
  196. dispatch technique (used in the Boost.Optional class):
  197. </p>
  198. <pre>struct C
  199. {
  200. C() : contained_(0) {}
  201. C ( X const& v ) : contained_ ( new X(v) ) {}
  202. template&lt;class Expr&gt
  203. C ( Expr const& expr )
  204. :
  205. contained_ ( uninitialized_storage() )
  206. {
  207. construct(expr,&expr)
  208. }
  209. ~C() { delete contained_ ; }
  210. template&lt;class InPlaceFactory&gt;
  211. void construct ( InPlaceFactory const& aFactory, boost::in_place_factory_base* )
  212. {
  213. aFactory.template apply&lt;X&gt;(contained_);
  214. }
  215. template&lt;class TypedInPlaceFactory&gt;
  216. void construct ( TypedInPlaceFactory const& aFactory, boost::typed_in_place_factory_base* )
  217. {
  218. aFactory.apply(contained_);
  219. }
  220. X* uninitialized_storage() { return static_cast&lt;X*&gt;(new char[sizeof(X)]) ; }
  221. X* contained_ ;
  222. } ;
  223. </pre>
  224. <hr>
  225. <h2><A NAME="user-usage">User-side Usage</a></h2>
  226. <p>End users pass to the container an instance of a factory object holding the actual parameters needed to construct the
  227. contained object directly within the container. For this, the helper template function 'in_place' is used.<br>
  228. The call 'in_place(a0,a1,a2,...,an)' constructs a (non-typed) 'in_place_factory' instance with the given argument list.<br>
  229. The call 'in_place&lt;T&gt;(a0,a1,a2,...,an)' constructs a 'typed_in_place_factory' instance with the given argument list for the
  230. type 'T'.</p>
  231. <pre>void foo()
  232. {
  233. C a( in_place(123,"hello") ) ; // in_place_factory passed
  234. C b( in_place&lt;X&gt;(456,"world") ) ; // typed_in_place_factory passed
  235. }
  236. </pre>
  237. <P>Revised September 17, 2004</P>
  238. <p>© Copyright Fernando Luis Cacciola Carballal, 2004</p>
  239. <p> Use, modification, and distribution are subject to the Boost Software
  240. License, Version 1.0. (See accompanying file <a href="../../LICENSE_1_0.txt">
  241. LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  242. www.boost.org/LICENSE_1_0.txt</a>)</p>
  243. <P>Developed by <A HREF="mailto:fernando_cacciola@hotmail.com">Fernando Cacciola</A>,
  244. the latest version of this file can be found at <A
  245. HREF="http://www.boost.org">www.boost.org</A>, and the boost
  246. <A HREF="http://www.boost.org/more/mailing_lists.htm#main">discussion lists</A></P>
  247. </BODY>
  248. </HTML>