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