How to remove the "*" in c++ macro args?

77 views Asked by At

I wanna make function defines with macro, if the arg is pointer type, i will use its pointed type name, else if the arg is pure type, i will use its orignial type name.

So the question is how to remove the "*" automatically in c++ macro args?

I have tried some methods like use VA_ARGS or macro combinations, but i cannot find the way to solve this which seems impossible for me.

E.G:

#define MA(arg) REMOVE_STAR(arg)##_1(arg a);

// i hope we can get this done:
MA(char) // char_1(char a);
MA(char *) // char_1(char* a);
MA(int *) // int_1(char* a);

Below is some descriptions and background for this problem, as there are so many people do not understand why i am asking for this.

Actually i know the Macro is not a good way to do programming. But in essence it's a way to reduce a lot of duplicate coding things, and as we all know, the unreal engine use macro to achieve its runtime reflection system.

So, consider such situation: i need to design a python c++ extension which will declare a lot of python methods and bind this methods to types.

  1. let us refer some code from python mathmodule.c, u can see there are many macros for developers to reduce duplicated codes which cannot be done by Template coding.

#define FUNC1(funcname, func, can_overflow, docstring)                  \
    static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
        return math_1(args, func, can_overflow);                            \
    }\
    PyDoc_STRVAR(math_##funcname##_doc, docstring);

#define FUNC1A(funcname, func, docstring)                               \
    static PyObject * math_##funcname(PyObject *self, PyObject *args) { \
        return math_1a(args, func);                                     \
    }\
    PyDoc_STRVAR(math_##funcname##_doc, docstring);

#define FUNC2(funcname, func, docstring) \
    static PyObject * math_##funcname(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { \
        return math_2(args, nargs, func, #funcname); \
    }\
    PyDoc_STRVAR(math_##funcname##_doc, docstring);

FUNC1(acos, acos, 0,
      "acos($module, x, /)\n--\n\n"
      "Return the arc cosine (measured in radians) of x.\n\n"
      "The result is between 0 and pi.")
FUNC1(acosh, acosh, 0,
      "acosh($module, x, /)\n--\n\n"
      "Return the inverse hyperbolic cosine of x.")
FUNC1(asin, asin, 0,
      "asin($module, x, /)\n--\n\n"
      "Return the arc sine (measured in radians) of x.\n\n"
      "The result is between -pi/2 and pi/2.")
  1. What i am gonna to achieve is: i want to declare a c++ object/pointer python wrapper type, export this wrapper to vm and add methods for this wrapper. I need the method name defined by the given args like:
#define PY_TYPE_METHOD_DEF(cls, name) cls##_##name##_DEF
#define PY_TYPE_METHOD(flags, doc, cls, name, ...) \
static PyObject * cls##_##name(PyBind<cls>* Self, __VA_ARGS__);\
static const PyMethodDef PY_TYPE_METHOD_DEF(cls, name){ \
#name, (PyCFunction) (cls##_##name), flags, doc \
}; \
static PyObject * cls##_##name(PyBind<cls>* Self, __VA_ARGS__)

And after this defines, i can add method defines to wrapper type like:

auto py_type = type_builder.
    .AddMethod(PY_TYPE_METHOD_DEF({c_type}, {method1}))  
    .AddMethod(PY_TYPE_METHOD_DEF({c_type}, {method2}))
    .AddMethod(PY_TYPE_METHOD_DEF({c_type}, {method3}))
    ...
    .Build();
    

But it comes out that if the cls is a pointer type, like int* or any other type pointer, the macro are working so bad.

So i am searching for a way to remove the "*" automatically in the macro while i know it may difficulty and even impossible.

Thks.

0

There are 0 answers