hpi_classes.cpp

Go to the documentation of this file.
00001 
00015 /*  This program is free software; you can redistribute it and/or
00016  *  modify it under the terms of the GNU General Public
00017  *  License as published by the Free Software Foundation; either
00018  *  version 2 of the License, or (at your option) any later version.
00019  *
00020  *  This software is distributed in the hope that it will be useful,
00021  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023  *  General Public License for more details.
00024  *
00025  *  You should have received a copy of the GNU General Public
00026  *  License along with this software; if not, write to the Free Software
00027  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  *  KFJ 2011-01-18
00030  *
00031  */
00032 
00033 #include "hpi_classes.h"
00034 
00035 namespace hpi
00036 {
00037 
00038 PyObject* python_interface::load_module ( const char* name )
00039 {
00040 #ifdef HPI_VERBOSE
00041     fprintf ( stdout , "HPI: loading module %s\n" , name ) ;
00042 #endif
00043     PyObject* pModule = PyImport_ImportModule ( name );
00044     if ( ! pModule )
00045     {
00046         PyErr_Print () ;
00047         fprintf ( stderr , "HPI ERROR: Failed to load module %s\n" , name );
00048         return NULL ;
00049     }
00050     return pModule;
00051 }
00052 
00053 python_interface::~python_interface()
00054 {
00055     if ( activated ) // the interface was used, do cleanup
00056     {
00057 #ifdef HPI_VERBOSE
00058         fprintf ( stdout , "HPI: finalizing Python\n" ) ;
00059 #endif
00060         // cleaning up
00061         Py_XDECREF ( hsi_module ) ;
00062         Py_XDECREF ( hpi_module ) ;
00063         Py_Finalize();
00064     }
00065 }
00066 
00067 bool python_interface::activate()
00068 {
00069     if ( activated )
00070     {
00071         return true ;
00072     }
00073 #ifdef HPI_VERBOSE
00074     fprintf ( stdout , "HPI: initializing Python\n" ) ;
00075 #endif
00076     Py_Initialize();
00077     // load hsi module
00078     hsi_module = load_module ( "hsi" ) ;
00079     if ( hsi_module )
00080     {
00081         // load hpi module
00082         hpi_module = load_module ("hpi");
00083         if ( hpi_module )
00084         {
00085             activated = true ;
00086         }
00087         else
00088         {
00089             Py_DECREF ( hsi_module ) ;
00090         }
00091     }
00092     if ( ! activated )
00093     {
00094         Py_Finalize() ;
00095     }
00096     return activated ;
00097 }
00098 
00099 int python_interface::call_hpi ( const char* hpi_func ,
00100                 PyObject* pArgs )
00101 {
00102     int result = -1 ;
00103 #ifdef HPI_VERBOSE
00104     fprintf ( stdout , "HPI: calling %s\n" , hpi_func ) ;
00105 #endif
00106     // look up the desired function in the hpi module's namespace
00107     PyObject* pFunc = PyObject_GetAttrString ( hpi_module , hpi_func ) ;
00108     // if it's found and can be called, go ahead call it
00109     if ( pFunc && PyCallable_Check ( pFunc ) )
00110     {
00111         PyObject* pValue = PyObject_CallObject ( pFunc , pArgs ) ;
00112         if ( pValue != NULL )
00113         {
00114             // the function should have returned an integer
00115             result = PyInt_AsLong ( pValue ) ;
00116             Py_DECREF(pValue);
00117         }
00118     }
00119     Py_XDECREF ( pFunc ) ;
00120 
00121 #ifdef HPI_VERBOSE
00122     fprintf ( stdout , "HPI: call returns: %d\n" , result );
00123 #endif
00124     return result ;
00125 };
00126 
00127 PyObject* python_arglist::make_hsi_object ( const char* hsi_type ,
00128                             void* hugin_value )
00129 {
00130     swig_type_info* swigtype = SWIG_Python_TypeQuery ( hsi_type );
00131 #ifdef HPI_VERBOSE
00132     fprintf ( stdout ,
00133                 "HPI: making a %s from %p\n" ,
00134                 hsi_type ,
00135                 hugin_value );
00136 #endif
00137     if ( ! swigtype )
00138     {
00139         fprintf ( stderr,
00140                     "HPI ERROR: can't find SWIG type for %s\n" ,
00141                     hsi_type );
00142         return NULL;
00143     }
00144 
00145     // we have managed to gain the SWIG type information,
00146     // se we can generate the Python object
00147     return SWIG_NewPointerObj ( hugin_value , swigtype , 0 );
00148 };
00149 
00150 python_arglist::python_arglist ( int _argc ) : argc ( _argc ) , have ( 0 )
00151 {
00152     pArgs = PyTuple_New ( argc );
00153 }
00154 
00155 python_arglist::~python_arglist()
00156 {
00157     Py_DECREF ( pArgs ) ;
00158 }
00159 
00160 bool python_arglist::add ( PyObject* python_arg )
00161 {
00162     if ( ( ! python_arg ) || ( have >= argc ) )
00163     {
00164         return false ;
00165     }
00166     PyTuple_SetItem ( pArgs , have++ , python_arg ) ;
00167     return true ;
00168 }
00169 
00170 bool python_arglist::add ( const char* str )
00171 {
00172 #ifdef HPI_VERBOSE
00173     fprintf ( stdout , "HPI: making a PyString from '%s'\n" , str ) ;
00174 #endif
00175 
00176 #if PY_MAJOR_VERSION>=3
00177     return add ( PyUnicode_FromString(str));
00178 #else
00179     return add ( PyString_FromString(str));
00180 #endif
00181 }
00182 
00183 PyObject* python_arglist::yield()
00184 {
00185     if ( argc != have )
00186     {
00187         return NULL ;
00188     }
00189     return pArgs ;
00190 }
00191 
00192 } //namespace

Generated on Thu Apr 24 01:25:40 2014 for Hugintrunk by  doxygen 1.3.9.1