All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CallbackStreamBuf.cpp
Go to the documentation of this file.
1 #include "Python.h" //included for the python API
2 // ============================================================================
4 // ============================================================================
5 #include <sstream> //included for stringbuf
6 // ============================================================================
8 // ============================================================================
9 #include "GaudiPython/CallbackStreamBuf.h" //include header file
10 // ============================================================================
11 
12 #define min(a,b) (((a)<(b))?(a):(b))
13 
14 using namespace std;
15 
16 // ============================================================================
18 // ============================================================================
20 ( PyObject* self, const char* method, char* buf )
21 {
22  if ( 0 == self || 0 == method ) { return 1 ; }
23  PyObject* r = PyObject_CallMethod(self, const_cast<char*>(method),
24  const_cast<char*>("s"), buf);
25  if ( 0 == r ) {
26  string err("Unsuccessful call to bound Python method");
27  err += method;
28  PyErr_SetString( PyExc_TypeError , err.c_str() ) ;
29  PyErr_Print() ;
30  return 1 ;
31  }
32  Py_DECREF( r );
33  return 0 ;
34 }
35 
36 // ============================================================================
38 // ============================================================================
40 (PyObject* self): stringbuf(), m_self(self)
41 {
42  Py_INCREF(this->m_self);
43  this->m_callbackBuff = new char[512]; //used for passing the flushed chars in the python callback
44 }
45 
46 // ============================================================================
48 // ============================================================================
50 {
51  Py_DECREF( this->m_self );
52  delete this->m_callbackBuff;
53 }
54 
55 // ============================================================================
57 // ============================================================================
59 {
60  int length;
61  char *x;
62  for ( length = 0, x = this->pbase(); x < this->epptr(); x++ , length++ ) ;
63  //getting in a null terminated buffer the characters
64  memcpy( this->m_callbackBuff, this->pbase(), min(length, 512) );
65  this->m_callbackBuff[ min(length, 512) ] = '\0';
66  //calling the python method
67  GaudiPython::call_python_method(this->m_self, "_sync", this->m_callbackBuff);
68  //reseting in/out buffer pointers
69  setp( this->pbase() , this->pbase() );
70  setg( this->eback(), this->eback(), this->eback() );
71  return 0;
72 }
virtual int sync()
reimplementation of stringbuf::sync()
~CallbackStreamBuf()
CallbackStreamBuf destructor.
#define min(a, b)
CallbackStreamBuf(PyObject *self)
CallbackStreamBuf constructor.
GAUDI_API StatusCode call_python_method(PyObject *self, const char *method)
call the python method
Definition: Algorithm.cpp:30