MethodProxy.h

Go to the documentation of this file.
00001 // @(#)root/pyroot:$Id: MethodProxy.h 35563 2010-09-22 00:32:31Z wlav $
00002 // Author: Wim Lavrijsen, Jan 2005
00003 
00004 #ifndef PYROOT_METHODPROXY_H
00005 #define PYROOT_METHODPROXY_H
00006 
00007 // ROOT
00008 #include "DllImport.h"
00009 
00010 // Bindings
00011 #include "PyCallable.h"
00012 
00013 // Standard
00014 #include <map>
00015 #include <string>
00016 #include <vector>
00017 
00018 
00019 namespace PyROOT {
00020 
00021 /** Method proxy object to hold ROOT methods
00022       @author  WLAV
00023       @date    01/07/2005
00024       @version 1.0
00025  */
00026 
00027    class MethodProxy {
00028    public:
00029       typedef std::map< Long_t, Int_t >  DispatchMap_t;
00030       typedef std::vector< PyCallable* > Methods_t;
00031 
00032       struct MethodInfo_t {
00033          MethodInfo_t() : fFlags( kNone ) { fRefCount = new int(1); }
00034          ~MethodInfo_t();
00035 
00036          enum EMethodInfoFlags {
00037             kNone            =  0,
00038             kIsSorted        =  1,      // if method overload priority determined
00039             kIsCreator       =  2,      // if method creates python-owned objects
00040             kIsConstructor   =  4,      // if method is a C++ constructor
00041             kIsHeuristics    =  8,      // if method requires heuristics memory policy
00042             kIsStrict        = 16,      // if method requires strict memory policy
00043             kReleaseGIL      = 32       // if method should release the GIL
00044          };
00045 
00046          std::string                 fName;
00047          MethodProxy::DispatchMap_t  fDispatchMap;
00048          MethodProxy::Methods_t      fMethods;
00049          UInt_t                      fFlags;
00050 
00051          int* fRefCount;
00052       };
00053 
00054    public:
00055       void Set( const std::string& name, std::vector< PyCallable* >& methods );
00056 
00057       const std::string& GetName() const { return fMethodInfo->fName; }
00058       void AddMethod( PyCallable* pc );
00059       void AddMethod( MethodProxy* meth );
00060 
00061    public:               // public, as the python C-API works with C structs
00062       PyObject_HEAD
00063       ObjectProxy*   fSelf;        // The instance it is bound to, or NULL
00064       MethodInfo_t*  fMethodInfo;
00065 
00066    private:              // private, as the python C-API will handle creation
00067       MethodProxy() {}
00068    };
00069 
00070 
00071 //- method proxy type and type verification ----------------------------------
00072    R__EXTERN PyTypeObject MethodProxy_Type;
00073 
00074    template< typename T >
00075    inline Bool_t MethodProxy_Check( T* object )
00076    {
00077       return object && PyObject_TypeCheck( object, &MethodProxy_Type );
00078    }
00079 
00080    template< typename T >
00081    inline Bool_t MethodProxy_CheckExact( T* object )
00082    {
00083       return object && Py_TYPE(object) == &MethodProxy_Type;
00084    }
00085 
00086 //- creation -----------------------------------------------------------------
00087    inline MethodProxy* MethodProxy_New(
00088          const std::string& name, std::vector< PyCallable* >& methods )
00089    {
00090       MethodProxy* pymeth = (MethodProxy*)MethodProxy_Type.tp_new( &MethodProxy_Type, 0, 0 );
00091       pymeth->Set( name, methods );
00092       return pymeth;
00093    }
00094 
00095    inline MethodProxy* MethodProxy_New( const std::string& name, PyCallable* method )
00096    {
00097       std::vector< PyCallable* > p;
00098       p.push_back( method );
00099       return MethodProxy_New( name, p );
00100    }
00101 
00102 } // namespace PyROOT
00103 
00104 #endif // !PYROOT_METHODPROXY_H

Generated on Tue Jul 5 14:10:13 2011 for ROOT_528-00b_version by  doxygen 1.5.1