Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
utility.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <algorithm>
9#include <condition_variable>
10#include <memory>
11#include <mutex>
12#include <optional>
13#include <ostream>
14#include <string>
15#include <string_view>
16#include <type_traits>
17#include <variant>
18#include <vector>
19
20#include <nlohmann/json.hpp>
21
22#define KITTY_WHILE_LOOP(x, y, z) \
23 { \
24 x; \
25 while (y) z \
26 }
27
28template<typename T>
30
31template<typename T, typename U>
32struct argument_type<T(U)> {
33 typedef U type;
34};
35
36#define KITTY_USING_MOVE_T(move_t, t, init_val, z) \
37 class move_t { \
38 public: \
39 using element_type = typename argument_type<void(t)>::type; \
40\
41 move_t(): \
42 el {init_val} { \
43 } \
44 template<class... Args> \
45 move_t(Args &&...args): \
46 el {std::forward<Args>(args)...} { \
47 } \
48 move_t(const move_t &) = delete; \
49\
50 move_t(move_t &&other) noexcept: \
51 el {std::move(other.el)} { \
52 other.el = element_type {init_val}; \
53 } \
54\
55 move_t &operator=(const move_t &) = delete; \
56\
57 move_t &operator=(move_t &&other) { \
58 std::swap(el, other.el); \
59 return *this; \
60 } \
61 element_type *operator->() { \
62 return &el; \
63 } \
64 const element_type *operator->() const { \
65 return &el; \
66 } \
67\
68 inline element_type release() { \
69 element_type val = std::move(el); \
70 el = element_type {init_val}; \
71 return val; \
72 } \
73\
74 ~move_t() z \
75\
76 element_type el; \
77 }
78
79#define KITTY_DECL_CONSTR(x) \
80 x(x &&) noexcept = default; \
81 x &operator=(x &&) noexcept = default; \
82 x();
83
84#define KITTY_DEFAULT_CONSTR_MOVE(x) \
85 x(x &&) noexcept = default; \
86 x &operator=(x &&) noexcept = default;
87
88#define KITTY_DEFAULT_CONSTR_MOVE_THROW(x) \
89 x(x &&) = default; \
90 x &operator=(x &&) = default; \
91 x() = default;
92
93#define KITTY_DEFAULT_CONSTR(x) \
94 KITTY_DEFAULT_CONSTR_MOVE(x) \
95 x(const x &) noexcept = default; \
96 x &operator=(const x &) = default;
97
98#define TUPLE_2D(a, b, expr) \
99 decltype(expr) a##_##b = expr; \
100 auto &a = std::get<0>(a##_##b); \
101 auto &b = std::get<1>(a##_##b)
102
103#define TUPLE_2D_REF(a, b, expr) \
104 auto &a##_##b = expr; \
105 auto &a = std::get<0>(a##_##b); \
106 auto &b = std::get<1>(a##_##b)
107
108#define TUPLE_3D(a, b, c, expr) \
109 decltype(expr) a##_##b##_##c = expr; \
110 auto &a = std::get<0>(a##_##b##_##c); \
111 auto &b = std::get<1>(a##_##b##_##c); \
112 auto &c = std::get<2>(a##_##b##_##c)
113
114#define TUPLE_3D_REF(a, b, c, expr) \
115 auto &a##_##b##_##c = expr; \
116 auto &a = std::get<0>(a##_##b##_##c); \
117 auto &b = std::get<1>(a##_##b##_##c); \
118 auto &c = std::get<2>(a##_##b##_##c)
119
120#define TUPLE_EL(a, b, expr) \
121 decltype(expr) a##_ = expr; \
122 auto &a = std::get<b>(a##_)
123
124#define TUPLE_EL_REF(a, b, expr) \
125 auto &a = std::get<b>(expr)
126
127namespace util {
128
129 template<template<typename...> class X, class... Y>
130 struct __instantiation_of: public std::false_type {};
131
132 template<template<typename...> class X, class... Y>
133 struct __instantiation_of<X, X<Y...>>: public std::true_type {};
134
135 template<template<typename...> class X, class T, class... Y>
136 static constexpr auto instantiation_of_v = __instantiation_of<X, T, Y...>::value;
137
138 template<bool V, class X, class Y>
139 struct __either;
140
141 template<class X, class Y>
142 struct __either<true, X, Y> {
143 using type = X;
144 };
145
146 template<class X, class Y>
147 struct __either<false, X, Y> {
148 using type = Y;
149 };
150
151 template<bool V, class X, class Y>
152 using either_t = typename __either<V, X, Y>::type;
153
154 template<class... Ts>
155 struct overloaded: Ts... {
156 using Ts::operator()...;
157 };
158 template<class... Ts>
159 overloaded(Ts...) -> overloaded<Ts...>;
160
161 template<class T>
162 class FailGuard {
163 public:
164 FailGuard() = delete;
165
166 FailGuard(T &&f) noexcept:
167 _func {std::forward<T>(f)} {
168 }
169
170 FailGuard(FailGuard &&other) noexcept:
171 _func {std::move(other._func)} {
172 this->failure = other.failure;
173
174 other.failure = false;
175 }
176
177 FailGuard(const FailGuard &) = delete;
178
179 FailGuard &operator=(const FailGuard &) = delete;
180 FailGuard &operator=(FailGuard &&other) = delete;
181
182 ~FailGuard() noexcept {
183 if (failure) {
184 _func();
185 }
186 }
187
188 void disable() {
189 failure = false;
190 }
191
192 bool failure {true};
193
194 private:
195 T _func;
196 };
197
198 template<class T>
199 [[nodiscard]] auto fail_guard(T &&f) {
200 return FailGuard<T> {std::forward<T>(f)};
201 }
202
203 template<class T>
204 void append_struct(std::vector<uint8_t> &buf, const T &_struct) {
205 constexpr size_t data_len = sizeof(_struct);
206
207 buf.reserve(data_len);
208
209 auto *data = (uint8_t *) &_struct;
210
211 for (size_t x = 0; x < data_len; ++x) {
212 buf.push_back(data[x]);
213 }
214 }
215
216 template<class T>
217 class Hex {
218 public:
219 typedef T elem_type;
220
221 private:
222 const char _bits[16] {
223 '0',
224 '1',
225 '2',
226 '3',
227 '4',
228 '5',
229 '6',
230 '7',
231 '8',
232 '9',
233 'A',
234 'B',
235 'C',
236 'D',
237 'E',
238 'F'
239 };
240
241 char _hex[sizeof(elem_type) * 2];
242
243 public:
244 Hex(const elem_type &elem, bool rev) {
245 if (!rev) {
246 const uint8_t *data = reinterpret_cast<const uint8_t *>(&elem) + sizeof(elem_type) - 1;
247 for (auto it = begin(); it < cend();) {
248 *it++ = _bits[*data / 16];
249 *it++ = _bits[*data-- % 16];
250 }
251 } else {
252 const uint8_t *data = reinterpret_cast<const uint8_t *>(&elem);
253 for (auto it = begin(); it < cend();) {
254 *it++ = _bits[*data / 16];
255 *it++ = _bits[*data++ % 16];
256 }
257 }
258 }
259
260 char *begin() {
261 return _hex;
262 }
263
264 char *end() {
265 return _hex + sizeof(elem_type) * 2;
266 }
267
268 const char *begin() const {
269 return _hex;
270 }
271
272 const char *end() const {
273 return _hex + sizeof(elem_type) * 2;
274 }
275
276 const char *cbegin() const {
277 return _hex;
278 }
279
280 const char *cend() const {
281 return _hex + sizeof(elem_type) * 2;
282 }
283
284 std::string to_string() const {
285 return {begin(), end()};
286 }
287
288 std::string_view to_string_view() const {
289 return {begin(), sizeof(elem_type) * 2};
290 }
291 };
292
293 template<class T>
294 Hex<T> hex(const T &elem, bool rev = false) {
295 return Hex<T>(elem, rev);
296 }
297
298 template<typename T>
299 std::string log_hex(const T &value) {
300 return "0x" + Hex<T>(value, false).to_string();
301 }
302
303 template<class It>
304 std::string hex_vec(It begin, It end, bool rev = false) {
305 auto str_size = 2 * std::distance(begin, end);
306
307 std::string hex;
308 hex.resize(str_size);
309
310 const char _bits[16] {
311 '0',
312 '1',
313 '2',
314 '3',
315 '4',
316 '5',
317 '6',
318 '7',
319 '8',
320 '9',
321 'A',
322 'B',
323 'C',
324 'D',
325 'E',
326 'F'
327 };
328
329 if (rev) {
330 for (auto it = std::begin(hex); it < std::end(hex);) {
331 *it++ = _bits[((uint8_t) *begin) / 16];
332 *it++ = _bits[((uint8_t) *begin++) % 16];
333 }
334 } else {
335 --end;
336 for (auto it = std::begin(hex); it < std::end(hex);) {
337 *it++ = _bits[((uint8_t) *end) / 16];
338 *it++ = _bits[((uint8_t) *end--) % 16];
339 }
340 }
341
342 return hex;
343 }
344
345 template<class C>
346 std::string hex_vec(C &&c, bool rev = false) {
347 return hex_vec(std::begin(c), std::end(c), rev);
348 }
349
350 template<class T>
351 T from_hex(const std::string_view &hex, bool rev = false) {
352 std::uint8_t buf[sizeof(T)];
353
354 static char constexpr shift_bit = 'a' - 'A';
355
356 auto is_convertable = [](char ch) -> bool {
357 if (isdigit(ch)) {
358 return true;
359 }
360
361 ch |= shift_bit;
362
363 if ('a' > ch || ch > 'z') {
364 return false;
365 }
366
367 return true;
368 };
369
370 auto buf_size = std::count_if(std::begin(hex), std::end(hex), is_convertable) / 2;
371 auto padding = sizeof(T) - buf_size;
372
373 const char *data = hex.data() + hex.size() - 1;
374
375 auto convert = [](char ch) -> std::uint8_t {
376 if (ch >= '0' && ch <= '9') {
377 return (std::uint8_t) ch - '0';
378 }
379
380 return (std::uint8_t) (ch | (char) 32) - 'a' + (char) 10;
381 };
382
383 std::fill_n(buf + buf_size, padding, 0);
384
385 std::for_each_n(buf, buf_size, [&](auto &el) {
386 while (!is_convertable(*data)) {
387 --data;
388 }
389 std::uint8_t ch_r = convert(*data--);
390
391 while (!is_convertable(*data)) {
392 --data;
393 }
394 std::uint8_t ch_l = convert(*data--);
395
396 el = (ch_l << 4) | ch_r;
397 });
398
399 if (rev) {
400 std::reverse(std::begin(buf), std::end(buf));
401 }
402
403 return *reinterpret_cast<T *>(buf);
404 }
405
406 inline std::string from_hex_vec(const std::string &hex, bool rev = false) {
407 std::string buf;
408
409 static char constexpr shift_bit = 'a' - 'A';
410 auto is_convertable = [](char ch) -> bool {
411 if (isdigit(ch)) {
412 return true;
413 }
414
415 ch |= shift_bit;
416
417 if ('a' > ch || ch > 'z') {
418 return false;
419 }
420
421 return true;
422 };
423
424 auto buf_size = std::count_if(std::begin(hex), std::end(hex), is_convertable) / 2;
425 buf.resize(buf_size);
426
427 const char *data = hex.data() + hex.size() - 1;
428
429 auto convert = [](char ch) -> std::uint8_t {
430 if (ch >= '0' && ch <= '9') {
431 return (std::uint8_t) ch - '0';
432 }
433
434 return (std::uint8_t) (ch | (char) 32) - 'a' + (char) 10;
435 };
436
437 for (auto &el : buf) {
438 while (!is_convertable(*data)) {
439 --data;
440 }
441 std::uint8_t ch_r = convert(*data--);
442
443 while (!is_convertable(*data)) {
444 --data;
445 }
446 std::uint8_t ch_l = convert(*data--);
447
448 el = (ch_l << 4) | ch_r;
449 }
450
451 if (rev) {
452 std::reverse(std::begin(buf), std::end(buf));
453 }
454
455 return buf;
456 }
457
458 template<typename T>
459 T get_non_string_json_value(const nlohmann::json& j, const std::string& key, const T& default_value = T{}) {
460 if (!j.contains(key))
461 return default_value;
462 const auto& val = j.at(key);
463 if (val.is_number() || val.is_boolean())
464 return val.get<T>();
465 if (val.is_string()) {
466 std::string s = val.get<std::string>();
467 if constexpr (std::is_same_v<T, int>) {
468 return std::stoi(s);
469 } else if constexpr (std::is_same_v<T, double>) {
470 return std::stod(s);
471 } else if constexpr (std::is_same_v<T, bool>) {
472 return s == "true";
473 } else if constexpr (std::is_same_v<T, std::string>) {
474 return s;
475 } else {
476 return default_value;
477 }
478 }
479 return default_value;
480 }
481
482 template<class T>
483 class hash {
484 public:
485 using value_type = T;
486
487 std::size_t operator()(const value_type &value) const {
488 const auto *p = reinterpret_cast<const char *>(&value);
489
490 return std::hash<std::string_view> {}(std::string_view {p, sizeof(value_type)});
491 }
492 };
493
494 template<class T>
495 auto enm(const T &val) -> const std::underlying_type_t<T> & {
496 return *reinterpret_cast<const std::underlying_type_t<T> *>(&val);
497 }
498
499 template<class T>
500 auto enm(T &val) -> std::underlying_type_t<T> & {
501 return *reinterpret_cast<std::underlying_type_t<T> *>(&val);
502 }
503
504 inline std::int64_t from_chars(const char *begin, const char *end) {
505 if (begin == end) {
506 return 0;
507 }
508
509 std::int64_t res {};
510 std::int64_t mul = 1;
511 while (begin != --end) {
512 res += (std::int64_t) (*end - '0') * mul;
513
514 mul *= 10;
515 }
516
517 return *begin != '-' ? res + (std::int64_t) (*begin - '0') * mul : -res;
518 }
519
520 inline std::int64_t from_view(const std::string_view &number) {
521 return from_chars(std::begin(number), std::end(number));
522 }
523
524 template<class X, class Y>
525 class Either: public std::variant<std::monostate, X, Y> {
526 public:
527 using std::variant<std::monostate, X, Y>::variant;
528
529 constexpr bool has_left() const {
530 return std::holds_alternative<X>(*this);
531 }
532
533 constexpr bool has_right() const {
534 return std::holds_alternative<Y>(*this);
535 }
536
537 X &left() {
538 return std::get<X>(*this);
539 }
540
541 Y &right() {
542 return std::get<Y>(*this);
543 }
544
545 const X &left() const {
546 return std::get<X>(*this);
547 }
548
549 const Y &right() const {
550 return std::get<Y>(*this);
551 }
552 };
553
554 // Compared to std::unique_ptr, it adds the ability to get the address of the pointer itself
555 template<typename T, typename D = std::default_delete<T>>
556 class uniq_ptr {
557 public:
558 using element_type = T;
559 using pointer = element_type *;
560 using const_pointer = element_type const *;
561 using deleter_type = D;
562
563 constexpr uniq_ptr() noexcept:
564 _p {nullptr} {
565 }
566
567 constexpr uniq_ptr(std::nullptr_t) noexcept:
568 _p {nullptr} {
569 }
570
571 uniq_ptr(const uniq_ptr &other) noexcept = delete;
572 uniq_ptr &operator=(const uniq_ptr &other) noexcept = delete;
573
574 template<class V>
575 uniq_ptr(V *p) noexcept:
576 _p {p} {
577 static_assert(std::is_same_v<element_type, void> || std::is_same_v<element_type, V> || std::is_base_of_v<element_type, V>, "element_type must be base class of V");
578 }
579
580 template<class V>
581 uniq_ptr(std::unique_ptr<V, deleter_type> &&uniq) noexcept:
582 _p {uniq.release()} {
583 static_assert(std::is_same_v<element_type, void> || std::is_same_v<T, V> || std::is_base_of_v<element_type, V>, "element_type must be base class of V");
584 }
585
586 template<class V>
587 uniq_ptr(uniq_ptr<V, deleter_type> &&other) noexcept:
588 _p {other.release()} {
589 static_assert(std::is_same_v<element_type, void> || std::is_same_v<T, V> || std::is_base_of_v<element_type, V>, "element_type must be base class of V");
590 }
591
592 template<class V>
593 uniq_ptr &operator=(uniq_ptr<V, deleter_type> &&other) noexcept {
594 static_assert(std::is_same_v<element_type, void> || std::is_same_v<T, V> || std::is_base_of_v<element_type, V>, "element_type must be base class of V");
595 reset(other.release());
596
597 return *this;
598 }
599
600 template<class V>
601 uniq_ptr &operator=(std::unique_ptr<V, deleter_type> &&uniq) noexcept {
602 static_assert(std::is_same_v<element_type, void> || std::is_same_v<T, V> || std::is_base_of_v<element_type, V>, "element_type must be base class of V");
603
604 reset(uniq.release());
605
606 return *this;
607 }
608
609 ~uniq_ptr() {
610 reset();
611 }
612
613 void reset(pointer p = pointer()) {
614 if (_p) {
615 _deleter(_p);
616 }
617
618 _p = p;
619 }
620
621 pointer release() {
622 auto tmp = _p;
623 _p = nullptr;
624 return tmp;
625 }
626
627 pointer get() {
628 return _p;
629 }
630
631 const_pointer get() const {
632 return _p;
633 }
634
635 std::add_lvalue_reference_t<element_type const> operator*() const {
636 return *_p;
637 }
638
639 std::add_lvalue_reference_t<element_type> operator*() {
640 return *_p;
641 }
642
643 const_pointer operator->() const {
644 return _p;
645 }
646
647 pointer operator->() {
648 return _p;
649 }
650
651 pointer *operator&() const {
652 return &_p;
653 }
654
655 pointer *operator&() {
656 return &_p;
657 }
658
659 deleter_type &get_deleter() {
660 return _deleter;
661 }
662
663 const deleter_type &get_deleter() const {
664 return _deleter;
665 }
666
667 explicit operator bool() const {
668 return _p != nullptr;
669 }
670
671 protected:
672 pointer _p;
673 deleter_type _deleter;
674 };
675
676 template<class T1, class D1, class T2, class D2>
677 bool operator==(const uniq_ptr<T1, D1> &x, const uniq_ptr<T2, D2> &y) {
678 return x.get() == y.get();
679 }
680
681 template<class T1, class D1, class T2, class D2>
682 bool operator!=(const uniq_ptr<T1, D1> &x, const uniq_ptr<T2, D2> &y) {
683 return x.get() != y.get();
684 }
685
686 template<class T1, class D1, class T2, class D2>
687 bool operator==(const std::unique_ptr<T1, D1> &x, const uniq_ptr<T2, D2> &y) {
688 return x.get() == y.get();
689 }
690
691 template<class T1, class D1, class T2, class D2>
692 bool operator!=(const std::unique_ptr<T1, D1> &x, const uniq_ptr<T2, D2> &y) {
693 return x.get() != y.get();
694 }
695
696 template<class T1, class D1, class T2, class D2>
697 bool operator==(const uniq_ptr<T1, D1> &x, const std::unique_ptr<T1, D1> &y) {
698 return x.get() == y.get();
699 }
700
701 template<class T1, class D1, class T2, class D2>
702 bool operator!=(const uniq_ptr<T1, D1> &x, const std::unique_ptr<T1, D1> &y) {
703 return x.get() != y.get();
704 }
705
706 template<class T, class D>
707 bool operator==(const uniq_ptr<T, D> &x, std::nullptr_t) {
708 return !(bool) x;
709 }
710
711 template<class T, class D>
712 bool operator!=(const uniq_ptr<T, D> &x, std::nullptr_t) {
713 return (bool) x;
714 }
715
716 template<class T, class D>
717 bool operator==(std::nullptr_t, const uniq_ptr<T, D> &y) {
718 return !(bool) y;
719 }
720
721 template<class T, class D>
722 bool operator!=(std::nullptr_t, const uniq_ptr<T, D> &y) {
723 return (bool) y;
724 }
725
726 template<class P>
727 using shared_t = std::shared_ptr<typename P::element_type>;
728
729 template<class P, class T>
730 shared_t<P> make_shared(T *pointer) {
731 return shared_t<P>(reinterpret_cast<typename P::pointer>(pointer), typename P::deleter_type());
732 }
733
734 template<class T>
735 class wrap_ptr {
736 public:
737 using element_type = T;
738 using pointer = element_type *;
739 using const_pointer = element_type const *;
740 using reference = element_type &;
741 using const_reference = element_type const &;
742
743 wrap_ptr():
744 _own_ptr {false},
745 _p {nullptr} {
746 }
747
748 wrap_ptr(pointer p):
749 _own_ptr {false},
750 _p {p} {
751 }
752
753 wrap_ptr(std::unique_ptr<element_type> &&uniq_p):
754 _own_ptr {true},
755 _p {uniq_p.release()} {
756 }
757
758 wrap_ptr(wrap_ptr &&other):
759 _own_ptr {other._own_ptr},
760 _p {other._p} {
761 other._own_ptr = false;
762 }
763
764 wrap_ptr &operator=(wrap_ptr &&other) noexcept {
765 if (_own_ptr) {
766 delete _p;
767 }
768
769 _p = other._p;
770
771 _own_ptr = other._own_ptr;
772 other._own_ptr = false;
773
774 return *this;
775 }
776
777 template<class V>
778 wrap_ptr &operator=(std::unique_ptr<V> &&uniq_ptr) {
779 static_assert(std::is_base_of_v<element_type, V>, "element_type must be base class of V");
780 _own_ptr = true;
781 _p = uniq_ptr.release();
782
783 return *this;
784 }
785
786 wrap_ptr &operator=(pointer p) {
787 if (_own_ptr) {
788 delete _p;
789 }
790
791 _p = p;
792 _own_ptr = false;
793
794 return *this;
795 }
796
797 ~wrap_ptr() {
798 if (_own_ptr) {
799 delete _p;
800 }
801
802 _own_ptr = false;
803 }
804
805 const_reference operator*() const {
806 return *_p;
807 }
808
809 reference operator*() {
810 return *_p;
811 }
812
813 const_pointer operator->() const {
814 return _p;
815 }
816
817 pointer operator->() {
818 return _p;
819 }
820
821 private:
822 bool _own_ptr;
823 pointer _p;
824 };
825
826 template<class T>
827 constexpr bool is_pointer_v =
828 instantiation_of_v<std::unique_ptr, T> ||
829 instantiation_of_v<std::shared_ptr, T> ||
830 instantiation_of_v<uniq_ptr, T> ||
831 std::is_pointer_v<T>;
832
833 template<class T, class V = void>
834 struct __false_v;
835
836 template<class T>
837 struct __false_v<T, std::enable_if_t<instantiation_of_v<std::optional, T>>> {
838 static constexpr std::nullopt_t value = std::nullopt;
839 };
840
841 template<class T>
842 struct __false_v<T, std::enable_if_t<is_pointer_v<T>>> {
843 static constexpr std::nullptr_t value = nullptr;
844 };
845
846 template<class T>
847 struct __false_v<T, std::enable_if_t<std::is_same_v<T, bool>>> {
848 static constexpr bool value = false;
849 };
850
851 template<class T>
852 static constexpr auto false_v = __false_v<T>::value;
853
854 template<class T>
855 using optional_t = either_t<
856 (std::is_same_v<T, bool> || is_pointer_v<T>),
857 T,
858 std::optional<T>>;
859
860 template<class T>
861 class buffer_t {
862 public:
863 buffer_t():
864 _els {0} {};
865
866 buffer_t(buffer_t &&o) noexcept:
867 _els {o._els},
868 _buf {std::move(o._buf)} {
869 o._els = 0;
870 }
871
872 buffer_t(const buffer_t &o):
873 _els {o._els},
874 _buf {std::make_unique<T[]>(_els)} {
875 std::copy(o.begin(), o.end(), begin());
876 }
877
878 buffer_t &operator=(buffer_t &&o) noexcept {
879 std::swap(_els, o._els);
880 std::swap(_buf, o._buf);
881
882 return *this;
883 };
884
885 explicit buffer_t(size_t elements):
886 _els {elements},
887 _buf {std::make_unique<T[]>(elements)} {
888 }
889
890 explicit buffer_t(size_t elements, const T &t):
891 _els {elements},
892 _buf {std::make_unique<T[]>(elements)} {
893 std::fill_n(_buf.get(), elements, t);
894 }
895
896 T &operator[](size_t el) {
897 return _buf[el];
898 }
899
900 const T &operator[](size_t el) const {
901 return _buf[el];
902 }
903
904 size_t size() const {
905 return _els;
906 }
907
908 void fake_resize(std::size_t els) {
909 _els = els;
910 }
911
912 T *begin() {
913 return _buf.get();
914 }
915
916 const T *begin() const {
917 return _buf.get();
918 }
919
920 T *end() {
921 return _buf.get() + _els;
922 }
923
924 const T *end() const {
925 return _buf.get() + _els;
926 }
927
928 private:
929 size_t _els;
930 std::unique_ptr<T[]> _buf;
931 };
932
933 template<class T>
934 T either(std::optional<T> &&l, T &&r) {
935 if (l) {
936 return std::move(*l);
937 }
938
939 return std::forward<T>(r);
940 }
941
942 template<class ReturnType, class... Args>
943 struct Function {
944 typedef ReturnType (*type)(Args...);
945 };
946
947 template<class T, class ReturnType, typename Function<ReturnType, T>::type function>
948 struct Destroy {
949 typedef T pointer;
950
951 void operator()(pointer p) {
952 function(p);
953 }
954 };
955
956 template<class T, typename Function<void, T *>::type function>
958
959 // You cannot specialize an alias
960 template<class T, class ReturnType, typename Function<ReturnType, T *>::type function>
962
963 template<class T>
964 void c_free(T *p) {
965 free(p);
966 }
967
968 template<class T, class ReturnType, ReturnType (**function)(T *)>
969 void dynamic(T *p) {
970 (*function)(p);
971 }
972
973 template<class T, void (**function)(T *)>
974 using dyn_safe_ptr = safe_ptr<T, dynamic<T, void, function>>;
975
976 template<class T, class ReturnType, ReturnType (**function)(T *)>
977 using dyn_safe_ptr_v2 = safe_ptr<T, dynamic<T, ReturnType, function>>;
978
979 template<class T>
980 using c_ptr = safe_ptr<T, c_free<T>>;
981
982 template<class It>
983 std::string_view view(It begin, It end) {
984 return std::string_view {(const char *) begin, (std::size_t) (end - begin)};
985 }
986
987 template<class T>
988 std::string_view view(const T &data) {
989 return std::string_view((const char *) &data, sizeof(T));
990 }
991
992 struct point_t {
993 double x;
994 double y;
995
996 friend std::ostream &operator<<(std::ostream &os, const point_t &p) {
997 return (os << "Point(x: " << p.x << ", y: " << p.y << ")");
998 }
999 };
1000
1001 namespace endian {
1002 template<class T = void>
1003 struct endianness {
1004 enum : bool {
1005#if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN || \
1006 defined(__BIG_ENDIAN__) || \
1007 defined(__ARMEB__) || \
1008 defined(__THUMBEB__) || \
1009 defined(__AARCH64EB__) || \
1010 defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__)
1011 // It's a big-endian target architecture
1012 little = false,
1013#elif defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN || \
1014 defined(__LITTLE_ENDIAN__) || \
1015 defined(__ARMEL__) || \
1016 defined(__THUMBEL__) || \
1017 defined(__AARCH64EL__) || \
1018 defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__)
1019 little = true,
1020#else
1021 #error "Unknown Endianness"
1022#endif
1023 big = !little
1024 };
1025 };
1026
1027 template<class T, class S = void>
1028 struct endian_helper {};
1029
1030 template<class T>
1031 struct endian_helper<T, std::enable_if_t<!(instantiation_of_v<std::optional, T>)>> {
1032 static inline T big(T x) {
1033 if constexpr (endianness<T>::little) {
1034 uint8_t *data = reinterpret_cast<uint8_t *>(&x);
1035
1036 std::reverse(data, data + sizeof(x));
1037 }
1038
1039 return x;
1040 }
1041
1042 static inline T little(T x) {
1043 if constexpr (endianness<T>::big) {
1044 uint8_t *data = reinterpret_cast<uint8_t *>(&x);
1045
1046 std::reverse(data, data + sizeof(x));
1047 }
1048
1049 return x;
1050 }
1051 };
1052
1053 template<class T>
1054 struct endian_helper<T, std::enable_if_t<instantiation_of_v<std::optional, T>>> {
1055 static inline T little(T x) {
1056 if (!x) {
1057 return x;
1058 }
1059
1060 if constexpr (endianness<T>::big) {
1061 auto *data = reinterpret_cast<uint8_t *>(&*x);
1062
1063 std::reverse(data, data + sizeof(*x));
1064 }
1065
1066 return x;
1067 }
1068
1069 static inline T big(T x) {
1070 if (!x) {
1071 return x;
1072 }
1073
1074 if constexpr (endianness<T>::little) {
1075 auto *data = reinterpret_cast<uint8_t *>(&*x);
1076
1077 std::reverse(data, data + sizeof(*x));
1078 }
1079
1080 return x;
1081 }
1082 };
1083
1084 template<class T>
1085 inline auto little(T x) {
1086 return endian_helper<T>::little(x);
1087 }
1088
1089 template<class T>
1090 inline auto big(T x) {
1091 return endian_helper<T>::big(x);
1092 }
1093 } // namespace endian
1094} // namespace util
@ big
big-endian target architecture
Definition utility.h:1023