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