llvmpy/newbinding/include/llvm_binding/capsule_context.h
Siu Kwan Lam a12c4da93d Init commit for work on new binding
This contains the foundation for the new binding as well as early work on Module and Type.
2013-02-05 13:47:32 -06:00

48 lines
1.2 KiB
C++

#ifndef LLVMPY_CAPSULE_CONTEXT_H_
#define LLVMPY_CAPSULE_CONTEXT_H_
#include <iostream>
#include <ctime>
typedef PyObject* Destructor_Fn;
static bool CapsuleContextDebug = false;
struct CapsuleContext {
const char* className;
Destructor_Fn destructor;
CapsuleContext(const char* cn, Destructor_Fn dtor=NULL)
: className(cn), destructor(dtor) { }
};
void capsule_destructor(PyObject* capsule){
using std::cerr;
using std::endl;
CapsuleContext* context = (CapsuleContext*)PyCapsule_GetContext(capsule);
if (context->destructor) {
if (CapsuleContextDebug) {
cerr << clock()
<< " == DEBUG =="
<< " destroy pointer: "
<< context->className
<< endl;
}
PyObject_CallMethodObjArgs(context->destructor, capsule, NULL);
} else {
if (CapsuleContextDebug) {
cerr << clock()
<< " == DEBUG =="
<< " keep pointer alive: "
<< context->className
<< endl;
}
}
delete context;
}
void enable_capsule_dtor_debug(bool enabled){
CapsuleContextDebug = enabled;
}
#endif //LLVMPY_CAPSULE_CONTEXT_H_