SWIG c ++ / python: как работать с std :: map для shared_ptr абстрактного класса - PullRequest
1 голос
/ 28 апреля 2020

Как я могу обработать карту абстрактных методов в python с помощью SWIG из следующего кода C ++:

class A : Base {
    virtual int f() = 0;
};

class B : public A {
    int f() { return 10 }; 
}; 

class C : public A {
    int f() { return 20 }; 
}; 

std::map< std::string, std::shared_ptr<A>> my_map; 

В python я бы тоже хотел сделать нечто подобное:

my_B = B()
my_map["foo"] = my_B

или может быть проще:

my_map["foo"] = B()

Я должен уточнить, что A или B могут быть классом директора, чтобы использовать межъязыковой полиморфизм.

Мои вопросы:

  1. Каким может быть минимальный файл .i, связанный с этой проблемой?
  2. Я также читал, что это может вызвать проблему python / C ++, если к примеру будет удален my_B. Как я могу легко перевести владение "my_B" с python на C ++?

Большое спасибо за вашу помощь

A.

1 Ответ

0 голосов
/ 02 мая 2020

Вот рабочий пример с отслеживанием для строительства / разрушения, чтобы показать, что подсчет ссылок общего указателя работает:

test.h

#include <map>
#include <memory>
#include <string>
#include <iostream>

class A {
public:
    virtual int f() = 0;
    A() { std::cout << "A()" << std::endl; }
    virtual ~A() { std::cout << "~A()" << std::endl; }
};

class B : public A {
public:
    int f() { return 10; }
    B() { std::cout << "B()" << std::endl; }
    virtual ~B() { std::cout << "~B()" << std::endl; }
};

class C : public A {
public:
    int f() { return 20; }
    C() { std::cout << "C()" << std::endl; }
    virtual ~C() { std::cout << "~C()" << std::endl; }
};

std::map< std::string, std::shared_ptr<A>> my_map;

test.i

%module test

%{
#include "test.h"
%}

%include <std_map.i>
%include <std_shared_ptr.i>
%include <std_string.i>

// declare all visible shared pointers so SWIG generates appropriate wrappers
// before including the header.
%shared_ptr(A)
%shared_ptr(B)
%shared_ptr(C)

%include "test.h"

// Declare the template instance used so SWIG will generate the wrapper.
%template(Map) std::map<std::string, std::shared_ptr<A>>;

Выход:

>>> import test
>>>
>>> m=test.cvar.my_map    # global variables are in module's cvar.
>>> m['foo'] = test.C()
A()
C()
>>> m['foo'].f()
20
>>> del m['foo']  # only reference, so it is freed
~C()
~A()
>>> b = test.B()  # 1st reference
A()
B()
>>> m['bar'] = b  # 2nd reference
>>> del m['bar']  # NOT freed.
>>> del b         # now it is freed.
~B()
~A()
...