swig:std/multimap.i似乎打破了TCL包装

SWIG: std/multimap.i seems broken for Tcl wrapping

本文关键字:TCL 包装 std multimap swig      更新时间:2023-10-16

我正在尝试包装一个containermap,该类contermap暴露了单个多胶合数据成员:

    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元素,然后查看含有术的内部即可。我写了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似乎想要使用单个字符串矢量参数为容器类生成Contructor包装器。我是否正确地得出结论,实际上不支持用于TCL目标的多胶合容器,还是我只是犯了一些愚蠢的错误?如果我的结论是正确的,您将如何建议编写SWIG代码以获取我可以用来阅读多件内容的迭代器?

我找到了以下链接,该链接提供了不完美的解决方法。从某种意义上说,它不提供所有多映射功能的完整包装,并且接口很尴尬:例如,迭代器只能作为容器本身的扩展方法操纵。当然,没有提供写入访问,但是出于我的目的,此限制还可以。我仍然有一个更好的答案。