// liquidx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
使用BOOST::PYTHON来写脚本的方法:
# include
#include "sample.h"
#include
#include
class sample
{
int m_value;
public:
int GetValue();
void SetValue(int v);
sample(int v=0);
~sample(void);
};
typedef boost::shared_ptr pSample;
sample::sample(int v):m_value(v)
{
}
int sample::GetValue()
{
return m_value;
}
void sample::SetValue(int v)
{
m_value=v;
}
sample::~sample(void)
{
fprintf(stderr,"Delete sample address is %x\n",this);
}
namespace boost{
namespace python{
BOOST_PYTHON_MODULE(pySample)
{
class_("sample")
.def("get",&sample::GetValue)
.def("set",&sample::SetValue)
;
register_ptr_to_python< pSample >();
}
}
};
using namespace boost::python;
int _tmain(int argc, _TCHAR* argv[])
{
Py_Initialize();
initpySample();
try
{
object main_module = import("__main__");
object main_namespace = main_module.attr("__dict__");
pSample ptr( new sample(25) );
main_namespace["sample"] = ptr;
exec_file( "sample.py", main_namespace,main_namespace );
fprintf( stderr,"print in c:%d %d\n", ptr->GetValue(), ptr.use_count() );
}
catch(error_already_set const &)
{
PyErr_Print();
}
fprintf(stderr,"Before finalize\n");
Py_Finalize();
fprintf(stderr,"After finalize\n");
getchar();
return 0;
}
Sample.py 如下:
print "print in python",sample.get()
print sample
sample.set(12345)
结果:
print in python 25
print in c:12345 2
Before finalize
Delete sample address is b46638
After finalize