Converters.h

Go to the documentation of this file.
00001 // @(#)root/pyroot:$Id: Converters.h 30993 2009-11-05 17:11:15Z wlav $
00002 // Author: Wim Lavrijsen, Jan 2005
00003 #ifndef PYROOT_CONVERTERS_H
00004 #define PYROOT_CONVERTERS_H
00005 
00006 // ROOT
00007 #include "DllImport.h"
00008 #include "TString.h"
00009 #include "TClassRef.h"
00010 
00011 // CINT
00012 namespace Cint {
00013    class G__CallFunc;
00014    class G__TypeInfo;
00015 }
00016 using namespace Cint;
00017 
00018 // Standard
00019 #include <limits.h>
00020 #include <string>
00021 #include <map>
00022 
00023 
00024 namespace PyROOT {
00025 
00026 /** Python to CINT call converters
00027       @author  WLAV
00028       @date    01/26/2005
00029       @version 1.0
00030 */
00031 
00032    union TParameter;
00033 
00034    class TConverter {
00035    public:
00036       virtual ~TConverter() {}
00037 
00038    public:
00039       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 ) = 0;
00040       virtual PyObject* FromMemory( void* address );
00041       virtual Bool_t ToMemory( PyObject* value, void* address );
00042    };
00043 
00044 #define PYROOT_DECLARE_BASIC_CONVERTER( name )                                \
00045    class T##name##Converter : public TConverter {                             \
00046    public:                                                                    \
00047       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );\
00048       virtual PyObject* FromMemory( void* );                                  \
00049       virtual Bool_t ToMemory( PyObject*, void* );                            \
00050    }
00051 
00052 #define PYROOT_DECLARE_BASIC_CONVERTER2( name, base )                         \
00053    class T##name##Converter : public T##base##Converter {                     \
00054    public:                                                                    \
00055       virtual PyObject* FromMemory( void* );                                  \
00056       virtual Bool_t ToMemory( PyObject*, void* );                            \
00057    }
00058 
00059 #define PYROOT_DECLARE_ARRAY_CONVERTER( name )                                \
00060    class T##name##Converter : public TConverter {                             \
00061    public:                                                                    \
00062       T##name##Converter( Py_ssize_t size = -1 ) { fSize = size; }            \
00063       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );\
00064       virtual PyObject* FromMemory( void* );                                  \
00065       virtual Bool_t ToMemory( PyObject*, void* );                            \
00066    private:                                                                   \
00067       Py_ssize_t fSize;                                                       \
00068    }
00069 
00070 // converters for built-ins
00071    PYROOT_DECLARE_BASIC_CONVERTER( Long );
00072    PYROOT_DECLARE_BASIC_CONVERTER( LongRef );
00073    PYROOT_DECLARE_BASIC_CONVERTER( Bool );
00074    PYROOT_DECLARE_BASIC_CONVERTER( Char );
00075    PYROOT_DECLARE_BASIC_CONVERTER( UChar );
00076    PYROOT_DECLARE_BASIC_CONVERTER2( Short, Long );
00077    PYROOT_DECLARE_BASIC_CONVERTER2( UShort, Long );
00078    PYROOT_DECLARE_BASIC_CONVERTER2( Int, Long );
00079    PYROOT_DECLARE_BASIC_CONVERTER( IntRef );
00080    PYROOT_DECLARE_BASIC_CONVERTER( ULong );
00081    PYROOT_DECLARE_BASIC_CONVERTER2( UInt, ULong );
00082    PYROOT_DECLARE_BASIC_CONVERTER( LongLong );
00083    PYROOT_DECLARE_BASIC_CONVERTER( ULongLong );
00084    PYROOT_DECLARE_BASIC_CONVERTER( Double );
00085    PYROOT_DECLARE_BASIC_CONVERTER2( Float, Double );
00086    PYROOT_DECLARE_BASIC_CONVERTER( DoubleRef );
00087 
00088    class TConstLongRefConverter : public TLongConverter {
00089    public:
00090       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00091 
00092    private:
00093       long fBuffer;
00094    };
00095 
00096    class TConstDoubleRefConverter : public TDoubleConverter { // required for Cintex only
00097    public:
00098       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00099 
00100    private:
00101       double fBuffer;
00102    };
00103 
00104    class TVoidConverter : public TConverter {
00105    public:
00106       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00107    };
00108 
00109    class TMacroConverter : public TConverter {
00110    public:
00111       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00112       virtual PyObject* FromMemory( void* address );
00113    };
00114 
00115    class TCStringConverter : public TConverter {
00116    public:
00117       TCStringConverter( UInt_t maxSize = UINT_MAX ) : fMaxSize( maxSize ) {}
00118 
00119    public:
00120       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00121       virtual PyObject* FromMemory( void* address );
00122       virtual Bool_t ToMemory( PyObject* value, void* address );
00123 
00124    private:
00125       std::string fBuffer;
00126       UInt_t fMaxSize;
00127    };
00128 
00129    class TNonConstCStringConverter : public TCStringConverter {
00130    public:
00131       TNonConstCStringConverter( UInt_t maxSize = UINT_MAX ) : TCStringConverter( maxSize ) {}
00132 
00133    public:
00134       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00135    };
00136 
00137 // pointer/array conversions
00138    class TVoidArrayConverter : public TConverter {
00139    public:
00140       TVoidArrayConverter( Bool_t keepControl = kTRUE ) { fKeepControl = keepControl; }
00141       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00142       virtual PyObject* FromMemory( void* address );
00143       virtual Bool_t ToMemory( PyObject* value, void* address );
00144 
00145    protected:
00146       virtual Bool_t GetAddressSpecialCase( PyObject* pyobject, void*& address );
00147 
00148    protected:
00149       Bool_t KeepControl() { return fKeepControl; }
00150 
00151    private:
00152       Bool_t fKeepControl;
00153    };
00154 
00155    PYROOT_DECLARE_ARRAY_CONVERTER( ShortArray );
00156    PYROOT_DECLARE_ARRAY_CONVERTER( UShortArray );
00157    PYROOT_DECLARE_ARRAY_CONVERTER( IntArray );
00158    PYROOT_DECLARE_ARRAY_CONVERTER( UIntArray );
00159    PYROOT_DECLARE_ARRAY_CONVERTER( LongArray );
00160    PYROOT_DECLARE_ARRAY_CONVERTER( ULongArray );
00161    PYROOT_DECLARE_ARRAY_CONVERTER( FloatArray );
00162    PYROOT_DECLARE_ARRAY_CONVERTER( DoubleArray );
00163 
00164    class TLongLongArrayConverter : public TVoidArrayConverter {
00165    public:
00166       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00167    };
00168 
00169 // converters for special cases
00170    class TRootObjectConverter : public TVoidArrayConverter {
00171    public:
00172       TRootObjectConverter( const TClassRef& klass, Bool_t keepControl = kFALSE ) :
00173          TVoidArrayConverter( keepControl ), fClass( klass ) {}
00174 
00175    public:
00176       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00177       virtual PyObject* FromMemory( void* address );
00178       virtual Bool_t ToMemory( PyObject* value, void* address );
00179 
00180    protected:
00181       TClassRef fClass;
00182    };
00183 
00184    class TStrictRootObjectConverter : public TRootObjectConverter {
00185    public:
00186       TStrictRootObjectConverter( const TClassRef& klass, Bool_t keepControl = kFALSE ) :
00187          TRootObjectConverter( klass, keepControl ) {}
00188 
00189    protected:
00190       virtual Bool_t GetAddressSpecialCase( PyObject*, void*& ) { return kFALSE; }
00191    };
00192 
00193    class TRootObjectPtrConverter : public TRootObjectConverter {
00194    public:
00195       TRootObjectPtrConverter( const TClassRef& klass, Bool_t keepControl = kFALSE ) :
00196          TRootObjectConverter( klass, keepControl ) {}
00197 
00198    public:
00199       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00200       virtual PyObject* FromMemory( void* address );
00201       virtual Bool_t ToMemory( PyObject* value, void* address );
00202    };
00203 
00204    class TVoidPtrRefConverter : public TConverter {
00205    public:
00206       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00207    };
00208 
00209    class TVoidPtrPtrConverter : public TConverter {
00210    public:
00211       virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );
00212       virtual PyObject* FromMemory( void* address );
00213    };
00214 
00215    PYROOT_DECLARE_BASIC_CONVERTER( PyObject );
00216 
00217 #define PYROOT_DECLARE_STRING_CONVERTER( name, strtype )                      \
00218    class T##name##Converter : public TRootObjectConverter {                   \
00219    public:                                                                    \
00220       T##name##Converter();                                                   \
00221    public:                                                                    \
00222    virtual Bool_t SetArg( PyObject*, TParameter&, G__CallFunc* = 0, Long_t = 0 );\
00223       virtual PyObject* FromMemory( void* address );                          \
00224       virtual Bool_t ToMemory( PyObject* value, void* address );              \
00225    private:                                                                   \
00226       strtype fBuffer;                                                        \
00227    }
00228 
00229    PYROOT_DECLARE_STRING_CONVERTER( TString,   TString );
00230    PYROOT_DECLARE_STRING_CONVERTER( STLString, std::string );
00231 
00232 // factories
00233    typedef TConverter* (*ConverterFactory_t) ( Long_t user );
00234    typedef std::map< std::string, ConverterFactory_t > ConvFactories_t;
00235    R__EXTERN ConvFactories_t gConvFactories;
00236 
00237 // create converter from fully qualified type
00238    TConverter* CreateConverter( const std::string& fullType, Long_t user = -1 );
00239 
00240 } // namespace PyROOT
00241 
00242 #endif // !PYROOT_CONVERTERS_H

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