default_delete.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. } //namespace move_upd {
  80. // @endcond
  81. namespace movelib {
  82. namespace bmupd = boost::move_upd;
  83. namespace bmupmu = ::boost::move_upmu;
  84. //!The class template <tt>default_delete</tt> serves as the default deleter
  85. //!(destruction policy) for the class template <tt>unique_ptr</tt>.
  86. //!
  87. //! \tparam T The type to be deleted. It may be an incomplete type
  88. template <class T>
  89. struct default_delete
  90. {
  91. //! Default constructor.
  92. //!
  93. BOOST_CONSTEXPR default_delete()
  94. //Avoid "defaulted on its first declaration must not have an exception-specification" error for GCC 4.6
  95. #if !defined(BOOST_GCC) || (BOOST_GCC < 40600 && BOOST_GCC >= 40700) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  96. BOOST_NOEXCEPT
  97. #endif
  98. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  99. = default;
  100. #else
  101. {};
  102. #endif
  103. #if defined(BOOST_MOVE_DOXYGEN_INVOKED)
  104. //! Trivial copy constructor
  105. //!
  106. default_delete(const default_delete&) BOOST_NOEXCEPT = default;
  107. //! Trivial assignment
  108. //!
  109. default_delete &operator=(const default_delete&) BOOST_NOEXCEPT = default;
  110. #else
  111. typedef typename bmupmu::remove_extent<T>::type element_type;
  112. #endif
  113. //! <b>Effects</b>: Constructs a default_delete object from another <tt>default_delete<U></tt> object.
  114. //!
  115. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  116. //! - If T is not an array type and U* is implicitly convertible to T*.
  117. //! - If T is an array type and U* is a more CV qualified pointer to remove_extent<T>::type.
  118. template <class U>
  119. default_delete(const default_delete<U>&
  120. BOOST_MOVE_DOCIGN(BOOST_MOVE_I typename bmupd::enable_def_del<U BOOST_MOVE_I T>::type* =0)
  121. ) BOOST_NOEXCEPT
  122. {
  123. //If T is not an array type, U derives from T
  124. //and T has no virtual destructor, then you have a problem
  125. BOOST_STATIC_ASSERT(( !::boost::move_upmu::missing_virtual_destructor<default_delete, U>::value ));
  126. }
  127. //! <b>Effects</b>: Constructs a default_delete object from another <tt>default_delete<U></tt> object.
  128. //!
  129. //! <b>Remarks</b>: This constructor shall not participate in overload resolution unless:
  130. //! - If T is not an array type and U* is implicitly convertible to T*.
  131. //! - If T is an array type and U* is a more CV qualified pointer to remove_extent<T>::type.
  132. template <class U>
  133. BOOST_MOVE_DOC1ST(default_delete&,
  134. typename bmupd::enable_def_del<U BOOST_MOVE_I T BOOST_MOVE_I default_delete &>::type)
  135. operator=(const default_delete<U>&) BOOST_NOEXCEPT
  136. {
  137. //If T is not an array type, U derives from T
  138. //and T has no virtual destructor, then you have a problem
  139. BOOST_STATIC_ASSERT(( !::boost::move_upmu::missing_virtual_destructor<default_delete, U>::value ));
  140. return *this;
  141. }
  142. //! <b>Effects</b>: if T is not an array type, calls <tt>delete</tt> on static_cast<T*>(ptr),
  143. //! otherwise calls <tt>delete[]</tt> on static_cast<remove_extent<T>::type*>(ptr).
  144. //!
  145. //! <b>Remarks</b>: If U is an incomplete type, the program is ill-formed.
  146. //! This operator shall not participate in overload resolution unless:
  147. //! - T is not an array type and U* is convertible to T*, OR
  148. //! - T is an array type, and remove_cv<U>::type is the same type as
  149. //! remove_cv<remove_extent<T>::type>::type and U* is convertible to remove_extent<T>::type*.
  150. template <class U>
  151. BOOST_MOVE_DOC1ST(void, typename bmupd::enable_defdel_call<U BOOST_MOVE_I T BOOST_MOVE_I void>::type)
  152. operator()(U* ptr) const BOOST_NOEXCEPT
  153. {
  154. //U must be a complete type
  155. BOOST_STATIC_ASSERT(sizeof(U) > 0);
  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_STATIC_ASSERT(( !::boost::move_upmu::missing_virtual_destructor<default_delete, U>::value ));
  159. element_type * const p = static_cast<element_type*>(ptr);
  160. bmupmu::is_array<T>::value ? delete [] p : delete p;
  161. }
  162. //! <b>Effects</b>: Same as <tt>(*this)(static_cast<element_type*>(nullptr))</tt>.
  163. //!
  164. void operator()(BOOST_MOVE_DOC0PTR(bmupd::nullptr_type)) const BOOST_NOEXCEPT
  165. { BOOST_STATIC_ASSERT(sizeof(element_type) > 0); }
  166. };
  167. } //namespace movelib {
  168. } //namespace boost{
  169. #include <boost/move/detail/config_end.hpp>
  170. #endif //#ifndef BOOST_MOVE_DEFAULT_DELETE_HPP_INCLUDED