libostd/octa/utility.h

125 lines
3.0 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 */
2015-04-18 01:11:16 +02:00
template<typename T>
static inline constexpr RemoveReference<T> &&
2015-04-18 01:11:16 +02:00
move(T &&v) noexcept {
return static_cast<RemoveReference<T> &&>(v);
2015-04-18 01:11:16 +02:00
}
2015-04-29 01:01:22 +02:00
/* forward */
2015-04-18 01:11:16 +02:00
template<typename T>
static inline constexpr T &&
forward(RemoveReference<T> &v) noexcept {
2015-04-18 01:11:16 +02:00
return static_cast<T &&>(v);
}
template<typename T>
static inline constexpr T &&
forward(RemoveReference<T> &&v) noexcept {
2015-04-18 01:11:16 +02:00
return static_cast<T &&>(v);
}
2015-04-18 17:46:44 +02:00
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 */
2015-04-28 19:48:58 +02:00
template<typename T> void swap(T &a, T &b)
noexcept(IsNothrowMoveConstructible<T>::value
&& IsNothrowMoveAssignable<T>::value) {
2015-04-18 22:46:31 +02:00
T c(move(a));
a = move(b);
b = move(c);
}
2015-04-29 01:01:22 +02:00
2015-04-28 19:48:58 +02:00
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N])
noexcept(noexcept(swap(*a, *b))) {
2015-04-18 22:46:31 +02:00
for (size_t i = 0; i < N; ++i) {
swap(a[i], b[i]);
}
}
2015-04-29 01:01:22 +02:00
/* 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(forward<TT>(x)), second(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(move(v.first)), second(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) noexcept(
IsNothrowMoveAssignable<T>::value
&& IsNothrowMoveAssignable<U>::value
) {
first = move(v.first);
second = move(v.second);
return *this;
}
template<typename TT, typename UU>
Pair &operator=(Pair<TT, UU> &&v) {
first = forward<TT>(v.first);
second = forward<UU>(v.second);
return *this;
}
void swap(Pair &v) noexcept(
noexcept(octa::swap(first, v.first))
&& noexcept(octa::swap(second, v.second))
) {
octa::swap(first, v.first);
octa::swap(second, v.second);
}
};
template<typename T, typename U>
void swap(Pair<T, U> &a, Pair<T, U> &b) noexcept(noexcept(a.swap(b))) {
a.swap(b);
}
}
#endif