allocate_shared_construct11_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // allocate_shared_construct11_test.cpp
  2. //
  3. // Test whether allocate_shared uses construct/destroy in C++11
  4. //
  5. // Copyright 2007-2009, 2014 Peter Dimov
  6. //
  7. // Distributed under the Boost Software License, Version 1.0.
  8. // See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/make_shared.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <boost/weak_ptr.hpp>
  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. template<class... Args> void construct( T * p, Args&&... args )
  35. {
  36. ::new( static_cast< void* >( p ) ) T( std::forward<Args>( args )... );
  37. }
  38. void destroy( T * p )
  39. {
  40. p->~T();
  41. }
  42. };
  43. class X
  44. {
  45. private:
  46. X( X const & );
  47. X & operator=( X const & );
  48. void * operator new( std::size_t n )
  49. {
  50. BOOST_ERROR( "private X::new called" );
  51. return ::operator new( n );
  52. }
  53. void operator delete( void * p )
  54. {
  55. BOOST_ERROR( "private X::delete called" );
  56. ::operator delete( p );
  57. }
  58. public:
  59. static int instances;
  60. int v;
  61. protected:
  62. explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0, int a5 = 0, int a6 = 0, int a7 = 0, int a8 = 0, int a9 = 0 ): v( a1+a2+a3+a4+a5+a6+a7+a8+a9 )
  63. {
  64. ++instances;
  65. }
  66. ~X()
  67. {
  68. --instances;
  69. }
  70. friend class cxx11_allocator<X>;
  71. };
  72. int X::instances = 0;
  73. int main()
  74. {
  75. BOOST_TEST( X::instances == 0 );
  76. {
  77. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>() );
  78. boost::weak_ptr<X> wp( pi );
  79. BOOST_TEST( X::instances == 1 );
  80. BOOST_TEST( pi.get() != 0 );
  81. BOOST_TEST( pi->v == 0 );
  82. pi.reset();
  83. BOOST_TEST( X::instances == 0 );
  84. }
  85. {
  86. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1 );
  87. boost::weak_ptr<X> wp( pi );
  88. BOOST_TEST( X::instances == 1 );
  89. BOOST_TEST( pi.get() != 0 );
  90. BOOST_TEST( pi->v == 1 );
  91. pi.reset();
  92. BOOST_TEST( X::instances == 0 );
  93. }
  94. {
  95. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2 );
  96. boost::weak_ptr<X> wp( pi );
  97. BOOST_TEST( X::instances == 1 );
  98. BOOST_TEST( pi.get() != 0 );
  99. BOOST_TEST( pi->v == 1+2 );
  100. pi.reset();
  101. BOOST_TEST( X::instances == 0 );
  102. }
  103. {
  104. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3 );
  105. boost::weak_ptr<X> wp( pi );
  106. BOOST_TEST( X::instances == 1 );
  107. BOOST_TEST( pi.get() != 0 );
  108. BOOST_TEST( pi->v == 1+2+3 );
  109. pi.reset();
  110. BOOST_TEST( X::instances == 0 );
  111. }
  112. {
  113. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4 );
  114. boost::weak_ptr<X> wp( pi );
  115. BOOST_TEST( X::instances == 1 );
  116. BOOST_TEST( pi.get() != 0 );
  117. BOOST_TEST( pi->v == 1+2+3+4 );
  118. pi.reset();
  119. BOOST_TEST( X::instances == 0 );
  120. }
  121. {
  122. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5 );
  123. boost::weak_ptr<X> wp( pi );
  124. BOOST_TEST( X::instances == 1 );
  125. BOOST_TEST( pi.get() != 0 );
  126. BOOST_TEST( pi->v == 1+2+3+4+5 );
  127. pi.reset();
  128. BOOST_TEST( X::instances == 0 );
  129. }
  130. {
  131. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6 );
  132. boost::weak_ptr<X> wp( pi );
  133. BOOST_TEST( X::instances == 1 );
  134. BOOST_TEST( pi.get() != 0 );
  135. BOOST_TEST( pi->v == 1+2+3+4+5+6 );
  136. pi.reset();
  137. BOOST_TEST( X::instances == 0 );
  138. }
  139. {
  140. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
  141. boost::weak_ptr<X> wp( pi );
  142. BOOST_TEST( X::instances == 1 );
  143. BOOST_TEST( pi.get() != 0 );
  144. BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
  145. pi.reset();
  146. BOOST_TEST( X::instances == 0 );
  147. }
  148. {
  149. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
  150. boost::weak_ptr<X> wp( pi );
  151. BOOST_TEST( X::instances == 1 );
  152. BOOST_TEST( pi.get() != 0 );
  153. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
  154. pi.reset();
  155. BOOST_TEST( X::instances == 0 );
  156. }
  157. {
  158. boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
  159. boost::weak_ptr<X> wp( pi );
  160. BOOST_TEST( X::instances == 1 );
  161. BOOST_TEST( pi.get() != 0 );
  162. BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
  163. pi.reset();
  164. BOOST_TEST( X::instances == 0 );
  165. }
  166. return boost::report_errors();
  167. }
  168. #else // !defined( BOOST_NO_CXX11_ALLOCATOR )
  169. int main()
  170. {
  171. return 0;
  172. }
  173. #endif