allocators.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2018 Glen Joseph Fernandes
  2. // (glenjofe@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MULTI_ARRAY_ALLOCATORS_HPP
  8. #define BOOST_MULTI_ARRAY_ALLOCATORS_HPP
  9. #include <boost/config.hpp>
  10. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  11. #include <memory>
  12. #else
  13. #include <new>
  14. #endif
  15. namespace boost {
  16. namespace detail {
  17. namespace multi_array {
  18. template<class A, class T>
  19. inline void destroy(A& allocator, T* ptr, T* end)
  20. {
  21. for (; ptr != end; ++ptr) {
  22. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  23. std::allocator_traits<A>::destroy(allocator,ptr);
  24. #else
  25. ptr->~T();
  26. #endif
  27. }
  28. }
  29. template<class A, class T>
  30. inline void construct(A& allocator, T* ptr)
  31. {
  32. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  33. std::allocator_traits<A>::construct(allocator,ptr);
  34. #else
  35. ::new(static_cast<void*>(ptr)) T();
  36. #endif
  37. }
  38. #if !defined(BOOST_NO_EXCEPTIONS)
  39. template<class A, class T>
  40. inline void construct(A& allocator, T* ptr, T* end)
  41. {
  42. T* start = ptr;
  43. try {
  44. for (; ptr != end; ++ptr) {
  45. boost::detail::multi_array::construct(allocator,ptr);
  46. }
  47. } catch (...) {
  48. boost::detail::multi_array::destroy(allocator,start,ptr);
  49. throw;
  50. }
  51. }
  52. #else
  53. template<class A, class T>
  54. inline void construct(A& allocator, T* ptr, T* end)
  55. {
  56. for (; ptr != end; ++ptr) {
  57. boost::detail::multi_array::construct(allocator,ptr);
  58. }
  59. }
  60. #endif
  61. } // multi_array
  62. } // detail
  63. } // boost
  64. #endif