conventions.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. Conventions
  6. +++++++++++
  7. There are a few design decisions that will affect how the classes are
  8. used. Besides these the classes are much like normal standard containers
  9. and provides almost the same interface. The new conventions are:
  10. .. contents:: :local:
  11. Null pointers are not allowed by default
  12. ----------------------------------------
  13. If the user tries to insert the null pointer, the operation will throw a
  14. ``bad_pointer`` exception (see `Example 1 <examples.html>`_).
  15. Use `nullable <reference.html#class-nullable>`_ to allow null pointers.
  16. Please notice that all preconditions of the form ::
  17. x != 0;
  18. are not active when the you have instantiated a container
  19. with ``nullable<T>`` as in ::
  20. boost::ptr_vector< boost::nullable<animal> > vec;
  21. vec.push_back( 0 ); // ok
  22. All default iterators apply an extra layer of indirection
  23. ---------------------------------------------------------
  24. This is done to
  25. make the containers easier and safer to use. It promotes a kind of
  26. pointer-less programming and the user of a class needs not worry about
  27. pointers except when allocating them (see `Example 2 <examples.html>`_). Iterators that
  28. provide access to the naked pointers are also provided since they might be
  29. useful in rare cases. For example, whenever ``begin()`` returns an iterator,
  30. ``ptr_begin()`` will return an iterator that allows one to iterate over the
  31. stored pointers.
  32. All comparison operations are done on the pointed to objects and not at the pointer level
  33. -----------------------------------------------------------------------------------------
  34. For example, in ``ptr_set<T>`` the ordering is by default done by
  35. ``boost::ptr_less<T>`` which compares the indirected pointers.
  36. Similarly, ``operator==()`` for ``container<Foo>`` compares all objects
  37. with ``operator==(const Foo&, const Foo&)``.
  38. Stored elements are required to be `Cloneable <reference.html#the-Cloneable-concept>`_ for a subset of the operations
  39. ---------------------------------------------------------------------------------------------------------------------
  40. This is because most polymorphic objects cannot be copied directly, but
  41. they can often be so by a use of a member function (see `Example 4 <examples.html>`_). Often
  42. it does not even make sense to clone an object in which case a large
  43. subset of the operations are still workable.
  44. Whenever objects are inserted into a container, they are cloned before insertion
  45. --------------------------------------------------------------------------------
  46. This is necessary because all pointer containers take ownerships of stored objects
  47. (see `Example 5 <examples.html>`_).
  48. Whenever pointers are inserted into a container, ownership is transferred to the container
  49. ------------------------------------------------------------------------------------------
  50. All containers take ownership of the stored pointers and therefore a
  51. container needs to have its own copies (see `Example 5 <examples.html>`_).
  52. Ownership can be transferred from a container on a per pointer basis
  53. --------------------------------------------------------------------
  54. This can of course also be convenient. Whenever it happens, an
  55. ``SmartContainer::auto_type`` object is used to provide an exception-safe transfer
  56. (see `Example 6 <examples.html>`_).
  57. Ownership can be transferred from a container to another container on a per iterator range basis
  58. ------------------------------------------------------------------------------------------------
  59. This makes it possible to exchange data safely between different pointer
  60. containers without cloning the objects again (see `Example 7 <examples.html>`_).
  61. A container can be cheaply returned from functions either by making a clone or by giving up ownership of the container
  62. ----------------------------------------------------------------------------------------------------------------------
  63. Two special member functions, ``clone()`` and ``release()``, both return a
  64. ``compatible-smart-ptr<SmartContainer>`` which can be assigned to another pointer container. This
  65. effectively reduces the cost of returning a container to one
  66. heap-allocation plus a call to ``swap()`` (see `Example 3 <examples.html>`_).
  67. Iterators are invalidated as in the corresponding standard container
  68. --------------------------------------------------------------------
  69. Because the containers in this library wrap standard containers, the
  70. rules for invalidation of iterators are the same as the rules
  71. of the corresponding standard container.
  72. For example, for both ``boost::ptr_vector<T>`` and ``std::vector<U>``
  73. insertion and deletion only invalidates the deleted
  74. element and elements following it; all elements before the inserted/deleted
  75. element remain valid.
  76. .. raw:: html
  77. <hr>
  78. **Navigate:**
  79. - `home <ptr_container.html>`_
  80. - `reference <reference.html>`_
  81. .. raw:: html
  82. <hr>
  83. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  84. __ http://www.boost.org/LICENSE_1_0.txt