remove unnecessary _()

This commit is contained in:
blueloveTH 2024-05-02 17:22:56 +08:00
parent a0f2c1a378
commit 3082645733
5 changed files with 9 additions and 21 deletions

View File

@ -122,9 +122,6 @@ struct wrapped__Point{
// wrapped value
Point value;
// special method _ returns a pointer of the wrapped value
Point* _() { return &value; }
// define default constructors
wrapped__Point() = default;
wrapped__Point(const wrapped__Point&) = default;
@ -136,9 +133,9 @@ struct wrapped__Point{
static void _register(VM* vm, PyObject* mod, PyObject* type){
// wrap field x
PY_FIELD(wrapped__Point, "x", x)
PY_FIELD(wrapped__Point, "x", value.x)
// wrap field y
PY_FIELD(wrapped__Point, "y", y)
PY_FIELD(wrapped__Point, "y", value.y)
// __init__ method
vm->bind(type, "__init__(self, x, y)", [](VM* vm, ArgsView args){

View File

@ -154,30 +154,29 @@ void _bind(VM* vm, PyObject* obj, const char* sig, Ret(T::*func)(Params...)){
/*****************************************************************/
#define PY_STRUCT_LIKE(wT) \
using vT = std::remove_pointer_t<decltype(std::declval<wT>()._())>; \
static_assert(std::is_trivially_copyable<vT>::value); \
static_assert(std::is_trivially_copyable<wT>::value); \
type->attr().set("__struct__", vm->True); \
vm->bind_func<1>(type, "from_struct", [](VM* vm, ArgsView args){ \
C99Struct& s = CAST(C99Struct&, args[0]); \
if(s.size != sizeof(vT)) vm->ValueError("size mismatch"); \
if(s.size != sizeof(wT)) vm->ValueError("size mismatch"); \
PyObject* obj = vm->new_user_object<wT>(); \
memcpy(_CAST(wT&, obj)._(), s.p, sizeof(vT)); \
memcpy(&_CAST(wT&, obj), s.p, sizeof(wT)); \
return obj; \
}, {}, BindType::STATICMETHOD); \
vm->bind_method<0>(type, "to_struct", [](VM* vm, ArgsView args){ \
wT& self = _CAST(wT&, args[0]); \
return vm->new_user_object<C99Struct>(self._(), sizeof(vT)); \
return vm->new_user_object<C99Struct>(&self, sizeof(wT)); \
}); \
vm->bind_method<0>(type, "addr", [](VM* vm, ArgsView args){ \
wT& self = _CAST(wT&, args[0]); \
return vm->new_user_object<VoidP>(self._()); \
return vm->new_user_object<VoidP>(&self); \
}); \
vm->bind_method<0>(type, "copy", [](VM* vm, ArgsView args){ \
wT& self = _CAST(wT&, args[0]); \
return vm->new_user_object<wT>(*self._()); \
return vm->new_user_object<wT>(self); \
}); \
vm->bind_method<0>(type, "sizeof", [](VM* vm, ArgsView args){ \
return VAR(sizeof(vT)); \
return VAR(sizeof(wT)); \
}); \
vm->bind__eq__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* _0, PyObject* _1){ \
wT& self = _CAST(wT&, _0); \

View File

@ -7,7 +7,6 @@ namespace pkpy{
inline bool isclose(float a, float b){ return std::fabs(a - b) < 1e-4; }
struct Vec2{
Vec2* _() { return this; }
static void _register(VM* vm, PyObject* mod, PyObject* type);
float x, y;
@ -33,7 +32,6 @@ struct Vec2{
};
struct Vec3{
Vec3* _() { return this; }
static void _register(VM* vm, PyObject* mod, PyObject* type);
float x, y, z;
@ -58,7 +56,6 @@ struct Vec3{
};
struct Vec4{
Vec4* _() { return this; }
static void _register(VM* vm, PyObject* mod, PyObject* type);
float x, y, z, w;
@ -82,7 +79,6 @@ struct Vec4{
};
struct Mat3x3{
Mat3x3* _(){ return this; }
static void _register(VM* vm, PyObject* mod, PyObject* type);
union {

View File

@ -17,8 +17,6 @@ struct Array2d{
numel = 0;
}
Array2d* _() { return this; }
void init(int n_cols, int n_rows){
this->n_cols = n_cols;
this->n_rows = n_rows;

View File

@ -36,8 +36,6 @@ struct PyStructTime{
tm_isdst = tm->tm_isdst;
}
PyStructTime* _() { return this; }
static void _register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_notimplemented_constructor<PyStructTime>(type);
PY_READONLY_FIELD(PyStructTime, "tm_year", tm_year);