Помимо Boost.Python (который, вероятно, является более дружественным решением для более крупных проектов, требующих однозначного сопоставления классов C ++ и классов Python), вы могли бы предоставить на стороне C ++ интерфейс C. Это одно из многих решений, поэтому у него есть свои компромиссы, но я представлю его для тех, кто не знаком с техникой. Для полного раскрытия, при таком подходе нельзя было бы сопрягать C ++ с Python, но C ++ с C на Python. Ниже я привел пример, который отвечает вашим требованиям, чтобы показать вам общее представление о внешнем средстве «c» компиляторов C ++.
//YourFile.cpp (compiled into a .dll or .so file)
#include <new> //For std::nothrow
//Either include a header defining your class, or define it here.
extern "C" //Tells the compile to use C-linkage for the next scope.
{
//Note: The interface this linkage region needs to use C only.
void * CreateInstanceOfClass( void )
{
// Note: Inside the function body, I can use C++.
return new(std::nothrow) MyClass;
}
//Thanks Chris.
void DeleteInstanceOfClass (void *ptr)
{
delete(std::nothrow) ptr;
}
int CallMemberTest(void *ptr)
{
// Note: A downside here is the lack of type safety.
// You could always internally(in the C++ library) save a reference to all
// pointers created of type MyClass and verify it is an element in that
//structure.
//
// Per comments with Andre, we should avoid throwing exceptions.
try
{
MyClass * ref = reinterpret_cast<MyClass *>(ptr);
return ref->Test();
}
catch(...)
{
return -1; //assuming -1 is an error condition.
}
}
} //End C linkage scope.
Вы можете скомпилировать этот код с помощью
gcc -shared -o test.so test.cpp
#creates test.so in your current working directory.
В своем коде Python вы можете сделать что-то вроде этого (показано интерактивное приглашение из 2.7):
>>> from ctypes import cdll
>>> stdc=cdll.LoadLibrary("libc.so.6") # or similar to load c library
>>> stdcpp=cdll.LoadLibrary("libstdc++.so.6") # or similar to load c++ library
>>> myLib=cdll.LoadLibrary("/path/to/test.so")
>>> spam = myLib.CreateInstanceOfClass()
>>> spam
[outputs the pointer address of the element]
>>> value=CallMemberTest(spam)
[does whatever Test does to the spam reference of the object]
Я уверен, что Boost.Python делает что-то похожее под капотом, но, возможно, полезно понять концепции нижних уровней. Я был бы более взволнован этим методом, если бы вы пытались получить доступ к функциональности библиотеки C ++, и отображение один-к-одному не требовалось.
Для получения дополнительной информации о взаимодействии C / C ++ посетите эту страницу от Sun: http://dsc.sun.com/solaris/articles/mixing.html#cpp_from_c