deuglify utility.h

master
Daniel Kolesa 2015-06-03 23:01:23 +01:00
parent e599a7f67a
commit 5a11390903
2 changed files with 140 additions and 132 deletions

View File

@ -10,23 +10,25 @@
#include <stddef.h> #include <stddef.h>
namespace octa { 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) #if defined(__CLANG_MAX_ALIGN_T_DEFINED) || defined(_GCC_MAX_ALIGN_T)
using ::max_align_t; using ::max_align_t;
#else #else
typedef long double max_align_t; typedef long double max_align_t;
#endif #endif
} }
using octa::schar; using octa::schar;

View File

@ -11,155 +11,161 @@
#include "octa/type_traits.h" #include "octa/type_traits.h"
namespace octa { namespace octa {
/* move */
/* move */
template<typename _T>
static inline constexpr RemoveReference<_T> &&move(_T &&v) {
return static_cast<RemoveReference<_T> &&>(v);
}
/* forward */
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &v) {
return static_cast<_T &&>(v);
}
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &&v) {
return static_cast<_T &&>(v);
}
/* exchange */
template<typename _T, typename _U = _T>
_T exchange(_T &v, _U &&nv) {
_T old = move(v);
v = forward<_U>(nv);
return old;
}
/* declval */
template<typename _T> AddRvalueReference<_T> declval();
/* swap */
namespace detail {
template<typename _T> template<typename _T>
static inline constexpr RemoveReference<_T> &&move(_T &&__v) { struct SwapTest {
return static_cast<RemoveReference<_T> &&>(__v); template<typename _U, void (_U::*)(_U &)> struct Test {};
} template<typename _U> static char test(Test<_U, &_U::swap> *);
template<typename _U> static int test(...);
/* forward */ static constexpr bool value = (sizeof(test<_T>(0)) == sizeof(char));
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &__v) {
return static_cast<_T &&>(__v);
}
template<typename _T>
static inline constexpr _T &&forward(RemoveReference<_T> &&__v) {
return static_cast<_T &&>(__v);
}
/* exchange */
template<typename _T, typename _U = _T>
_T exchange(_T &__v, _U &&__nv) {
_T __old = move(__v);
__v = forward<_U>(__nv);
return __old;
}
/* declval */
template<typename _T> AddRvalueReference<_T> declval();
/* 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< template<typename _T> inline void swap(_T &a, _T &b, EnableIf<
__OctaSwapTest<_T>::value, bool octa::detail::SwapTest<_T>::value, bool
> = true) { > = true) {
__a.swap(__b); a.swap(b);
} }
template<typename _T> inline void __octa_swap(_T &__a, _T &__b, EnableIf< template<typename _T> inline void swap(_T &a, _T &b, EnableIf<
!__OctaSwapTest<_T>::value, bool !octa::detail::SwapTest<_T>::value, bool
> = true) { > = true) {
_T __c(octa::move(__a)); _T c(octa::move(a));
__a = octa::move(__b); a = octa::move(b);
__b = octa::move(__c); b = octa::move(c);
}
}
template<typename _T> void swap(_T &a, _T &b) {
octa::detail::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]);
}
}
/* pair */
template<typename _T, typename _U>
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<typename _TT, typename _UU>
Pair(_TT &&x, _UU &&y):
first(octa::forward<_TT>(x)), second(octa::forward<_UU>(y)) {}
template<typename _TT, typename _UU>
Pair(const Pair<_TT, _UU> &v): first(v.first), second(v.second) {}
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;
return *this;
} }
template<typename _T> void swap(_T &__a, _T &__b) { template<typename _TT, typename _UU>
__octa_swap(__a, __b); Pair &operator=(const Pair<_TT, _UU> &v) {
first = v.first;
second = v.second;
return *this;
} }
template<typename _T, size_t _N> void swap(_T (&__a)[_N], _T (&__b)[_N]) { Pair &operator=(Pair &&v) {
for (size_t __i = 0; __i < _N; ++__i) { first = octa::move(v.first);
octa::swap(__a[__i], __b[__i]); second = octa::move(v.second);
} return *this;
} }
/* pair */ template<typename _TT, typename _UU>
Pair &operator=(Pair<_TT, _UU> &&v) {
first = octa::forward<_TT>(v.first);
second = octa::forward<_UU>(v.second);
return *this;
}
template<typename _T, typename _U> void swap(Pair &v) {
struct Pair { octa::swap(first, v.first);
_T first; octa::swap(second, v.second);
_U second; }
};
Pair() = default; template<typename _T> struct ReferenceWrapper;
~Pair() = default;
Pair(const Pair &) = default;
Pair(Pair &&) = default;
Pair(const _T &__x, const _U &__y): first(__x), second(__y) {}
template<typename _TT, typename _UU>
Pair(_TT &&__x, _UU &&__y):
first(octa::forward<_TT>(__x)), second(octa::forward<_UU>(__y)) {}
template<typename _TT, typename _UU>
Pair(const Pair<_TT, _UU> &__v): first(__v.first), second(__v.second) {}
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;
return *this;
}
template<typename _TT, typename _UU>
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<typename _TT, typename _UU>
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<typename _T> struct ReferenceWrapper;
namespace detail {
template<typename _T> template<typename _T>
struct __OctaMakePairRetBase { struct MakePairRetBase {
typedef _T Type; typedef _T Type;
}; };
template<typename _T> template<typename _T>
struct __OctaMakePairRetBase<ReferenceWrapper<_T>> { struct MakePairRetBase<ReferenceWrapper<_T>> {
typedef _T &Type; typedef _T &Type;
}; };
template<typename _T> template<typename _T>
struct __OctaMakePairRet { struct MakePairRet {
typedef typename __OctaMakePairRetBase<octa::Decay<_T>>::Type Type; typedef typename octa::detail::MakePairRetBase<octa::Decay<_T>>::Type Type;
}; };
} /* namespace detail */
template<typename _T, typename _U> template<typename _T, typename _U>
Pair<typename __OctaMakePairRet<_T>::Type, Pair<typename octa::detail::MakePairRet<_T>::Type,
typename __OctaMakePairRet<_U>::Type typename octa::detail::MakePairRet<_U>::Type
> make_pair(_T &&__a, _U &&__b) { > make_pair(_T &&a, _U &&b) {
return Pair<typename __OctaMakePairRet<_T>::Type, return Pair<typename octa::detail::MakePairRet<_T>::Type,
typename __OctaMakePairRet<_U>::Type typename octa::detail::MakePairRet<_U>::Type
>(forward<_T>(__a), forward<_U>(__b));; >(forward<_T>(a), forward<_U>(b));;
}
} }
} /* namespace octa */
#endif #endif