examples.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. ========
  6. Examples
  7. ========
  8. Some examples are given here and in the accompanying test files:
  9. .. contents:: :local:
  10. .. _`Example 1`:
  11. 1. Null pointers cannot be stored in the containers
  12. +++++++++++++++++++++++++++++++++++++++++++++++++++
  13. ::
  14. my_container.push_back( 0 ); // throws bad_ptr
  15. my_container.replace( an_iterator, 0 ); // throws bad_ptr
  16. my_container.insert( an_iterator, 0 ); // throws bad_ptr
  17. std::auto_ptr<T> p( 0 );
  18. my_container.push_back( p ); // throws bad_ptr
  19. .. _`Example 2`:
  20. 2. Iterators and other operations return indirected values
  21. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  22. ::
  23. ptr_vector<X> pvec;
  24. std::vector<X*> vec;
  25. *vec.begin() = new X; // fine, memory leak
  26. *pvec.begin() = new X; // compile time error
  27. ( *vec.begin() )->foo(); // call X::foo(), a bit clumsy
  28. pvec.begin()->foo(); // no indirection needed
  29. *vec.front() = X(); // overwrite first element
  30. pvec.front() = X(); // no indirection needed
  31. .. _`Example 3`:
  32. 3. Copy-semantics of pointer containers
  33. +++++++++++++++++++++++++++++++++++++++
  34. ::
  35. ptr_vector<T> vec1;
  36. ...
  37. ptr_vector<T> vec2( vec1.clone() ); // deep copy objects of 'vec1' and use them to construct 'vec2', could be very expensive
  38. vec2 = vec1.release(); // give up ownership of pointers in 'vec1' and pass the ownership to 'vec2', rather cheap
  39. vec2.release(); // give up ownership; the objects will be deallocated if not assigned to another container
  40. vec1 = vec2; // deep copy objects of 'vec2' and assign them to 'vec1', could be very expensive
  41. ptr_vector<T> vec3( vec1 ); // deep copy objects of 'vec1', could be very expensive
  42. .. _`Example 4`:
  43. 4. Making a non-copyable type Cloneable
  44. +++++++++++++++++++++++++++++++++++++++
  45. ::
  46. // a class that has no normal copy semantics
  47. class X : boost::noncopyable { public: X* clone() const; ... };
  48. // this will be found by the library by argument dependent lookup (ADL)
  49. X* new_clone( const X& x )
  50. { return x.clone(); }
  51. // we can now use the interface that requires cloneability
  52. ptr_vector<X> vec1, vec2;
  53. ...
  54. vec2 = vec1.clone(); // 'clone()' requires cloning <g>
  55. vec2.insert( vec2.end(), vec1.begin(), vec1.end() ); // inserting always means inserting clones
  56. .. _`Example 5`:
  57. 5. Objects are cloned before insertion, inserted pointers are owned by the container
  58. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  59. ::
  60. class X { ... }; // assume 'X' is Cloneable
  61. X x; // and 'X' can be stack-allocated
  62. ptr_list<X> list;
  63. list.push_back( new_clone( x ) ); // insert a clone
  64. list.push_back( new X ); // always give the pointer directly to the container to avoid leaks
  65. list.push_back( &x ); // don't do this!!!
  66. std::auto_ptr<X> p( new X );
  67. list.push_back( p ); // give up ownership
  68. BOOST_ASSERT( p.get() == 0 );
  69. .. _`Example 6`:
  70. 6. Transferring ownership of a single element
  71. +++++++++++++++++++++++++++++++++++++++++++++
  72. ::
  73. ptr_deque<T> deq;
  74. typedef ptr_deque<T>::auto_type auto_type;
  75. // ... fill the container somehow
  76. auto_type ptr = deq.pop_back(); // remove back element from container and give up ownership
  77. auto_type ptr2 = deq.release( deq.begin() + 2 ); // use an iterator to determine the element to release
  78. ptr = deq.pop_front(); // supported for 'ptr_list' and 'ptr_deque'
  79. deq.push_back( ptr.release() ); // give ownership back to the container
  80. .. _`Example 7`:
  81. 7. Transferring ownership of pointers between different pointer containers
  82. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  83. ::
  84. ptr_list<X> list; ptr_vector<X> vec;
  85. ...
  86. //
  87. // note: no cloning happens in these examples
  88. //
  89. list.transfer( list.begin(), vec.begin(), vec ); // make the first element of 'vec' the first element of 'list'
  90. vec.transfer( vec.end(), list.begin(), list.end(), list ); // put all the lists element into the vector
  91. We can also transfer objects from ``ptr_container<Derived>`` to ``ptr_container<Base>`` without any problems.
  92. .. _`Example 8`:
  93. 8. Selected test files
  94. ++++++++++++++++++++++
  95. :incomplete_type_test.cpp_: Shows how to implement the Composite pattern.
  96. :simple_test.cpp_: Shows how the usage of pointer container compares with a
  97. container of smart pointers
  98. :view_example.cpp_: Shows how to use a pointer container as a view into other container
  99. :tree_test.cpp_: Shows how to make a tree-structure
  100. :array_test.cpp_: Shows how to make an n-ary tree
  101. .. _incomplete_type_test.cpp : ../test/incomplete_type_test.cpp
  102. .. _simple_test.cpp : ../test/simple_test.cpp
  103. .. _view_example.cpp : ../test/view_example.cpp
  104. .. _tree_test.cpp : ../test/tree_test.cpp
  105. .. _array_test.cpp : ../test/ptr_array.cpp
  106. 9. A large example
  107. ++++++++++++++++++
  108. This example shows many of the most common
  109. features at work. The example provide lots of comments.
  110. The source code can also be found `here <../test/tut1.cpp>`_.
  111. .. raw:: html
  112. :file: tutorial_example.html
  113. ..
  114. 10. Changing the Clone Allocator
  115. ++++++++++++++++++++++++++++++++
  116. This example shows how we can change
  117. the Clone Allocator to use the pointer containers
  118. as view into other containers:
  119. .. raw:: html
  120. :file: tut2.html
  121. .. raw:: html
  122. <hr>
  123. **Navigate:**
  124. - `home <ptr_container.html>`_
  125. - `reference <reference.html>`_
  126. .. raw:: html
  127. <hr>
  128. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  129. __ http://www.boost.org/LICENSE_1_0.txt