checked_delete.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef BOOST_CORE_CHECKED_DELETE_HPP
  2. #define BOOST_CORE_CHECKED_DELETE_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. #include <boost/config.hpp>
  8. //
  9. // boost/checked_delete.hpp
  10. //
  11. // Copyright (c) 2002, 2003 Peter Dimov
  12. // Copyright (c) 2003 Daniel Frey
  13. // Copyright (c) 2003 Howard Hinnant
  14. //
  15. // Distributed under the Boost Software License, Version 1.0. (See
  16. // accompanying file LICENSE_1_0.txt or copy at
  17. // http://www.boost.org/LICENSE_1_0.txt)
  18. //
  19. // See http://www.boost.org/libs/core/doc/html/core/checked_delete.html for documentation.
  20. //
  21. namespace boost
  22. {
  23. // verify that types are complete for increased safety
  24. template<class T> inline void checked_delete(T * x) BOOST_NOEXCEPT
  25. {
  26. #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410L
  27. static_assert( sizeof(T) != 0, "Type must be complete" );
  28. #else
  29. typedef char type_must_be_complete[ sizeof(T) ];
  30. (void) sizeof(type_must_be_complete);
  31. #endif
  32. delete x;
  33. }
  34. template<class T> inline void checked_array_delete(T * x) BOOST_NOEXCEPT
  35. {
  36. #if defined(__cpp_static_assert) && __cpp_static_assert >= 200410L
  37. static_assert( sizeof(T) != 0, "Type must be complete" );
  38. #else
  39. typedef char type_must_be_complete[ sizeof(T) ];
  40. (void) sizeof(type_must_be_complete);
  41. #endif
  42. delete [] x;
  43. }
  44. // Block unintended ADL
  45. namespace checked_deleters
  46. {
  47. template<class T> struct checked_deleter
  48. {
  49. typedef void result_type;
  50. typedef T * argument_type;
  51. void operator()(T * x) const BOOST_NOEXCEPT
  52. {
  53. // boost:: disables ADL
  54. boost::checked_delete(x);
  55. }
  56. };
  57. template<class T> struct checked_array_deleter
  58. {
  59. typedef void result_type;
  60. typedef T * argument_type;
  61. void operator()(T * x) const BOOST_NOEXCEPT
  62. {
  63. boost::checked_array_delete(x);
  64. }
  65. };
  66. } // namespace checked_deleters
  67. using checked_deleters::checked_deleter;
  68. using checked_deleters::checked_array_deleter;
  69. } // namespace boost
  70. #endif // #ifndef BOOST_CORE_CHECKED_DELETE_HPP