destruct_n.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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_DESTRUCT_N_H
  9. #define _LIBCPP___MEMORY_DESTRUCT_N_H
  10. #include <__config>
  11. #include <__type_traits/integral_constant.h>
  12. #include <__type_traits/is_trivially_destructible.h>
  13. #include <cstddef>
  14. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  15. # pragma GCC system_header
  16. #endif
  17. _LIBCPP_BEGIN_NAMESPACE_STD
  18. struct __destruct_n
  19. {
  20. private:
  21. size_t __size_;
  22. template <class _Tp>
  23. _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
  24. {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
  25. template <class _Tp>
  26. _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
  27. {}
  28. _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
  29. {++__size_;}
  30. _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
  31. {}
  32. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
  33. {__size_ = __s;}
  34. _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
  35. {}
  36. public:
  37. _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
  38. : __size_(__s) {}
  39. template <class _Tp>
  40. _LIBCPP_INLINE_VISIBILITY void __incr() _NOEXCEPT
  41. {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  42. template <class _Tp>
  43. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
  44. {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  45. template <class _Tp>
  46. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
  47. {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  48. };
  49. _LIBCPP_END_NAMESPACE_STD
  50. #endif // _LIBCPP___MEMORY_DESTRUCT_N_H