Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

FunctionWrap.cxx

Go to the documentation of this file.
00001 
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015 
00016 #include "FunctionWrap.h"
00017 #ifndef BOOST_DEFECT
00018 
00019 #include <vector>
00020 
00021 using std::string;
00022 using std::vector;
00023 
00024 using namespace boost::python;
00025 
00026 namespace hippodraw {
00027   namespace Python {
00028     void
00029     export_FunctionBase ()
00030     {
00031       class_ < FunctionWrap, std::auto_ptr < FunctionWrap> >
00032         ( "FunctionBase",
00033           "The base class for raw functions.   FunctionBase objects need to\n"
00034           "wrapped with Function objects to be displayed.   FunctionBase\n"
00035           "can be derived from and cloned so they can be added to the\n"
00036           "FunctionFactory.",
00037           init < const FunctionBase & >
00038           ( "FunctionBase () -> FunctionBase\n"
00039             "FunctinBase ( FunctionBase ) -> FunctionBase\n"
00040             "\n"
00041             "Constructors of the FuncitonBase object.\n" ) )
00042 
00043         .def ( init <> () )
00044 
00045         .def ( init < const FunctionWrap & > () )
00046 
00047         .def ( "initialize", &FunctionWrap::initialize,
00048                "initialize () -> None\n"
00049                "\n"
00050                "Initializes the function parameter values and the names.\n"
00051                "The number of parameters is taken from the number of names." )
00052 
00053         .def ( "name", &FunctionBase::name,
00054                return_value_policy < copy_const_reference > (),
00055                "name () -> string\n"
00056                "\n"
00057                "Returns the name of the function." )
00058 
00059         .def ( "setName", &FunctionWrap::setName,
00060                "setName ( string ) -> None\n"
00061                "\n"
00062                "Sets the name of the function." )
00063 
00064         .def ( "parmNames",
00065                &FunctionBase::parmNames,
00066                return_value_policy < copy_const_reference > (),
00067                "parmNames () -> sequence\n"
00068                "\n"
00069                "Returns the names of the parameters" )
00070 
00071         .def ( "setParmNames",
00072                &FunctionWrap::setParmNames,
00073                "setParmNames ( sequence ) -> None\n"
00074                "\n"
00075                "Sets the parameter names and resizes the parameters" )
00076 
00077         .def ( "setParameters", 
00078                ( void ( FunctionBase::* ) // function pointer cast
00079                ( const std::vector < double > & ) )  // function signature
00080                &FunctionBase::setParameters,
00081                "setParameters ( sequence ) -> None\n"
00082                "\n"
00083                "Sets the values of the parameters." )
00084 
00085         .def ( "getParameters", &FunctionBase::getParameters,
00086                return_value_policy < copy_const_reference > (),
00087                "getParameters () -> sequence\n"
00088                "\n"
00089                "Returns the current function parameter values." )
00090 
00091         ;
00092     }
00093   }
00094 }
00095 
00096 FunctionWrap::
00097 FunctionWrap ( )
00098   : FunctionBase ()
00099 {
00100 }
00101 
00102 FunctionWrap::
00103 FunctionWrap ( const FunctionBase & base )
00104   : FunctionBase ( base )//,
00105 {
00106 }
00107 
00108 FunctionWrap::
00109 FunctionWrap ( const FunctionWrap & wrap )
00110   : FunctionBase ( wrap )
00111 {
00112 }
00113 
00114 FunctionWrap::
00115 ~FunctionWrap ()
00116 {
00117 //   PyGILState_STATE state = PyGILState_Ensure ();
00118 
00119 //   extract<std::auto_ptr<FunctionWrap>&> x(get_owner(this));
00120 //   if ( x.check() ) x().release();
00121 
00122 //   PyGILState_Release ( state );
00123 }
00124 
00125 template <class T>
00126 object
00127 FunctionWrap::
00128 get_owner(T* me) const
00129 {
00130       // Use secret interface to get the Python object
00131       // that owns *this.  I guess I will have to make that
00132       // interface public. -- Dave Abrahams
00133       return
00134 object ( handle<> ( borrowed ( detail::wrapper_base_::get_owner(*this))));
00135 }
00136 
00137 
00138 FunctionBase *
00139 FunctionWrap::
00140 clone () const
00141 {
00142 #ifndef HAVE_OLD_PYTHON
00143   PyGILState_STATE state = PyGILState_Ensure ();
00144   object py_result;
00145 
00146   if (override clone = this->get_override("clone"))
00147     {
00148       // The Python class author overrode clone; do
00149       // whatever she says
00150       py_result = clone();
00151     }
00152   else
00153     {
00154       FunctionWrap * t = const_cast < FunctionWrap * > ( this );
00155       object self = get_owner ( t );
00156 
00157       // Find its most-derived Python class
00158       object my_class = self.attr("__class__");
00159 
00160       // call the copy ctor through Python.
00161       py_result = my_class ( self );
00162     }
00163   FunctionWrap* result = extract<FunctionWrap*>(py_result);
00164 
00165   // Make the C++ result control the destiny of the Python result.
00166   result->invert_ownership = py_result;
00167   PyGILState_Release ( state );
00168 
00169   return result;
00170 #else // old Python
00171   return 0;
00172 #endif
00173 }
00174 
00179 void
00180 FunctionWrap::
00181 setName ( const std::string & name )
00182 {
00183   FunctionBase::setName ( name.c_str() );
00184 }
00185 
00186 void
00187 FunctionWrap::
00188 setParmNames ( const std::vector < std::string > & names )
00189 {
00190   m_parm_names = names;
00191   resize ();
00192 }
00193 
00196 double
00197 FunctionWrap::
00198 derivByParm ( int i , double x ) const
00199 {
00200 #ifndef HAVE_OLD_PYTHON
00201   double value = 0.;
00202   PyGILState_STATE state = PyGILState_Ensure ();
00203 
00204   FunctionWrap * t = const_cast < FunctionWrap * > ( this );
00205   object self = get_owner ( t );
00206 
00207   try {
00208     value = call_method < double, int, double > 
00209       ( self.ptr(), "derivByParm", i, x );
00210   }  catch ( ... ) {
00211     value = 0.;
00212   }
00213 
00214   PyGILState_Release ( state );
00215 
00216   return value;
00217 #else // old Python
00218   return 0;
00219 #endif
00220 }
00221 
00224 void
00225 FunctionWrap::
00226 initialize ()
00227 {
00228 #ifndef HAVE_OLD_PYTHON
00229   PyGILState_STATE state = PyGILState_Ensure ();
00230 
00231   FunctionWrap * t = const_cast < FunctionWrap * > ( this );
00232   object self = get_owner ( t );
00233   call_method < void > ( self.ptr(), "initialize" );
00234   resize ();
00235 
00236   PyGILState_Release ( state );
00237 #endif
00238 }
00239 
00242 double
00243 FunctionWrap::
00244 operator () ( double x ) const
00245 {
00246 #ifndef HAVE_OLD_PYTHON
00247   PyGILState_STATE state = PyGILState_Ensure ();
00248   FunctionWrap * t = const_cast < FunctionWrap * > ( this );
00249   object self = get_owner ( t );
00250   double value = call_method < double, double > ( self.ptr(), "valueAt", x );
00251   PyGILState_Release ( state );
00252 
00253   return value;
00254 #else // old Python
00255   return 0;
00256 #endif
00257 }
00258 
00261 double
00262 FunctionWrap::
00263 operator () ( const std::vector < double > & x ) const
00264 {
00265 #ifndef HAVE_OLD_PYTHON
00266   PyGILState_STATE state = PyGILState_Ensure ();
00267   FunctionWrap * t = const_cast < FunctionWrap * > ( this );
00268   object self = get_owner ( t );
00269   double value 
00270     = call_method < double, double, double > 
00271     ( self.ptr(), "valueAt", x[0], x[1] );
00272   PyGILState_Release ( state );
00273 
00274   return value;
00275 #else // old Python
00276   return 0;
00277 #endif
00278 }
00279 
00280 void
00281 FunctionWrap::
00282 initialParameters ( const FunctionHelper * )
00283 {
00284 }
00285 
00286 #endif // BOOST_DEFECT

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3