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 
23 // IWYU pragma: private, include "pedigree/kernel/utilities/utility.h"
24 
25 #ifdef __cplusplus
26 
27 #ifdef UTILITY_LINUX
28 #include <string.h>
29 #endif
30 
31 #include "pedigree/kernel/processor/types.h"
32 #include "pedigree/kernel/utilities/lib.h"
33 #include "pedigree/kernel/utilities/template.h" // IWYU pragma: export
34 
35 // Include our custom <new> header which defines all operators as noexcept
36 #include "pedigree/kernel/utilities/new" // IWYU pragma: export
37 
38 namespace pedigree_std
39 {
40 template <typename T1, typename T2>
41 struct is
42 {
43  static const bool value = false;
44 };
45 
46 template <typename T1, typename T2>
47 struct is_not
48 {
49  static const bool value = true;
50 };
51 
52 template <typename T>
53 struct is<T, T>
54 {
55  static const bool value = true;
56 };
57 
58 template <typename T>
59 struct is_not<T, T>
60 {
61  static const bool value = false;
62 };
63 
64 template <typename T>
65 struct is_integral
66 {
67  static const bool value = false;
68 };
69 template <>
70 struct is_integral<bool>
71 {
72  static const bool value = true;
73 };
74 template <>
75 struct is_integral<char>
76 {
77  static const bool value = true;
78 };
79 template <>
80 struct is_integral<unsigned char>
81 {
82  static const bool value = true;
83 };
84 template <>
85 struct is_integral<signed char>
86 {
87  static const bool value = true;
88 };
89 template <>
90 struct is_integral<short>
91 {
92  static const bool value = true;
93 };
94 template <>
95 struct is_integral<unsigned short>
96 {
97  static const bool value = true;
98 };
99 template <>
100 struct is_integral<int>
101 {
102  static const bool value = true;
103 };
104 template <>
105 struct is_integral<unsigned int>
106 {
107  static const bool value = true;
108 };
109 template <>
110 struct is_integral<long>
111 {
112  static const bool value = true;
113 };
114 template <>
115 struct is_integral<unsigned long>
116 {
117  static const bool value = true;
118 };
119 template <>
120 struct is_integral<long long>
121 {
122  static const bool value = true;
123 };
124 template <>
125 struct is_integral<unsigned long long>
126 {
127  static const bool value = true;
128 };
129 
130 template <typename T>
131 struct is_pointer
132 {
133  static const bool value = false;
134 };
135 template <typename T>
136 struct is_pointer<T *>
137 {
138  static const bool value = true;
139 };
140 
142 template <class T>
143 struct remove_all_extents
144 {
145  typedef T type;
146 };
147 
148 template <class T>
149 struct remove_all_extents<T[]>
150 {
151  typedef typename remove_all_extents<T>::type type;
152 };
153 
154 template <class T, size_t N>
155 struct remove_all_extents<T[N]>
156 {
157  typedef typename remove_all_extents<T>::type type;
158 };
159 
161 template <bool, class T = void>
162 struct enable_if
163 {
164 };
165 
166 template <class T>
167 struct enable_if<true, T>
168 {
169  typedef T type;
170 };
171 
173 template <class T, T v>
174 struct integral_constant
175 {
176  static constexpr T value = v;
177  typedef T value_type;
178  typedef integral_constant type;
179  constexpr operator value_type() const noexcept
180  {
181  return value;
182  }
183 };
184 
185 template <class T, T v>
186 constexpr T integral_constant<T, v>::value;
187 
188 typedef integral_constant<bool, true> true_type;
189 typedef integral_constant<bool, false> false_type;
190 
192 template <class T>
194 struct is_scalar
195  : integral_constant<bool, is_integral<T>::value || is_pointer<T>::value>
196 {
197 };
198 
200 template <class T>
201 struct is_trivial : public integral_constant<bool, __is_trivial(T)>
202 {
203 };
204 
206 template <class T>
207 struct is_trivially_copyable
208  : public integral_constant<
209  bool, is_scalar<typename remove_all_extents<T>::type>::value>
210 {
211 };
212 
213 template <class T, class U>
214 struct is_same : false_type
215 {
216 };
217 
218 template <class T>
219 struct is_same<T, T> : true_type
220 {
221 };
222 
224 template <class T>
225 typename enable_if<is_trivially_copyable<T>::value>::type *
226 copy(T *dest, const T *src, size_t count)
227 {
228 #ifdef UTILITY_LINUX
229  return memmove(dest, src, count * sizeof(T));
230 #else
231  return MemoryCopy(dest, src, count * sizeof(T));
232 #endif
233 }
234 
236 template <class T>
237 typename enable_if<!is_trivially_copyable<T>::value>::type *
238 copy(T *dest, const T *src, size_t count)
239 {
240  if (overlaps(dest, src, count * sizeof(T)))
241  {
242  for (ssize_t i = count - 1; i >= 0; --i)
243  {
244  dest[i] = src[i];
245  }
246  }
247  else
248  {
249  for (size_t i = 0; i < count; ++i)
250  {
251  dest[i] = src[i];
252  }
253  }
254  return dest;
255 }
256 
258 template <class T>
259 struct function_traits : public function_traits<decltype(&T::operator())>
260 {
261 };
262 
263 template <class C, class R, class... Args>
264 struct function_traits<R (C::*)(Args...) const>
265 {
266  typedef R return_type;
267  typedef C class_type;
268 };
269 
270 template <class R, class... Args>
271 struct function_traits<R (*)(Args...)>
272 {
273  typedef R return_type;
274 };
275 
276 template <class R, class... Args>
277 struct function_traits<R(Args...)>
278 {
279  typedef R return_type;
280 };
281 
283 template <class F>
284 class Callable
285 {
286  public:
287  Callable(const F &x) : func(x)
288  {
289  }
290 
291  template <class... Args>
292  typename function_traits<F>::return_type operator()(Args... args)
293  {
294  return func(args...);
295  }
296 
297  private:
298  const F &func;
299 };
300 
302 template <class T>
303 Callable<T> make_callable(T &f)
304 {
305  return Callable<T>(f);
306 }
307 
309 template <class T>
310 struct remove_pointer
311 {
312  typedef T type;
313 };
314 
315 template <class T>
316 struct remove_pointer<T *>
317 {
318  typedef T type;
319 };
320 
322 template <class T>
323 struct remove_reference
324 {
325  typedef T type;
326 };
327 
328 template <class T>
329 struct remove_reference<T &>
330 {
331  typedef T type;
332 };
333 
335 template <class T>
336 typename remove_reference<T>::type &&move(T &&a)
337 {
338  return static_cast<typename remove_reference<T>::type &&>(a);
339 }
340 
342 template <class T>
343 const T &min(const T& a, const T &b)
344 {
345  return a <= b ? a : b;
346 }
347 
349 template <class T>
350 const T &max(const T& a, const T &b)
351 {
352  return a >= b ? a : b;
353 }
354 
355 } // namespace pedigree_std
356 
357 using pedigree_std::is_integral;
358 using pedigree_std::is_pointer;
359 
360 #endif // __cplusplus
361 
362 #endif // KERNEL_UTILITIES_CPP_H