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