reference.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. =========
  6. Reference
  7. =========
  8. The documentation is divided into an explanation for
  9. each container. When containers have the same interface, that common interface is explained only once,
  10. but links are always provided to more relevant information.
  11. Please make sure you understand
  12. the `Clonable <reference.html#the-Clonable-concept>`_ concept and
  13. the `Clone Allocator <reference.html#the-clone-allocator-concept>`_ concept.
  14. - `Conventions <conventions.html>`_
  15. - `The Clonable concept`_
  16. - `The Clone Allocator concept`_
  17. - `Class hierarchy`_:
  18. - `reversible_ptr_container <reversible_ptr_container.html>`_
  19. - `ptr_sequence_adapter <ptr_sequence_adapter.html>`_
  20. - `ptr_vector <ptr_vector.html>`_
  21. - `ptr_list <ptr_list.html>`_
  22. - `ptr_deque <ptr_deque.html>`_
  23. - `ptr_array <ptr_array.html>`_
  24. - `associative_ptr_container <associative_ptr_container.html>`_
  25. - `ptr_set_adapter <ptr_set_adapter.html>`_
  26. - `ptr_multiset_adapter <ptr_multiset_adapter.html>`_
  27. - `ptr_map_adapter <ptr_map_adapter.html>`_
  28. - `ptr_multi_map_adapter <ptr_multimap_adapter.html>`_
  29. - `ptr_set <ptr_set.html>`_
  30. - `ptr_multi_set <ptr_multiset.html>`_
  31. - `ptr_map <ptr_map.html>`_
  32. - `ptr_multimap <ptr_multimap.html>`_
  33. - `Serialization`_
  34. - `Indirected functions <indirect_fun.html>`_
  35. - `Insert iterators <ptr_inserter.html>`_
  36. - `Class nullable`_
  37. - `Exception classes`_
  38. - `Disabling the use of exceptions`_
  39. ..
  40. - Class `reversible_ptr_container <reversible_ptr_container.html>`_
  41. - Class `associative_ptr_container <associative_ptr_container.html>`_
  42. - `Pointer container adapters`_
  43. - `ptr_sequence_adapter <ptr_sequence_adapter.html>`_
  44. - `ptr_set_adapter <ptr_set_adapter.html>`_
  45. - `ptr_multiset_adapter <ptr_multiset_adapter.html>`_
  46. - `ptr_map_adapter <ptr_map_adapter.html>`_
  47. - `ptr_multimap_adapter <ptr_multimap_adapter.html>`_
  48. - `Sequence containers`_
  49. - `ptr_vector <ptr_vector.html>`_
  50. - `ptr_deque <ptr_deque.html>`_
  51. - `ptr_list <ptr_list.html>`_
  52. - `ptr_array <ptr_array.html>`_
  53. - `Associative containers`_
  54. - `ptr_set <ptr_set.html>`_
  55. - `ptr_multiset <ptr_multiset.html>`_
  56. - `ptr_map <ptr_map.html>`_
  57. - `ptr_multimap <ptr_multimap.html>`_
  58. The Clonable concept
  59. ++++++++++++++++++++
  60. **Refinement of**
  61. - Heap Allocable
  62. - Heap Deallocable
  63. The Clonable concept is introduced to formalize the requirements for
  64. copying heap-allocated objects. A type ``T`` might be Clonable even though it
  65. is not Assignable or Copy Constructible. Notice that many operations on
  66. the containers do not even require the stored type to be Clonable.
  67. **Notation**
  68. ======================= ============================================ =================== =====================
  69. **Type** **Object** (``const`` or non-``const``) **Pointer** **Describes**
  70. ``T`` ``a`` ``ptr`` A Clonable type
  71. ======================= ============================================ =================== =====================
  72. **Valid expressions**
  73. ===================================== =========================== ======================================================================================== ===================================
  74. **Expression** **Type** **Semantics** **Postcondition**
  75. ``new_clone(a);`` ``T*`` Allocate a new object that can be considered equivalent to the ``a`` object ``typeid(*new_clone(a)) == typeid(a)``
  76. ``delete_clone(ptr);`` ``void`` Deallocate an object previously allocated with ``allocate_clone()``. Must not throw
  77. ===================================== =========================== ======================================================================================== ===================================
  78. Default implementation
  79. ----------------------
  80. In the ``<boost/ptr_container/clone_allocator.hpp>`` header a default implementation
  81. of the two functions is given:
  82. .. parsed-literal::
  83. namespace boost
  84. {
  85. template< class T >
  86. inline T* new_clone( const T& t )
  87. {
  88. return new T( t );
  89. }
  90. template< class T >
  91. void delete_clone( const T* t )
  92. {
  93. checked_delete( t );
  94. }
  95. }
  96. Notice that this implementation makes normal Copy Constructible classes automatically
  97. Clonable unless ``operator new()`` or ``operator delete()`` are hidden.
  98. The two functions represent a layer of indirection which is necessary to support
  99. classes that are not Copy Constructible by default. Notice that the implementation
  100. relies on argument-dependent lookup (ADL) to find the right version of
  101. ``new_clone()`` and ``delete_clone()``. This means that one does not need to overload or specialize
  102. the function in the boost namespace, but it can be placed together with
  103. the rest of the interface of the class. If you are implementing a class
  104. inline in headers, remember to forward declare the functions.
  105. **Warning: We are considering the removal of default implementation above. Therefore always make sure that you overload the functions for your types and do not rely on the defaults in any way.**
  106. The Clone Allocator concept
  107. +++++++++++++++++++++++++++
  108. The Clone Allocator concept is introduced to formalize the way
  109. pointer containers control memory of
  110. the stored objects (and not the pointers to the stored objects).
  111. The clone allocator allows
  112. users to apply custom allocators/deallocators for the cloned objects.
  113. More information can be found below:
  114. .. contents:: :depth: 1
  115. :local:
  116. Clone Allocator requirements
  117. ----------------------------
  118. **Notation**
  119. ===================== ============================================= ==================================================
  120. **Type** **Object** (``const`` or non-``const``) **Describes**
  121. ``T`` ``a`` A type
  122. ``T*`` ``ptr`` A pointer to ``T``
  123. ===================== ============================================= ==================================================
  124. **Valid expressions**
  125. ============================================== ============= ============================================================================= =============================================================
  126. **Expression** **Type** **Semantics** **Postcondition**
  127. ``CloneAllocator::allocate_clone(a);`` ``T*`` Allocate a new object that can be considered equivalent to the
  128. ``a`` object ``typeid(*CloneAllocator::allocate_clone(a)) == typeid(a)``
  129. ``CloneAllocator::deallocate_clone(ptr);`` ``void`` Deallocate an object previously allocated with
  130. ``CloneAllocator::allocate_clone()`` or a compatible allocator.
  131. Must not throw.
  132. ============================================== ============= ============================================================================= =============================================================
  133. The library comes with two predefined clone allocators.
  134. Class ``heap_clone_allocator``
  135. ------------------------------
  136. This is the default clone allocator used by all pointer containers. For most
  137. purposes you will never have to change this default.
  138. **Definition**
  139. .. parsed-literal::
  140. namespace boost
  141. {
  142. struct heap_clone_allocator
  143. {
  144. template< class U >
  145. static U* allocate_clone( const U& r )
  146. {
  147. return new_clone( r );
  148. }
  149. template< class U >
  150. static void deallocate_clone( const U* r )
  151. {
  152. delete_clone( r );
  153. }
  154. };
  155. }
  156. Notice that the above definition allows you to support custom allocation
  157. schemes by relying on ``new_clone()`` and ``delete_clone()``.
  158. Class ``view_clone_allocator``
  159. ------------------------------
  160. This class provides a way to remove ownership properties of the
  161. pointer containers. As its name implies, this means that you can
  162. instead use the pointer containers as a view into an existing
  163. container.
  164. **Definition**
  165. .. parsed-literal::
  166. namespace boost
  167. {
  168. struct view_clone_allocator
  169. {
  170. template< class U >
  171. static U* allocate_clone( const U& r )
  172. {
  173. return const_cast<U*>(&r);
  174. }
  175. template< class U >
  176. static void deallocate_clone( const U* )
  177. {
  178. // empty
  179. }
  180. };
  181. }
  182. .. **See also**
  183. - `Changing the clone allocator <examples.html#changing-the-clone-allocator>`_
  184. Class hierarchy
  185. +++++++++++++++
  186. The library consists of the following types of classes:
  187. 1. Pointer container adapters
  188. ..
  189. 2. Pointer containers
  190. The pointer container adapters are used when you
  191. want to make a pointer container starting from
  192. your own "normal" container. For example, you
  193. might have a map class that extends ``std::map``
  194. in some way; the adapter class then allows you
  195. to use your map class as a basis for a new
  196. pointer container.
  197. The library provides an adapter for each type
  198. of standard container highlighted as links below:
  199. - ``reversible_ptr_container``
  200. - `ptr_sequence_adapter <ptr_sequence_adapter.html>`_
  201. - ``ptr_vector``
  202. - ``ptr_list``
  203. - ``ptr_deque``
  204. - ``ptr_array``
  205. - ``associative_ptr_container``
  206. - `ptr_set_adapter <ptr_set_adapter.html>`_
  207. - `ptr_multiset_adapter <ptr_multiset_adapter.html>`_
  208. - `ptr_map_adapter <ptr_map_adapter.html>`_
  209. - `ptr_multi_map_adapter <ptr_multimap_adapter.html>`_
  210. - ``ptr_set``
  211. - ``ptr_multi_set``
  212. - ``ptr_map``
  213. - ``ptr_multimap``
  214. The pointer containers of this library are all built using
  215. the adapters. There is a pointer container
  216. for each type of "normal" standard container highlighted as links below.
  217. - ``reversible_ptr_container``
  218. - ``ptr_sequence_adapter``
  219. - `ptr_vector <ptr_vector.html>`_
  220. - `ptr_list <ptr_list.html>`_
  221. - `ptr_deque <ptr_deque.html>`_
  222. - `ptr_array <ptr_array.html>`_
  223. - ``associative_ptr_container``
  224. - ``ptr_set_adapter``
  225. - ``ptr_multiset_adapter``
  226. - ``ptr_map_adapter``
  227. - ``ptr_multi_map_adapter``
  228. - `ptr_set <ptr_set.html>`_
  229. - `ptr_multi_set <ptr_multiset.html>`_
  230. - `ptr_map <ptr_map.html>`_
  231. - `ptr_multimap <ptr_multimap.html>`_
  232. Serialization
  233. +++++++++++++
  234. As of version 1.34.0 of Boost, the library supports
  235. serialization via `Boost.Serialization`__.
  236. .. __: ../../serialization/index.html
  237. Of course, for serialization to work it is required
  238. that the stored type itself is serializable. For maps, both
  239. the key type and the mapped type must be serializable.
  240. When dealing with serialization (and serialization of polymophic objects in particular),
  241. pay special attention to these parts of Boost.Serialization:
  242. 1. Output/saving requires a const-reference::
  243. //
  244. // serialization helper: we can't save a non-const object
  245. //
  246. template< class T >
  247. inline T const& as_const( T const& r )
  248. {
  249. return r;
  250. }
  251. ...
  252. Container cont;
  253. std::ofstream ofs("filename");
  254. boost::archive::text_oarchive oa(ofs);
  255. oa << as_const(cont);
  256. See `Compile time trap when saving a non-const value`__ for
  257. details.
  258. .. __: ../../serialization/doc/rationale.html#trap
  259. 2. Derived classes need to call ``base_object()`` function::
  260. struct Derived : Base
  261. {
  262. template< class Archive >
  263. void serialize( Archive& ar, const unsigned int version )
  264. {
  265. ar & boost::serialization::base_object<Base>( *this );
  266. ...
  267. }
  268. };
  269. For details, see `Derived Classes`_.
  270. .. _`Derived Classes`: ../../serialization/doc/tutorial.html#derivedclasses
  271. 3. You need to use ``BOOST_CLASS_EXPORT`` to register the
  272. derived classes in your class hierarchy::
  273. BOOST_CLASS_EXPORT( Derived )
  274. See `Export Key`__ and `Object Tracking`_
  275. for details.
  276. .. __: ../../serialization/doc/traits.html#export
  277. .. _`Object Tracking`: ../../serialization/doc/special.html
  278. Remember these three issues and it might save you some trouble.
  279. ..
  280. Map iterator operations
  281. +++++++++++++++++++++++
  282. The map iterators are a bit different compared to the normal ones. The
  283. reason is that it is a bit clumsy to access the key and the mapped object
  284. through i->first and i->second, and one tends to forget what is what.
  285. Moreover, and more importantly, we also want to hide the pointer as much as possibble.
  286. The new style can be illustrated with a small example::
  287. typedef ptr_map<string,int> map_t;
  288. map_t m;
  289. m[ "foo" ] = 4; // insert pair
  290. m[ "bar" ] = 5; // ditto
  291. ...
  292. for( map_t::iterator i = m.begin(); i != m.end(); ++i )
  293. {
  294. *i += 42; // add 42 to each value
  295. cout << "value=" << *i << ", key=" << i.key() << "n";
  296. }
  297. So the difference from the normal map iterator is that
  298. - ``operator*()`` returns a reference to the mapped object (normally it returns a reference to a ``std::pair``, and
  299. - that the key can be accessed through the ``key()`` function.
  300. Class ``nullable``
  301. ++++++++++++++++++
  302. The purpose of the class is simply to tell the containers
  303. that null values should be allowed. Its definition is
  304. trivial::
  305. namespace boost
  306. {
  307. template< class T >
  308. struct nullable
  309. {
  310. typedef T type;
  311. };
  312. }
  313. Please notice that ``nullable`` has no effect on the containers
  314. interface (except for ``is_null()`` functions). For example, it
  315. does not make sense to do ::
  316. boost::ptr_vector< boost::nullable<T> > vec;
  317. vec.push_back( 0 ); // ok
  318. vec.push_back( new boost::nullable<T> ); // no no!
  319. boost::nullable<T>& ref = vec[0]; // also no no!
  320. Exception classes
  321. +++++++++++++++++
  322. There are three exceptions that are thrown by this library. The exception
  323. hierarchy looks as follows::
  324. namespace boost
  325. {
  326. class bad_ptr_container_operation : public std::exception
  327. {
  328. public:
  329. bad_ptr_container_operation( const char* what );
  330. };
  331. class bad_index : public bad_ptr_container_operation
  332. {
  333. public:
  334. bad_index( const char* what );
  335. };
  336. class bad_pointer : public bad_ptr_container_operation
  337. {
  338. public:
  339. bad_pointer();
  340. bad_pointer( const char* what );
  341. };
  342. }
  343. Disabling the use of exceptions
  344. +++++++++++++++++++++++++++++++
  345. As of version 1.34.0 of Boost, the library allows you to disable exceptions
  346. completely. This means the library is more fit for domains where exceptions
  347. are not used. Furthermore, it also speeds up a operations a little. Instead
  348. of throwing an exception, the library simply calls `BOOST_ASSERT`__.
  349. .. __: ../../utility/assert.html
  350. To disable exceptions, simply define this macro before including any header::
  351. #define BOOST_PTR_CONTAINER_NO_EXCEPTIONS 1
  352. #include <boost/ptr_container/ptr_vector.hpp>
  353. It is, however, recommended that you define the macro on the command-line, so
  354. you are absolutely certain that all headers are compiled the same way. Otherwise
  355. you might end up breaking the One Definition Rule.
  356. If ``BOOST_NO_EXCEPTIONS`` is defined, then ``BOOST_PTR_CONTAINER_NO_EXCEPTIONS``
  357. is also defined.
  358. .. raw:: html
  359. <hr>
  360. **Navigate:**
  361. - `home <ptr_container.html>`_
  362. .. raw:: html
  363. <hr>
  364. :Copyright: Thorsten Ottosen 2004-2007. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  365. __ http://www.boost.org/LICENSE_1_0.txt