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