temp_value.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___MEMORY_TEMP_VALUE_H
  9. #define _LIBCPP___MEMORY_TEMP_VALUE_H
  10. #include <__config>
  11. #include <__memory/addressof.h>
  12. #include <__memory/allocator_traits.h>
  13. #include <__type_traits/aligned_storage.h>
  14. #include <__utility/forward.h>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. template <class _Tp, class _Alloc>
  20. struct __temp_value {
  21. typedef allocator_traits<_Alloc> _Traits;
  22. #ifdef _LIBCPP_CXX03_LANG
  23. typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
  24. #else
  25. union { _Tp __v; };
  26. #endif
  27. _Alloc &__a;
  28. _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp *__addr() {
  29. #ifdef _LIBCPP_CXX03_LANG
  30. return reinterpret_cast<_Tp*>(std::addressof(__v));
  31. #else
  32. return std::addressof(__v);
  33. #endif
  34. }
  35. _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp & get() { return *__addr(); }
  36. template<class... _Args>
  37. _LIBCPP_NO_CFI
  38. _LIBCPP_CONSTEXPR_SINCE_CXX20 __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
  39. _Traits::construct(__a, __addr(), std::forward<_Args>(__args)...);
  40. }
  41. _LIBCPP_CONSTEXPR_SINCE_CXX20 ~__temp_value() { _Traits::destroy(__a, __addr()); }
  42. };
  43. _LIBCPP_END_NAMESPACE_STD
  44. #endif // _LIBCPP___MEMORY_TEMP_VALUE_H