xref: /freebsd-src/contrib/llvm-project/libcxx/include/__memory/destruct_n.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric 
9bdd1243dSDimitry Andric #ifndef _LIBCPP___MEMORY_DESTRUCT_N_H
10bdd1243dSDimitry Andric #define _LIBCPP___MEMORY_DESTRUCT_N_H
11bdd1243dSDimitry Andric 
12bdd1243dSDimitry Andric #include <__config>
13bdd1243dSDimitry Andric #include <__type_traits/integral_constant.h>
14bdd1243dSDimitry Andric #include <__type_traits/is_trivially_destructible.h>
15bdd1243dSDimitry Andric #include <cstddef>
16bdd1243dSDimitry Andric 
17bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18bdd1243dSDimitry Andric #  pragma GCC system_header
19bdd1243dSDimitry Andric #endif
20bdd1243dSDimitry Andric 
21bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
22bdd1243dSDimitry Andric 
23*cb14a3feSDimitry Andric struct __destruct_n {
24bdd1243dSDimitry Andric private:
25bdd1243dSDimitry Andric   size_t __size_;
26bdd1243dSDimitry Andric 
27bdd1243dSDimitry Andric   template <class _Tp>
__process__destruct_n28*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __process(_Tp* __p, false_type) _NOEXCEPT {
29*cb14a3feSDimitry Andric     for (size_t __i = 0; __i < __size_; ++__i, ++__p)
30*cb14a3feSDimitry Andric       __p->~_Tp();
31*cb14a3feSDimitry Andric   }
32bdd1243dSDimitry Andric 
33bdd1243dSDimitry Andric   template <class _Tp>
__process__destruct_n34*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __process(_Tp*, true_type) _NOEXCEPT {}
35bdd1243dSDimitry Andric 
__incr__destruct_n36*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __incr(false_type) _NOEXCEPT { ++__size_; }
__incr__destruct_n37*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __incr(true_type) _NOEXCEPT {}
38bdd1243dSDimitry Andric 
__set__destruct_n39*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __set(size_t __s, false_type) _NOEXCEPT { __size_ = __s; }
__set__destruct_n40*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __set(size_t, true_type) _NOEXCEPT {}
41*cb14a3feSDimitry Andric 
42bdd1243dSDimitry Andric public:
__destruct_n__destruct_n43*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI explicit __destruct_n(size_t __s) _NOEXCEPT : __size_(__s) {}
44bdd1243dSDimitry Andric 
45bdd1243dSDimitry Andric   template <class _Tp>
__incr__destruct_n46*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __incr() _NOEXCEPT {
47*cb14a3feSDimitry Andric     __incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());
48*cb14a3feSDimitry Andric   }
49bdd1243dSDimitry Andric 
50bdd1243dSDimitry Andric   template <class _Tp>
__set__destruct_n51*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void __set(size_t __s, _Tp*) _NOEXCEPT {
52*cb14a3feSDimitry Andric     __set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());
53*cb14a3feSDimitry Andric   }
54bdd1243dSDimitry Andric 
55bdd1243dSDimitry Andric   template <class _Tp>
operator__destruct_n56*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI void operator()(_Tp* __p) _NOEXCEPT {
57*cb14a3feSDimitry Andric     __process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());
58*cb14a3feSDimitry Andric   }
59bdd1243dSDimitry Andric };
60bdd1243dSDimitry Andric 
61bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD
62bdd1243dSDimitry Andric 
63bdd1243dSDimitry Andric #endif // _LIBCPP___MEMORY_DESTRUCT_N_H
64