intro.xml 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!--
  2. //
  3. // Boost.Pointer Container
  4. //
  5. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  6. // distribution is subject to the Boost Software License, Version
  7. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // For more information, see http://www.boost.org/libs/ptr_container/
  11. //
  12. -->
  13. <?xml version="1.0" encoding="utf-8"?>
  14. <!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  15. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  16. <section id="ptr_container.intro" last-revision="$Date$">
  17. <title>Introduction</title>
  18. <para>
  19. This library provides standard-like containers that are suitable
  20. for storing pointers to both polymorphic and non-polymorphic objects.
  21. For each of the standard containers there is a pointer container
  22. equivalent that takes ownership of the stored pointers in an exception
  23. safe manner. In this respect it is intended to solve
  24. the so-called <emphasis>polymorphic class problem. </emphasis>
  25. </para>
  26. <para>
  27. The main advantages are
  28. <itemizedlist>
  29. <listitem> Exception-safe and fool proof pointer storage and manipulation.</listitem>.
  30. <listitem> Exception-guarantees are generally much better than with standard containers (at least the strong guarantee</listitem>
  31. <listitem> Notational convinience compared to the use of containers of smart pointers.</listitem>
  32. <listitem> Iterators are automatically indirected so the comparison operations can be kept
  33. on object basis instead of making/adding pointer based variants.</listitem>
  34. <listitem> No memory-overhead as containers of smart_pointers can have.</listitem>
  35. <listitem> Faster than using containers of smart pointers.</listitem>
  36. <listitem> Provides an elegant solution to <code> vector< vector<T> > </code> performance
  37. problems; simply use <code>ptr_vector< vector<T> ></code></listtem>
  38. </para>
  39. <para>
  40. Below is given some example that show how the usage compares to a container of smart pointers:
  41. <programlisting>
  42. using namespace boost;
  43. using namespace std;
  44. class Poly
  45. {
  46. public:
  47. virtual ~Poly() {}
  48. void foo() { doFoo(); }
  49. private:
  50. virtual void doFoo()
  51. {
  52. int i;
  53. ++i;
  54. }
  55. };
  56. //
  57. // one doesn't need to introduce new names or live with long ones
  58. //
  59. typedef shared_ptr<Poly> PolyPtr;
  60. //
  61. // one doesn't need to write this anymore
  62. //
  63. struct PolyPtrOps
  64. {
  65. void operator()( const PolyPtr & a )
  66. { a->foo(); }
  67. };
  68. int main()
  69. {
  70. enum { size = 2000000 };
  71. vector<PolyPtr> svec
  72. ptr_vector<Poly> pvec;
  73. for( int i = 0; i < size; ++i )
  74. {
  75. svec.push_back( PolyPtr( new Poly ) );
  76. pvec.push_back( new Poly ); // no extra syntax
  77. }
  78. for_each( svec.begin(), svec.end(), PolyPtrOps() );
  79. for_each( pvec.begin(), pvec.end(), mem_fun_ref( &Poly::foo ) );
  80. }
  81. </programlisting>
  82. </para>
  83. </section>