auto_space.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2003-2008 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_AUTO_SPACE_HPP
  10. #if defined(_MSC_VER)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <algorithm>
  15. #include <boost/detail/allocator_utilities.hpp>
  16. #include <boost/multi_index/detail/adl_swap.hpp>
  17. #include <boost/multi_index/detail/prevent_eti.hpp>
  18. #include <boost/noncopyable.hpp>
  19. #include <memory>
  20. namespace boost{
  21. namespace multi_index{
  22. namespace detail{
  23. /* auto_space provides uninitialized space suitably to store
  24. * a given number of elements of a given type.
  25. */
  26. /* NB: it is not clear whether using an allocator to handle
  27. * zero-sized arrays of elements is conformant or not. GCC 3.3.1
  28. * and prior fail here, other stdlibs handle the issue gracefully.
  29. * To be on the safe side, the case n==0 is given special treatment.
  30. * References:
  31. * GCC Bugzilla, "standard allocator crashes when deallocating segment
  32. * "of zero length", http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14176
  33. * C++ Standard Library Defect Report List (Revision 28), issue 199
  34. * "What does allocate(0) return?",
  35. * http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#199
  36. */
  37. template<typename T,typename Allocator=std::allocator<T> >
  38. struct auto_space:private noncopyable
  39. {
  40. typedef typename prevent_eti<
  41. Allocator,
  42. typename boost::detail::allocator::rebind_to<
  43. Allocator,T
  44. >::type
  45. >::type::pointer pointer;
  46. explicit auto_space(const Allocator& al=Allocator(),std::size_t n=1):
  47. al_(al),n_(n),data_(n_?al_.allocate(n_):pointer(0))
  48. {}
  49. ~auto_space()
  50. {
  51. if(n_)al_.deallocate(data_,n_);
  52. }
  53. Allocator get_allocator()const{return al_;}
  54. pointer data()const{return data_;}
  55. void swap(auto_space& x)
  56. {
  57. if(al_!=x.al_)adl_swap(al_,x.al_);
  58. std::swap(n_,x.n_);
  59. std::swap(data_,x.data_);
  60. }
  61. private:
  62. typename boost::detail::allocator::rebind_to<
  63. Allocator,T>::type al_;
  64. std::size_t n_;
  65. pointer data_;
  66. };
  67. template<typename T,typename Allocator>
  68. void swap(auto_space<T,Allocator>& x,auto_space<T,Allocator>& y)
  69. {
  70. x.swap(y);
  71. }
  72. } /* namespace multi_index::detail */
  73. } /* namespace multi_index */
  74. } /* namespace boost */
  75. #endif