Код: Выделить всё
EventProcessorКод: Выделить всё
registerEventProcessor()). Но могу ли я определить класс в Python, который является производным от базового класса EventProcessorПростой класс C++ и глобальная функция для переноса представлены следующим заголовочным файлом.< /p>
example4.h
Код: Выделить всё
#pragma once
class EventProcessor
{
private:
int m_value;
public:
EventProcessor( void )
: m_value( 42 )
{
}
int getValue( void ) const
{
return m_value;
}
void setValue( int value )
{
m_value = value;
}
};
void registerEventProcessor( int packetId, EventProcessor * pProc )
{
if ( pProc )
{
// Something simple to demonstrate that the function is successful:
// Store the given packet ID.
pProc->setValue( packetId );
}
}
example4.swg
Код: Выделить всё
%module example4
%{
#define SWIG_FILE_WITH_INIT
#include "example4.h"
%}
%include "example4.h"
Код: Выделить всё
finch@laptop:~/work/swig_example$ swig -c++ -python example4.swg
finch@laptop:~/work/swig_example$ python3 example4_setup.py build_ext --inplace
running build_ext
building '_example4' extension
... a lot of output, no errors ...
Ниже показано, как я могу успешно создать экземпляр базового класса C++ и обеспечить его экземпляр глобальной функции. Глобальная функция сохраняет заданный идентификатор пакета в экземпляре, который я могу получить впоследствии.
Код: Выделить всё
finch@laptop:~/work/swig_example$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example4
>>> b = example4.EventProcessor()
>>> b.getValue()
42
>>> b.setValue( 20 )
>>> b.getValue()
20
>>> example4.registerEventProcessor( 30, b )
>>> b.getValue()
30
Производный класс Python представлен следующим файлом.
производный4.py
Код: Выделить всё
import example4
class SOFProcessor( example4.EventProcessor ):
def __init__( self ):
print( "SOFProcessor.ctor" )
Код: Выделить всё
finch@laptop:~/work/swig_example$ python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example4
>>> import derived4
>>> d = derived4.SOFProcessor()
SOFProcessor.ctor
Код: Выделить всё
>>> d.getValue()
Traceback (most recent call last):
File "", line 1, in
File "/home/finch/work/swig_example/example4.py", line 72, in getValue
return _example4.EventProcessor_getValue(self)
TypeError: in method 'EventProcessor_getValue', argument 1 of type 'EventProcessor const *'
Код: Выделить всё
>>> example4.registerEventProcessor( 100, d )
Traceback (most recent call last):
File "", line 1, in
File "/home/finch/work/swig_example/example4.py", line 83, in registerEventProcessor
return _example4.registerEventProcessor(packetId, pProc)
TypeError: in method 'registerEventProcessor', argument 2 of type 'EventProcessor *'
Есть ли определение типа, которое я могу определить в файле интерфейса SWIG, чтобы экземпляр производного Класс Python можно правильно обернуть и предоставить стороне C++?
Подробнее здесь: https://stackoverflow.com/questions/789 ... ed-c-class
Мобильная версия