scoped_deleter.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #ifndef BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
  12. #define BOOST_PTR_CONTAINER_SCOPED_DELETER_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif
  16. #include <iterator>
  17. #include <cstddef>
  18. #include <boost/scoped_array.hpp>
  19. namespace boost
  20. {
  21. namespace ptr_container_detail
  22. {
  23. template< class T, class CloneAllocator >
  24. class scoped_deleter
  25. {
  26. typedef std::size_t size_type;
  27. scoped_array<T*> ptrs_;
  28. size_type stored_;
  29. bool released_;
  30. public:
  31. scoped_deleter( T** a, size_type size )
  32. : ptrs_( a ), stored_( size ), released_( false )
  33. {
  34. BOOST_ASSERT( a );
  35. }
  36. scoped_deleter( size_type size )
  37. : ptrs_( new T*[size] ), stored_( 0 ),
  38. released_( false )
  39. {
  40. BOOST_ASSERT( size > 0 );
  41. }
  42. scoped_deleter( size_type n, const T& x ) // strong
  43. : ptrs_( new T*[n] ), stored_(0),
  44. released_( false )
  45. {
  46. for( size_type i = 0; i != n; i++ )
  47. add( CloneAllocator::allocate_clone( &x ) );
  48. BOOST_ASSERT( stored_ > 0 );
  49. }
  50. template< class InputIterator >
  51. scoped_deleter ( InputIterator first, InputIterator last ) // strong
  52. : ptrs_( new T*[ std::distance(first,last) ] ),
  53. stored_(0),
  54. released_( false )
  55. {
  56. for( ; first != last; ++first )
  57. add( CloneAllocator::allocate_clone_from_iterator( first ) );
  58. BOOST_ASSERT( stored_ > 0 );
  59. }
  60. ~scoped_deleter()
  61. {
  62. if ( !released_ )
  63. {
  64. for( size_type i = 0u; i != stored_; ++i )
  65. CloneAllocator::deallocate_clone( ptrs_[i] );
  66. }
  67. }
  68. void add( T* t )
  69. {
  70. BOOST_ASSERT( ptrs_.get() != 0 );
  71. ptrs_[stored_] = t;
  72. ++stored_;
  73. }
  74. void release()
  75. {
  76. released_ = true;
  77. }
  78. T** begin()
  79. {
  80. BOOST_ASSERT( ptrs_.get() != 0 );
  81. return &ptrs_[0];
  82. }
  83. T** end()
  84. {
  85. BOOST_ASSERT( ptrs_.get() != 0 );
  86. return &ptrs_[stored_];
  87. }
  88. }; // class 'scoped_deleter'
  89. }
  90. }
  91. #endif