libostd/octa/utility.h

171 lines
3.8 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
2015-06-04 00:01:23 +02:00
/* move */
2015-04-18 01:11:16 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
static inline constexpr RemoveReference<T> &&move(T &&v) {
return static_cast<RemoveReference<T> &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-04-29 01:01:22 +02:00
2015-06-04 00:01:23 +02:00
/* forward */
2015-04-18 01:11:16 +02:00
2015-06-04 23:57:06 +02:00
template<typename T>
static inline constexpr T &&forward(RemoveReference<T> &v) {
return static_cast<T &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename T>
static inline constexpr T &&forward(RemoveReference<T> &&v) {
return static_cast<T &&>(v);
2015-06-04 00:01:23 +02:00
}
2015-04-18 17:46:44 +02:00
2015-06-04 00:01:23 +02:00
/* exchange */
2015-06-02 03:01:32 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, typename U = T>
T exchange(T &v, U &&nv) {
T old = move(v);
v = forward<U>(nv);
2015-06-04 00:01:23 +02:00
return old;
}
2015-06-02 03:01:32 +02:00
2015-06-04 00:01:23 +02:00
/* declval */
2015-04-29 01:01:22 +02:00
2015-06-04 23:57:06 +02:00
template<typename T> AddRvalueReference<T> declval();
2015-04-18 18:19:45 +02:00
2015-06-04 00:01:23 +02:00
/* swap */
2015-04-29 01:01:22 +02:00
2015-06-04 00:01:23 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 00:01:23 +02:00
struct SwapTest {
2015-06-04 23:57:06 +02: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-06-04 23:57:06 +02:00
template<typename T> inline void swap(T &a, T &b, EnableIf<
octa::detail::SwapTest<T>::value, bool
> = true) {
2015-06-04 00:01:23 +02:00
a.swap(b);
}
2015-06-04 23:57:06 +02:00
template<typename T> inline void swap(T &a, T &b, EnableIf<
!octa::detail::SwapTest<T>::value, bool
> = true) {
2015-06-04 23:57:06 +02:00
T c(octa::move(a));
2015-06-04 00:01:23 +02:00
a = octa::move(b);
b = octa::move(c);
2015-04-18 22:46:31 +02:00
}
2015-06-04 00:01:23 +02:00
}
2015-04-29 01:01:22 +02:00
2015-06-04 23:57:06 +02:00
template<typename T> void swap(T &a, T &b) {
2015-06-04 00:01:23 +02:00
octa::detail::swap(a, b);
}
template<typename T, octa::Size N> void swap(T (&a)[N], T (&b)[N]) {
for (octa::Size i = 0; i < N; ++i) {
2015-06-04 00:01:23 +02:00
octa::swap(a[i], b[i]);
}
2015-06-04 00:01:23 +02:00
}
/* pair */
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
2015-06-04 00:01:23 +02:00
struct Pair {
2015-06-04 23:57:06 +02:00
T first;
U second;
2015-06-04 00:01:23 +02:00
Pair() = default;
~Pair() = default;
Pair(const Pair &) = default;
Pair(Pair &&) = default;
2015-06-04 23:57:06 +02:00
Pair(const T &x, const U &y): first(x), second(y) {}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair(TT &&x, UU &&y):
first(octa::forward<TT>(x)), second(octa::forward<UU>(y)) {}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair(const Pair<TT, UU> &v): first(v.first), second(v.second) {}
2015-06-04 00:01:23 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair(Pair<TT, UU> &&v):
2015-06-04 00:01:23 +02:00
first(octa::move(v.first)), second(octa::move(v.second)) {}
Pair &operator=(const Pair &v) {
first = v.first;
second = v.second;
return *this;
2015-04-18 22:46:31 +02:00
}
2015-04-29 01:01:22 +02:00
2015-06-04 23:57:06 +02:00
template<typename TT, typename UU>
Pair &operator=(const Pair<TT, UU> &v) {
2015-06-04 00:01:23 +02:00
first = v.first;
second = v.second;
return *this;
}
2015-06-02 03:01:32 +02:00
2015-06-04 00:01:23 +02:00
Pair &operator=(Pair &&v) {
first = octa::move(v.first);
second = octa::move(v.second);
return *this;
}
2015-06-02 03:01:32 +02:00
2015-06-04 23:57:06 +02: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-06-04 00:01:23 +02:00
return *this;
}
void swap(Pair &v) {
octa::swap(first, v.first);
octa::swap(second, v.second);
}
};
2015-06-04 23:57:06 +02:00
template<typename T> struct ReferenceWrapper;
2015-06-04 00:01:23 +02:00
namespace detail {
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 00:01:23 +02:00
struct MakePairRetBase {
2015-06-08 01:55:08 +02:00
using Type = T;
2015-06-02 03:01:32 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
struct MakePairRetBase<ReferenceWrapper<T>> {
2015-06-08 01:55:08 +02:00
using Type = T &;
2015-06-02 03:01:32 +02:00
};
2015-06-04 23:57:06 +02:00
template<typename T>
2015-06-04 00:01:23 +02:00
struct MakePairRet {
2015-06-08 01:55:08 +02:00
using Type = typename octa::detail::MakePairRetBase<octa::Decay<T>>::Type;
2015-06-02 03:01:32 +02:00
};
2015-06-04 00:01:23 +02:00
} /* namespace detail */
2015-06-04 23:57:06 +02:00
template<typename T, typename U>
Pair<typename octa::detail::MakePairRet<T>::Type,
typename octa::detail::MakePairRet<U>::Type
> make_pair(T &&a, U &&b) {
return Pair<typename octa::detail::MakePairRet<T>::Type,
typename octa::detail::MakePairRet<U>::Type
>(forward<T>(a), forward<U>(b));;
}
2015-06-04 00:01:23 +02:00
} /* namespace octa */
#endif