factory.qbk 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. [/
  2. Copyright 2007,2008 Tobias Schwinger
  3. Copyright 2019 Glen Joseph Fernandes
  4. (glenjofe@gmail.com)
  5. Distributed under the Boost Software License, Version 1.0.
  6. (http://www.boost.org/LICENSE_1_0.txt)
  7. ]
  8. [library Boost.Functional/Factory
  9. [quickbook 1.5]
  10. [version 1.0]
  11. [authors [Schwinger, Tobias], [Fernandes, Glen]]
  12. [copyright 2007 2008 Tobias Schwinger]
  13. [copyright 2019 Glen Joseph Fernandes]
  14. [license Distributed under the Boost Software License, Version 1.0.]
  15. [purpose Function object templates for object creation.]
  16. [category higher-order]
  17. [category generic]]
  18. [def __boost__bind__
  19. [@http://www.boost.org/libs/bind/bind.html `boost::bind`]]
  20. [def __boost__forward_adapter__
  21. [@http://www.boost.org/libs/functional/forward/doc/index.html
  22. `boost::forward_adapter`]]
  23. [def __boost_function__
  24. [@http://www.boost.org/doc/html/function.html Boost.Function]]
  25. [def __smart_pointer__
  26. [@http://www.boost.org/libs/smart_ptr/index.html Smart Pointer]]
  27. [def __smart_pointers__
  28. [@http://www.boost.org/libs/smart_ptr/index.html Smart Pointers]]
  29. [def __boost__shared_ptr__
  30. [@http://www.boost.org/libs/smart_ptr/shared_ptr.htm `boost::shared_ptr`]]
  31. [def __allocator__ [@https://www.boost.org/sgi/stl/Allocators.html Allocator]]
  32. [def __std_allocators__
  33. [@https://www.boost.org/sgi/stl/Allocators.html Allocators]]
  34. [def __boost__factory__ `boost::factory`]
  35. [def __boost__value_factory__ `boost::value_factory`]
  36. [def __value_factory__ `value_factory`]
  37. [section Brief Description]
  38. The template __boost__factory__ lets you encapsulate a `new` expression as a
  39. function object, __boost__value_factory__ encapsulates a constructor invocation
  40. without `new`.
  41. ```
  42. __boost__factory__<T*>()(arg1,arg2,arg3)
  43. // same as new T(arg1,arg2,arg3)
  44. __boost__value_factory__<T>()(arg1,arg2,arg3)
  45. // same as T(arg1,arg2,arg3)
  46. ```
  47. Before C++11 the arguments to the function objects have to be LValues. A
  48. factory that also accepts RValues can be composed using the
  49. __boost__forward_adapter__ or __boost__bind__. In C++11 or higher the
  50. arguments can be LValues or RValues.
  51. [endsect]
  52. [section Background]
  53. In traditional Object Oriented Programming a Factory is an object implementing
  54. an interface of one or more methods that construct objects conforming to known
  55. interfaces.
  56. ```
  57. // assuming a_concrete_class and another_concrete_class are derived
  58. // from an_abstract_class
  59. struct a_factory {
  60. virtual an_abstract_class* create() const = 0;
  61. virtual ~a_factory() { }
  62. };
  63. struct a_concrete_factory
  64. : a_factory {
  65. an_abstract_class* create() const {
  66. return new a_concrete_class();
  67. }
  68. };
  69. struct another_concrete_factory
  70. : a_factory {
  71. an_abstract_class* create() const {
  72. return new another_concrete_class();
  73. }
  74. };
  75. // [...]
  76. int main()
  77. {
  78. boost::ptr_map<std::string, a_factory> factories;
  79. // [...]
  80. factories.insert("a_name",
  81. std::unique_ptr<a_factory>(new a_concrete_factory));
  82. factories.insert("another_name",
  83. std::unique_ptr<a_factory>(new another_concrete_factory));
  84. // [...]
  85. std::unique_ptr<an_abstract_class> x(factories.at(some_name).create());
  86. // [...]
  87. }
  88. ```
  89. This approach has several drawbacks. The most obvious one is that there is lots
  90. of boilerplate code. In other words there is too much code to express a rather
  91. simple intention. We could use templates to get rid of some of it but the
  92. approach remains inflexible:
  93. * We may want a factory that takes some arguments that are forwarded to the
  94. constructor,
  95. * we will probably want to use smart pointers,
  96. * we may want several member functions to create different kinds of objects,
  97. * we might not necessarily need a polymorphic base class for the objects,
  98. * as we will see, we do not need a factory base class at all,
  99. * we might want to just call the constructor - without `new` to create an
  100. object on the stack, and
  101. * finally we might want to use customized memory management.
  102. Experience has shown that using function objects and generic Boost components
  103. for their composition, Design Patterns that describe callback mechanisms
  104. (typically requiring a high percentage of boilerplate code with pure Object
  105. Oriented methodology) become implementable with just few code lines and without
  106. extra classes.
  107. Factories are callback mechanisms for constructors, so we provide two class
  108. templates, __boost__value_factory__ and __boost__factory__, that encapsulate
  109. object construction via direct application of the constructor and the `new`
  110. operator, respectively.
  111. We let the function objects forward their arguments to the construction
  112. expressions they encapsulate. Over this __boost__factory__ optionally allows
  113. the use of smart pointers and __std_allocators__.
  114. Compile-time polymorphism can be used where appropriate,
  115. ```
  116. template<class T>
  117. void do_something()
  118. {
  119. // [...]
  120. T x = T(a, b);
  121. // for conceptually similar objects x we neither need virtual
  122. // functions nor a common base class in this context.
  123. // [...]
  124. }
  125. ```
  126. Now, to allow inhomogeneous signatures for the constructors of the types passed
  127. in for `T` we can use __value_factory__ and __boost__bind__ to normalize
  128. between them.
  129. ```
  130. template<class ValueFactory>
  131. void do_something(ValueFactory make_obj = ValueFactory())
  132. {
  133. // [...]
  134. typename ValueFactory::result_type x = make_obj(a, b);
  135. // for conceptually similar objects x we neither need virtual
  136. // functions nor a common base class in this context.
  137. // [...]
  138. }
  139. int main()
  140. {
  141. // [...]
  142. do_something(boost::value_factory<X>());
  143. do_something(boost::bind(boost::value_factory<Y>(), _1, 5, _2));
  144. // construct X(a, b) and Y(a, 5, b), respectively.
  145. // [...]
  146. }
  147. ```
  148. Maybe we want our objects to outlive the function's scope, in this case we have
  149. to use dynamic allocation;
  150. ```
  151. template<class Factory>
  152. whatever do_something(Factory new_obj = Factory())
  153. {
  154. typename Factory::result_type ptr = new_obj(a, b);
  155. // again, no common base class or virtual functions needed,
  156. // we could enforce a polymorphic base by writing e.g.
  157. // boost::shared_ptr<base>
  158. // instead of
  159. // typename Factory::result_type
  160. // above.
  161. // Note that we are also free to have the type erasure happen
  162. // somewhere else (e.g. in the constructor of this function's
  163. // result type).
  164. // [...]
  165. }
  166. // [... call do_something like above but with boost::factory instead
  167. // of boost::value_factory]
  168. ```
  169. Although we might have created polymorphic objects in the previous example, we
  170. have used compile time polymorphism for the factory. If we want to erase the
  171. type of the factory and thus allow polymorphism at run time, we can use
  172. __boost_function__ to do so. The first example can be rewritten as follows.
  173. ```
  174. typedef boost::function<an_abstract_class*()> a_factory;
  175. // [...]
  176. int main()
  177. {
  178. std::map<std::string, a_factory> factories;
  179. // [...]
  180. factories["a_name"] = boost::factory<a_concrete_class*>();
  181. factories["another_name"] = boost::factory<another_concrete_class*>();
  182. // [...]
  183. }
  184. ```
  185. Of course we can just as easy create factories that take arguments and/or
  186. return __smart_pointers__.
  187. [endsect]
  188. [section:reference Reference]
  189. [section value_factory]
  190. [heading Description]
  191. Function object template that invokes the constructor of the type `T`.
  192. [heading Header]
  193. ```
  194. #include <boost/functional/value_factory.hpp>
  195. ```
  196. [heading Synopsis]
  197. ```
  198. namespace boost {
  199. template<class T>
  200. class value_factory;
  201. } // boost
  202. ```
  203. [variablelist Notation
  204. [[`T`][an arbitrary type with at least one public constructor]]
  205. [[`a0`...`aN`][argument values to a constructor of `T`]]
  206. [[`F`][the type `value_factory<F>`]]
  207. [[`f`][an instance object of `F`]]]
  208. [heading Expression Semantics]
  209. [table
  210. [[Expression][Semantics]]
  211. [[`F()`][creates an object of type `F`.]]
  212. [[`F(f)`][creates an object of type `F`.]]
  213. [[`f(a0`...`aN)`][returns `T(a0`...`aN)`.]]
  214. [[`F::result_type`][is the type `T`.]]]
  215. [heading Limits]
  216. Before C++11, the maximum number of arguments supported is 10. Since C++11 an
  217. arbitrary number of arguments is supported.
  218. [endsect]
  219. [section factory]
  220. [heading Description]
  221. Function object template that dynamically constructs a pointee object for the
  222. type of pointer given as template argument. Smart pointers may be used for the
  223. template argument, given that `pointer_traits<Pointer>::element_type` yields
  224. the pointee type.
  225. If an __allocator__ is given, it is used for memory allocation and the
  226. placement form of the `new` operator is used to construct the object. A
  227. function object that calls the destructor and deallocates the memory with a
  228. copy of the Allocator is used for the second constructor argument of `Pointer`
  229. (thus it must be a __smart_pointer__ that provides a suitable constructor,
  230. such as __boost__shared_ptr__).
  231. If a third template argument is `factory_passes_alloc_to_smart_pointer`, the
  232. allocator itself is used for the third constructor argument of `Pointer`
  233. (__boost__shared_ptr__ then uses the allocator to manage the memory of its
  234. separately allocated reference counter).
  235. [heading Header]
  236. ```
  237. #include <boost/functional/factory.hpp>
  238. ```
  239. [heading Synopsis]
  240. ```
  241. namespace boost {
  242. enum factory_alloc_propagation {
  243. factory_alloc_for_pointee_and_deleter,
  244. factory_passes_alloc_to_smart_pointer
  245. };
  246. template<class Pointer,
  247. class Allocator = void,
  248. factory_alloc_propagation Policy = factory_alloc_for_pointee_and_deleter>
  249. class factory;
  250. } // boost
  251. ```
  252. [variablelist Notation
  253. [[`T`][an arbitrary type with at least one public constructor]]
  254. [[`P`][pointer or smart pointer to `T`]]
  255. [[`a0`...`aN`][argument values to a constructor of `T`]]
  256. [[`F`][the type `factory<P>`]]
  257. [[`f`][an instance object of `F`]]]
  258. [heading Expression Semantics]
  259. [table
  260. [[Expression][Semantics]]
  261. [[`F()`][creates an object of type `F`.]]
  262. [[`F(f)`][creates an object of type `F`.]]
  263. [[`f(a0`...`aN)`]
  264. [dynamically creates an object of type `T` using `a0`...`aN` as arguments for
  265. the constructor invocation.]]
  266. [[`F::result_type`][is the type `P` with top-level cv-qualifiers removed.]]]
  267. [heading Limits]
  268. Before C++11, the maximum number of arguments supported is 10. Since C++11 an
  269. arbitrary number of arguments is supported.
  270. [endsect]
  271. [endsect]
  272. [section Changes]
  273. [heading Boost 1.72.0]
  274. Glen Fernandes rewrote the implementations of `factory` and `value_factory` to
  275. provide the following features:
  276. * Support r-value arguments when available
  277. * Support arbitrary number of arguments via variadic templates when available
  278. * Support allocators that are final
  279. * Support allocators that use fancy pointers
  280. * Support for disabled exceptions (`BOOST_NO_EXCEPTIONS`)
  281. * Improved compilation times
  282. The following features have been removed:
  283. * Increasing limits for C++03 compilers through
  284. `BOOST_FUNCTIONAL_VALUE_FACTORY_MAX_ARITY`
  285. * Using `boost::none_t` in place of `void` through
  286. `BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T`
  287. [heading Boost 1.58.0]
  288. In order to remove the dependency on Boost.Optional, the default parameter for
  289. allocators has been changed from `boost::none_t` to `void`. If you have code
  290. that has stopped working because it uses `boost::none_t`, a quick fix is to
  291. define `BOOST_FUNCTIONAL_FACTORY_SUPPORT_NONE_T`, which will restore support,
  292. but this will be removed in a future release. It should be be relatively easy
  293. to fix this properly.
  294. [endsect]
  295. [section Acknowledgements]
  296. Tobias Schwinger for creating this library.
  297. Eric Niebler requested a function to invoke a type's constructor (with the
  298. arguments supplied as a Tuple) as a Fusion feature. These Factory utilities are
  299. a factored-out generalization of this idea.
  300. Dave Abrahams suggested Smart Pointer support for exception safety, providing
  301. useful hints for the implementation.
  302. Joel de Guzman's documentation style was copied from Fusion.
  303. Peter Dimov for sharing his insights on language details and their evolution.
  304. [endsect]
  305. [section References]
  306. # [@http://en.wikipedia.org/wiki/Design_Patterns Design Patterns],
  307. Gamma et al. - Addison Wesley Publishing, 1995
  308. # [@https://boost.org/sgi/stl/ Standard Template Library Programmer's Guide],
  309. Hewlett-Packard Company, 1994
  310. # [@http://www.boost.org/libs/bind/bind.html Boost.Bind],
  311. Peter Dimov, 2001-2005
  312. # [@http://www.boost.org/doc/html/function.html Boost.Function],
  313. Douglas Gregor, 2001-2004
  314. [endsect]