acme
Foundational cross-platform layer of the ca2 framework
Loading...
Searching...
No Matches
_inline_functions.h
1// Created by camilo on 2022-11-08 23:10 <3ThomasBorregaardSorensen!!
2#pragma once
3
4
5#include <math.h>
6#include <limits>
7
8
9template < typename T >
10T default_epsilon()
11{
12
13 return {};
14
15}
16
17
18template < prototype_integral INTEGRAL >
19constexpr INTEGRAL default_epsilon()
20{
21
22 return 0;
23
24}
25
26
27template < prototype_floating FLOATING >
28constexpr FLOATING default_epsilon()
29{
30
31 return ::std::numeric_limits<FLOATING>::epsilon();
32
33}
34
35
36//template < typename A, typename B >
37//inline void swap(A & a, B & b)
38//{
39//
40// auto t = a;
41//
42// a = b;
43//
44// b = t;
45//
46//}
47
48
49
50inline bool is_empty(const ::ansi_character * p) { return is_string_empty(p); }
51inline bool is_empty(const ::wd16_character * p) { return is_string_empty(p); }
52inline bool is_empty(const ::wd32_character * p) { return is_string_empty(p); }
53
54
55inline bool has_character(const ::ansi_character * p) { return !is_empty(p); }
56inline bool has_character(const ::wd16_character * p) { return !is_empty(p); }
57inline bool has_character(const ::wd32_character * p) { return !is_empty(p); }
58
59
60
61inline ::u64 make64_from32(::u32 l, ::u32 h)
62{
63
64 return ((::u64)l) | (((::u64)h) << 32);
65
66}
67
68CLASS_DECL_ACME void this_is_a_debugging_consumer(::i32 & i);
69
70inline bool is_memory_segment_ok(const void * pMemory, memsize s)
71{
72
73 auto p = (const ::u8 *)pMemory;
74
75 auto e = p + s;
76
77 try
78 {
79
80#ifdef DEEP_MEMORY_SEGMENT_CHECK
81
82 ::i32 sum = 0;
83
84 while (p < e)
85 {
86
87 sum += *b; // tests read of ::u8 *p
88
89 p++;
90
91 }
92
93#else // DEEP_MEMORY_SEGMENT_CHECK
94// __pragma(clang)
95//#pragma clang diagnostic push
96//#pragma clang diagnostic ignored "-Wunused-but-set-variable"
97
98 if (s >= 1)
99 {
100
101 ::i32 sum = 0;
102
103 sum += p[0]; // tests read of ::u8 p[0]
104
105 if (s >= 2)
106 {
107
108 sum += *(e-1); // tests read of ::u8 e[-1]
109
110 }
111
112 this_is_a_debugging_consumer(sum);
113
114 }
115
116//#pragma clang diagnostic pop
117
118#endif // else DEEP_MEMORY_SEGMENT_CHECK
119
120 return true;
121
122 }
123 catch (...)
124 {
125
126 }
127
128 return false;
129
130}
131
132
133inline bool is_memory_segment_ok(void * pMemory, memsize s)
134{
135
136 auto p = (::u8 *)pMemory;
137
138 auto e = p + s;
139
140 try
141 {
142
143#ifdef DEEP_MEMORY_SEGMENT_CHECK
144
145 while (p < e)
146 {
147
148 ::u8 & b = *p;
149
150 b++; // tests read/write of ::u8 b
151
152 b--; // restablish it
153
154 p++;
155
156 }
157
158
159#else // DEEP_MEMORY_SEGMENT_CHECK
160
161 if (s >= 1)
162 {
163
164 ::u8 & b = p[0];
165
166 b++; // tests read/write of ::u8 p[0]
167
168 b--; // restablish it
169
170 if (s >= 2)
171 {
172
173 ::u8 & b = e[-1];
174
175 b++; // tests read/write of ::u8 e[-1]
176
177 b--; // restablish it
178
179
180 }
181
182 }
183
184#endif // else DEEP_MEMORY_SEGMENT_CHECK
185
186 return true;
187
188 }
189 catch (...)
190 {
191
192 }
193
194 return false;
195
196}
197
198
199template < typename TYPE >
200inline bool is_array_ok(TYPE * p, ::collection::count c)
201{
202
203 return is_memory_segment_ok(p, c * sizeof(TYPE));
204
205}
206
207
208template < typename TYPE >
209inline bool is_array_ok(const TYPE * p, ::collection::count c)
210{
211
212 return is_memory_segment_ok(p, c * sizeof(TYPE));
213
214}
215
216
217inline bool is_string_ok(::wide_character * p, character_count s)
218{
219
220 return ::is_array_ok(p, s);
221
222}
223
224
225inline bool is_string_ok(const ::wide_character * p, character_count s)
226{
227
228 return ::is_array_ok(p, s);
229
230}
231
232
233inline bool is_string_ok(::ansi_character * p, character_count s)
234{
235
236 return ::is_array_ok(p, s);
237
238}
239
240
241inline bool is_string_ok(const ::ansi_character * p, character_count s)
242{
243
244 return ::is_array_ok(p, s);
245
246}
247
248
249template < typename TYPE >
250inline bool is_null_terminated_primitive_array_ok(TYPE * p)
251{
252
253 try
254 {
255
256 while (*p)
257 {
258
259 auto & item = *p;
260
261 item++; // tests read/write of ::u8 b
262
263 item--; // restablish it
264
265 p++;
266
267 }
268
269 return true;
270
271 }
272 catch (...)
273 {
274
275 }
276
277 return false;
278
279}
280
281
282template < typename TYPE >
283inline bool is_null_terminated_primitive_array_ok(const TYPE * p)
284{
285
286 try
287 {
288
289 TYPE t{};
290
291 while (*p)
292 {
293
294 t ^= *p;
295
296 p++;
297
298 }
299
300 return true;
301
302 }
303 catch (...)
304 {
305
306 }
307
308 return false;
309
310}
311
312
313inline bool is_string_ok(::wide_character * p)
314{
315
316 return is_null_terminated_primitive_array_ok(p);
317
318}
319
320
321inline bool is_string_ok(const ::wide_character * p)
322{
323
324 return is_null_terminated_primitive_array_ok(p);
325
326}
327
328
329inline bool is_string_ok(::ansi_character * p)
330{
331
332 return is_null_terminated_primitive_array_ok(p);
333
334}
335
336
337inline bool is_string_ok(const ::ansi_character * p)
338{
339
340 return is_null_terminated_primitive_array_ok(p);
341
342}
343
344
345inline ::i32_bool address_overlaps(const void * p1, const void * p2, character_count len)
346{
347
348 return (((::u8*)p1) <= (((::u8*)p2) + len))
349 && (((::u8*)p2) <= (((::u8*)p1) + len));
350
351}
352
353
354//template < typename TYPE > inline TYPE*& __defer_new(TYPE*& p)
355//{
356//
357// if (!p) p = allocateø TYPE();
358//
359// return p;
360//
361//}
362//
363//
364//inline const ::particle * context_trace_object() { return general_trace_object(); }
365//
366//
367//#ifdef CPP17
368//
369//template <typename PRED, typename ... Args >
370//inline bool _returns_true(PRED pred, const true_type& predicate_returns_void, bool bOnVoid, Args... args)
371//{
372// pred(args...);
373// return bOnVoid;
374//}
375//
376//template <typename PRED, typename ... Args >
377//inline bool _returns_true(PRED pred, const false_type& predicate_returns_void, bool, Args... args)
378//{
379// return (bool)pred(args...);
380//}
381//
382//template <typename PRED, typename ... Args >
383//inline bool returns_true(PRED pred, bool bOnVoid, Args... args)
384//{
385//
386// return _returns_true(pred, is_void<decltype(pred(args...))>, bOnVoid, args...);
387//
388//}
389//
390//template <typename PRED, typename ... Args >
391//inline bool _returns_false(PRED pred, const true_type& predicate_returns_void, bool bOnVoid, Args... args)
392//{
393// pred(args...);
394// return bOnVoid;
395//}
396//
397//template <typename PRED, typename ... Args >
398//inline bool _returns_false(PRED pred, const false_type& predicate_returns_void, bool, Args... args)
399//{
400// return !(bool)pred(args...);
401//}
402//
403//template <typename PRED, typename ... Args >
404//inline bool returns_false(PRED pred, bool bOnVoid, Args... args)
405//{
406//
407// return _returns_false(pred, is_void < decltype(pred(args...)) >, bOnVoid, args...);
408//
409//}
410//
411//#endif
412//
413
414
415inline const ::particle* trace_object(const ::particle* pparticle) { return pparticle; }
416
417
418//inline bool failed(const ::payload& payload) { return !::succeeded(payload); }
419
420
421inline ::i32 read_char(::u8*& pdata, memsize& s, char_pointer pch)
422{
423
424 if (s < 1)
425 {
426
427 return 0;
428
429 }
430
431 *pch = (::i8)*pdata;
432
433 pdata++;
434
435 s--;
436
437 return 1;
438
439}
440
441
442//
443//
444//template < typename TYPE >
445//inline bool is_reference_null(const TYPE& t)
446//{
447//
448// return is_null(&t);
449//
450//}
451//
452//
453//template < typename TYPE >
454//inline bool is_reference_set(const TYPE& t)
455//{
456//
457// return is_set(&t);
458//
459//}
460//
461
462
463inline bool is_impact_group(::u64 u, ::u64 uGroup) { return u >= uGroup && u < uGroup + 1000; }
464
465
466inline bool is_impact_subgroup(::u64 u, ::u64 uGroup) { return u >= uGroup && u < uGroup + 100; }
467
468
469inline bool is_file_or_folder2(const ::file::enum_type& etype)
470{
471
472 return (etype & ::file::e_type_file_or_folder2);
473
474}
475
476
477inline bool is_existing_file_or_folder(const ::file::enum_type& etype)
478{
479
480 return (etype & ::file::e_type_file_or_folder2) > ::file::e_type_exists;
481
482}
483
484
485inline bool exists(const ::file::enum_type& etype)
486{
487
488 return etype & ::file::e_type_exists;
489
490}
491
492
493//
494//template < class T >
495//inline void __dynamic_cast(T*& ptarget, T* psource)
496//{
497//
498// ptarget = psource;
499//
500//}
501//
502//
503//template < class T , typename T2 >
504//inline void __dynamic_cast(T * & ptarget, T2 * psource)
505//{
506//
507// ptarget = dynamic_cast < T * >(psource);
508//
509//}
510//
511//
512//template < typename T1, typename T2 >
513//inline bool __sort(T1& t1, T2& t2)
514//{
515//
516// if (t2 >= t1)
517// {
518//
519// return false;
520//
521// }
522//
523// auto t = t2;
524//
525// t2 = t1;
526//
527// t1 = t;
528//
529// return true;
530//
531//}
532//
533//
534
535
536
537
538//template < prototype_integral SECOND, prototype_integral NANOSECOND >
539//constexpr void normalize_second_nanosecond(SECOND & second, NANOSECOND & nanosecond)
540//{
541//
542// auto extrasecond = nanosecond > 0 ? (nanosecond / 1'000'000'000) : ((nanosecond / 1'000'000'000) - 1);
543//
544// second += extrasecond;
545//
546// nanosecond -= extrasecond * 1'000'000'000;
547//
548//}
549//
550//
551//
552//template < prototype_integral SECOND1, prototype_integral NANOSECOND1, prototype_integral SECOND2, prototype_integral NANOSECOND2 >
553//constexpr void add_second_nanosecond(SECOND1 & second1, NANOSECOND1 & nanosecond1, const SECOND2 & second2, const NANOSECOND2 & nanosecond2)
554//{
555//
556// auto nanos = nanosecond1 + nanosecond2;
557//
558// auto extrasecond = nanos > 0 ? (nanos / 1'000'000'000) : ((nanos / 1'000'000'000) - 1);
559//
560// second1 = second1 + second2 + extrasecond;
561//
562// nanosecond1 = nanos - extrasecond * 1'000'000'000;
563//
564//}
565//
566//
567//
568//template < prototype_integral SECOND1, prototype_integral NANOSECOND1, prototype_integral SECOND2, prototype_integral NANOSECOND2 >
569//constexpr void subtract_second_nanosecond(SECOND1 & second1, NANOSECOND1 & nanosecond1, const SECOND2 & second2, const NANOSECOND2 & nanosecond2)
570//{
571//
572// auto nanos = nanosecond1 - nanosecond2;
573//
574// auto extrasecond = nanos > 0 ? (nanos / 1'000'000'000) : ((nanos / 1'000'000'000) - 1);
575//
576// second1 = second1 - second2 + extrasecond;
577//
578// nanosecond1 = nanos - extrasecond * 1'000'000'000;
579//
580//}
581
582
583CLASS_DECL_ACME void throw_exception(enum_status estatus);
584
585
586constexpr ::std::strong_ordering negation(const ::std::strong_ordering & ordering)
587{
588
589 if(ordering < 0)
590 {
591
592 return ::std::strong_ordering::greater;
593
594 }
595 else if (ordering > 0)
596 {
597
598 return ::std::strong_ordering::less;
599
600 }
601 else
602 {
603
604 return ::std::strong_ordering::equal;
605
606 }
607
608}
609
610
611template < prototype_floating NUMBER1, prototype_floating NUMBER2 >
612bool is_equal(const NUMBER1 & n1, const NUMBER2 & n2, largest_number < NUMBER2, NUMBER2 > epsilon = default_epsilon < largest_number < NUMBER2, NUMBER2 > >())
613{
614
615 return ::std::abs(n1 - n2) < epsilon;
616
617}
618
619
620template < prototype_floating NUMBER1, prototype_integral NUMBER2 >
621bool is_equal(const NUMBER1 & n1, const NUMBER2 & n2, largest_number < NUMBER2, NUMBER2 > epsilon = default_epsilon < largest_number < NUMBER2, NUMBER2 > >())
622{
623
624 return ::std::abs(n1 - n2) < epsilon;
625
626}
627
628
629template < prototype_integral NUMBER1, prototype_floating NUMBER2 >
630bool is_equal(const NUMBER1 & n1, const NUMBER2 & n2, largest_number < NUMBER2, NUMBER2 > epsilon = default_epsilon < largest_number < NUMBER2, NUMBER2 > >())
631{
632
633 return ::std::abs(n1 - n2) < epsilon;
634
635}
636
637
638template < prototype_integral NUMBER1, prototype_integral NUMBER2 >
639bool is_equal(const NUMBER1 & n1, const NUMBER2 & n2, largest_number < NUMBER2, NUMBER2 > epsilon = 0)
640{
641
642 return n1 == n2;
643
644}
645
646
648template < prototype_number NUMBER1, prototype_number NUMBER2 >
649constexpr void _expand_minimum_maximum(NUMBER1 & minimum, NUMBER1 & maximum, NUMBER2 n)
650{
651
652 if (n < minimum)
653 {
654
655 minimum = (NUMBER1) n;
656
657 }
658 else if (n > maximum)
659 {
660
661 maximum = (NUMBER1) n;
662
663 }
664
665}
666
667
669template < prototype_integral NUMBER1, prototype_integral NUMBER2 >
670constexpr void expand_minimum_maximum(NUMBER1 & minimum, NUMBER1 & maximum, NUMBER2 n)
671{
672
673 _expand_minimum_maximum(minimum, maximum, n);
674
675}
676
677
679template < prototype_floating NUMBER1, prototype_floating NUMBER2 >
680constexpr void expand_minimum_maximum(NUMBER1 & minimum, NUMBER1 & maximum, NUMBER2 n)
681{
682
683 _expand_minimum_maximum(minimum, maximum, n);
684
685}
686
687
689template < prototype_floating NUMBER1, prototype_integral NUMBER2 >
690constexpr void expand_minimum_maximum(NUMBER1 & minimum, NUMBER1 & maximum, NUMBER2 n)
691{
692
693 _expand_minimum_maximum(minimum, maximum, n);
694
695}
696
697
699template < prototype_integral NUMBER1, prototype_floating NUMBER2 >
700constexpr void expand_minimum_maximum(NUMBER1 & minimum, NUMBER1 & maximum, NUMBER2 n)
701{
702
703 if (n < minimum)
704 {
705
706 minimum = (NUMBER1) floor(n);
707
708 }
709 else if (n > maximum)
710 {
711
712 maximum = (NUMBER1) ceil(n);
713
714 }
715
716}
717
718
719template < typename TYPE >
720inline TYPE & null_reference()
721{
722
723 return *((TYPE *)nullptr);
724
725}
726
727
728
Definition item.h:93