blueloveTH b126fc2c9b ...
2023-09-22 00:28:29 +08:00

154 lines
3.9 KiB
C++

#pragma once
#include <cmath>
#include <cstring>
#include <sstream>
#include <regex>
#include <stdexcept>
#include <vector>
#include <string>
#include <chrono>
#include <string_view>
#include <iomanip>
#include <memory>
#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <initializer_list>
#include <variant>
#include <type_traits>
#include <random>
#define PK_VERSION "1.2.2"
#include "config.h"
#include "export.h"
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
/*******************************************************************************/
#if PK_ENABLE_STD_FUNCTION
#include <functional>
#endif
/*******************************************************************************/
#if PK_ENABLE_THREAD
#define PK_THREAD_LOCAL thread_local
#include <mutex>
struct GIL {
inline static std::mutex _mutex;
explicit GIL() { _mutex.lock(); }
~GIL() { _mutex.unlock(); }
};
#define PK_GLOBAL_SCOPE_LOCK() GIL _lock;
#else
#define PK_THREAD_LOCAL
#define PK_GLOBAL_SCOPE_LOCK()
#endif
/*******************************************************************************/
#define PK_UNUSED(x) (void)(x)
#define PK_LOCAL_STATIC static
namespace pkpy{
namespace std = ::std;
template <size_t T>
struct NumberTraits;
template <>
struct NumberTraits<4> {
using int_t = int32_t;
using float_t = float;
template<typename... Args>
static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
static constexpr int_t c0 = 0b00000000011111111111111111111100;
static constexpr int_t c1 = 0b11111111111111111111111111111100;
static constexpr int_t c2 = 0b00000000000000000000000000000011;
};
template <>
struct NumberTraits<8> {
using int_t = int64_t;
using float_t = double;
template<typename... Args>
static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
};
using Number = NumberTraits<sizeof(void*)>;
using i64 = int64_t;
using f64 = Number::float_t;
static_assert(sizeof(i64) == 8);
static_assert(sizeof(f64) == sizeof(void*));
static_assert(std::numeric_limits<f64>::is_iec559);
struct Dummy { };
struct DummyInstance { };
struct DummyModule { };
struct NoReturn { };
struct Discarded { };
struct Type {
int index;
Type(): index(-1) {}
Type(int index): index(index) {}
bool operator==(Type other) const noexcept { return this->index == other.index; }
bool operator!=(Type other) const noexcept { return this->index != other.index; }
operator int() const noexcept { return this->index; }
};
#define PK_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
#define PK_VAR_LAMBDA(x) ([](VM* vm, ArgsView args) { return VAR(x); })
#define PK_ACTION(x) ([](VM* vm, ArgsView args) { x; return vm->None; })
#ifdef POCKETPY_H
#define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
#else
#define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
#endif
#define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
struct PyObject;
#define PK_BITS(p) (reinterpret_cast<Number::int_t>(p))
// special singals, is_tagged() for them is true
inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
// is_pod<> for c++17 and c++20
template<typename T>
struct is_pod {
static constexpr bool value = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
};
#define PK_ALWAYS_PASS_BY_POINTER(T) \
T(const T&) = delete; \
T& operator=(const T&) = delete; \
T(T&&) = delete; \
T& operator=(T&&) = delete;
} // namespace pkpy