From: Bob T. <bo...@ey...> - 2005-11-30 19:59:11
|
This is a bit of a style question, but important in understanding a general question about trying to make Java users happy but still trying to deal with the differences between Java and C++. I know there are typemaps (and examples in the Manual) for dealing with this particular question, but I am curious to know if they are the only way to do things. So for example, if I have a C++ function: bool GetFoo(int i, int j, float &x); note that I want to keep the bool return value, so I can't just change this to: float GetFoo(int i, int j); the docs show example code (using some built-in typemaps) that looks like: float [] x = {0.0}; boolean res = GetFoo(1, 2, x); if (res) System.out.println(x[0]); which, while that fact that it works, out-of-the-box, is nice, I'm not sure how happy this would make the average Java programmer. I have other functions that have several return parameters, so having to write something like: float [] x = {0.0}; float [] y = {0.0}; float [] z = {0.0}; boolean res = GetBar( 1, 2, 3, x, y, z); if (res) System.out.println(x[0]+" "+y[0]+" "+z[0]); seems a bit much. any suggestions to make this api a bit more Java-friendly would be greatly appreciated. Or if this way really is the Java way to pass-by-reference, then I'll use the built-in typemaps. And maybe a different way to phrase the question is: If you were going to write the GetFoo or GetBar functions from scratch in Java, how would you return the results, while still allowing the notion of the boolean return value to know that the function worked? Thanks for any input... Bob |
From: Marcelo M. <mm...@ac...> - 2005-11-30 22:01:55
|
Bob Tolbert wrote: >This is a bit of a style question, but important in understanding a general >question about trying to make Java users happy but still trying to deal with >the differences between Java and C++. > >I know there are typemaps (and examples in the Manual) for dealing with this >particular question, but I am curious to know if they are the only way to do >things. > >So for example, if I have a C++ function: > >bool GetFoo(int i, int j, float &x); > >note that I want to keep the bool return value, so I can't just change this >to: > >float GetFoo(int i, int j); > >the docs show example code (using some built-in typemaps) that looks like: > >float [] x = {0.0}; >boolean res = GetFoo(1, 2, x); >if (res) > System.out.println(x[0]); > >which, while that fact that it works, out-of-the-box, is nice, I'm not sure >how happy this would make the average Java programmer. > >I have other functions that have several return parameters, so having to >write something like: > >float [] x = {0.0}; >float [] y = {0.0}; >float [] z = {0.0}; >boolean res = GetBar( 1, 2, 3, x, y, z); >if (res) > System.out.println(x[0]+" "+y[0]+" "+z[0]); > >seems a bit much. > >any suggestions to make this api a bit more Java-friendly would be greatly >appreciated. Or if this way really is the Java way to pass-by-reference, >then I'll use the built-in typemaps. > >And maybe a different way to phrase the question is: If you were going to >write the GetFoo or GetBar functions from scratch in Java, how would you >return the results, while still allowing the notion of the boolean return >value to know that the function worked? > > maybe float GetFoo(int i, int j) throw(std::out_of_range); where you can deal properly with the exception in the java and C++ side. Marcelo >Thanks for any input... >Bob > > > >------------------------------------------------------- >This SF.net email is sponsored by: Splunk Inc. Do you grep through log files >for problems? Stop! Download the new AJAX search engine that makes >searching your log files as easy as surfing the web. DOWNLOAD SPLUNK! >http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click >_______________________________________________ >Swig-user mailing list >Swi...@li... >https://lists.sourceforge.net/lists/listinfo/swig-user > > |
From: William S F. <ws...@fu...> - 2005-11-30 22:57:54
|
Marcelo Matus wrote: > Bob Tolbert wrote: > >> This is a bit of a style question, but important in understanding a >> general >> question about trying to make Java users happy but still trying to >> deal with >> the differences between Java and C++. >> >> I know there are typemaps (and examples in the Manual) for dealing >> with this >> particular question, but I am curious to know if they are the only way >> to do >> things. >> >> So for example, if I have a C++ function: >> >> bool GetFoo(int i, int j, float &x); >> >> note that I want to keep the bool return value, so I can't just change >> this >> to: >> >> float GetFoo(int i, int j); >> >> the docs show example code (using some built-in typemaps) that looks >> like: >> >> float [] x = {0.0}; >> boolean res = GetFoo(1, 2, x); >> if (res) >> System.out.println(x[0]); >> >> which, while that fact that it works, out-of-the-box, is nice, I'm not >> sure >> how happy this would make the average Java programmer. >> I have other functions that have several return parameters, so having to >> write something like: >> >> float [] x = {0.0}; >> float [] y = {0.0}; >> float [] z = {0.0}; >> boolean res = GetBar( 1, 2, 3, x, y, z); >> if (res) >> System.out.println(x[0]+" "+y[0]+" "+z[0]); >> >> seems a bit much. >> >> any suggestions to make this api a bit more Java-friendly would be >> greatly >> appreciated. Or if this way really is the Java way to pass-by-reference, >> then I'll use the built-in typemaps. >> >> And maybe a different way to phrase the question is: If you were going to >> write the GetFoo or GetBar functions from scratch in Java, how would you >> return the results, while still allowing the notion of the boolean return >> value to know that the function worked? >> >> > > maybe > > float GetFoo(int i, int j) throw(std::out_of_range); > > where you can deal properly with the exception in the java and C++ side. > > Marcelo > Yes. Basically your C style function prototype would never get written like that in Java, whereas the C++ approach Marcelo mentioned is very analogous to what a Java programmer would do. This is one of the dilemas to deal with when doing cross language development. Float or float in Java cannot be modified by a function and results returned to the caller. Other alternatives to using a 1D Float array is to use a class that can be passed by reference. I think this is in the docs, but basically create a class like FloatWrapper: struct FloatWrapper { float value; }; and use that. You could make it a pure Java class or a C++ class, I don't think there will be much difference in performance penalties. William |