default_delete.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/move for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_MOVE_DEFAULT_DELETE_HPP_INCLUDED
  11. #define BOOST_MOVE_DEFAULT_DELETE_HPP_INCLUDED
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/move/detail/config_begin.hpp>
  20. #include <boost/move/detail/workaround.hpp>
  21. #include <boost/move/detail/unique_ptr_meta_utils.hpp>
  22. #include <boost/move/utility_core.hpp>
  23. #include <boost/static_assert.hpp>
  24. #include <cstddef> //For std::size_t,std::nullptr_t
  25. //!\file
  26. //! Describes the default deleter (destruction policy) of <tt>unique_ptr</tt>: <tt>default_delete</tt>.
  27. namespace boost{
  28. // @cond
  29. namespace move_upd {
  30. namespace bmupmu = ::boost::move_upmu;
  31. ////////////////////////////////////////
  32. //// enable_def_del
  33. ////////////////////////////////////////
  34. //compatible with a pointer type T*:
  35. //When either Y* is convertible to T*
  36. //Y is U[N] and T is U cv []
  37. template<class U, class T>
  38. struct def_del_compatible_cond
  39. : bmupmu::is_convertible<U*, T*>
  40. {};
  41. template<class U, class T, std::size_t N>
  42. struct def_del_compatible_cond<U[N], T[]>
  43. : def_del_compatible_cond<U[], T[]>
  44. {};
  45. template<class U, class T, class Type = bmupmu::nat>
  46. struct enable_def_del
  47. : bmupmu::enable_if_c<def_del_compatible_cond<U, T>::value, Type>
  48. {};
  49. ////////////////////////////////////////
  50. //// enable_defdel_call
  51. ////////////////////////////////////////
  52. //When 2nd is T[N], 1st(*)[N] shall be convertible to T(*)[N];
  53. //When 2nd is T[], 1st(*)[] shall be convertible to T(*)[];
  54. //Otherwise, 1st* shall be convertible to 2nd*.
  55. template<class U, class T, class Type = bmupmu::nat>
  56. struct enable_defdel_call
  57. : public enable_def_del<U, T, Type>
  58. {};
  59. template<class U, class T, class Type>
  60. struct enable_defdel_call<U, T[], Type>
  61. : public enable_def_del<U[], T[], Type>
  62. {};
  63. template<class U, class T, class Type, std::size_t N>
  64. struct enable_defdel_call<U, T[N], Type>
  65. : public enable_def_del<U[N], T[N], Type>
  66. {};
  67. ////////////////////////////////////////
  68. //// Some bool literal zero conversion utilities
  69. ////////////////////////////////////////
  70. struct bool_conversion {int for_bool; int for_arg(); };
  71. typedef int bool_conversion::* explicit_bool_arg;
  72. #if !defined(BOOST_NO_CXX11_NULLPTR) && !defined(BOOST_NO_CXX11_DECLTYPE)
  73. typedef decltype(nullptr) nullptr_type;
  74. #elif !defined(BOOST_NO_CXX11_NULLPTR)
  75. typedef std::nullptr_t nullptr_type;
  76. #else
  77. typedef int (bool_conversion::*nullptr_type)();
  78. #endif
  79. template<bool B>
  80. struct is_array_del
  81. {};
  82. template<class T>
  83. void call_delete(T *p, is_array_del<true>)
  84. {
  85. delete [] p;
  86. }
  87. template<class T>
  88. void call_delete(T *p, is_array_del<false>)
  89. {
  90. delete p;
  91. }
  92. template< class T, class U
  93. , bool enable = def_del_compatible_cond< U, T>::value &&
  94. !move_upmu::is_array<T>::value &&
  95. !move_upmu::is_same<typename move_upmu::remove_cv<T>::type, void>::value &&
  96. !move_upmu::is_same<typename move_upmu::remove_cv<U>::type, typename move_upmu::remove_cv<T>::type>::value
  97. >
  98. struct missing_virtual_destructor_default_delete
  99. { static const bool value = !move_upmu::has_virtual_destructor<T>::value; };
  100. template<class T, class U>
  101. struct missing_virtual_destructor_default_delete<T, U, false>
  102. { static const bool value = false; };
  103. //////////////////////////////////////
  104. // missing_virtual_destructor
  105. //////////////////////////////////////
  106. template<class Deleter, class U>
  107. struct missing_virtual_destructor
  108. { static const bool value = false; };
  109. template<class T, class U>
  110. struct missing_virtual_destructor< ::boost::movelib::default_delete<T>, U >
  111. : missing_virtual_destructor_default_delete<T, U>
  112. {};
  113. } //namespace move_upd {
  114. // @endcond
  115. namespace movelib {
  116. namespace bmupd = boost::move_upd;
  117. namespace bmupmu = ::boost::move_upmu;
  118. //!The class template <tt>default_delete</tt> serves as the default deleter
  119. //!(destruction policy) for the class template <tt>unique_ptr</tt>.
  120. //!
  121. //! \tparam T The type to be deleted. It may be an incomplete type
  122. template <class T>
  123. struct default_delete
  124. {
  125. //! Default constructor.
  126. //!
  127. BOOST_CONSTEXPR default_delete()
  128. //Avoid "defaulted on its first declaration must not have an exception-specification" error for GCC 4.6
  129. #if !defined(BOOST_GCC) || (BOOST_GCC < 40600 && BOOST_GCC >= 40700) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  130. BOOST_NOEXCEPT
  131. #endif
  132. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  133. = default;
  134. #else
  135. {};
  136. #endif
  137. #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
  138. //! Trivial copy constructor
  139. //!
  140. default_delete(const default_delete&) BOOST_NOEXCEPT = default;
  141. //! Trivial assignment
  142. //!
  143. default_delete &operator=(const default_delete&) BOOST_NOEXCEPT = default;
  144. #else
  145. typedef typename bmupmu::remove_extent<T>::type element_type;
  146. #endif
  147. //! <b>Effects</b>: Constructs a default_delete object from another <tt>default_delete<U></tt> object.
  148. //!
  149. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  150. //! - If T is not an array type and U* is implicitly convertible to T*.
  151. //! - If T is an array type and U* is a more CV qualified pointer to remove_extent<T>::type.
  152. template <class U>
  153. default_delete(const default_delete<U>&
  154. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_def_del<U BOOST_MOVE_I T>::type* =0)
  155. ) BOOST_NOEXCEPT
  156. {
  157. //If T is not an array type, U derives from T
  158. //and T has no virtual destructor, then you have a problem
  159. BOOST_STATIC_ASSERT(( !bmupd::missing_virtual_destructor<default_delete, U>::value ));
  160. }
  161. //! <b>Effects</b>: Constructs a default_delete object from another <tt>default_delete<U></tt> object.
  162. //!
  163. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  164. //! - If T is not an array type and U* is implicitly convertible to T*.
  165. //! - If T is an array type and U* is a more CV qualified pointer to remove_extent<T>::type.
  166. template <class U>
  167. BOOST_MOVE_DOC1ST(default_delete&,
  168. typename bmupd::enable_def_del<U BOOST_MOVE_I T BOOST_MOVE_I default_delete &>::type)
  169. operator=(const default_delete<U>&) BOOST_NOEXCEPT
  170. {
  171. //If T is not an array type, U derives from T
  172. //and T has no virtual destructor, then you have a problem
  173. BOOST_STATIC_ASSERT(( !bmupd::missing_virtual_destructor<default_delete, U>::value ));
  174. return *this;
  175. }
  176. //! <b>Effects</b>: if T is not an array type, calls <tt>delete</tt> on static_cast<T*>(ptr),
  177. //! otherwise calls <tt>delete[]</tt> on static_cast<remove_extent<T>::type*>(ptr).
  178. //!
  179. //! <b>Remarks</b>: If U is an incomplete type, the program is ill-formed.
  180. //! This operator shall not participate in overload resolution unless:
  181. //! - T is not an array type and U* is convertible to T*, OR
  182. //! - T is an array type, and remove_cv<U>::type is the same type as
  183. //! remove_cv<remove_extent<T>::type>::type and U* is convertible to remove_extent<T>::type*.
  184. template <class U>
  185. BOOST_MOVE_DOC1ST(void, typename bmupd::enable_defdel_call<U BOOST_MOVE_I T BOOST_MOVE_I void>::type)
  186. operator()(U* ptr) const BOOST_NOEXCEPT
  187. {
  188. //U must be a complete type
  189. BOOST_STATIC_ASSERT(sizeof(U) > 0);
  190. //If T is not an array type, U derives from T
  191. //and T has no virtual destructor, then you have a problem
  192. BOOST_STATIC_ASSERT(( !bmupd::missing_virtual_destructor<default_delete, U>::value ));
  193. element_type * const p = static_cast<element_type*>(ptr);
  194. move_upd::call_delete(p, move_upd::is_array_del<bmupmu::is_array<T>::value>());
  195. }
  196. //! <b>Effects</b>: Same as <tt>(*this)(static_cast<element_type*>(nullptr))</tt>.
  197. //!
  198. void operator()(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) const BOOST_NOEXCEPT
  199. { BOOST_STATIC_ASSERT(sizeof(element_type) > 0); }
  200. };
  201. } //namespace movelib {
  202. } //namespace boost{
  203. #include <boost/move/detail/config_end.hpp>
  204. #endif //#ifndef BOOST_MOVE_DEFAULT_DELETE_HPP_INCLUDED