9#include <condition_variable>
20#include <nlohmann/json.hpp>
22#define KITTY_WHILE_LOOP(x, y, z) \
31template<
typename T,
typename U>
36#define KITTY_USING_MOVE_T(move_t, t, init_val, z) \
39 using element_type = typename argument_type<void(t)>::type; \
44 template<class... Args> \
45 move_t(Args &&...args): \
46 el {std::forward<Args>(args)...} { \
48 move_t(const move_t &) = delete; \
50 move_t(move_t &&other) noexcept: \
51 el {std::move(other.el)} { \
52 other.el = element_type {init_val}; \
55 move_t &operator=(const move_t &) = delete; \
57 move_t &operator=(move_t &&other) { \
58 std::swap(el, other.el); \
61 element_type *operator->() { \
64 const element_type *operator->() const { \
68 inline element_type release() { \
69 element_type val = std::move(el); \
70 el = element_type {init_val}; \
79#define KITTY_DECL_CONSTR(x) \
80 x(x &&) noexcept = default; \
81 x &operator=(x &&) noexcept = default; \
84#define KITTY_DEFAULT_CONSTR_MOVE(x) \
85 x(x &&) noexcept = default; \
86 x &operator=(x &&) noexcept = default;
88#define KITTY_DEFAULT_CONSTR_MOVE_THROW(x) \
90 x &operator=(x &&) = default; \
93#define KITTY_DEFAULT_CONSTR(x) \
94 KITTY_DEFAULT_CONSTR_MOVE(x) \
95 x(const x &) noexcept = default; \
96 x &operator=(const x &) = default;
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)
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)
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)
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)
120#define TUPLE_EL(a, b, expr) \
121 decltype(expr) a##_ = expr; \
122 auto &a = std::get<b>(a##_)
124#define TUPLE_EL_REF(a, b, expr) \
125 auto &a = std::get<b>(expr)
129 template<
template<
typename...>
class X,
class... Y>
132 template<
template<
typename...>
class X,
class... Y>
135 template<
template<
typename...>
class X,
class T,
class... Y>
138 template<
bool V,
class X,
class Y>
141 template<
class X,
class Y>
146 template<
class X,
class Y>
151 template<
bool V,
class X,
class Y>
154 template<
class... Ts>
156 using Ts::operator()...;
158 template<
class... Ts>
164 FailGuard() =
delete;
166 FailGuard(T &&f)
noexcept:
167 _func {std::forward<T>(f)} {
170 FailGuard(FailGuard &&other)
noexcept:
171 _func {std::move(other._func)} {
172 this->failure = other.failure;
174 other.failure =
false;
177 FailGuard(
const FailGuard &) =
delete;
179 FailGuard &operator=(
const FailGuard &) =
delete;
180 FailGuard &operator=(FailGuard &&other) =
delete;
182 ~FailGuard()
noexcept {
199 [[nodiscard]]
auto fail_guard(T &&f) {
204 void append_struct(std::vector<uint8_t> &buf,
const T &_struct) {
205 constexpr size_t data_len =
sizeof(_struct);
207 buf.reserve(data_len);
209 auto *data = (uint8_t *) &_struct;
211 for (
size_t x = 0; x < data_len; ++x) {
212 buf.push_back(data[x]);
222 const char _bits[16] {
241 char _hex[
sizeof(elem_type) * 2];
244 Hex(
const elem_type &elem,
bool 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];
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];
265 return _hex +
sizeof(elem_type) * 2;
268 const char *begin()
const {
272 const char *end()
const {
273 return _hex +
sizeof(elem_type) * 2;
276 const char *cbegin()
const {
280 const char *cend()
const {
281 return _hex +
sizeof(elem_type) * 2;
284 std::string to_string()
const {
285 return {begin(), end()};
288 std::string_view to_string_view()
const {
289 return {begin(),
sizeof(elem_type) * 2};
294 Hex<T> hex(
const T &elem,
bool rev =
false) {
299 std::string log_hex(
const T &value) {
300 return "0x" + Hex<T>(value,
false).to_string();
304 std::string hex_vec(It begin, It end,
bool rev =
false) {
305 auto str_size = 2 * std::distance(begin, end);
308 hex.resize(str_size);
310 const char _bits[16] {
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];
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];
346 std::string hex_vec(C &&c,
bool rev =
false) {
347 return hex_vec(std::begin(c), std::end(c), rev);
351 T from_hex(
const std::string_view &hex,
bool rev =
false) {
352 std::uint8_t buf[
sizeof(T)];
354 static char constexpr shift_bit =
'a' -
'A';
356 auto is_convertable = [](
char ch) ->
bool {
363 if (
'a' > ch || ch >
'z') {
370 auto buf_size = std::count_if(std::begin(hex), std::end(hex), is_convertable) / 2;
371 auto padding =
sizeof(T) - buf_size;
373 const char *data = hex.data() + hex.size() - 1;
375 auto convert = [](
char ch) -> std::uint8_t {
376 if (ch >=
'0' && ch <=
'9') {
377 return (std::uint8_t) ch -
'0';
380 return (std::uint8_t) (ch | (char) 32) -
'a' + (
char) 10;
383 std::fill_n(buf + buf_size, padding, 0);
385 std::for_each_n(buf, buf_size, [&](
auto &el) {
386 while (!is_convertable(*data)) {
389 std::uint8_t ch_r = convert(*data--);
391 while (!is_convertable(*data)) {
394 std::uint8_t ch_l = convert(*data--);
396 el = (ch_l << 4) | ch_r;
400 std::reverse(std::begin(buf), std::end(buf));
403 return *
reinterpret_cast<T *
>(buf);
406 inline std::string from_hex_vec(
const std::string &hex,
bool rev =
false) {
409 static char constexpr shift_bit =
'a' -
'A';
410 auto is_convertable = [](
char ch) ->
bool {
417 if (
'a' > ch || ch >
'z') {
424 auto buf_size = std::count_if(std::begin(hex), std::end(hex), is_convertable) / 2;
425 buf.resize(buf_size);
427 const char *data = hex.data() + hex.size() - 1;
429 auto convert = [](
char ch) -> std::uint8_t {
430 if (ch >=
'0' && ch <=
'9') {
431 return (std::uint8_t) ch -
'0';
434 return (std::uint8_t) (ch | (char) 32) -
'a' + (
char) 10;
437 for (
auto &el : buf) {
438 while (!is_convertable(*data)) {
441 std::uint8_t ch_r = convert(*data--);
443 while (!is_convertable(*data)) {
446 std::uint8_t ch_l = convert(*data--);
448 el = (ch_l << 4) | ch_r;
452 std::reverse(std::begin(buf), std::end(buf));
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())
465 if (val.is_string()) {
466 std::string s = val.get<std::string>();
467 if constexpr (std::is_same_v<T, int>) {
469 }
else if constexpr (std::is_same_v<T, double>) {
471 }
else if constexpr (std::is_same_v<T, bool>) {
473 }
else if constexpr (std::is_same_v<T, std::string>) {
476 return default_value;
479 return default_value;
485 using value_type = T;
487 std::size_t operator()(
const value_type &value)
const {
488 const auto *p =
reinterpret_cast<const char *
>(&value);
490 return std::hash<std::string_view> {}(std::string_view {p,
sizeof(value_type)});
495 auto enm(
const T &val) ->
const std::underlying_type_t<T> & {
496 return *
reinterpret_cast<const std::underlying_type_t<T> *
>(&val);
500 auto enm(T &val) -> std::underlying_type_t<T> & {
501 return *
reinterpret_cast<std::underlying_type_t<T> *
>(&val);
504 inline std::int64_t from_chars(
const char *begin,
const char *end) {
510 std::int64_t mul = 1;
511 while (begin != --end) {
512 res += (std::int64_t) (*end -
'0') * mul;
517 return *begin !=
'-' ? res + (std::int64_t) (*begin -
'0') * mul : -res;
520 inline std::int64_t from_view(
const std::string_view &number) {
521 return from_chars(std::begin(number), std::end(number));
524 template<
class X,
class Y>
525 class Either:
public std::variant<std::monostate, X, Y> {
527 using std::variant<std::monostate, X, Y>::variant;
529 constexpr bool has_left()
const {
530 return std::holds_alternative<X>(*
this);
533 constexpr bool has_right()
const {
534 return std::holds_alternative<Y>(*
this);
538 return std::get<X>(*
this);
542 return std::get<Y>(*
this);
545 const X &left()
const {
546 return std::get<X>(*
this);
549 const Y &right()
const {
550 return std::get<Y>(*
this);
555 template<
typename T,
typename D = std::default_delete<T>>
558 using element_type = T;
559 using pointer = element_type *;
560 using const_pointer = element_type
const *;
561 using deleter_type = D;
563 constexpr uniq_ptr()
noexcept:
567 constexpr uniq_ptr(std::nullptr_t)
noexcept:
571 uniq_ptr(
const uniq_ptr &other)
noexcept =
delete;
572 uniq_ptr &operator=(
const uniq_ptr &other)
noexcept =
delete;
575 uniq_ptr(V *p)
noexcept:
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");
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");
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");
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());
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");
604 reset(uniq.release());
613 void reset(pointer p = pointer()) {
631 const_pointer get()
const {
635 std::add_lvalue_reference_t<element_type const> operator*()
const {
639 std::add_lvalue_reference_t<element_type> operator*() {
643 const_pointer operator->()
const {
647 pointer operator->() {
651 pointer *operator&()
const {
655 pointer *operator&() {
659 deleter_type &get_deleter() {
663 const deleter_type &get_deleter()
const {
667 explicit operator bool()
const {
668 return _p !=
nullptr;
673 deleter_type _deleter;
676 template<
class T1,
class D1,
class T2,
class D2>
678 return x.get() == y.get();
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();
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();
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();
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();
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();
706 template<
class T,
class D>
711 template<
class T,
class D>
716 template<
class T,
class D>
721 template<
class T,
class D>
727 using shared_t = std::shared_ptr<typename P::element_type>;
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());
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 &;
753 wrap_ptr(std::unique_ptr<element_type> &&uniq_p):
755 _p {uniq_p.release()} {
758 wrap_ptr(wrap_ptr &&other):
759 _own_ptr {other._own_ptr},
761 other._own_ptr =
false;
764 wrap_ptr &operator=(wrap_ptr &&other)
noexcept {
771 _own_ptr = other._own_ptr;
772 other._own_ptr =
false;
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");
786 wrap_ptr &operator=(pointer p) {
805 const_reference operator*()
const {
809 reference operator*() {
813 const_pointer operator->()
const {
817 pointer operator->() {
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>;
833 template<
class T,
class V =
void>
837 struct __false_v<T, std::enable_if_t<instantiation_of_v<std::optional, T>>> {
838 static constexpr std::nullopt_t value = std::nullopt;
842 struct __false_v<T, std::enable_if_t<is_pointer_v<T>>> {
843 static constexpr std::nullptr_t value =
nullptr;
847 struct __false_v<T, std::enable_if_t<std::is_same_v<T, bool>>> {
848 static constexpr bool value =
false;
855 using optional_t = either_t<
856 (std::is_same_v<T, bool> || is_pointer_v<T>),
866 buffer_t(buffer_t &&o)
noexcept:
868 _buf {std::move(o._buf)} {
872 buffer_t(
const buffer_t &o):
874 _buf {std::make_unique<T[]>(_els)} {
875 std::copy(o.begin(), o.end(), begin());
878 buffer_t &operator=(buffer_t &&o)
noexcept {
879 std::swap(_els, o._els);
880 std::swap(_buf, o._buf);
885 explicit buffer_t(
size_t elements):
887 _buf {std::make_unique<T[]>(elements)} {
890 explicit buffer_t(
size_t elements,
const T &t):
892 _buf {std::make_unique<T[]>(elements)} {
893 std::fill_n(_buf.get(), elements, t);
896 T &operator[](
size_t el) {
900 const T &operator[](
size_t el)
const {
904 size_t size()
const {
908 void fake_resize(std::size_t els) {
916 const T *begin()
const {
921 return _buf.get() + _els;
924 const T *end()
const {
925 return _buf.get() + _els;
930 std::unique_ptr<T[]> _buf;
934 T either(std::optional<T> &&l, T &&r) {
936 return std::move(*l);
939 return std::forward<T>(r);
942 template<
class ReturnType,
class... Args>
944 typedef ReturnType (*type)(Args...);
947 template<class T, class ReturnType, typename Function<ReturnType, T>::type function>
951 void operator()(pointer p) {
956 template<class T, typename Function<void, T *>::type function>
960 template<class T, class ReturnType, typename Function<ReturnType, T *>::type function>
968 template<
class T,
class ReturnType, ReturnType (**function)(T *)>
973 template<
class T,
void (**function)(T *)>
974 using dyn_safe_ptr = safe_ptr<T, dynamic<T, void, function>>;
976 template<
class T,
class ReturnType, ReturnType (**function)(T *)>
977 using dyn_safe_ptr_v2 = safe_ptr<T, dynamic<T, ReturnType, function>>;
980 using c_ptr = safe_ptr<T, c_free<T>>;
983 std::string_view view(It begin, It end) {
984 return std::string_view {(
const char *) begin, (std::size_t) (end - begin)};
988 std::string_view view(
const T &data) {
989 return std::string_view((
const char *) &data,
sizeof(T));
996 friend std::ostream &operator<<(std::ostream &os,
const point_t &p) {
997 return (os <<
"Point(x: " << p.x <<
", y: " << p.y <<
")");
1002 template<
class T =
void>
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__)
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__)
1021 #error "Unknown Endianness"
1027 template<
class T,
class S =
void>
1031 struct endian_helper<T, std::enable_if_t<!(instantiation_of_v<std::optional, T>)>> {
1032 static inline T big(T x) {
1034 uint8_t *data =
reinterpret_cast<uint8_t *
>(&x);
1036 std::reverse(data, data +
sizeof(x));
1042 static inline T little(T x) {
1044 uint8_t *data =
reinterpret_cast<uint8_t *
>(&x);
1046 std::reverse(data, data +
sizeof(x));
1054 struct endian_helper<T, std::enable_if_t<instantiation_of_v<std::optional, T>>> {
1055 static inline T little(T x) {
1061 auto *data =
reinterpret_cast<uint8_t *
>(&*x);
1063 std::reverse(data, data +
sizeof(*x));
1069 static inline T big(T x) {
1075 auto *data =
reinterpret_cast<uint8_t *
>(&*x);
1077 std::reverse(data, data +
sizeof(*x));
1085 inline auto little(T x) {
1090 inline auto big(T x) {
1091 return endian_helper<T>::big(x);
@ big
big-endian target architecture