#ifndef P4P_H #define P4P_H #include #include #include #include #include #include #include #include #include #include #include #include #ifdef READONLY // don't want def from shareLib.h # undef READONLY #endif #include #include typedef epicsGuard Guard; typedef epicsGuardRelease UnGuard; struct SB { std::ostringstream strm; operator std::string() { return strm.str(); } template SB& operator<<(const T& v) { strm<=201103L explicit operator bool() const { return valid(); } #else private: typedef bool (PyRef::*bool_type)() const; public: operator bool_type() const { return !!obj ? &PyRef::valid : 0; } #endif void swap(PyRef& o) { std::swap(obj, o.obj); } struct CollectReturn { PyObject *V; PyRef& R; CollectReturn(PyRef& R) :V(0), R(R) {} ~CollectReturn() { R.reset(V); } PyObject **get() { return &V; } }; CollectReturn collect() { return CollectReturn(*this); } }; struct PyString { PyObject *base; PyRef temp; explicit PyString(PyObject *b) :base(b) { if(PyUnicode_Check(b)) { temp.reset(PyUnicode_AsUTF8String(b)); if(!temp) throw std::runtime_error("PyString Unicode Error"); } else if(!PyBytes_Check(b)) throw std::runtime_error(SB()<tp_name<<" is not bytes or unicode"); } std::string str() { PyObject *X = temp ? temp.get() : base; return std::string(PyBytes_AS_STRING(X), PyBytes_GET_SIZE(X)); } }; // release GIL struct PyUnlock { PyThreadState *state; PyUnlock() :state(PyEval_SaveThread()) {} ~PyUnlock() { PyEval_RestoreThread(state); } }; // acquire GIL struct PyLock { PyGILState_STATE state; PyLock() :state(PyGILState_Ensure()) {} ~PyLock() { PyGILState_Release(state); } }; // helper when a PyRef may be free'd outside python code // beware of lock ordering wrt. the GIL struct PyExternalRef { PyRef ref; PyExternalRef() {} ~PyExternalRef() { if(ref) { PyLock G; ref.reset(); } } void swap(PyRef& o) { ref.swap(o); } void swap(PyExternalRef& o) { ref.swap(o.ref); } }; #define CATCH() catch(std::exception& e) { if(!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, e.what()); } } // enable extremely verbose low level debugging prints. #if 0 #define TRACING std::ostream& show_time(std::ostream&); #define TRACE(ARG) do{ show_time(std::cerr<<"TRACE ")<<__FUNCTION__<<" "<= 3 # define MODINIT_RET(VAL) return (VAL) #else # define MODINIT_RET(VAL) do {(void)(VAL); return; }while(0) #endif #if PY_MAJOR_VERSION >= 3 #define PyMOD(NAME) PyObject* PyInit_##NAME (void) #else #define PyMOD(NAME) void init##NAME (void) #endif #if PY_MAJOR_VERSION < 3 // quiet some warnings about implict const char* -> char* cast // for API functions. These are corrected in py >= 3.x #ifndef PyObject_CallFunction # define PyObject_CallFunction(O, FMT, ...) PyObject_CallFunction(O, (char*)(FMT), ##__VA_ARGS__) # define PyObject_CallMethod(O, METH, FMT, ...) PyObject_CallMethod(O, (char*)(METH), (char*)(FMT), ##__VA_ARGS__) #endif #endif void p4p_type_register(PyObject *mod); void p4p_value_register(PyObject *mod); void p4p_server_register(PyObject *mod); void p4p_server_sharedpv_register(PyObject *mod); void p4p_array_register(PyObject *mod); void p4p_client_register(PyObject *mod); epics::pvAccess::ChannelProvider::shared_pointer p4p_build_provider(PyRef &handler, const std::string& name); epics::pvAccess::ChannelProvider::shared_pointer p4p_unwrap_provider(PyObject *provider); PyObject* p4p_add_provider(PyObject *junk, PyObject *args, PyObject *kwds); PyObject* p4p_remove_provider(PyObject *junk, PyObject *args, PyObject *kwds); PyObject* p4p_remove_all(PyObject *junk, PyObject *args, PyObject *kwds); void p4p_server_provider_register(PyObject *mod); extern PyTypeObject* P4PType_type; // Extract Structure from P4PType PyObject* P4PType_wrap(PyTypeObject *type, const epics::pvData::Structure::const_shared_pointer &); epics::pvData::Structure::const_shared_pointer P4PType_unwrap(PyObject *); // Find a Field capable of storing the provided value epics::pvData::Field::const_shared_pointer P4PType_guess(PyObject *); typedef epics::pvData::shared_vector array_type; extern PyTypeObject* P4PArray_type; PyObject* P4PArray_make(const array_type& v); const array_type& P4PArray_extract(PyObject* o); extern PyTypeObject* P4PValue_type; epics::pvData::PVStructure::shared_pointer P4PValue_unwrap(PyObject *, epics::pvData::BitSet* =0); std::tr1::shared_ptr P4PValue_unwrap_bitset(PyObject *); PyObject *P4PValue_wrap(PyTypeObject *type, const epics::pvData::PVStructure::shared_pointer&, const epics::pvData::BitSet::shared_pointer& = epics::pvData::BitSet::shared_pointer()); extern PyObject* P4PCancelled; extern PyTypeObject* P4PSharedPV_type; std::tr1::shared_ptr P4PSharedPV_unwrap(PyObject *obj); PyObject *P4PSharedPV_wrap(const std::tr1::shared_ptr& pv); #define PyClassWrapper_DEF(TYPE, NAME) \ template<> PyTypeObject TYPE::type = { \ PyVarObject_HEAD_INIT(NULL, 0) \ "p4p._p4p." NAME, \ sizeof(TYPE), \ }; \ template<> size_t TYPE::num_instances = 0; template struct PyClassWrapper { PyObject_HEAD PyObject *weak; typedef C value_type; typedef C* pointer_type; typedef C& reference_type; C I; static size_t num_instances; static PyTypeObject type; static void buildType() { type.tp_flags = Py_TPFLAGS_DEFAULT; type.tp_new = &tp_new; type.tp_dealloc = &tp_dealloc; type.tp_weaklistoffset = offsetof(PyClassWrapper, weak); epics::registerRefCounter(type.tp_name, &num_instances); } static void finishType(PyObject *mod, const char *name) { if(PyType_Ready(&type)) throw std::runtime_error("failed to initialize extension type"); Py_INCREF((PyObject*)&type); if(PyModule_AddObject(mod, name, (PyObject*)&type)) { Py_DECREF((PyObject*)&type); throw std::runtime_error("failed to add extension type"); } } static PyObject* tp_new(PyTypeObject *atype, PyObject *args, PyObject *kwds) { try { if(!PyType_IsSubtype(atype, &type)) return PyErr_Format(PyExc_RuntimeError, "P4P tp_new inconsistency %s %s", atype->tp_name, type.tp_name); // we use python alloc instead of new here so that we could participate in GC PyRef self(atype->tp_alloc(atype, 0)); PyClassWrapper *SELF = (PyClassWrapper*)self.get(); SELF->weak = NULL; // The following can zero out the PyObject_HEAD members //new (self.get()) P4PType(); // instead we only C++ initialize the sub-struct C new (&SELF->I) C(); REFTRACE_INCREMENT(num_instances); TRACE("tp_new "<tp_name); return self.release(); } CATCH() return NULL; } static void tp_dealloc(PyObject *raw) { PyClassWrapper *self = (PyClassWrapper*)raw; TRACE("tp_dealloc "<tp_name); if(self->weak) PyObject_ClearWeakRefs(raw); PyTypeObject *klass = Py_TYPE(raw); if(klass->tp_clear) (klass->tp_clear)(raw); REFTRACE_DECREMENT(num_instances); try { if(unlockdtor) { PyUnlock U; self->I.~C(); } else { self->I.~C(); } } CATCH() Py_TYPE(self)->tp_free((PyObject*)self); } static C& unwrap(PyObject *obj) { if(!PyObject_TypeCheck(obj, &type)) throw std::runtime_error("Unable to unwrap, wrong type"); PyClassWrapper *W = (PyClassWrapper*)obj; return W->I; } static PyObject *wrap(C* self) { return (PyObject*)((char*)self - offsetof(PyClassWrapper, I)); } }; #endif // P4P_H