libostd/octa/utility.h

96 lines
2.9 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>
/* must be in std namespace otherwise the compiler won't know about it */
namespace std {
template<typename T>
class initializer_list {
const T *p_buf;
size_t p_len;
initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {}
public:
struct type {
typedef T value;
typedef T &reference;
typedef const T &const_reference;
typedef T *pointer;
typedef const T *const_pointer;
typedef size_t size;
typedef ptrdiff_t difference;
};
2015-04-15 23:41:32 +02:00
initializer_list(): p_buf(nullptr), p_len(0) {}
size_t length() const { return p_len; }
const T *get() const { return p_buf; }
};
}
namespace octa {
template<typename T> using InitializerList = std::initializer_list<T>;
2015-04-18 01:07:37 +02:00
template<typename T> void swap(T &a, T &b) {
T c(move(a));
a = move(b);
b = move(c);
}
template<typename T, size_t N> void swap(T (&a)[N], T (&b)[N]) {
for (size_t i = 0; i < N; ++i) {
swap(a[i], b[i]);
}
}
2015-04-18 01:11:16 +02:00
2015-04-18 17:46:44 +02:00
/* aliased in traits.h later */
namespace internal {
template<typename T> struct RemoveReference { typedef T type; };
template<typename T> struct RemoveReference<T&> { typedef T type; };
template<typename T> struct RemoveReference<T&&> { typedef T type; };
template<typename T> struct AddRvalueReference { typedef T &&type; };
template<typename T> struct AddRvalueReference<T &> { typedef T &&type; };
template<typename T> struct AddRvalueReference<T &&> { typedef T &&type; };
template<> struct AddRvalueReference<void> {
typedef void type;
};
template<> struct AddRvalueReference<const void> {
typedef const void type;
};
template<> struct AddRvalueReference<volatile void> {
typedef volatile void type;
};
template<> struct AddRvalueReference<const volatile void> {
typedef const volatile void type;
};
}
2015-04-18 01:11:16 +02:00
template<typename T>
2015-04-18 17:46:44 +02:00
static inline constexpr typename internal::RemoveReference<T>::type &&
2015-04-18 01:11:16 +02:00
move(T &&v) noexcept {
2015-04-18 17:46:44 +02:00
return static_cast<typename internal::RemoveReference<T>::type &&>(v);
2015-04-18 01:11:16 +02:00
}
template<typename T>
static inline constexpr T &&
2015-04-18 17:46:44 +02:00
forward(typename internal::RemoveReference<T>::type &v) noexcept {
2015-04-18 01:11:16 +02:00
return static_cast<T &&>(v);
}
template<typename T>
static inline constexpr T &&
2015-04-18 17:46:44 +02:00
forward(typename internal::RemoveReference<T>::type &&v) noexcept {
2015-04-18 01:11:16 +02:00
return static_cast<T &&>(v);
}
2015-04-18 17:46:44 +02:00
template<typename T> typename internal::AddRvalueReference<T>::type declval();
}
#endif