1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___MEMORY_CONSTRUCT_AT_H
11#define _LIBCPP___MEMORY_CONSTRUCT_AT_H
12
13#include <__assert>
14#include <__config>
15#include <__iterator/access.h>
16#include <__memory/addressof.h>
17#include <__new/placement_new_delete.h>
18#include <__type_traits/enable_if.h>
19#include <__type_traits/is_array.h>
20#include <__utility/declval.h>
21#include <__utility/forward.h>
22#include <__utility/move.h>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25# pragma GCC system_header
26#endif
27
28_LIBCPP_PUSH_MACROS
29#include <__undef_macros>
30
31_LIBCPP_BEGIN_NAMESPACE_STD
32
33// construct_at
34
35#if _LIBCPP_STD_VER >= 20
36
37template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
38_LIBCPP_HIDE_FROM_ABI constexpr _Tp* construct_at(_Tp* __location, _Args&&... __args) {
39 _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at");
40 return ::new (static_cast<void*>(__location)) _Tp(std::forward<_Args>(__args)...);
41}
42
43#endif
44
45template <class _Tp, class... _Args, class = decltype(::new(std::declval<void*>()) _Tp(std::declval<_Args>()...))>
46_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _Tp* __construct_at(_Tp* __location, _Args&&... __args) {
47#if _LIBCPP_STD_VER >= 20
48 return std::construct_at(__location, std::forward<_Args>(__args)...);
49#else
50 return _LIBCPP_ASSERT_NON_NULL(__location != nullptr, "null pointer given to construct_at"),
51 ::new (static_cast<void*>(__location)) _Tp(std::forward<_Args>(__args)...);
52#endif
53}
54
55// destroy_at
56
57// The internal functions are available regardless of the language version (with the exception of the `__destroy_at`
58// taking an array).
59
60template <class _Tp, __enable_if_t<!is_array<_Tp>::value, int> = 0>
61_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
62 _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
63 __loc->~_Tp();
64}
65
66#if _LIBCPP_STD_VER >= 20
67template <class _Tp, __enable_if_t<is_array<_Tp>::value, int> = 0>
68_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __destroy_at(_Tp* __loc) {
69 _LIBCPP_ASSERT_NON_NULL(__loc != nullptr, "null pointer given to destroy_at");
70 auto const __end = std::end(*__loc);
71 for (auto __it = std::begin(*__loc); __it != __end; ++__it)
72 std::__destroy_at(__it);
73}
74#endif
75
76#if _LIBCPP_STD_VER >= 17
77
78template <class _Tp, enable_if_t<!is_array_v<_Tp>, int> = 0>
79_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
80 std::__destroy_at(__loc);
81}
82
83# if _LIBCPP_STD_VER >= 20
84template <class _Tp, enable_if_t<is_array_v<_Tp>, int> = 0>
85_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void destroy_at(_Tp* __loc) {
86 std::__destroy_at(__loc);
87}
88# endif
89
90#endif // _LIBCPP_STD_VER >= 17
91
92_LIBCPP_END_NAMESPACE_STD
93
94_LIBCPP_POP_MACROS
95
96#endif // _LIBCPP___MEMORY_CONSTRUCT_AT_H
97