20#ifndef KERNEL_UTILITIES_CPP_H
21#define KERNEL_UTILITIES_CPP_H
32#include "pedigree/kernel/processor/types.h"
33#include "pedigree/kernel/utilities/lib.h"
34#include "pedigree/kernel/utilities/template.h"
37#include "pedigree/kernel/utilities/new"
39namespace pedigree_std {
40template <
typename T1,
typename T2>
42 static const bool value =
false;
45template <
typename T1,
typename T2>
47 static const bool value =
true;
52 static const bool value =
true;
57 static const bool value =
false;
62 static const bool value =
false;
65struct is_integral<bool> {
66 static const bool value =
true;
69struct is_integral<char> {
70 static const bool value =
true;
73struct is_integral<unsigned char> {
74 static const bool value =
true;
77struct is_integral<signed char> {
78 static const bool value =
true;
81struct is_integral<short> {
82 static const bool value =
true;
85struct is_integral<unsigned short> {
86 static const bool value =
true;
89struct is_integral<int> {
90 static const bool value =
true;
93struct is_integral<unsigned int> {
94 static const bool value =
true;
97struct is_integral<long> {
98 static const bool value =
true;
101struct is_integral<unsigned long> {
102 static const bool value =
true;
105struct is_integral<long long> {
106 static const bool value =
true;
109struct is_integral<unsigned long long> {
110 static const bool value =
true;
115 static const bool value =
false;
118struct is_pointer<T*> {
119 static const bool value =
true;
124struct remove_all_extents {
129struct remove_all_extents<T[]> {
130 typedef typename remove_all_extents<T>::type type;
133template <
class T,
size_t N>
134struct remove_all_extents<T[N]> {
135 typedef typename remove_all_extents<T>::type type;
139template <
bool,
class T =
void>
143struct enable_if<true, T> {
148template <
class T, T v>
149struct integral_constant {
150 static constexpr T value = v;
151 typedef T value_type;
152 typedef integral_constant type;
153 constexpr operator value_type() const noexcept {
158template <
bool b,
class TTrue,
class TFalse>
159struct conditional {};
161template <
class TTrue,
class TFalse>
162struct conditional<true, TTrue, TFalse> {
166template <
class TTrue,
class TFalse>
167struct conditional<false, TTrue, TFalse> {
171template <
class T, T v>
172constexpr T integral_constant<T, v>::value;
174typedef integral_constant<bool, true> true_type;
175typedef integral_constant<bool, false> false_type;
180struct is_scalar : integral_constant<bool, is_integral<T>::value || is_pointer<T>::value> {};
184struct is_trivial :
public integral_constant<bool, __is_trivial(T)> {};
188struct is_trivially_copyable
189 :
public integral_constant<bool, is_scalar<typename remove_all_extents<T>::type>::value> {};
191template <
class T,
class U>
192struct is_same : false_type {};
195struct is_same<T, T> : true_type {};
199typename enable_if<is_trivially_copyable<T>::value>::type* copy(T* dest,
const T* src,
202 return memmove(dest, src, count *
sizeof(T));
204 return MemoryCopy(dest, src, count *
sizeof(T));
210typename enable_if<!is_trivially_copyable<T>::value>::type* copy(T* dest,
const T* src,
212 const uintptr_t destAddress =
reinterpret_cast<uintptr_t
>(dest);
213 const uintptr_t srcAddress =
reinterpret_cast<uintptr_t
>(src);
214 if (overlaps(dest, src, count *
sizeof(T)) && (destAddress > srcAddress)) {
215 for (ssize_t i = count - 1; i >= 0; --i) {
219 for (
size_t i = 0; i < count; ++i) {
228struct function_traits :
public function_traits<decltype(&T::operator())> {};
230template <
class C,
class R,
class... Args>
231struct function_traits<R (C::*)(Args...) const> {
232 typedef R return_type;
233 typedef C class_type;
236template <
class R,
class... Args>
237struct function_traits<R (*)(Args...)> {
238 typedef R return_type;
241template <
class R,
class... Args>
242struct function_traits<R(Args...)> {
243 typedef R return_type;
250 Callable(
const F& x) : func(x) {}
252 template <
class... Args>
253 typename function_traits<F>::return_type operator()(Args... args) {
254 return func(args...);
263Callable<T> make_callable(T& f) {
264 return Callable<T>(f);
269struct remove_pointer {
274struct remove_pointer<T*> {
280struct remove_reference {
285struct remove_reference<T&> {
290struct remove_reference<T&&> {
295T&& forward(
typename remove_reference<T>::type& a)
noexcept {
296 return static_cast<T&&
>(a);
300T&& forward(
typename remove_reference<T>::type&& a)
noexcept {
301 return static_cast<T&&
>(a);
306typename remove_reference<T>::type&& move(T&& a) {
307 return static_cast<typename remove_reference<T>::type&&
>(a);
312const T& min(
const T& a,
const T& b) {
313 return a <= b ? a : b;
318const T& max(
const T& a,
const T& b) {
319 return a >= b ? a : b;
324using pedigree_std::is_integral;
325using pedigree_std::is_pointer;