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