From: Sergio G. V. <sg...@ac...> - 2011-04-01 15:59:13
|
I want to return a "copy" of the C++ structure as a C#/Java structure instead of getting a C#/Java structure which manages a native instance. This is because I don't want accessing to a native instance to access a field into the C#/Java structure. That is, if a method returns an structure and this structure won't be updated anymore... why it is necessary to going down to the native access each time I want access a field of the structure in the target language? For example, %{ struct Point2D { int X, Y; }; %} %cs_struct(Point2D, System.Drawing.Point) %inline %{ void Test1(Point2D p) {} void Test2(Point2D& p) {} %} this maps a C++ struct with the System.Drawing.Point class defined at C#, so once Test1 or Test2 has been executed, I will access Point instance as a common C# object (a Point2D wrapper handler class is not generated). The proble is, what happens if I have an stl string or a wchar_t * moreover of "int X,Y" ? Maybe this could be done easier, what do you mean exactly by "return a copy"? Could you provide an example? Thanks, Sergio On Apr 1, 2011, at 5:19 PM, Daniel Mostovoy wrote: > Hi Sergio... > > i don't have experience wrapping structs - though i'd imagine that they'd behave just like public classes... i did notice in your sourceforge query that your getter function is getting the object by reference... if you want a copy of the struct in the target language why not return a copy? i believe swig handles such cases well - i do it all the time. Judging by the simplicity of my anwer, i'm guessing I'm not understanding your problem :-) > > re wrapping for java as a target language - swig needs to know your target package name for it to work properly. not sure if you know this, i'm just mentioning it because it took me forever to figure this out for myself :-) if you're using anything other than the default package you MUST use the -package option with swig. run swig -help for a complete list of options ... I also find the - outdir super useful to put the resultant .java files right into your package. > > good luck! > > d. > > On 01/04/11 15:53, Sergio Gómez Villamor wrote: >> >> No, I have tested stl strings (or wstrings) and "wchar *" with no problems when they are member variables or arguments of methods into a wrapped class. >> >> My problem is when I want to return an struct without using for that a wapped class. I explained this in detail here: >> http://sourceforge.net/mailarchive/forum.php?thread_name=407C5F1E-4632-4FD7-B01F-61268BBBC8E1%40ac.upc.edu&forum_name=swig-user >> Thanks to David and using and interface file he provided me (cs_struct) with different typemaps, I could do it for C# (not for Java yet). >> But I get in problems when I have a stl string into the structure. Maybe this case cannot be solved? >> >> Thanks, >> Sergio >> >> On Apr 1, 2011, at 4:40 PM, Daniel Mostovoy wrote: >> >>> Sergio... >>> >>> You're having trouble wrapping a C++ class with an stl string as a member variable? Is that correct? sorry I've not been following this thread from the beginning... >>> >>> d. >>> >>> On 01/04/11 15:34, Sergio Gómez Villamor wrote: >>>> >>>> Mmm... in fact I'm using std::wstring and the typemap for this (std_wstring.i). >>>> Then, do you think it should work? If so, could you provide me a simple example? As before, this way I could check out what I'm doing wrong. >>>> >>>> Thanks, >>>> Sergio >>>> >>>> >>>> On Apr 1, 2011, at 4:10 PM, Daniel Mostovoy wrote: >>>> >>>>> Sergio... are you using the typemap for std::string? It comes with the swig distro & is called string.i i think... d. >>>>> On 01/04/11 14:40, Sergio Gómez Villamor wrote: >>>>>> >>>>>> Thanks David, >>>>>> >>>>>> Starting from this simple example, I could do the same with my structure. >>>>>> >>>>>> Anyway, I have some questions? >>>>>> >>>>>> - If I use common data types into the structure (int, long, double...) I have no problems, but if I try to add an string into the struct, then it fails (AccessViolationException). I have tried "std::string" and "wchar_t *" with no success (of course I have added corresponding "std_wstring.i" or "wchar.i" to do that). Is there something I should take into account or cs_struct has not been though to do that? >>>>>> >>>>>> - cs_struct just works for C#, what about java? I remember you told me you do not know how to do this for Java. Would you tell me if there is someone else who could help me with Java? >>>>>> >>>>>> Thanks, >>>>>> Sergio >>>>>> >>>>>> >>>>>> On Mar 31, 2011, at 9:39 PM, David Piepgrass wrote: >>>>>> >>>>>>> Sorry, I don’t know what’s wrong. I suggest that you try something simple such as this: >>>>>>> >>>>>>> %{ >>>>>>> struct Point2D { >>>>>>> int X, Y; >>>>>>> }; >>>>>>> %} >>>>>>> %cs_struct(Point2D, System.Drawing.Point) >>>>>>> %inline %{ >>>>>>> void Test1(Point2D p) {} >>>>>>> void Test2(Point2D& p) {} >>>>>>> %} >>>>>>> >>>>>>> If Test1 and Test2 are wrapped correctly, look for the differences between this code and your code. >>>>>>> >>>>>>> >>>>>> >>>>>> >>>>>> ------------------------------------------------------------------------------ >>>>>> Create and publish websites with WebMatrix >>>>>> Use the most popular FREE web apps or write code yourself; >>>>>> WebMatrix provides all the features you need to develop and >>>>>> publish your website. http://p.sf.net/sfu/ms-webmatrix-sf >>>>>> >>>>>> _______________________________________________ >>>>>> Swig-user mailing list >>>>>> Swi...@li... >>>>>> https://lists.sourceforge.net/lists/listinfo/swig-user >>>>> >>>> >>> >> > |