The Pedigree Project 0.1
cpp.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_UTILITIES_CPP_H
21#define KERNEL_UTILITIES_CPP_H
22#include <config.h>
23
24// IWYU pragma: private, include "pedigree/kernel/utilities/utility.h"
25
26#ifdef __cplusplus
27
28#if UTILITY_LINUX
29#include <string.h>
30#endif
31
32#include "pedigree/kernel/processor/types.h"
33#include "pedigree/kernel/utilities/lib.h"
34#include "pedigree/kernel/utilities/template.h" // IWYU pragma: export
35
36// Include our custom allocation operator declarations.
37#include "pedigree/kernel/utilities/new" // IWYU pragma: export
38
39namespace pedigree_std {
40template <typename T1, typename T2>
41struct is {
42 static const bool value = false;
43};
44
45template <typename T1, typename T2>
46struct is_not {
47 static const bool value = true;
48};
49
50template <typename T>
51struct is<T, T> {
52 static const bool value = true;
53};
54
55template <typename T>
56struct is_not<T, T> {
57 static const bool value = false;
58};
59
60template <typename T>
61struct is_integral {
62 static const bool value = false;
63};
64template <>
65struct is_integral<bool> {
66 static const bool value = true;
67};
68template <>
69struct is_integral<char> {
70 static const bool value = true;
71};
72template <>
73struct is_integral<unsigned char> {
74 static const bool value = true;
75};
76template <>
77struct is_integral<signed char> {
78 static const bool value = true;
79};
80template <>
81struct is_integral<short> {
82 static const bool value = true;
83};
84template <>
85struct is_integral<unsigned short> {
86 static const bool value = true;
87};
88template <>
89struct is_integral<int> {
90 static const bool value = true;
91};
92template <>
93struct is_integral<unsigned int> {
94 static const bool value = true;
95};
96template <>
97struct is_integral<long> {
98 static const bool value = true;
99};
100template <>
101struct is_integral<unsigned long> {
102 static const bool value = true;
103};
104template <>
105struct is_integral<long long> {
106 static const bool value = true;
107};
108template <>
109struct is_integral<unsigned long long> {
110 static const bool value = true;
111};
112
113template <typename T>
114struct is_pointer {
115 static const bool value = false;
116};
117template <typename T>
118struct is_pointer<T*> {
119 static const bool value = true;
120};
121
123template <class T>
124struct remove_all_extents {
125 typedef T type;
126};
127
128template <class T>
129struct remove_all_extents<T[]> {
130 typedef typename remove_all_extents<T>::type type;
131};
132
133template <class T, size_t N>
134struct remove_all_extents<T[N]> {
135 typedef typename remove_all_extents<T>::type type;
136};
137
139template <bool, class T = void>
140struct enable_if {};
141
142template <class T>
143struct enable_if<true, T> {
144 typedef T type;
145};
146
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 {
154 return value;
155 }
156};
157
158template <bool b, class TTrue, class TFalse>
159struct conditional {};
160
161template <class TTrue, class TFalse>
162struct conditional<true, TTrue, TFalse> {
163 typedef TTrue type;
164};
165
166template <class TTrue, class TFalse>
167struct conditional<false, TTrue, TFalse> {
168 typedef TFalse type;
169};
170
171template <class T, T v>
172constexpr T integral_constant<T, v>::value;
173
174typedef integral_constant<bool, true> true_type;
175typedef integral_constant<bool, false> false_type;
176
179template <class T>
180struct is_scalar : integral_constant<bool, is_integral<T>::value || is_pointer<T>::value> {};
181
183template <class T>
184struct is_trivial : public integral_constant<bool, __is_trivial(T)> {};
185
187template <class T>
188struct is_trivially_copyable
189 : public integral_constant<bool, is_scalar<typename remove_all_extents<T>::type>::value> {};
190
191template <class T, class U>
192struct is_same : false_type {};
193
194template <class T>
195struct is_same<T, T> : true_type {};
196
198template <class T>
199typename enable_if<is_trivially_copyable<T>::value>::type* copy(T* dest, const T* src,
200 size_t count) {
201#if UTILITY_LINUX
202 return memmove(dest, src, count * sizeof(T));
203#else
204 return MemoryCopy(dest, src, count * sizeof(T));
205#endif
206}
207
209template <class T>
210typename enable_if<!is_trivially_copyable<T>::value>::type* copy(T* dest, const T* src,
211 size_t count) {
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) {
216 dest[i] = src[i];
217 }
218 } else {
219 for (size_t i = 0; i < count; ++i) {
220 dest[i] = src[i];
221 }
222 }
223 return dest;
224}
225
227template <class T>
228struct function_traits : public function_traits<decltype(&T::operator())> {};
229
230template <class C, class R, class... Args>
231struct function_traits<R (C::*)(Args...) const> {
232 typedef R return_type;
233 typedef C class_type;
234};
235
236template <class R, class... Args>
237struct function_traits<R (*)(Args...)> {
238 typedef R return_type;
239};
240
241template <class R, class... Args>
242struct function_traits<R(Args...)> {
243 typedef R return_type;
244};
245
247template <class F>
248class Callable {
249 public:
250 Callable(const F& x) : func(x) {}
251
252 template <class... Args>
253 typename function_traits<F>::return_type operator()(Args... args) {
254 return func(args...);
255 }
256
257 private:
258 const F& func;
259};
260
262template <class T>
263Callable<T> make_callable(T& f) {
264 return Callable<T>(f);
265}
266
268template <class T>
269struct remove_pointer {
270 typedef T type;
271};
272
273template <class T>
274struct remove_pointer<T*> {
275 typedef T type;
276};
277
279template <class T>
280struct remove_reference {
281 typedef T type;
282};
283
284template <class T>
285struct remove_reference<T&> {
286 typedef T type;
287};
288
289template <class T>
290struct remove_reference<T&&> {
291 typedef T type;
292};
293
294template <class T>
295T&& forward(typename remove_reference<T>::type& a) noexcept {
296 return static_cast<T&&>(a);
297}
298
299template <class T>
300T&& forward(typename remove_reference<T>::type&& a) noexcept {
301 return static_cast<T&&>(a);
302}
303
305template <class T>
306typename remove_reference<T>::type&& move(T&& a) {
307 return static_cast<typename remove_reference<T>::type&&>(a);
308}
309
311template <class T>
312const T& min(const T& a, const T& b) {
313 return a <= b ? a : b;
314}
315
317template <class T>
318const T& max(const T& a, const T& b) {
319 return a >= b ? a : b;
320}
321
322} // namespace pedigree_std
323
324using pedigree_std::is_integral;
325using pedigree_std::is_pointer;
326
327#endif // __cplusplus
328
329#endif // KERNEL_UTILITIES_CPP_H