Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

pythonsample.cpp

Go to the documentation of this file.
00001 
00002 
00003 // please define EXP_PYTHON_EMBEDDING in Makefile or ProjectFile
00004 
00005 #include "Value.h"
00006 
00007 extern "C" void initCValue(void);
00008 extern "C" void initExpressionModule(void);
00009 #include "StringValue.h"
00010 #include "IntValue.h"
00011 
00012 CValue* gTestValue = NULL;
00013 
00014 PyObject*       MyTake(PyObject* ignored,PyObject* args)

00015 {
00016         if (!gTestValue)
00017         {
00018                 gTestValue = new CStringValue("cpp_value","");
00019         }
00020         return gTestValue->AddRef();
00021 }
00022 
00023 static PyMethodDef      CTestMethods[] = 
00024 {
00025         { "take", MyTake , Py_NEWARGS},
00026         { NULL,NULL}    // Sentinel
00027 };
00028 
00029 extern "C" {
00030         void initTestModule(void)

00031         {
00032                 Py_InitModule("TestModule",CTestMethods);
00033         }
00034 }
00035 
00036 void main(int argc,char**argv)

00037 {
00038         Py_SetProgramName(argv[0]);
00039         Py_Initialize();
00040         initCValue();
00041         initExpressionModule();
00042         initTestModule();
00043 
00044         gTestValue = new CStringValue("cpp_parent_object","");
00045         CValue* innie = new CIntValue(15);
00046         innie->SetProperty("subproperty",new CStringValue("subprop_value",""));
00047         gTestValue->SetProperty("cpp_property",innie);
00048 
00049         // python can retrieve properties from C++ object, and vice versa
00050         PyRun_SimpleString("import TestModule\n");      
00051         PyRun_SimpleString("A = TestModule.take()\n");
00052         PyRun_SimpleString("print A\n");
00053         PyRun_SimpleString("P = hasattr(A,\"cpp_property\")\n");
00054         PyRun_SimpleString("print P\n");
00055         PyRun_SimpleString("B = A.cpp_property.subproperty\n");
00056         
00057         PyRun_SimpleString("print B\n");
00058         PyRun_SimpleString("B = 2000\n");
00059         PyRun_SimpleString("A.appel=\"greetings_from_within_python\"\n");
00060                 
00061         /*      

00062         PyRun_SimpleString("import TestModule\n");

00063         PyRun_SimpleString("A = Expression.calc(\"10.1\")\n");

00064         PyRun_SimpleString("B = Expression.calc(\"23.4\")\n");

00065         PyRun_SimpleString("C = Expression.calc(\"43.2\")\n");

00066         PyRun_SimpleString("D = -A \n");

00067         PyRun_SimpleString("F = 10\n");

00068         PyRun_SimpleString("F.subresult.bluf = 10\n");

00069         PyRun_SimpleString("E = D.subresult.bluf\n");

00070         PyRun_SimpleString("print E\n");

00071         */
00072         Py_Finalize();
00073 
00074         if (gTestValue)
00075         {
00076                 // gTestValue will have two properties, created from within previous (not existing anymore!) python interpreter
00077                 int numprops = gTestValue->GetPropertyCount();
00078                 for (int i=0;i<numprops;i++)
00079                 {
00080                         CValue* tmpprop = gTestValue->GetProperty(i);
00081                         printf(tmpprop->GetText());
00082                 }
00083                 gTestValue->Release();
00084         }
00085 }

Generated at Thu Feb 1 13:03:08 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000