common.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #pragma once
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <stdexcept>
  6. #include <vector>
  7. #include <string>
  8. #include <chrono>
  9. #include <string_view>
  10. #include <memory>
  11. #include <iostream>
  12. #include <map>
  13. #include <set>
  14. #include <algorithm>
  15. #include <variant>
  16. #include <type_traits>
  17. #include <deque>
  18. #include <typeindex>
  19. #include <initializer_list>
  20. #define PK_VERSION "1.4.6"
  21. #include "config.h"
  22. #include "export.h"
  23. #include "_generated.h"
  24. #ifdef min
  25. #undef min
  26. #endif
  27. #ifdef max
  28. #undef max
  29. #endif
  30. /*******************************************************************************/
  31. #if PK_ENABLE_STD_FUNCTION
  32. #include <functional>
  33. #endif
  34. /*******************************************************************************/
  35. #if PK_ENABLE_THREAD
  36. #define PK_THREAD_LOCAL thread_local
  37. #include <mutex>
  38. struct GIL {
  39. inline static std::mutex _mutex;
  40. explicit GIL() { _mutex.lock(); }
  41. ~GIL() { _mutex.unlock(); }
  42. };
  43. #define PK_GLOBAL_SCOPE_LOCK() GIL _lock;
  44. #else
  45. #define PK_THREAD_LOCAL
  46. #define PK_GLOBAL_SCOPE_LOCK()
  47. #endif
  48. /*******************************************************************************/
  49. #define PK_UNUSED(x) (void)(x)
  50. #define PK_LOCAL_STATIC static
  51. namespace pkpy{
  52. namespace std = ::std;
  53. template <size_t T>
  54. struct NumberTraits;
  55. template <>
  56. struct NumberTraits<4> {
  57. using int_t = int32_t;
  58. static constexpr int_t kMaxSmallInt = (1 << 28) - 1;
  59. static constexpr int_t kMinSmallInt = 0;
  60. };
  61. template <>
  62. struct NumberTraits<8> {
  63. using int_t = int64_t;
  64. static constexpr int_t kMaxSmallInt = (1ll << 60) - 1;
  65. static constexpr int_t kMinSmallInt = 0;
  66. };
  67. using Number = NumberTraits<sizeof(void*)>;
  68. using i64 = int64_t; // always 64-bit
  69. using f64 = double; // always 64-bit
  70. static_assert(sizeof(i64) == 8);
  71. struct Dummy { }; // for special objects: True, False, None, Ellipsis, etc.
  72. struct DummyInstance { };
  73. struct DummyModule { };
  74. struct NoReturn { };
  75. struct Discarded { };
  76. struct Type {
  77. int index;
  78. constexpr Type(): index(-1) {}
  79. constexpr Type(int index): index(index) {}
  80. bool operator==(Type other) const { return this->index == other.index; }
  81. bool operator!=(Type other) const { return this->index != other.index; }
  82. operator int() const { return this->index; }
  83. };
  84. #define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  85. #define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); })
  86. #define PK_ACTION(x) ([](VM* vm, ArgsView args) { x; return vm->None; })
  87. #ifdef POCKETPY_H
  88. #define PK_FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  89. #else
  90. #define PK_FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  91. #endif
  92. #define PK_ASSERT(x) if(!(x)) PK_FATAL_ERROR();
  93. #if PK_DEBUG_EXTRA_CHECK
  94. #define PK_DEBUG_ASSERT(x) if(!(x)) PK_FATAL_ERROR();
  95. #else
  96. #define PK_DEBUG_ASSERT(x)
  97. #endif
  98. struct PyObject;
  99. #define PK_BITS(p) (reinterpret_cast<i64>(p))
  100. #define PK_SMALL_INT(val) (reinterpret_cast<PyObject*>(val << 2 | 0b10))
  101. // is_pod<> for c++17 and c++20
  102. template<typename T>
  103. struct is_pod {
  104. static constexpr bool value = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
  105. };
  106. #define PK_ALWAYS_PASS_BY_POINTER(T) \
  107. T(const T&) = delete; \
  108. T& operator=(const T&) = delete; \
  109. T(T&&) = delete; \
  110. T& operator=(T&&) = delete;
  111. inline const char* kPlatformStrings[] = {
  112. "win32", // 0
  113. "emscripten", // 1
  114. "ios", // 2
  115. "darwin", // 3
  116. "android", // 4
  117. "linux", // 5
  118. "unknown" // 6
  119. };
  120. #define PK_SLICE_LOOP(i, start, stop, step) for(int i=start; step>0?i<stop:i>stop; i+=step)
  121. template<typename T>
  122. inline constexpr bool is_integral_v = std::is_same_v<T, char>
  123. || std::is_same_v<T, short>
  124. || std::is_same_v<T, int>
  125. || std::is_same_v<T, long>
  126. || std::is_same_v<T, long long>
  127. || std::is_same_v<T, unsigned char>
  128. || std::is_same_v<T, unsigned short>
  129. || std::is_same_v<T, unsigned int>
  130. || std::is_same_v<T, unsigned long>
  131. || std::is_same_v<T, unsigned long long>
  132. || std::is_same_v<T, signed char>; // for imgui
  133. template<typename T>
  134. inline constexpr bool is_floating_point_v = std::is_same_v<T, float> || std::is_same_v<T, double>;
  135. inline const char* PK_HEX_TABLE = "0123456789abcdef";
  136. } // namespace pkpy