2015-04-15 21:38:15 +00:00
|
|
|
/* 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 15:13:21 +00:00
|
|
|
#include "octa/type_traits.h"
|
2015-04-18 15:46:44 +00:00
|
|
|
|
2015-04-23 19:53:05 +00:00
|
|
|
namespace octa {
|
2015-04-28 23:01:22 +00:00
|
|
|
/* move */
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T>
|
|
|
|
static inline constexpr RemoveReference<_T> &&move(_T &&__v) {
|
|
|
|
return static_cast<RemoveReference<_T> &&>(__v);
|
2015-04-17 23:11:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 23:01:22 +00:00
|
|
|
/* forward */
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T>
|
|
|
|
static inline constexpr _T &&forward(RemoveReference<_T> &__v) {
|
|
|
|
return static_cast<_T &&>(__v);
|
2015-04-17 23:11:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T>
|
|
|
|
static inline constexpr _T &&forward(RemoveReference<_T> &&__v) {
|
|
|
|
return static_cast<_T &&>(__v);
|
2015-04-17 23:11:16 +00:00
|
|
|
}
|
2015-04-18 15:46:44 +00:00
|
|
|
|
2015-06-02 01:01:32 +00: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-28 23:01:22 +00:00
|
|
|
/* declval */
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T> AddRvalueReference<_T> declval();
|
2015-04-18 16:19:45 +00:00
|
|
|
|
2015-04-28 23:01:22 +00:00
|
|
|
/* swap */
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T>
|
2015-05-26 20:28:12 +00:00
|
|
|
struct __OctaSwapTest {
|
2015-06-01 23:57:34 +00:00
|
|
|
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));
|
2015-05-26 20:28:12 +00:00
|
|
|
};
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T> inline void __octa_swap(_T &__a, _T &__b, EnableIf<
|
|
|
|
__OctaSwapTest<_T>::value, bool
|
2015-05-26 20:28:12 +00:00
|
|
|
> = true) {
|
2015-06-01 23:57:34 +00:00
|
|
|
__a.swap(__b);
|
2015-05-26 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T> inline void __octa_swap(_T &__a, _T &__b, EnableIf<
|
|
|
|
!__OctaSwapTest<_T>::value, bool
|
2015-05-26 20:28:12 +00:00
|
|
|
> = true) {
|
2015-06-01 23:57:34 +00:00
|
|
|
_T __c(octa::move(__a));
|
|
|
|
__a = octa::move(__b);
|
|
|
|
__b = octa::move(__c);
|
2015-04-18 20:46:31 +00:00
|
|
|
}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T> void swap(_T &__a, _T &__b) {
|
|
|
|
__octa_swap(__a, __b);
|
2015-05-26 20:28:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
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 20:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
|
|
|
/* pair */
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T, typename _U>
|
2015-04-28 23:01:22 +00:00
|
|
|
struct Pair {
|
2015-06-01 23:57:34 +00:00
|
|
|
_T first;
|
|
|
|
_U second;
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-05-20 21:05:41 +00:00
|
|
|
Pair() = default;
|
2015-04-28 23:01:22 +00:00
|
|
|
~Pair() = default;
|
|
|
|
|
2015-05-20 21:05:41 +00:00
|
|
|
Pair(const Pair &) = default;
|
|
|
|
Pair(Pair &&) = default;
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
Pair(const _T &__x, const _U &__y): first(__x), second(__y) {}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _TT, typename _UU>
|
|
|
|
Pair(_TT &&__x, _UU &&__y):
|
|
|
|
first(octa::forward<_TT>(__x)), second(octa::forward<_UU>(__y)) {}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _TT, typename _UU>
|
|
|
|
Pair(const Pair<_TT, _UU> &__v): first(__v.first), second(__v.second) {}
|
2015-04-28 23:01:22 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _TT, typename _UU>
|
|
|
|
Pair(Pair<_TT, _UU> &&__v):
|
|
|
|
first(octa::move(__v.first)), second(octa::move(__v.second)) {}
|
2015-05-20 21:05:41 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
Pair &operator=(const Pair &__v) {
|
|
|
|
first = __v.first;
|
|
|
|
second = __v.second;
|
2015-04-28 23:01:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _TT, typename _UU>
|
|
|
|
Pair &operator=(const Pair<_TT, _UU> &__v) {
|
|
|
|
first = __v.first;
|
|
|
|
second = __v.second;
|
2015-04-28 23:01:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
Pair &operator=(Pair &&__v) {
|
|
|
|
first = octa::move(__v.first);
|
|
|
|
second = octa::move(__v.second);
|
2015-04-28 23:01:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
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-28 23:01:22 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
void swap(Pair &__v) {
|
|
|
|
octa::swap(first, __v.first);
|
|
|
|
octa::swap(second, __v.second);
|
2015-04-28 23:01:22 +00:00
|
|
|
}
|
|
|
|
};
|
2015-06-02 01:01:32 +00: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));;
|
|
|
|
}
|
2015-04-15 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|