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