default_delete.hpp 8.3 KB

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