libostd/octa/utility.h

165 lines
4.3 KiB
C
Raw Normal View History

/* Utilities for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
#ifndef OCTA_UTILITY_H
#define OCTA_UTILITY_H
#include <stddef.h>
2015-04-25 17:13:21 +02:00
#include "octa/type_traits.h"
2015-04-18 17:46:44 +02:00
namespace octa {
2015-04-29 01:01:22 +02:00
/* move */
template<typename _T>
static inline constexpr RemoveReference<_T> &&move(_T &&__v) {
return static_cast<RemoveReference<_T> &&>(__v);
2015-04-18 01:11:16 +02:00
}
2015-04-29 01:01:22 +02:00
/* forward */
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &__v) {
return static_cast<_T &&>(__v);
2015-04-18 01:11:16 +02:00
}
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &&__v) {
return static_cast<_T &&>(__v);
2015-04-18 01:11:16 +02:00
}
2015-04-18 17:46:44 +02:00
2015-06-02 03:01:32 +02:00
/* exchange */
template<typename _T, typename _U = _T>
_T exchange(_T &__v, _U &&__nv) {
_T __old = move(__v);
__v = forward<_U>(__nv);
return __old;
}
2015-04-29 01:01:22 +02:00
/* declval */
template<typename _T> AddRvalueReference<_T> declval();
2015-04-18 18:19:45 +02:00
2015-04-29 01:01:22 +02:00
/* swap */
template<typename _T>
struct __OctaSwapTest {
template<typename _U, void (_U::*)(_U &)> struct __Test {};
template<typename _U> static char __test(__Test<_U, &_U::swap> *);
template<typename _U> static int __test(...);
static constexpr bool value = (sizeof(__test<_T>(0)) == sizeof(char));
};
template<typename _T> inline void __octa_swap(_T &__a, _T &__b, EnableIf<
__OctaSwapTest<_T>::value, bool
> = true) {
__a.swap(__b);
}
template<typename _T> inline void __octa_swap(_T &__a, _T &__b, EnableIf<
!__OctaSwapTest<_T>::value, bool
> = true) {
_T __c(octa::move(__a));
__a = octa::move(__b);
__b = octa::move(__c);
2015-04-18 22:46:31 +02:00
}
2015-04-29 01:01:22 +02:00
template<typename _T> void swap(_T &__a, _T &__b) {
__octa_swap(__a, __b);
}
template<typename _T, size_t _N> void swap(_T (&__a)[_N], _T (&__b)[_N]) {
for (size_t __i = 0; __i < _N; ++__i) {
octa::swap(__a[__i], __b[__i]);
2015-04-18 22:46:31 +02:00
}
}
2015-04-29 01:01:22 +02:00
/* pair */
template<typename _T, typename _U>
2015-04-29 01:01:22 +02:00
struct Pair {
_T first;
_U second;
2015-04-29 01:01:22 +02:00
Pair() = default;
2015-04-29 01:01:22 +02:00
~Pair() = default;
Pair(const Pair &) = default;
Pair(Pair &&) = default;
2015-04-29 01:01:22 +02:00
Pair(const _T &__x, const _U &__y): first(__x), second(__y) {}
2015-04-29 01:01:22 +02:00
template<typename _TT, typename _UU>
Pair(_TT &&__x, _UU &&__y):
first(octa::forward<_TT>(__x)), second(octa::forward<_UU>(__y)) {}
2015-04-29 01:01:22 +02:00
template<typename _TT, typename _UU>
Pair(const Pair<_TT, _UU> &__v): first(__v.first), second(__v.second) {}
2015-04-29 01:01:22 +02:00
template<typename _TT, typename _UU>
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;
2015-04-29 01:01:22 +02:00
return *this;
}
template<typename _TT, typename _UU>
Pair &operator=(const Pair<_TT, _UU> &__v) {
first = __v.first;
second = __v.second;
2015-04-29 01:01:22 +02:00
return *this;
}
Pair &operator=(Pair &&__v) {
first = octa::move(__v.first);
second = octa::move(__v.second);
2015-04-29 01:01:22 +02:00
return *this;
}
template<typename _TT, typename _UU>
Pair &operator=(Pair<_TT, _UU> &&__v) {
first = octa::forward<_TT>(__v.first);
second = octa::forward<_UU>(__v.second);
2015-04-29 01:01:22 +02:00
return *this;
}
void swap(Pair &__v) {
octa::swap(first, __v.first);
octa::swap(second, __v.second);
2015-04-29 01:01:22 +02:00
}
};
2015-06-02 03:01:32 +02:00
template<typename _T> struct ReferenceWrapper;
template<typename _T>
struct __OctaMakePairRetBase {
typedef _T Type;
};
template<typename _T>
struct __OctaMakePairRetBase<ReferenceWrapper<_T>> {
typedef _T &Type;
};
template<typename _T>
struct __OctaMakePairRet {
typedef typename __OctaMakePairRetBase<octa::Decay<_T>>::Type Type;
};
template<typename _T, typename _U>
Pair<typename __OctaMakePairRet<_T>::Type,
typename __OctaMakePairRet<_U>::Type
> make_pair(_T &&__a, _U &&__b) {
return Pair<typename __OctaMakePairRet<_T>::Type,
typename __OctaMakePairRet<_U>::Type
>(forward<_T>(__a), forward<_U>(__b));;
}
}
#endif