shared_ptr_alloc11_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <boost/config.hpp>
  2. // shared_ptr_alloc11_test.cpp
  3. //
  4. // Test the allocator constructor with a C++11 minimal allocator
  5. //
  6. // Copyright (c) 2005, 2014 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0.
  9. // See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <memory>
  14. #include <cstddef>
  15. #if !defined( BOOST_NO_CXX11_ALLOCATOR )
  16. template< class T > class cxx11_allocator
  17. {
  18. public:
  19. typedef T value_type;
  20. cxx11_allocator()
  21. {
  22. }
  23. template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
  24. {
  25. }
  26. T * allocate( std::size_t n )
  27. {
  28. return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
  29. }
  30. void deallocate( T * p, std::size_t n )
  31. {
  32. ::operator delete( p );
  33. }
  34. };
  35. //
  36. struct D;
  37. struct X
  38. {
  39. static int instances;
  40. X(): deleted_( false )
  41. {
  42. ++instances;
  43. }
  44. ~X()
  45. {
  46. BOOST_TEST( deleted_ );
  47. --instances;
  48. }
  49. private:
  50. friend struct D;
  51. bool deleted_;
  52. X( X const & );
  53. X & operator=( X const & );
  54. };
  55. int X::instances = 0;
  56. struct D
  57. {
  58. void operator()( X * px ) const
  59. {
  60. px->deleted_ = true;
  61. delete px;
  62. }
  63. };
  64. int main()
  65. {
  66. BOOST_TEST( X::instances == 0 );
  67. boost::shared_ptr<void> pv( new X, D(), cxx11_allocator<X>() );
  68. BOOST_TEST( X::instances == 1 );
  69. pv.reset();
  70. BOOST_TEST( X::instances == 0 );
  71. pv.reset( new X, D(), cxx11_allocator<void>() );
  72. BOOST_TEST( X::instances == 1 );
  73. pv.reset();
  74. BOOST_TEST( X::instances == 0 );
  75. return boost::report_errors();
  76. }
  77. #else // !defined( BOOST_NO_CXX11_ALLOCATOR )
  78. int main()
  79. {
  80. return 0;
  81. }
  82. #endif