我正在尝试包装一个类ContainerMap,它公开了一个多映射数据成员:
namespace MYCLASSES {
class ContainedAttributes {
std::string _value;
};
class NameList {
public:
std::vector<std::string> _names;
};
typedef std::multimap<NameList, ContainedAttributes> ContainerMap;
class Container {
public:
ContainerMap _contents;
};
}显然,上述类的C++应用程序接口的复杂性更高,但在Tcl级别,我只需要遍历_contents元素并查看ContainedAttributes内部。我写了SWIG包装代码,看起来像下面这样:
%module myclasswrapper
%nodefaultctor; // Disable creation of default constructors
%nodefaultdtor; // Disable creation of default constructors
%include <stl.i>
%include <std_string.i>
%include <std_vector.i>
%include <std/std_multimap.i>
%{
#include "my_classes.h"
#include <vector>
#include <string>
#include <map>
%}
namespace MYCLASSES {
using namespace std;
class NameList {
vector<string> _names;
};
class Container {
};
class ContainedAttributes {
};
}
using namespace MYCLASSES;
using namespace std;
%template(ContainerMap) multimap<NameList, ContainedAttributes >;
%template(StringVector) vector<string>
namespace MYCLASSES {
%extend Container {
MYCLASSES::ContainerMap & get_contents {
return self->_contents;
}
}
<more code here>
}
%clearnodefaultctor; // Enable the creation of default constructors again
%clearnodefaultdtor; // Enable the creation of default constructors again显然,还有更多的代码来包装其他类。无论我使用哪个版本的SWIG,我总是得到相同的错误:
> swig -c++ -tcl8 -ltclsh.i example.i
.../share/swig/4.0.0/std/std_multimap.i:89: Error: Syntax error in input(3).我做了很多尝试,包括注释std_multimap.i文件中的一些有问题的行,但是我甚至不能正确地编译它。即使在注释了使swig barf的行(第89行和第98行)之后,我仍然无法编译生成的包装器代码,因为swig似乎希望使用单个字符串向量参数为容器类生成构造器包装器。我的结论是,实际上不支持Tcl目标的multimap容器,或者我只是犯了一些愚蠢的错误,这是正确的吗?如果我的结论是正确的,你会建议如何编写swig代码,以便获得我可以用来读取multimap内容的迭代器?
发布于 2019-05-09 22:42:03
我发现下面的link提供了一个不完美的解决方法。它并不完美,因为它没有提供对所有多map功能的全面包装,而且接口也很笨拙:例如,迭代器只能作为容器本身的扩展方法来操作。当然,没有提供写访问权限,但对我来说,这个限制是可以的。我仍然在寻找一个更好的答案。
https://stackoverflow.com/questions/56030654
复制相似问题