【发布时间】:2019-09-25 14:12:23
【问题描述】:
我正在尝试包装一个公开单个多图数据成员的类 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++ API 更复杂,但在 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 目标的多映射容器,或者我只是犯了一些愚蠢的错误? 如果我的结论是正确的,您如何建议编写 swig 代码以获得可用于读取多图内容的迭代器?
【问题讨论】:
标签: c++ tcl swig readonly multimap