I'm experiencing an odd issue with a SWIG-generated Python wrapper to a C++ class, wherein I cannot seem to use the standard accessor functions of std::map when it is wrapped as a std::shared_ptr type. I managed to produce a MWE that reproduces the odd behavior I am observing.
TestMap.h
#include <iostream>
#include <map>
#include <memory>
class fooType{
public:
fooType() { };
~fooType() { };
void printFoo() { std::cerr << "FOO!" << std::endl; }
static std::shared_ptr<fooType> make_shared() {
return std::shared_ptr<fooType>(new fooType());
}
};
class testMap : public std::map<int, std::shared_ptr<fooType> > {
public:
void printBar() { std::cerr << "bar." << std::endl; }
};
And then my SWIG interface file:
TestMap.i
%module TestMap
%include <std_map.i>
%include <std_shared_ptr.i>
%{
#include "TestMap.h"
%}
%shared_ptr(fooType);
%shared_ptr(testMap);
%shared_ptr(std::map<int, std::shared_ptr<fooType> > );
%template(fooMap) std::map< int, std::shared_ptr<fooType> >;
%include "TestMap.h"
Finally, the test script I'm using to test out the interface:
test_interface.py
import TestMap as tm
ft = tm.fooType.make_shared()
myTestMap = tm.testMap()
myTestMap[1] = ft
As-written, I get the following error when I try to use the map accessor:
Traceback (most recent call last):
File "test_interface.py", line 9, in <module>
myTestMap[1] = ft
File "/home/sskutnik/tstSWIG/TestMap.py", line 217, in __setitem__
return _TestMap.fooMap___setitem__(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'fooMap___setitem__'.
Possible C/C++ prototypes are:
std::map< int,std::shared_ptr< fooType > >::__setitem__(std::map< int,std::shared_ptr< fooType > >::key_type const &)
std::map< int,std::shared_ptr< fooType > >::__setitem__(std::map< int,std::shared_ptr< fooType > >::key_type const &,std::map< int,std::shared_ptr< fooType > >::mapped_type const &
When I check the type of ft and myTestMap, both are std::shared_ptr references of their respective classes:
<TestMap.fooType; proxy of <Swig Object of type 'std::shared_ptr< fooType > *' at 0x7fa812e80a80> >
<TestMap.testMap; proxy of <Swig Object of type 'std::shared_ptr< testMap > *' at 0x7fa812e80c90> >
Now for the odd part - if I omit the %shared_ptr(TestMap) declaration from my SWIG interface file and recompile, the map accessor (in test_interface.py) happily works. When I check the type of myTestMap, it's:
<TestMap.testMap; proxy of <Swig Object of type 'testMap *' at 0x7f8eceb50630> >
So, two questions:
- Why does my accessor call work correctly when I have a SWIG object pointer reference (
testMap*), but not when I have ashared_ptrreference (e.g.,std::shared_ptr< testMap > *)? - How do I get around this, given that I need a
shared_ptrfor my derived map type?
Bonus question: why does SWIG automatically convert testMap* into a std::shared_ptr<testMap> type if I declare the existence of a shared_ptr type for the testMap type (even if it's not initialized as such?)