diff --git a/octa/types.h b/octa/types.h index a2944f8..942e9a4 100644 --- a/octa/types.h +++ b/octa/types.h @@ -10,23 +10,25 @@ #include namespace octa { - typedef signed char schar; - typedef unsigned char uchar; - typedef unsigned short ushort; - typedef unsigned int uint; - typedef unsigned long ulong; - typedef unsigned long long ullong; - typedef long long llong; - typedef long double ldouble; +typedef signed char schar; +typedef unsigned char uchar; +typedef unsigned short ushort; +typedef unsigned int uint; +typedef unsigned long ulong; +typedef unsigned long long ullong; +typedef long long llong; - typedef decltype(nullptr) nullptr_t; +typedef long double ldouble; + +typedef decltype(nullptr) nullptr_t; #if defined(__CLANG_MAX_ALIGN_T_DEFINED) || defined(_GCC_MAX_ALIGN_T) - using ::max_align_t; +using ::max_align_t; #else - typedef long double max_align_t; +typedef long double max_align_t; #endif + } using octa::schar; diff --git a/octa/utility.h b/octa/utility.h index 8b3429e..f78fc40 100644 --- a/octa/utility.h +++ b/octa/utility.h @@ -11,155 +11,161 @@ #include "octa/type_traits.h" namespace octa { - /* move */ +/* move */ + +template +static inline constexpr RemoveReference<_T> &&move(_T &&v) { + return static_cast &&>(v); +} + +/* forward */ + +template +static inline constexpr _T &&forward(RemoveReference<_T> &v) { + return static_cast<_T &&>(v); +} + +template +static inline constexpr _T &&forward(RemoveReference<_T> &&v) { + return static_cast<_T &&>(v); +} + +/* exchange */ + +template +_T exchange(_T &v, _U &&nv) { + _T old = move(v); + v = forward<_U>(nv); + return old; +} + +/* declval */ + +template AddRvalueReference<_T> declval(); + +/* swap */ + +namespace detail { template - static inline constexpr RemoveReference<_T> &&move(_T &&__v) { - return static_cast &&>(__v); - } - - /* forward */ - - template - static inline constexpr _T &&forward(RemoveReference<_T> &__v) { - return static_cast<_T &&>(__v); - } - - template - static inline constexpr _T &&forward(RemoveReference<_T> &&__v) { - return static_cast<_T &&>(__v); - } - - /* exchange */ - - template - _T exchange(_T &__v, _U &&__nv) { - _T __old = move(__v); - __v = forward<_U>(__nv); - return __old; - } - - /* declval */ - - template AddRvalueReference<_T> declval(); - - /* swap */ - - template - struct __OctaSwapTest { - template struct __Test {}; - template static char __test(__Test<_U, &_U::swap> *); - template static int __test(...); - static constexpr bool value = (sizeof(__test<_T>(0)) == sizeof(char)); + struct SwapTest { + template struct Test {}; + template static char test(Test<_U, &_U::swap> *); + template static int test(...); + static constexpr bool value = (sizeof(test<_T>(0)) == sizeof(char)); }; - template inline void __octa_swap(_T &__a, _T &__b, EnableIf< - __OctaSwapTest<_T>::value, bool + template inline void swap(_T &a, _T &b, EnableIf< + octa::detail::SwapTest<_T>::value, bool > = true) { - __a.swap(__b); + a.swap(b); } - template inline void __octa_swap(_T &__a, _T &__b, EnableIf< - !__OctaSwapTest<_T>::value, bool + template inline void swap(_T &a, _T &b, EnableIf< + !octa::detail::SwapTest<_T>::value, bool > = true) { - _T __c(octa::move(__a)); - __a = octa::move(__b); - __b = octa::move(__c); + _T c(octa::move(a)); + a = octa::move(b); + b = octa::move(c); + } +} + +template void swap(_T &a, _T &b) { + octa::detail::swap(a, b); +} + +template void swap(_T (&a)[_N], _T (&b)[_N]) { + for (size_t i = 0; i < _N; ++i) { + octa::swap(a[i], b[i]); + } +} + +/* pair */ + +template +struct Pair { + _T first; + _U second; + + Pair() = default; + ~Pair() = default; + + Pair(const Pair &) = default; + Pair(Pair &&) = default; + + Pair(const _T &x, const _U &y): first(x), second(y) {} + + template + Pair(_TT &&x, _UU &&y): + first(octa::forward<_TT>(x)), second(octa::forward<_UU>(y)) {} + + template + Pair(const Pair<_TT, _UU> &v): first(v.first), second(v.second) {} + + template + Pair(Pair<_TT, _UU> &&v): + first(octa::move(v.first)), second(octa::move(v.second)) {} + + Pair &operator=(const Pair &v) { + first = v.first; + second = v.second; + return *this; } - template void swap(_T &__a, _T &__b) { - __octa_swap(__a, __b); + template + Pair &operator=(const Pair<_TT, _UU> &v) { + first = v.first; + second = v.second; + return *this; } - template void swap(_T (&__a)[_N], _T (&__b)[_N]) { - for (size_t __i = 0; __i < _N; ++__i) { - octa::swap(__a[__i], __b[__i]); - } + Pair &operator=(Pair &&v) { + first = octa::move(v.first); + second = octa::move(v.second); + return *this; } - /* pair */ + template + Pair &operator=(Pair<_TT, _UU> &&v) { + first = octa::forward<_TT>(v.first); + second = octa::forward<_UU>(v.second); + return *this; + } - template - struct Pair { - _T first; - _U second; + void swap(Pair &v) { + octa::swap(first, v.first); + octa::swap(second, v.second); + } +}; - Pair() = default; - ~Pair() = default; - - Pair(const Pair &) = default; - Pair(Pair &&) = default; - - Pair(const _T &__x, const _U &__y): first(__x), second(__y) {} - - template - Pair(_TT &&__x, _UU &&__y): - first(octa::forward<_TT>(__x)), second(octa::forward<_UU>(__y)) {} - - template - Pair(const Pair<_TT, _UU> &__v): first(__v.first), second(__v.second) {} - - template - Pair(Pair<_TT, _UU> &&__v): - first(octa::move(__v.first)), second(octa::move(__v.second)) {} - - Pair &operator=(const Pair &__v) { - first = __v.first; - second = __v.second; - return *this; - } - - template - Pair &operator=(const Pair<_TT, _UU> &__v) { - first = __v.first; - second = __v.second; - return *this; - } - - Pair &operator=(Pair &&__v) { - first = octa::move(__v.first); - second = octa::move(__v.second); - return *this; - } - - template - Pair &operator=(Pair<_TT, _UU> &&__v) { - first = octa::forward<_TT>(__v.first); - second = octa::forward<_UU>(__v.second); - return *this; - } - - void swap(Pair &__v) { - octa::swap(first, __v.first); - octa::swap(second, __v.second); - } - }; - - template struct ReferenceWrapper; +template struct ReferenceWrapper; +namespace detail { template - struct __OctaMakePairRetBase { + struct MakePairRetBase { typedef _T Type; }; template - struct __OctaMakePairRetBase> { + struct MakePairRetBase> { typedef _T &Type; }; template - struct __OctaMakePairRet { - typedef typename __OctaMakePairRetBase>::Type Type; + struct MakePairRet { + typedef typename octa::detail::MakePairRetBase>::Type Type; }; +} /* namespace detail */ - template - Pair::Type, - typename __OctaMakePairRet<_U>::Type - > make_pair(_T &&__a, _U &&__b) { - return Pair::Type, - typename __OctaMakePairRet<_U>::Type - >(forward<_T>(__a), forward<_U>(__b));; - } +template +Pair::Type, + typename octa::detail::MakePairRet<_U>::Type + > make_pair(_T &&a, _U &&b) { + return Pair::Type, + typename octa::detail::MakePairRet<_U>::Type + >(forward<_T>(a), forward<_U>(b));; } +} /* namespace octa */ + #endif \ No newline at end of file