1

I am trying to wrap a class ContainerMap which exposes a single multimap data member:

    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;
    };
    }

Obviously, the C++ API to the said classes have more complexity in them, but at the Tcl level, I would just need to iterate over _contents elements and look at the inside of ContainedAttributes. I wrote SWIG wrapping code that looks like the following:

    %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

Obviously there is further code to wrap the other classes. Regardless of which version of SWIG I use, I always get the same error:

      > swig -c++ -tcl8 -ltclsh.i example.i
      .../share/swig/4.0.0/std/std_multimap.i:89: Error: Syntax error in input(3).

I have done a lot of trials including commenting some offending lines in the std_multimap.i file, but I cannot get this to even compile properly. Even after commenting the lines that make swig barf (lines 89 and 98), I still cannot compile the generated wrapper code as swig seems to want to generate contructor wrappers for the container class with a single string vector argument. Am I correct in concluding that there is in fact no support for multimap containers for a Tcl target, or am I just making some stupid mistake? If my conclusion is correct, how would you advise to write swig code in order to get iterators that I can use to read the contents of the multimap?

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
LMNCA
  • 21
  • 6

1 Answers1

0

I found the following link which provides an imperfect workaround. Imperfect in the sense that it does not provide a full-fledged wrapping of all the multi-map functionality, and the interface is awkward: e.g. iterators can only be manipulated as extension methods of the container itself. And of course, write access isn't provided, but for my purpose this limitation is ok. I am still out for a better answer.

LMNCA
  • 21
  • 6
  • Really would be grateful if somebody could give ANY feedback about that question. If some elaboration of my problem is required before some answer can be provided, could I at least know that? – LMNCA May 10 '19 at 13:56