xref: /xnu-12377.41.6/libkern/os/cpp_util.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions #ifndef _OS_CPP_UTIL_H
2*bbb1b6f9SApple OSS Distributions #define _OS_CPP_UTIL_H
3*bbb1b6f9SApple OSS Distributions 
4*bbb1b6f9SApple OSS Distributions #include <sys/cdefs.h>
5*bbb1b6f9SApple OSS Distributions #include <sys/_types/_size_t.h>
6*bbb1b6f9SApple OSS Distributions 
7*bbb1b6f9SApple OSS Distributions #if __has_feature(cxx_nullptr) && __has_feature(cxx_decltype)
8*bbb1b6f9SApple OSS Distributions # define OS_HAS_NULLPTR 1
9*bbb1b6f9SApple OSS Distributions #endif
10*bbb1b6f9SApple OSS Distributions 
11*bbb1b6f9SApple OSS Distributions #if __has_feature(cxx_rvalue_references) || __has_extension(cxx_rvalue_references)
12*bbb1b6f9SApple OSS Distributions # define OS_HAS_RVALUE_REFERENCES 1
13*bbb1b6f9SApple OSS Distributions #endif
14*bbb1b6f9SApple OSS Distributions 
15*bbb1b6f9SApple OSS Distributions #if (defined(__has_include) && __has_include(<__xnu_libcxx_sentinel.h>) && __has_include(<new>))
16*bbb1b6f9SApple OSS Distributions #include <new>
17*bbb1b6f9SApple OSS Distributions #else
18*bbb1b6f9SApple OSS Distributions void* operator new(size_t, void*) noexcept; // forward declaration needed for placement-new
19*bbb1b6f9SApple OSS Distributions #endif
20*bbb1b6f9SApple OSS Distributions 
21*bbb1b6f9SApple OSS Distributions namespace os {
22*bbb1b6f9SApple OSS Distributions #if OS_HAS_NULLPTR
23*bbb1b6f9SApple OSS Distributions typedef decltype(nullptr) nullptr_t;
24*bbb1b6f9SApple OSS Distributions #endif
25*bbb1b6f9SApple OSS Distributions 
26*bbb1b6f9SApple OSS Distributions /*
27*bbb1b6f9SApple OSS Distributions  * Reference removal
28*bbb1b6f9SApple OSS Distributions  */
29*bbb1b6f9SApple OSS Distributions 
30*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_reference       {typedef _T type;};
31*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_reference<_T&>  {typedef _T type;};
32*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_reference<_T &&> {typedef _T type;};
33*bbb1b6f9SApple OSS Distributions template <class _T> using remove_reference_t = typename remove_reference<_T>::type;
34*bbb1b6f9SApple OSS Distributions 
35*bbb1b6f9SApple OSS Distributions /*
36*bbb1b6f9SApple OSS Distributions  * Pointer removal
37*bbb1b6f9SApple OSS Distributions  */
38*bbb1b6f9SApple OSS Distributions 
39*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_pointer                     {typedef _T type;};
40*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_pointer<_T*>                {typedef _T type;};
41*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_pointer<_T* const>          {typedef _T type;};
42*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_pointer<_T* volatile>       {typedef _T type;};
43*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_pointer<_T* const volatile> {typedef _T type;};
44*bbb1b6f9SApple OSS Distributions template <class _T> using remove_pointer_t = typename remove_pointer<_T>::type;
45*bbb1b6f9SApple OSS Distributions 
46*bbb1b6f9SApple OSS Distributions /*
47*bbb1b6f9SApple OSS Distributions  * Const removal
48*bbb1b6f9SApple OSS Distributions  */
49*bbb1b6f9SApple OSS Distributions 
50*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_const           {typedef _T type;};
51*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_const<const _T> {typedef _T type;};
52*bbb1b6f9SApple OSS Distributions template <class _T> using remove_const_t = typename remove_const<_T>::type;
53*bbb1b6f9SApple OSS Distributions 
54*bbb1b6f9SApple OSS Distributions /*
55*bbb1b6f9SApple OSS Distributions  * Volatile removal
56*bbb1b6f9SApple OSS Distributions  */
57*bbb1b6f9SApple OSS Distributions 
58*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_volatile              {typedef _T type;};
59*bbb1b6f9SApple OSS Distributions template <class _T> struct remove_volatile<volatile _T> {typedef _T type;};
60*bbb1b6f9SApple OSS Distributions template <class _T> using remove_volatile_t = typename remove_volatile<_T>::type;
61*bbb1b6f9SApple OSS Distributions 
62*bbb1b6f9SApple OSS Distributions /*
63*bbb1b6f9SApple OSS Distributions  * Extent removal
64*bbb1b6f9SApple OSS Distributions  */
65*bbb1b6f9SApple OSS Distributions 
66*bbb1b6f9SApple OSS Distributions template<class _T> struct remove_extent { typedef _T type; };
67*bbb1b6f9SApple OSS Distributions template<class _T> struct remove_extent<_T[]> { typedef _T type; };
68*bbb1b6f9SApple OSS Distributions template<class _T, size_t N> struct remove_extent<_T[N]> { typedef _T type; };
69*bbb1b6f9SApple OSS Distributions template <class _T> using remove_extent_t = typename remove_extent<_T>::type;
70*bbb1b6f9SApple OSS Distributions 
71*bbb1b6f9SApple OSS Distributions 
72*bbb1b6f9SApple OSS Distributions template <class T> struct is_lvalue_reference { static constexpr bool value = false; };
73*bbb1b6f9SApple OSS Distributions template <class T> struct is_lvalue_reference<T&> { static constexpr bool value = true; };
74*bbb1b6f9SApple OSS Distributions 
75*bbb1b6f9SApple OSS Distributions /*
76*bbb1b6f9SApple OSS Distributions  * is_same
77*bbb1b6f9SApple OSS Distributions  */
78*bbb1b6f9SApple OSS Distributions 
79*bbb1b6f9SApple OSS Distributions template<class T, class U> struct is_same { static constexpr bool value = false; };
80*bbb1b6f9SApple OSS Distributions template<class T> struct is_same<T, T> { static constexpr bool value = true; };
81*bbb1b6f9SApple OSS Distributions 
82*bbb1b6f9SApple OSS Distributions /*
83*bbb1b6f9SApple OSS Distributions  * Move
84*bbb1b6f9SApple OSS Distributions  */
85*bbb1b6f9SApple OSS Distributions 
86*bbb1b6f9SApple OSS Distributions template <class _T>
87*bbb1b6f9SApple OSS Distributions inline typename remove_reference<_T>::type &&
88*bbb1b6f9SApple OSS Distributions move(_T && _t)
89*bbb1b6f9SApple OSS Distributions {
90*bbb1b6f9SApple OSS Distributions 	typedef typename os::remove_reference<_T>::type _U;
91*bbb1b6f9SApple OSS Distributions 	return static_cast<_U &&>(_t);
92*bbb1b6f9SApple OSS Distributions }
93*bbb1b6f9SApple OSS Distributions 
94*bbb1b6f9SApple OSS Distributions template <class T>
95*bbb1b6f9SApple OSS Distributions T*
96*bbb1b6f9SApple OSS Distributions move(T* first, T* last, T* d_first)
97*bbb1b6f9SApple OSS Distributions {
98*bbb1b6f9SApple OSS Distributions 	for (; first != last; ++d_first, (void)++first) {
99*bbb1b6f9SApple OSS Distributions 		*d_first = os::move(*first);
100*bbb1b6f9SApple OSS Distributions 	}
101*bbb1b6f9SApple OSS Distributions 	return d_first;
102*bbb1b6f9SApple OSS Distributions }
103*bbb1b6f9SApple OSS Distributions 
104*bbb1b6f9SApple OSS Distributions template <class T>
105*bbb1b6f9SApple OSS Distributions constexpr T && forward(os::remove_reference_t<T>&t) noexcept {
106*bbb1b6f9SApple OSS Distributions 	return static_cast<T &&>(t);
107*bbb1b6f9SApple OSS Distributions }
108*bbb1b6f9SApple OSS Distributions 
109*bbb1b6f9SApple OSS Distributions template <class T>
110*bbb1b6f9SApple OSS Distributions constexpr T && forward(os::remove_reference_t<T>&& t) noexcept {
111*bbb1b6f9SApple OSS Distributions 	static_assert(!os::is_lvalue_reference<T>::value,
112*bbb1b6f9SApple OSS Distributions 	    "can not forward an rvalue as an lvalue");
113*bbb1b6f9SApple OSS Distributions 	return static_cast<T &&>(t);
114*bbb1b6f9SApple OSS Distributions }
115*bbb1b6f9SApple OSS Distributions 
116*bbb1b6f9SApple OSS Distributions // Moves [first, last) into the range ending at d_last,
117*bbb1b6f9SApple OSS Distributions // proceeding backwards (from last to first)
118*bbb1b6f9SApple OSS Distributions // UB if d_last is within (first, last]
119*bbb1b6f9SApple OSS Distributions template <class T>
120*bbb1b6f9SApple OSS Distributions T*
121*bbb1b6f9SApple OSS Distributions move_backward(T* first, T* last, T* d_last)
122*bbb1b6f9SApple OSS Distributions {
123*bbb1b6f9SApple OSS Distributions 	while (first != last) {
124*bbb1b6f9SApple OSS Distributions 		*(--d_last) = os::move(*(--last));
125*bbb1b6f9SApple OSS Distributions 	}
126*bbb1b6f9SApple OSS Distributions 	return d_last;
127*bbb1b6f9SApple OSS Distributions }
128*bbb1b6f9SApple OSS Distributions 
129*bbb1b6f9SApple OSS Distributions template <class T>
130*bbb1b6f9SApple OSS Distributions T*
131*bbb1b6f9SApple OSS Distributions uninitialized_move(T* first, T* last, T* d_first)
132*bbb1b6f9SApple OSS Distributions {
133*bbb1b6f9SApple OSS Distributions 	for (; first != last; ++d_first, (void) ++first) {
134*bbb1b6f9SApple OSS Distributions 		::new (static_cast<void*>(d_first)) T(os::move(*first));
135*bbb1b6f9SApple OSS Distributions 	}
136*bbb1b6f9SApple OSS Distributions 	return first;
137*bbb1b6f9SApple OSS Distributions }
138*bbb1b6f9SApple OSS Distributions 
139*bbb1b6f9SApple OSS Distributions template <class T>
140*bbb1b6f9SApple OSS Distributions void
141*bbb1b6f9SApple OSS Distributions destroy(T* first, T* last)
142*bbb1b6f9SApple OSS Distributions {
143*bbb1b6f9SApple OSS Distributions 	for (; first != last; ++first) {
144*bbb1b6f9SApple OSS Distributions 		first->~T();
145*bbb1b6f9SApple OSS Distributions 	}
146*bbb1b6f9SApple OSS Distributions }
147*bbb1b6f9SApple OSS Distributions 
148*bbb1b6f9SApple OSS Distributions template <class T>
149*bbb1b6f9SApple OSS Distributions void
150*bbb1b6f9SApple OSS Distributions uninitialized_value_construct(T* first, T* last)
151*bbb1b6f9SApple OSS Distributions {
152*bbb1b6f9SApple OSS Distributions 	for (; first != last; ++first) {
153*bbb1b6f9SApple OSS Distributions 		::new (static_cast<void*>(first)) T();
154*bbb1b6f9SApple OSS Distributions 	}
155*bbb1b6f9SApple OSS Distributions }
156*bbb1b6f9SApple OSS Distributions }
157*bbb1b6f9SApple OSS Distributions 
158*bbb1b6f9SApple OSS Distributions #endif /* _OS_CPP_UTIL_H */
159