Logo ROOT   6.14/05
Reference Guide
MethodProxy.h
Go to the documentation of this file.
1 // @(#)root/pyroot:$Id$
2 // Author: Wim Lavrijsen, Jan 2005
3 
4 #ifndef PYROOT_METHODPROXY_H
5 #define PYROOT_METHODPROXY_H
6 
7 // ROOT
8 #include "DllImport.h"
9 
10 // Bindings
11 #include "PyCallable.h"
12 
13 // Standard
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 
19 namespace PyROOT {
20 
21  class MethodProxy {
22  public:
23  typedef std::map< Long_t, Int_t > DispatchMap_t;
24  typedef std::vector< PyCallable* > Methods_t;
25 
26  struct MethodInfo_t {
27  MethodInfo_t() : fFlags( TCallContext::kNone ) { fRefCount = new int(1); }
28  ~MethodInfo_t();
29 
30  std::string fName;
34 
35  int* fRefCount;
36 
37  private:
38  MethodInfo_t( const MethodInfo_t& ) = delete;
39  MethodInfo_t& operator=( const MethodInfo_t& ) = delete;
40  };
41 
42  public:
43  void Set( const std::string& name, std::vector< PyCallable* >& methods );
44 
45  const std::string& GetName() const { return fMethodInfo->fName; }
46  void AddMethod( PyCallable* pc );
47  void AddMethod( MethodProxy* meth );
48 
49  public: // public, as the python C-API works with C structs
50  PyObject_HEAD
51  ObjectProxy* fSelf; // must be first (same layout as TemplateProxy)
53 
54  private: // private, as the python C-API will handle creation
56  };
57 
58 
59 //- method proxy type and type verification ----------------------------------
60  R__EXTERN PyTypeObject MethodProxy_Type;
61 
62  template< typename T >
63  inline Bool_t MethodProxy_Check( T* object )
64  {
65  return object && PyObject_TypeCheck( object, &MethodProxy_Type );
66  }
67 
68  template< typename T >
69  inline Bool_t MethodProxy_CheckExact( T* object )
70  {
71  return object && Py_TYPE(object) == &MethodProxy_Type;
72  }
73 
74 //- creation -----------------------------------------------------------------
76  const std::string& name, std::vector< PyCallable* >& methods )
77  {
78  // Create and initialize a new method proxy from the overloads.
79  MethodProxy* pymeth = (MethodProxy*)MethodProxy_Type.tp_new( &MethodProxy_Type, 0, 0 );
80  pymeth->Set( name, methods );
81  return pymeth;
82  }
83 
84  inline MethodProxy* MethodProxy_New( const std::string& name, PyCallable* method )
85  {
86  // Create and initialize a new method proxy from the method.
87  std::vector< PyCallable* > p;
88  p.push_back( method );
89  return MethodProxy_New( name, p );
90  }
91 
92 } // namespace PyROOT
93 
94 #endif // !PYROOT_METHODPROXY_H
MethodInfo_t & operator=(const MethodInfo_t &)=delete
double T(double x)
Definition: ChebyshevPol.h:34
void Set(const std::string &name, std::vector< PyCallable * > &methods)
MethodProxy * MethodProxy_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition: MethodProxy.h:75
std::vector< PyCallable *> Methods_t
Definition: MethodProxy.h:24
bool Bool_t
Definition: RtypesCore.h:59
PyObject_HEAD ObjectProxy * fSelf
Definition: MethodProxy.h:51
MethodProxy::Methods_t fMethods
Definition: MethodProxy.h:32
MethodProxy::DispatchMap_t fDispatchMap
Definition: MethodProxy.h:31
std::map< Long_t, Int_t > DispatchMap_t
Definition: MethodProxy.h:23
unsigned int UInt_t
Definition: RtypesCore.h:42
PyTypeObject MethodProxy_Type
MethodInfo_t * fMethodInfo
Definition: MethodProxy.h:52
const std::string & GetName() const
Definition: MethodProxy.h:45
void AddMethod(PyCallable *pc)
Fill in the data of a freshly created method proxy.
~MethodInfo_t()
Destructor (this object is reference counted).
#define R__EXTERN
Definition: DllImport.h:27
#define Py_TYPE(ob)
Definition: PyROOT.h:151
Bool_t MethodProxy_Check(T *object)
Definition: MethodProxy.h:63
static constexpr double pc
Bool_t MethodProxy_CheckExact(T *object)
Definition: MethodProxy.h:69
char name[80]
Definition: TGX11.cxx:109