ptr_container.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. :Authors: Thorsten Ottosen
  6. :Contact: nesotto@cs.aau.dk or tottosen@dezide.com
  7. :Organizations: `Department of Computer Science`_, Aalborg University, and `Dezide Aps`_
  8. :date: 27th of October 2007
  9. :Copyright: Thorsten Ottosen 2004-2007. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  10. __ http://www.boost.org/LICENSE_1_0.txt
  11. .. _`Department of Computer Science`: http://www.cs.aau.dk
  12. .. _`Dezide Aps`: http://www.dezide.com
  13. ========
  14. Overview
  15. ========
  16. Boost.Pointer Container provides containers for holding heap-allocated
  17. objects in an exception-safe manner and with minimal overhead.
  18. The aim of the library is in particular to make OO programming
  19. easier in C++ by establishing a standard set of classes, methods
  20. and designs for dealing with OO specific problems
  21. * Motivation_
  22. * Tutorial_
  23. * Reference_
  24. * `Usage guidelines`_
  25. * Examples_
  26. * `Library headers`_
  27. * FAQ_
  28. * `Upgrading from Boost v. 1.33.*`_
  29. * `Upgrading from Boost v. 1.34.*`_
  30. * `Upgrading from Boost v. 1.35.*`_
  31. * `Upgrading from Boost v. 1.66.*`_
  32. * `Future Developments`_
  33. * Acknowledgements_
  34. * References_
  35. ..
  36. - `Conventions <conventions.html>`_
  37. - `The Clonable Concept <reference.html#the-clonable-concept>`_
  38. - `The Clone Allocator Concept <reference.html#the-clone-allocator-concept>`_
  39. - `Pointer container adapters <reference.html#pointer-container-adapters>`_
  40. - `Sequence container classes <reference.html#sequence-containers>`_
  41. - `ptr_vector <ptr_vector.html>`_
  42. - `ptr_deque <ptr_deque.html>`_
  43. - `ptr_list <ptr_list.html>`_
  44. - `ptr_array <ptr_array.html>`_
  45. - `Associative container classes <reference.html#associative-containers>`_
  46. - `ptr_set <ptr_set.html>`_
  47. - `ptr_multiset <ptr_multiset.html>`_
  48. - `ptr_map <ptr_map.html>`_
  49. - `ptr_multimap <ptr_multimap.html>`_
  50. - `Indirected functions <indirect_fun.html>`_
  51. - `Class nullable <reference.html#class-nullable>`_
  52. - `Exception classes <reference.html#exception-classes>`_
  53. .. _Tutorial: tutorial.html
  54. .. _Reference: reference.html
  55. .. _`Usage guidelines`: guidelines.html
  56. .. _Examples: examples.html
  57. .. _`Library headers`: headers.html
  58. .. _FAQ: faq.html
  59. ==========
  60. Motivation
  61. ==========
  62. Whenever a programmer wants to have a container of pointers to
  63. heap-allocated objects, there is usually only one exception-safe way:
  64. to make a container of smart pointers like `boost::shared_ptr <../../smart_ptr/shared_ptr.htm>`_
  65. This approach is suboptimal if
  66. 1. the stored objects are not shared, but owned exclusively, or
  67. ..
  68. 2. the overhead implied by smart pointers is inappropriate
  69. This library therefore provides standard-like containers that are for storing
  70. heap-allocated or `cloned <reference.html#the-clonable-concept>`_ objects (or in case of a map, the mapped object must be
  71. a heap-allocated or cloned object). For each of the standard
  72. containers there is a pointer container equivalent that takes ownership of
  73. the objects in an exception safe manner. In this respect the library is intended
  74. to solve the so-called
  75. `polymorphic class problem <faq.html#what-is-the-polymorphic-class-problem>`_.
  76. The advantages of pointer containers are
  77. 1. Exception-safe pointer storage and manipulation.
  78. ..
  79. 2. Notational convenience compared to the use of containers of pointers.
  80. ..
  81. 3. Can be used for types that are neither Assignable nor Copy Constructible.
  82. ..
  83. 4. No memory-overhead as containers of smart pointers can have (see [11]_ and [12]_).
  84. ..
  85. 5. Usually faster than using containers of smart pointers (see [11]_ and [12]_).
  86. ..
  87. 6. The interface is slightly changed towards the domain of pointers
  88. instead of relying on the normal value-based interface. For example,
  89. now it is possible for ``pop_back()`` to return the removed element.
  90. ..
  91. 7. Propagates constness such that one cannot modify the objects via a ``const_iterator``.
  92. ..
  93. 8. Built-in support for deep-copy semantics via the `the Clonable concept`__
  94. .. __: reference.html#the-clonable-concept
  95. The disadvantages are
  96. 1. Less flexible than containers of smart pointers like `boost::shared_ptr <../../smart_ptr/shared_ptr.htm>`_
  97. When you do need shared semantics, this library is not what you need.
  98. ====================================
  99. Upgrading from Boost v. ``1.33.*``
  100. ====================================
  101. If you upgrade from one of these versions of Boost, then there has been one
  102. major interface change: map iterators now mimic iterators from ``std::map``.
  103. Previously you may have written ::
  104. for( boost::ptr_map<std::string,T>::iterator i = m.begin(), e = m.end();
  105. i != e; ++i )
  106. {
  107. std::cout << "key:" << i.key();
  108. std::cout << "value:" << *i;
  109. i->foo(); // call T::foo()
  110. }
  111. and this now needs to be converted into ::
  112. for( boost::ptr_map<std::string,T>::iterator i = m.begin(), e = m.end();
  113. i != e; ++i )
  114. {
  115. std::cout << "key:" << i->first;
  116. std::cout << "value:" << *i->second;
  117. i->second->foo(); // call T::foo()
  118. }
  119. Apart from the above change, the library now also introduces
  120. - ``std::auto_ptr<T>`` overloads::
  121. std::auto_ptr<T> p( new T );
  122. container.push_back( p );
  123. - Derived-to-Base conversion in ``transfer()``::
  124. boost::ptr_vector<Base> vec;
  125. boost::ptr_list<Derived> list;
  126. ...
  127. vec.transfer( vec.begin(), list ); // now ok
  128. Also note that `Boost.Assign <../../assign/index.html>`_ introduces better support
  129. for pointer containers.
  130. ====================================
  131. Upgrading from Boost v. ``1.34.*``
  132. ====================================
  133. Serialization has now been made optional thanks to Sebastian Ramacher.
  134. You simply include ``<boost/ptr_container/serialize.hpp>`` or perhaps
  135. just one of the more specialized headers.
  136. All containers are now copy-constructible and assignable. So you can e.g. now
  137. do::
  138. boost::ptr_vector<Derived> derived = ...;
  139. boost::ptr_vector<Base> base( derived );
  140. base = derived;
  141. As the example shows, derived-to-base class conversions are also allowed.
  142. A few general functions have been added::
  143. VoidPtrContainer& base();
  144. const VoidPtrContainer& base() const;
  145. These allow direct access to the wrapped container which is
  146. sometimes needed when you want to provide extra functionality.
  147. A few new functions have been added to sequences::
  148. void resize( size_type size );
  149. void resize( size_type size, T* to_clone );
  150. ``ptr_vector<T>`` has a few new helper functions to integrate better with C-arrays::
  151. void transfer( iterator before, T** from, size_type size, bool delete_from = true );
  152. T** c_array();
  153. Finally, you can now also "copy" and "assign" an ``auto_type`` ptr by calling ``move()``::
  154. boost::ptr_vector<T>::auto_type move_ptr = ...;
  155. return boost::ptr_container::move( move_ptr );
  156. ====================================
  157. Upgrading from Boost v. ``1.35.*``
  158. ====================================
  159. The library has been fairly stable, but a few new containers have been supported:
  160. - ``boost::ptr_unordered_set<T>`` in ``<boost/ptr_container/ptr_unordered_set.hpp>``
  161. - ``boost::ptr_unordered_map<Key,T>`` in ``<boost/ptr_container/ptr_unordered_map.hpp>``
  162. - ``boost::ptr_circular_buffer<T>`` in ``<boost/ptr_container/ptr_circular_buffer.hpp>``
  163. There are no docs for these classes yet, but they are almost identical to
  164. ``boost::ptr_set<T>``, ``boost::ptr_map<Key,T>`` and ``boost::ptr_array<T,N>``, respectively.
  165. The underlying containers stem from the two boost libraries
  166. - `Boost.Unordered <../../unordered/index.html>`_
  167. - `Boost.Circular Buffer <../../circular_buffer/index.html>`_
  168. Furthermore, `insert iterators <ptr_inserter.html>`_ have been added.
  169. ===================================
  170. Upgrading from Boost v. ``1.66.*``
  171. ===================================
  172. Starting with Boost v. ``1.67.0``, Boost.Pointer Container will use
  173. `Boost.Config <../../config/index.html>`_ to conditionally provide
  174. ``std::unique_ptr``-based interfaces in addition to, or instead of,
  175. interfaces using ``std::auto_ptr``. Details
  176. are on the `Compatible Smart Pointer <compatible_smart_ptr.html>`_
  177. page, which also explains the convention in this documentation of using
  178. .. parsed-literal::
  179. *compatible-smart-ptr*\ <T>
  180. to indicate such conditional interfaces.
  181. For C++98/03 users, this change has no observable effect.
  182. For C++11/14 users, there is no effect on existing code that used
  183. previous versions of Boost.Pointer Container, but now all function
  184. overloads taking an ``std::auto_ptr`` parameter are accompanied by an
  185. overload taking ``std::unique_ptr``. In the case of return types,
  186. ``std::auto_ptr`` is still always used. Note however that until C++17,
  187. it is possible to construct ``std::unique_ptr<T>`` implicitly from
  188. ``std::auto_ptr<T>``. Thus, users are free to modernize their code by
  189. replacing any explicit mention of ``std::auto_ptr`` with
  190. ``std::unique_ptr``. This change falls just short of a
  191. search-and-replace conversion, as it is possible that some code may
  192. have relied on ``std::auto_ptr`` being copyable. But such situations
  193. will result in compile-time errors which should be easy to fix.
  194. Although ``std::auto_ptr`` is formally removed as of ISO C++17,
  195. certain compiler or standard library vendors have chosen to leave it
  196. in for backwards compatibility. For compilers and standard libraries
  197. where this is *not* the case, C++17 compilation of code using
  198. Boost.Pointer Container is not possible with Boost v. ``1.66.*`` or
  199. earlier. This situation is fixed as of Boost v. ``1.67.0``.
  200. =====================
  201. Future Developments
  202. =====================
  203. There are indications that the ``void*`` implementation has a slight
  204. performance overhead compared to a ``T*`` based implementation. Furthermore, a
  205. ``T*`` based implementation is so much easier to use type-safely
  206. with algorithms. Therefore I anticipate to move to a ``T*`` based implementation.
  207. Furthermore, the clone allocator might be allowed to have state.
  208. This design requires some thought, so if you have good ideas and use-cases'
  209. for this, please don't hesitate to contact me.
  210. Also, support for Boost.Interprocess is on the todo list.
  211. There has been a few request for ``boost::ptr_multi_index_container<T,...>``.
  212. I investigated how difficult it would be, and it did turn out to be difficult, albeit
  213. not impossible. But I don't have the resources to implement this beast
  214. for years to come, so if someone really needs this container, I suggest that they
  215. talk with me in private about how it can be done.
  216. ================
  217. Acknowledgements
  218. ================
  219. The following people have been very helpful:
  220. - Bjørn D. Rasmussen for unintentionally motivating me to start this library
  221. - Pavel Vozenilek for asking me to make the adapters
  222. - David Abrahams for the ``indirect_fun`` design
  223. - Pavol Droba for being review manager
  224. - Ross Boylan for trying out a prototype for real
  225. - Felipe Magno de Almeida for giving fedback based on using the
  226. library in production code even before the library was part of boost
  227. - Jonathan Turkanis for supplying his ``move_ptr`` framework
  228. which is used internally
  229. - Stefan Slapeta and Howard Hinnant for Metrowerks support
  230. - Russell Hind for help with Borland compatibility
  231. - Jonathan Wakely for his great help with GCC compatibility and bug fixes
  232. - Pavel Chikulaev for comments and bug-fixes
  233. - Andreas Hommel for fixing the nasty Metrowerks bug
  234. - Charles Brockman for his many comments on the documentation
  235. - Sebastian Ramacher for implementing the optional serialization support
  236. ==========
  237. References
  238. ==========
  239. .. [1] Matt Austern: `"The Standard Librarian: Containers of Pointers"`__ , C/C++ Users Journal Experts Forum.
  240. __ http://www.cuj.com/documents/s=7990/cujcexp1910austern/
  241. .. [2] Bjarne Stroustrup, "The C++ Programming Language", `Appendix E: "Standard-Library Exception Safety"`__
  242. __ http://www.research.att.com/~bs/3rd_safe.pdf
  243. .. [3] Herb Sutter, "Exceptional C++".
  244. .. [4] Herb Sutter, "More Exceptional C++".
  245. .. [5] Kevlin Henney: `"From Mechanism to Method: The Safe Stacking of Cats"`__ , C++ Experts Forum, February 2002.
  246. __ http://www.cuj.com/documents/s=7986/cujcexp2002henney/henney.htm
  247. .. [6] Some of the few earlier attempts of pointer containers I have seen are the rather interesting NTL_ and the
  248. pointainer_.
  249. As of this writing both libraries are not exceptions-safe and can leak.
  250. .. [7] INTERNATIONAL STANDARD, Programming languages --- C++, ISO/IEC 14882, 1998. See section 23 in particular.
  251. .. [8] C++ Standard Library Closed Issues List (Revision 27),
  252. Item 218, `Algorithms do not use binary predicate objects for default comparisons`__.
  253. __ http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#218
  254. .. [9] C++ Standard Library Active Issues List (Revision 27),
  255. Item 226, `User supplied specializations or overloads of namespace std function templates`__.
  256. __ http://gcc.gnu.org/onlinedocs/libstdc++/ext/lwg-active.html#226
  257. .. [10] Harald Nowak, "A remove_if for vector", C/C++ Users Journal, July 2001.
  258. .. [11] Boost smart pointer timings__
  259. __ http://www.boost.org/libs/smart_ptr/smarttests.htm
  260. .. [12] NTL_: Array vs std::vector and boost::shared_ptr
  261. .. [13] Kevlin Henney, `Null Object`__, 2002.
  262. __ http://www.two-sdg.demon.co.uk/curbralan/papers/europlop/NullObject.pdf
  263. .. _NTL: http://www.ntllib.org/asp.html
  264. .. _pointainer: http://ootips.org/yonat/4dev/pointainer.h
  265. .. raw:: html
  266. <hr>
  267. :Copyright: Thorsten Ottosen 2004-2006.