【问题标题】:SWIG: std/multimap.i seems broken for Tcl wrappingSWIG:std/multimap.i 似乎因 Tcl 包装而损坏
【发布时间】: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


    【解决方案1】:

    我发现以下link 提供了一个不完美的解决方法。从某种意义上说,它没有提供对所有多地图功能的完整包装,并且界面很尴尬:例如迭代器只能作为容器本身的扩展方法来操作。当然,没有提供写访问权限,但出于我的目的,这个限制是可以的。我还在寻找更好的答案。

    【讨论】:

    • 如果有人能就这个问题提供任何反馈,我将不胜感激。如果在提供答案之前需要对我的问题进行一些详细说明,我至少可以知道吗?
    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 2018-05-22
    • 2018-02-24
    • 2018-04-30
    • 2018-05-17
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多