checked_delete.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef BHO_CORE_CHECKED_DELETE_HPP
  2. #define BHO_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 <asio2/bho/config.hpp>
  8. //
  9. // bho/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 bho
  22. {
  23. // verify that types are complete for increased safety
  24. template<class T> inline void checked_delete(T * x) BHO_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) BHO_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. template<class T> struct checked_deleter
  45. {
  46. typedef void result_type;
  47. typedef T * argument_type;
  48. void operator()(T * x) const BHO_NOEXCEPT
  49. {
  50. // bho:: disables ADL
  51. bho::checked_delete(x);
  52. }
  53. };
  54. template<class T> struct checked_array_deleter
  55. {
  56. typedef void result_type;
  57. typedef T * argument_type;
  58. void operator()(T * x) const BHO_NOEXCEPT
  59. {
  60. bho::checked_array_delete(x);
  61. }
  62. };
  63. } // namespace bho
  64. #endif // #ifndef BHO_CORE_CHECKED_DELETE_HPP