common.h 4.4 KB

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