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