Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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;
31  MethodProxy::DispatchMap_t fDispatchMap;
32  MethodProxy::Methods_t fMethods;
33  UInt_t fFlags;
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)
52  MethodInfo_t* fMethodInfo;
53 
54  private: // private, as the python C-API will handle creation
55  MethodProxy() {}
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 -----------------------------------------------------------------
75  inline MethodProxy* MethodProxy_New(
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