New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C#] - inheritance lost in vector #1007
Comments
For sure, I tried again, it seems that returning the element by copy preserves the inheritance, but by const reference it doesn't. |
It seems it doesn't come from a wrong vector wrapper implementation, but from a problem that has already been fedback. Same issue in Java here; |
Heyo @schullq , have you tried https://stackoverflow.com/a/7160064/3938401? I'm using it in my C# SWIG-based project for casting from a parent to a child, and it may work similarly for you here.
|
Thanks for your answer. However, I was aware of this way of doing, it just fixes the problem partially. To be more precise, I think the problem comes from the "getitem(int index)" method defined in std_vector.i. |
@schullq this is a problem in your C++ code. Read around "object slicing". |
I may have been unclear, but I have a vector of pointers to class A. So the rules of object slicing shouldn't happen. I am still trying to find the problem out anyway, maybe I missed something and I am wrong. I'll let you know. |
Are you using pointers or shared_ptr? If shared_ptr as you put in your edited post, you are not using the shared_ptr typemaps and the exception message in your post is not possible. I think it would help if you gave all the details of exactly what you are doing with a small standalone example. |
Ok well: readerprovider.hpp:
examplereaderprovider.hpp:
core.i
readerconfig.cs (extract)
Output:
|
This is definitely not a standalone example of the problem. I started fixing numerous errors and don't have time to finish guessing what you are doing. Using %inline, you should be able to provide one SWIG interface file for SWIG to use. Then provide a snippet of C# code using the resulting output. |
Ok, sorry, my bad, I will give you a full standalone example in one interface. |
Here is a little standalone, I tried to simplify as much as I could, to obtain the same issue I had, in the exact same case. sample.i:
I compiled the interface using the following line:
And finally a little test case in C#:
Hope this time you will understand my point. |
--- example-original.i 2017-07-17 18:05:13.413154777 +0100
+++ example.i 2017-07-17 19:18:26.688651748 +0100
@@ -33,17 +33,18 @@
%shared_ptr(Human);
%shared_ptr(Pilot);
-%template(HumanEnableShared) std::enable_shared_from_this<Human>;
-%template(HumanFeatureEnableShared) std::enable_shared_from_this<HumanFeature>;
-%template(HumanFeaturePtrVector) std::vector<std::shared_ptr<HumanFeature> >;
-
%typemap(csout, excode=SWIGEXCODE)
+ std::shared_ptr<HumanFeature> &,
HumanFeature*, std::shared_ptr<HumanFeature> {
System.IntPtr cPtr = $imcall;
- HumanFeature ret = samplePINVOKE.createHumanFeature(cPtr, $owner);$excode
+ HumanFeature ret = $modulePINVOKE.createHumanFeature(cPtr, $owner);$excode
return ret;
}
+%template(HumanEnableShared) std::enable_shared_from_this<Human>;
+%template(HumanFeatureEnableShared) std::enable_shared_from_this<HumanFeature>;
+%template(HumanFeaturePtrVector) std::vector<std::shared_ptr<HumanFeature> >;
+
%inline %{
class HumanFeature : public std::enable_shared_from_this<HumanFeature>
{ Why you might ask? Should be clear if you run swig with the template<class T> class vector {
...
T const& getitem(int index) throw (std::out_of_range) { I would customise all the other typemaps you have overridden in csharp\boost_shared_ptr.i. Please look at the docs at http://swig.org/Doc3.0/Typemaps.html especially the debugging typemap section. |
This is totally right. Thanks a lot @wsfulton ! |
Hi,
I found out a situation where a C++ method returning a std::vector of pointers to class "A", once wrapped in C# returns a List-like class which lost all notion of inheritance.
Example:
C++ side:
C# side:
Imagine I check there using the name property which class is at index 0, A, B or C. After that, I am sure what class it is (let's say it' class B). Then:
This is the exception I get:
To go further, I tried some manipulations in the SWIG to understand from where this error could come. I wrote a little "ToArray()" method, that just copies the list in an array of templated type. So it uses the getitemcopy method implemented in the std_vector.i. With this, it works, but other way, when the wrapper uses the getitem which should get a const reference to the C++-side item, it doesn't.
The text was updated successfully, but these errors were encountered: