default_deleter.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // (C) Copyright Jonathan Turkanis 2004-2005.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  4. // Contains the definition of move_ptrs::default_deleter, the default
  5. // Deleter template argument to move_ptr. Uses a technique of Daniel
  6. // Wallin to capture the type of a pointer at the time the deleter
  7. // is constructed, so that move_ptrs can delete objects of incomplete
  8. // type by default.
  9. #ifndef BOOST_MOVE_PTR_DEFAULT_DELETER_HPP_INCLUDED
  10. #define BOOST_MOVE_PTR_DEFAULT_DELETER_HPP_INCLUDED
  11. #include <boost/checked_delete.hpp>
  12. #include <boost/mpl/if.hpp>
  13. #include <boost/type_traits/is_array.hpp>
  14. #include <boost/type_traits/remove_bounds.hpp>
  15. namespace boost { namespace ptr_container_detail { namespace move_ptrs {
  16. namespace ptr_container_detail {
  17. template<typename T>
  18. struct deleter_base {
  19. typedef void (*deleter)(T*);
  20. deleter_base(deleter d) { delete_ = d; }
  21. void operator() (T* t) const { delete_(t); }
  22. static deleter delete_;
  23. };
  24. template<class T>
  25. typename deleter_base<T>::deleter
  26. deleter_base<T>::delete_;
  27. template<typename T>
  28. struct scalar_deleter : deleter_base<T> {
  29. typedef deleter_base<T> base;
  30. scalar_deleter() : base(do_delete) { }
  31. static void do_delete(T* t) { checked_delete(t); }
  32. };
  33. template<typename T>
  34. struct array_deleter
  35. : deleter_base<typename remove_bounds<T>::type>
  36. {
  37. typedef typename remove_bounds<T>::type element_type;
  38. typedef deleter_base<element_type> base;
  39. array_deleter() : base(do_delete) { }
  40. static void do_delete(element_type* t) { checked_array_delete(t); }
  41. };
  42. } // End namespace ptr_container_detail.
  43. template<typename T>
  44. struct default_deleter
  45. : mpl::if_<
  46. is_array<T>,
  47. ptr_container_detail::array_deleter<T>,
  48. ptr_container_detail::scalar_deleter<T>
  49. >::type
  50. {
  51. default_deleter() { }
  52. template<typename TT>
  53. default_deleter(default_deleter<TT>) { }
  54. };
  55. } } } // End namespaces ptr_container_detail, move_ptrs, boost.
  56. #endif // #ifndef BOOST_MOVE_PTR_DEFAULT_DELETER_HPP_INCLUDED