vector::size_type could not be converted to int - C#...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

vector::size_type could not be converted to int - C#...

Adam Fowler
Hello all,

Great work on SWIG. I’m having an interesting issue though. I’ve checked the list archive and read std_vector.i to no avail. I was hoping someone on the list could please help.

I had an issue whereby a typedef in my C++ code was being ignored by SWIG:-

typedef std::vector<Document> DocumentSet

The same for Collection, Permission, and other places I used the same approach…

This was parsed successfully, but no DocumentSet class was generated in my CS source. I got around this by including the following in my i file:-

using namespace std;
namespace std {
  %template(DocumentSet) std::vector<mlclient::Document>;
  %template(PermissionSet) std::vector<mlclient::Permission>;
  %template(CollectionSet) std::vector<mlclient::Collection>;
  %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
}


This gives me a DocumentSet class with all the lovely links to Enumerator, and the idiomatic methods I’d expect in C#. So far so good.

Trying to compile this though in Xamarin I get the following issue reported:-

Error: cannot convert type ‘mlclient.SWIGTYPE_p_std__vectorT_mlclient__Collection_t__size_type’ to ‘int’

And occurs in this CS code generated by Swig:-

  public int Capacity {
    get {
      return (int)capacity(); // <—— HERE IS THE ERROR
    }
    set {
      if (value < size())
        throw new global::System.ArgumentOutOfRangeException("Capacity");
      reserve((uint)value);
    }
  }  

This is in CollectionSet.cs.

I’m assuming some type mapping is needed for std::vector<Collection>::size_type, but I cannot find an example for vector’s size_type, which is a little strange given this must be very, very common. Reading csharp/std_vector.i does not reveal anything to get me around this issue.

In my i file I am including this too, which I believe should be sufficient for STL collection support:-
%module(directors="1")  mlclientcs

%include <exception.i>

%include <typemaps.i>

%include std_except.i
%include std_string.i
%include std_vector.i
%include std_map.i
%include std_pair.i

Could someone please suggest an approach? This is all that stands between me and a software release.

I can work around this by abstracting away all vector code from my C++ library, but I’d really rather not as the typedef does everything I need it too, and it’d be a lot of code and move my API away from C++ norms.

Thanks very much in advance.

System info:-
SWIG 3.0.10 (also tried on 3.0.8, same result)
Mac Yosemite


Best regards,

Adam.



Adam Fowler BSc MBCS

Public Sector Solution Architect, EMEA
MarkLogic Corporation
Phone: +447880358293
Blog: http://adamfowlerml.wordpress.com
Twitter: @adamfowleruk
Skype: adamfowleruk








------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: vector::size_type could not be converted to int - C#...

William S Fulton
You must have some customised typemaps or something else in your
interface file that is affecting the code generation. An ordering
problem perhaps, but hard to guess exactly what though. The following
stand-alone example works, so I would compare it with what you really
have:

%module(directors="1") mlclient

%inline %{
namespace mlclient {
  class Document {};
  class Permission {};
  class Collection {};
  class DocumentUri {};
}
%}


%include <exception.i>

%include <typemaps.i>

%include std_except.i
%include std_string.i
%include std_vector.i
%include std_map.i
%include std_pair.i

using namespace std; // remove this line 1
namespace std { // remove this line 2
  %template(DocumentSet) std::vector<mlclient::Document>;
  %template(PermissionSet) std::vector<mlclient::Permission>;
  %template(CollectionSet) std::vector<mlclient::Collection>;
  %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
} // remove this 3.

I suggest removing the 3 lines indicated though as using namespace is
not always 100% in SWIG.

William

On 4 September 2016 at 17:55, Adam Fowler <[hidden email]> wrote:

> Hello all,
>
> Great work on SWIG. I’m having an interesting issue though. I’ve checked the list archive and read std_vector.i to no avail. I was hoping someone on the list could please help.
>
> I had an issue whereby a typedef in my C++ code was being ignored by SWIG:-
>
> typedef std::vector<Document> DocumentSet
>
> The same for Collection, Permission, and other places I used the same approach…
>
> This was parsed successfully, but no DocumentSet class was generated in my CS source. I got around this by including the following in my i file:-
>
> using namespace std;
> namespace std {
>   %template(DocumentSet) std::vector<mlclient::Document>;
>   %template(PermissionSet) std::vector<mlclient::Permission>;
>   %template(CollectionSet) std::vector<mlclient::Collection>;
>   %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
> }
>
>
> This gives me a DocumentSet class with all the lovely links to Enumerator, and the idiomatic methods I’d expect in C#. So far so good.
>
> Trying to compile this though in Xamarin I get the following issue reported:-
>
> Error: cannot convert type ‘mlclient.SWIGTYPE_p_std__vectorT_mlclient__Collection_t__size_type’ to ‘int’
>
> And occurs in this CS code generated by Swig:-
>
>   public int Capacity {
>     get {
>       return (int)capacity(); // <—— HERE IS THE ERROR
>     }
>     set {
>       if (value < size())
>         throw new global::System.ArgumentOutOfRangeException("Capacity");
>       reserve((uint)value);
>     }
>   }
>
> This is in CollectionSet.cs.
>
> I’m assuming some type mapping is needed for std::vector<Collection>::size_type, but I cannot find an example for vector’s size_type, which is a little strange given this must be very, very common. Reading csharp/std_vector.i does not reveal anything to get me around this issue.
>
> In my i file I am including this too, which I believe should be sufficient for STL collection support:-
> %module(directors="1")  mlclientcs
>
> %include <exception.i>
>
> %include <typemaps.i>
>
> %include std_except.i
> %include std_string.i
> %include std_vector.i
> %include std_map.i
> %include std_pair.i
>
> Could someone please suggest an approach? This is all that stands between me and a software release.
>
> I can work around this by abstracting away all vector code from my C++ library, but I’d really rather not as the typedef does everything I need it too, and it’d be a lot of code and move my API away from C++ norms.
>
> Thanks very much in advance.
>
> System info:-
> SWIG 3.0.10 (also tried on 3.0.8, same result)
> Mac Yosemite
>
>
> Best regards,
>
> Adam.
>
> —
>
> Adam Fowler BSc MBCS
>
> Public Sector Solution Architect, EMEA
> MarkLogic Corporation
> Phone: +447880358293
> Blog: http://adamfowlerml.wordpress.com
> Twitter: @adamfowleruk
> Skype: adamfowleruk
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: vector::size_type could not be converted to int - C#...

Adam Fowler
Hi William,

Thanks for this. I now have this all working. For the benefit of the list, what I needed was the below:-

%module(directors="1")  mlclientcs

%include <exception.i>

%include <typemaps.i>

%include std_except.i
%include std_string.i
%include std_vector.i
%include std_map.i
%include std_pair.i


// These are needed to avoid an error about not being able to convert std::vector<whatever>::size_t to an int in the Capacity() function created by SWIG
%apply int { size_t };
%apply int { uint };


SWIG_STD_VECTOR_ENHANCED(mlclient::Document)

// The following 3 are required because providing the typedef seems to stop SWIG applying the equivalent to std::vector<std::string>, from the bottom of csharp/std_vector.i
SWIG_STD_VECTOR_ENHANCED(mlclient::Collection)
SWIG_STD_VECTOR_ENHANCED(mlclient::DocumentUri)

%apply int { std::vector<mlclient::Collection>::size_type };
%apply int { std::vector<mlclient::DocumentUri>::size_type };
%apply int { std::vector<mlclient::Document>::size_type };
%apply int { std::vector<mlclient::Permission>::size_type };

using namespace std;
namespace std {
  %template(DocumentSet) std::vector<mlclient::Document>;
  %template(PermissionSet) std::vector<mlclient::Permission>;
  %template(CollectionSet) std::vector<mlclient::Collection>;
  %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
}


%feature("director") IBatchNotifiable; // This allows me to correctly create a C# subclass of a C++ abstract class

Also importantly, I think I found a bug! I had to change this line in csharp/std_vector.i otherwise the CS generated code would never compile:-

From 3.0.10 csharp/std_vector.i

  public int Capacity {
  get {
    return (int)capacity();
  }
  set {
    if (value < size())
    throw new global::System.ArgumentOutOfRangeException("Capacity");
    reserve((int)value); // <--- This was generated as (uint)value, but that fails with a conversion error. I had to manually hack this to get it all to compile and work.
  }
  }

How should I report this as a bug?



Thanks a lot for your help.

Best regards,

Adam.



Adam Fowler BSc MBCS

Public Sector Solution Architect, EMEA
MarkLogic Corporation
Phone: +447880358293
Blog: http://adamfowlerml.wordpress.com/ 
Twitter: @adamfowleruk
Skype: adamfowleruk








On 05/09/2016 07:35, "[hidden email] on behalf of William S Fulton" <[hidden email] on behalf of [hidden email]> wrote:

You must have some customised typemaps or something else in your
interface file that is affecting the code generation. An ordering
problem perhaps, but hard to guess exactly what though. The following
stand-alone example works, so I would compare it with what you really
have:

%module(directors="1") mlclient

%inline %{
namespace mlclient {
  class Document {};
  class Permission {};
  class Collection {};
  class DocumentUri {};
}
%}


%include <exception.i>

%include <typemaps.i>

%include std_except.i
%include std_string.i
%include std_vector.i
%include std_map.i
%include std_pair.i

using namespace std; // remove this line 1
namespace std { // remove this line 2
  %template(DocumentSet) std::vector<mlclient::Document>;
  %template(PermissionSet) std::vector<mlclient::Permission>;
  %template(CollectionSet) std::vector<mlclient::Collection>;
  %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
} // remove this 3.

I suggest removing the 3 lines indicated though as using namespace is
not always 100% in SWIG.

William

On 4 September 2016 at 17:55, Adam Fowler <[hidden email]> wrote:

> Hello all,
>
> Great work on SWIG. I’m having an interesting issue though. I’ve checked the list archive and read std_vector.i to no avail. I was hoping someone on the list could please help.
>
> I had an issue whereby a typedef in my C++ code was being ignored by SWIG:-
>
> typedef std::vector<Document> DocumentSet
>
> The same for Collection, Permission, and other places I used the same approach…
>
> This was parsed successfully, but no DocumentSet class was generated in my CS source. I got around this by including the following in my i file:-
>
> using namespace std;
> namespace std {
>   %template(DocumentSet) std::vector<mlclient::Document>;
>   %template(PermissionSet) std::vector<mlclient::Permission>;
>   %template(CollectionSet) std::vector<mlclient::Collection>;
>   %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
> }
>
>
> This gives me a DocumentSet class with all the lovely links to Enumerator, and the idiomatic methods I’d expect in C#. So far so good.
>
> Trying to compile this though in Xamarin I get the following issue reported:-
>
> Error: cannot convert type ‘mlclient.SWIGTYPE_p_std__vectorT_mlclient__Collection_t__size_type’ to ‘int’
>
> And occurs in this CS code generated by Swig:-
>
>   public int Capacity {
>     get {
>       return (int)capacity(); // <—— HERE IS THE ERROR
>     }
>     set {
>       if (value < size())
>         throw new global::System.ArgumentOutOfRangeException("Capacity");
>       reserve((uint)value);
>     }
>   }
>
> This is in CollectionSet.cs.
>
> I’m assuming some type mapping is needed for std::vector<Collection>::size_type, but I cannot find an example for vector’s size_type, which is a little strange given this must be very, very common. Reading csharp/std_vector.i does not reveal anything to get me around this issue.
>
> In my i file I am including this too, which I believe should be sufficient for STL collection support:-
> %module(directors="1")  mlclientcs
>
> %include <exception.i>
>
> %include <typemaps.i>
>
> %include std_except.i
> %include std_string.i
> %include std_vector.i
> %include std_map.i
> %include std_pair.i
>
> Could someone please suggest an approach? This is all that stands between me and a software release.
>
> I can work around this by abstracting away all vector code from my C++ library, but I’d really rather not as the typedef does everything I need it too, and it’d be a lot of code and move my API away from C++ norms.
>
> Thanks very much in advance.
>
> System info:-
> SWIG 3.0.10 (also tried on 3.0.8, same result)
> Mac Yosemite
>
>
> Best regards,
>
> Adam.
>
> —
>
> Adam Fowler BSc MBCS
>
> Public Sector Solution Architect, EMEA
> MarkLogic Corporation
> Phone: +447880358293
> Blog: http://adamfowlerml.wordpress.com
> Twitter: @adamfowleruk
> Skype: adamfowleruk
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: vector::size_type could not be converted to int - C#...

William S Fulton
No, no, no! While that may work, something has gone wrong as all the
extra customization should not be necessary. If you modify the
standalone example to replicate the problem, we can probably work out
what is wrong.

On 5 September 2016 at 15:19, Adam Fowler <[hidden email]> wrote:

> Hi William,
>
> Thanks for this. I now have this all working. For the benefit of the list, what I needed was the below:-
>
> %module(directors="1")  mlclientcs
>
> %include <exception.i>
>
> %include <typemaps.i>
>
> %include std_except.i
> %include std_string.i
> %include std_vector.i
> %include std_map.i
> %include std_pair.i
>
>
> // These are needed to avoid an error about not being able to convert std::vector<whatever>::size_t to an int in the Capacity() function created by SWIG
> %apply int { size_t };
> %apply int { uint };
>
>
> SWIG_STD_VECTOR_ENHANCED(mlclient::Document)
>
> // The following 3 are required because providing the typedef seems to stop SWIG applying the equivalent to std::vector<std::string>, from the bottom of csharp/std_vector.i
> SWIG_STD_VECTOR_ENHANCED(mlclient::Collection)
> SWIG_STD_VECTOR_ENHANCED(mlclient::DocumentUri)
>
> %apply int { std::vector<mlclient::Collection>::size_type };
> %apply int { std::vector<mlclient::DocumentUri>::size_type };
> %apply int { std::vector<mlclient::Document>::size_type };
> %apply int { std::vector<mlclient::Permission>::size_type };
>
> using namespace std;
> namespace std {
>   %template(DocumentSet) std::vector<mlclient::Document>;
>   %template(PermissionSet) std::vector<mlclient::Permission>;
>   %template(CollectionSet) std::vector<mlclient::Collection>;
>   %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
> }
>
>
> %feature("director") IBatchNotifiable; // This allows me to correctly create a C# subclass of a C++ abstract class
>
> Also importantly, I think I found a bug! I had to change this line in csharp/std_vector.i otherwise the CS generated code would never compile:-
>
> From 3.0.10 csharp/std_vector.i
>
>   public int Capacity {
>   get {
>     return (int)capacity();
>   }
>   set {
>     if (value < size())
>     throw new global::System.ArgumentOutOfRangeException("Capacity");
>     reserve((int)value); // <--- This was generated as (uint)value, but that fails with a conversion error. I had to manually hack this to get it all to compile and work.
>   }
>   }
>
> How should I report this as a bug?
>
>
>
> Thanks a lot for your help.
>
> Best regards,
>
> Adam.
>
> —
>
> Adam Fowler BSc MBCS
>
> Public Sector Solution Architect, EMEA
> MarkLogic Corporation
> Phone: +447880358293
> Blog: http://adamfowlerml.wordpress.com/
> Twitter: @adamfowleruk
> Skype: adamfowleruk
>
>
>
>
>
>
>
>
> On 05/09/2016 07:35, "[hidden email] on behalf of William S Fulton" <[hidden email] on behalf of [hidden email]> wrote:
>
> You must have some customised typemaps or something else in your
> interface file that is affecting the code generation. An ordering
> problem perhaps, but hard to guess exactly what though. The following
> stand-alone example works, so I would compare it with what you really
> have:
>
> %module(directors="1") mlclient
>
> %inline %{
> namespace mlclient {
>   class Document {};
>   class Permission {};
>   class Collection {};
>   class DocumentUri {};
> }
> %}
>
>
> %include <exception.i>
>
> %include <typemaps.i>
>
> %include std_except.i
> %include std_string.i
> %include std_vector.i
> %include std_map.i
> %include std_pair.i
>
> using namespace std; // remove this line 1
> namespace std { // remove this line 2
>   %template(DocumentSet) std::vector<mlclient::Document>;
>   %template(PermissionSet) std::vector<mlclient::Permission>;
>   %template(CollectionSet) std::vector<mlclient::Collection>;
>   %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
> } // remove this 3.
>
> I suggest removing the 3 lines indicated though as using namespace is
> not always 100% in SWIG.
>
> William
>
> On 4 September 2016 at 17:55, Adam Fowler <[hidden email]> wrote:
>> Hello all,
>>
>> Great work on SWIG. I’m having an interesting issue though. I’ve checked the list archive and read std_vector.i to no avail. I was hoping someone on the list could please help.
>>
>> I had an issue whereby a typedef in my C++ code was being ignored by SWIG:-
>>
>> typedef std::vector<Document> DocumentSet
>>
>> The same for Collection, Permission, and other places I used the same approach…
>>
>> This was parsed successfully, but no DocumentSet class was generated in my CS source. I got around this by including the following in my i file:-
>>
>> using namespace std;
>> namespace std {
>>   %template(DocumentSet) std::vector<mlclient::Document>;
>>   %template(PermissionSet) std::vector<mlclient::Permission>;
>>   %template(CollectionSet) std::vector<mlclient::Collection>;
>>   %template(DocumentUriSet) std::vector<mlclient::DocumentUri>;
>> }
>>
>>
>> This gives me a DocumentSet class with all the lovely links to Enumerator, and the idiomatic methods I’d expect in C#. So far so good.
>>
>> Trying to compile this though in Xamarin I get the following issue reported:-
>>
>> Error: cannot convert type ‘mlclient.SWIGTYPE_p_std__vectorT_mlclient__Collection_t__size_type’ to ‘int’
>>
>> And occurs in this CS code generated by Swig:-
>>
>>   public int Capacity {
>>     get {
>>       return (int)capacity(); // <—— HERE IS THE ERROR
>>     }
>>     set {
>>       if (value < size())
>>         throw new global::System.ArgumentOutOfRangeException("Capacity");
>>       reserve((uint)value);
>>     }
>>   }
>>
>> This is in CollectionSet.cs.
>>
>> I’m assuming some type mapping is needed for std::vector<Collection>::size_type, but I cannot find an example for vector’s size_type, which is a little strange given this must be very, very common. Reading csharp/std_vector.i does not reveal anything to get me around this issue.
>>
>> In my i file I am including this too, which I believe should be sufficient for STL collection support:-
>> %module(directors="1")  mlclientcs
>>
>> %include <exception.i>
>>
>> %include <typemaps.i>
>>
>> %include std_except.i
>> %include std_string.i
>> %include std_vector.i
>> %include std_map.i
>> %include std_pair.i
>>
>> Could someone please suggest an approach? This is all that stands between me and a software release.
>>
>> I can work around this by abstracting away all vector code from my C++ library, but I’d really rather not as the typedef does everything I need it too, and it’d be a lot of code and move my API away from C++ norms.
>>
>> Thanks very much in advance.
>>
>> System info:-
>> SWIG 3.0.10 (also tried on 3.0.8, same result)
>> Mac Yosemite
>>
>>
>> Best regards,
>>
>> Adam.
>>
>> —
>>
>> Adam Fowler BSc MBCS
>>
>> Public Sector Solution Architect, EMEA
>> MarkLogic Corporation
>> Phone: +447880358293
>> Blog: http://adamfowlerml.wordpress.com
>> Twitter: @adamfowleruk
>> Skype: adamfowleruk
>>
>>
>>
>>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Swig-user mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...