alloc_construct.qbk 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. [/
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. ]
  7. [section:alloc_construct alloc_construct, alloc_destroy]
  8. [simplesect Authors]
  9. * Glen Fernandes
  10. [endsimplesect]
  11. [section Overview]
  12. The header <boost/core/alloc_construct.hpp> provides function templates
  13. `alloc_construct`, `alloc_construct_n`, `alloc_destroy`, and `alloc_destroy_n`
  14. for allocator aware and exception safe construction and destruction of objects
  15. and arrays.
  16. [endsect]
  17. [section Example]
  18. The following example allocates storage for an array of `n` elements of `T`
  19. using an allocator `a` and constructs `T` elements in that storage. If any
  20. exception was thrown during construction of an element, the constructed
  21. elements are destroyed in reverse order.
  22. ```
  23. template<class A>
  24. auto create(A& a, std::size_t n)
  25. {
  26. auto p = a.allocate(n);
  27. try {
  28. boost::alloc_construct_n(a, boost::to_address(p), n);
  29. } catch (...) {
  30. a.deallocate(p, n);
  31. throw;
  32. }
  33. return p;
  34. }
  35. ```
  36. [endsect]
  37. [section Reference]
  38. ```
  39. namespace boost {
  40. template<class A, class T>
  41. void alloc_destroy(A& a, T* p);
  42. template<class A, class T>
  43. void alloc_destroy_n(A& a, T* p, std::size_t n);
  44. template<class A, class T, class Args>
  45. void alloc_construct(A& a, T* p, Args&&... args);
  46. template<class A, class T>
  47. void alloc_construct_n(A& a, T* p, std::size_t n);
  48. template<class A, class T>
  49. void alloc_construct_n(A& a, T* p, std::size_t n, const T* l, std::size_t m);
  50. template<class A, class T, class I>
  51. void alloc_construct_n(A& a, T* p, std::size_t n, I begin);
  52. } /* boost */
  53. ```
  54. [section Functions]
  55. [variablelist
  56. [[`template<class A, class T> void alloc_destroy(A& a, T* p);`]
  57. [[variablelist
  58. [[Requires][`A` is an /Allocator/]]
  59. [[Effects][`std::allocator_traits<A>::destroy(a, p)`.]]]]]
  60. [[`template<class A, class T> void alloc_destroy_n(A& a, T* p,
  61. std::size_t n);`]
  62. [[variablelist
  63. [[Requires][`A` is an /Allocator/]]
  64. [[Effects]
  65. [Destroys each `i`-th element in reverse order by calling
  66. `std::allocator_traits<A>::destroy(a, &p[i])`.]]]]]
  67. [[`template<class A, class T, class Args> void alloc_construct(A& a, T* p,
  68. Args&&... args);`]
  69. [[variablelist
  70. [[Requires][`A` is an /Allocator/]]
  71. [[Effects]
  72. [`std::allocator_traits<A>::construct(a, p, std::forward<Args>(args)...)`.]]]]]
  73. [[`template<class A, class T> void alloc_construct_n(A& a, T* p,
  74. std::size_t n);`]
  75. [[variablelist
  76. [[Requires][`A` is an /Allocator/]]
  77. [[Effects]
  78. [Constructs each `i`-th element in order by calling
  79. `std::allocator_traits<A>::construct(a, &p[i])`.]]
  80. [[Remarks]
  81. [If an exception is thrown destroys each already constructed `j`-th element in
  82. reverse order by calling `std::allocator_traits<A>::destroy(a, &p[j])`.]]]]]
  83. [[`template<class A, class T> void alloc_construct_n(A& a, T* p, std::size_t n,
  84. const T* l, std::size_t m);`]
  85. [[variablelist
  86. [[Requires][`A` is an /Allocator/]]
  87. [[Effects]
  88. [Constructs each `i`-th element in order by calling
  89. `std::allocator_traits<A>::construct(a, &p[i], l[i % m])`.]]
  90. [[Remarks]
  91. [If an exception is thrown destroys each already constructed `j`-th element in
  92. reverse order by calling `std::allocator_traits<A>::destroy(a, &p[j])`.]]]]]
  93. [[`template<class A, class T, class I> void alloc_construct_n(A& a, T* p,
  94. std::size_t n, I begin);`]
  95. [[variablelist
  96. [[Requires]
  97. [[itemized_list
  98. [`A` is an /Allocator/][`I` is an /InputIterator/]]]]
  99. [[Effects]
  100. [Constructs each `i`-th element in order by calling
  101. `std::allocator_traits<A>::construct(a, &p[i], *begin++])`.]]
  102. [[Remarks]
  103. [If an exception is thrown destroys each already constructed `j`-th element in
  104. reverse order by calling `std::allocator_traits<A>::destroy(a, &p[j])`.]]]]]]
  105. [endsect]
  106. [endsect]
  107. [section Compatibility]
  108. When `BOOST_NO_CXX11_ALLOCATOR` is defined, and the C++11 allocator model is
  109. not supported, these functions invoke constructors and destructors directly
  110. without going through the supplied allocator.
  111. [endsect]
  112. [section Acknowledgments]
  113. Glen Fernandes originally implemented this functionality in Boost.Smart_Ptr and
  114. later moved these functions to Boost.Core for use in other Boost libraries,
  115. such as Boost.Multi_Array and Boost.Histogram.
  116. [endsect]
  117. [endsect]