1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| * #include <iostream>
* #include <fstream>
* #include <string>
* using namespace std;
*
* #include <boost/python.hpp>
* #include <graminit.h>
* using namespace boost::python;
*
* // Classe "Base" C++
* class Base
* {
* public:
* Base(int iPoids = 0) {_Poids = iPoids;};
* ~Base() {};
* int GetPoids() {return _Poids;};
* void SetPoids(int iPoids) {_Poids = iPoids;};
* private:
* int _Poids;
* };
*
* // Classe "Base" Python
* BOOST_PYTHON_MODULE(DIP)
* {
* class_<Base>("Base", init<int>())
* .def("GetPoids", &Base::GetPoids)
* .def("SetPoids", &Base::SetPoids)
* ;
* }
*
* string *readPythonScript( string fileName ); // fonction implémentée et qui marche, non explicité ici
*
* //-----------------------------------------------------------------------------
* // Name: main()
* // Desc: Application's main entry point.
* //-----------------------------------------------------------------------------
* void main( void )
* {
*
* Base Agent; // j'aimerais passer Agent dans le script
*
* if( PyImport_AppendInittab( "DIP", initDIP ) == -1 )
* throw runtime_error( "Failed to add \"DIP\" to the "
* "interpreter's built-in modules" );
*
* // Setup Python to be embedded...
* Py_Initialize();
*
* object main_module(( handle<>(borrowed(PyImport_AddModule("__main__")))));
* object main_namespace = main_module.attr("__dict__");
*
* string *pythonScript = readPythonScript( "test.py" );
*
* if( pythonScript != NULL )
* {
* PyRun_String( pythonScript->c_str(), Py_file_input,
* main_namespace.ptr(), main_namespace.ptr() );
* }
*
* delete pythonScript;
*
* Agent = extract<Base&>(main_namespace["Agent"]); //Récup de l'instance agent du script dans l'objet C++
* cout << "Agent: " << Agent.GetPoids() << endl;
*
* // Cleanup after Python...
* Py_Finalize();
*
* system("pause");
* } |
Partager