allocator_constructed.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright 2024 Braden Ganetsky.
  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 https://www.boost.org/libs/unordered for library home page.
  7. */
  8. #ifndef BOOST_UNORDERED_DETAIL_ALLOCATOR_CONSTRUCTED_HPP
  9. #define BOOST_UNORDERED_DETAIL_ALLOCATOR_CONSTRUCTED_HPP
  10. #include <boost/core/allocator_traits.hpp>
  11. #include <boost/unordered/detail/opt_storage.hpp>
  12. namespace boost {
  13. namespace unordered {
  14. namespace detail {
  15. struct allocator_policy
  16. {
  17. template <class Allocator, class T, class... Args>
  18. static void construct(Allocator& a, T* p, Args&&... args)
  19. {
  20. boost::allocator_construct(a, p, std::forward<Args>(args)...);
  21. }
  22. template <class Allocator, class T>
  23. static void destroy(Allocator& a, T* p)
  24. {
  25. boost::allocator_destroy(a, p);
  26. }
  27. };
  28. /* constructs a stack-based object with the given policy and allocator */
  29. template <class Allocator, class T, class Policy = allocator_policy>
  30. class allocator_constructed
  31. {
  32. opt_storage<T> storage;
  33. Allocator alloc;
  34. public:
  35. template <class... Args>
  36. allocator_constructed(Allocator const& alloc_, Args&&... args)
  37. : alloc(alloc_)
  38. {
  39. Policy::construct(
  40. alloc, storage.address(), std::forward<Args>(args)...);
  41. }
  42. ~allocator_constructed() { Policy::destroy(alloc, storage.address()); }
  43. T& value() { return *storage.address(); }
  44. };
  45. } /* namespace detail */
  46. } /* namespace unordered */
  47. } /* namespace boost */
  48. #endif