Commit 2d45696c authored by Xavier Thompson's avatar Xavier Thompson

Let cypclass refcount functions also accept non-cypclass args and just do nothing

parent 8d3b0171
......@@ -652,15 +652,24 @@ def init_builtin_structs():
name, "struct", scope, 1, None, cname = cname)
def inject_cypclass_refcount_macros():
incref_type = PyrexTypes.CFuncType(PyrexTypes.c_void_type, [PyrexTypes.CFuncTypeArg("obj", PyrexTypes.cy_object_type, None)], nogil = 1)
template_placeholder_type = PyrexTypes.TemplatePlaceholderType("T")
incref_type = PyrexTypes.CFuncType(
PyrexTypes.c_void_type, [PyrexTypes.CFuncTypeArg("obj", template_placeholder_type, None)],
nogil = 1,
templates = [template_placeholder_type]
)
# The decref macros set their argument to NULL when the counter reaches 0,
# so we pretend to Cython that it's a c++ function with the argument passed by reference.
# This keeps the compiler from using std::move when calling the macro for instance.
reference_to_cy_object_type = PyrexTypes.CReferenceType(PyrexTypes.cy_object_type)
decref_type = PyrexTypes.CFuncType(PyrexTypes.c_void_type, [PyrexTypes.CFuncTypeArg("obj", reference_to_cy_object_type, None)], nogil = 1)
reference_to_template_type = PyrexTypes.CReferenceType(template_placeholder_type)
decref_type = PyrexTypes.CFuncType(
PyrexTypes.c_void_type, [PyrexTypes.CFuncTypeArg("obj", reference_to_template_type, None)],
nogil = 1,
templates = [template_placeholder_type]
)
getref_type = PyrexTypes.CFuncType(PyrexTypes.c_int_type, [PyrexTypes.CFuncTypeArg("obj", PyrexTypes.cy_object_type, None)], nogil = 1)
getref_type = PyrexTypes.CFuncType(
PyrexTypes.c_int_type, [PyrexTypes.CFuncTypeArg("obj", PyrexTypes.cy_object_type, None)],
nogil = 1,
)
for macro, macro_type in [("Cy_INCREF", incref_type), ("Cy_DECREF", decref_type), ("Cy_XDECREF", decref_type), ("Cy_GETREF", getref_type)]:
builtin_scope.declare_builtin_cfunction(macro, macro_type, macro)
......
......@@ -123,12 +123,38 @@
virtual ~ActhonActivableClass();
};
static inline int _Cy_DECREF(CyObject *op) {
return op->CyObject_DECREF();
/*
* Let Cy_INCREF, Cy_DECREF and Cy_XDECREF accept any argument type
* but only do the work when the argument is actually a CyObject
*/
template <typename T, typename std::enable_if<!std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_DECREF(T) {}
template <typename T, typename std::enable_if<!std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_XDECREF(T) {}
template <typename T, typename std::enable_if<!std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_INCREF(T) {}
template <typename T, typename std::enable_if<std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_DECREF(T &op) {
if(op->CyObject_DECREF())
op = NULL;
}
static inline void _Cy_INCREF(CyObject *op) {
op->CyObject_INCREF();
template <typename T, typename std::enable_if<std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_XDECREF(T &op) {
if (op != NULL) {
if(op->CyObject_DECREF())
op = NULL;
}
}
template <typename T, typename std::enable_if<std::is_convertible<T, CyObject*>::value, int>::type = 0>
static inline void Cy_INCREF(T op) {
if (op != NULL)
op->CyObject_INCREF();
}
static inline int _Cy_GETREF(CyObject *op) {
......@@ -241,12 +267,10 @@
return underlying;
}
/* Cast argument to CyObject* type. */
#define _CyObject_CAST(op) op
#define Cy_INCREF(op) do {if (op != NULL) {_Cy_INCREF(_CyObject_CAST(op));}} while(0)
#define Cy_DECREF(op) do {if (_Cy_DECREF(_CyObject_CAST(op))) {op = NULL;}} while(0)
#define Cy_XDECREF(op) do {if (op != NULL) {Cy_DECREF(op);}} while(0)
#define Cy_GETREF(op) (_Cy_GETREF(_CyObject_CAST(op)))
#define Cy_GOTREF(op)
#define Cy_XGOTREF(op)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment