Best way to return an array of values from c++?
I have a a group of structs made of a mix of primitive types in c++. I need to return these to Java but Im not sure the best way to return them quickly.
Should I create a class corresponding to the structure in Java then create the class in C++ and set its fields and return that? Or should I create arrays of different primitive types and return those? Can I even do that? Say, store a group of arrays of different types in a jobjectArray and return that? If I can, what would be the return type? Object[][]?
Java side:
package jni;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
* @author Ian Schneider
public class Struct {
static {
System.loadLibrary("struct");
public static void doit() {
ByteBuffer bytes = ByteBuffer.allocateDirect(structSize());
bytes.order(ByteOrder.nativeOrder());
getData(bytes);
System.out.println("int : " + bytes.getInt());
System.out.println("double : " + bytes.getDouble());
private static native void getData(ByteBuffer bytes);
private static native int structSize();
public static void main(String[] args) throws Exception {
doit();
}C side (I'm not a C programmer, so be nice):
#include "jni_Struct.h"
struct foo {
int x;
double y;
} foo;
JNIEXPORT void JNICALL Java_jni_Struct_getData
(JNIEnv * env, jobject obj, jobject buf) {
struct foo * f = (void*) malloc(sizeof(foo));
f->x = 123;
f->y = 456.;
void * data = (void *) (*env)->GetDirectBufferAddress(env,buf);
memcpy(data,f,sizeof(foo));
free(f);
JNIEXPORT jint JNICALL Java_jni_Struct_structSize
(JNIEnv * env, jobject obj) {
return sizeof(foo);
}This is a bit simplistic as foo is static in size (no pointers), but hopefully you get the point.
Similar Messages
-
Best way to return more than 1 value in a function?
Hi all,
What's the best way to return more than 1 value from a function? returning a cursor? varray? objects? etc? I thought of a cursor first, but i was hesitant since i am not sure if the cursor will be automatically closed when you return a cursor(open cursor no longer used is bad). Example:
BEGIN
OPEN c_temp_cursor;
RETURN c_temp_cursor;
END;
With above example, c_temp_cursor is remained open. Or is it automatically closed once it exits from the function? Need some suggestions and expert advice.
Thanks.
Note: Function is to be used to return and not a procedure (This is a requirement. Can't explain the details on why).
Edited by: dongzky on Jul 3, 2010 4:17 PM
typo: "ir exists" to "it exits" (in bold)First create your pl/sql table type
CREATE OR REPLACE TYPE pmc_tab AS TABLE OF NUMBER;
Then a table:-
CREATE TABLE v_stats_daily(start_date date, field1 number, field2 number, field3 number);
Some insert into the table so we've got test data...
insert into v_stats_daily values('08-OCT-2003',10,20,30);
insert into v_stats_daily values('08-OCT-2003',40,50,60);
insert into v_stats_daily values('08-OCT-2003',70,80,90);
Then create your function:-
CREATE OR REPLACE FUNCTION PMC_STATS
(pStatDate Date) RETURN pmc_tab IS
MyArray pmc_tab;
vstat1 NUMBER;
vstat2 NUMBER;
vstat3 NUMBER;
BEGIN
MyArray := pmc_tab();
select sum(Field1), sum(field2),sum(field3)
into vstat1, vstat2,vstat3
from v_stats_daily
where Start_date = pStatDate;
MyArray.extend;
MyArray(1) := vstat1;
MyArray.extend;
MyArray(2) := vstat2;
MyArray.extend;
MyArray(3) := vstat3;
RETURN MyArray;
END;
In SQL*Plus:-
SQL>set serverout on
Then a lump of PL/SQL to run your function:-
DECLARE
MyDate DATE;
MyArray pmc_tab;
i NUMBER;
numOut NUMBER;
BEGIN
MyArray := pmc_stats('08-OCT-2003');
dbms_output.put_line('Table count: '||to_char(MyArray.count));
for i in 1..MyArray.last LOOP
numOut := MyArray(i);
--if numOut is null then
dbms_output.put_line('Value: '||to_char(numOut));
--end if;
END LOOP;
END;
Your output will look like:-
Table count: 3
Value: 120
Value: 150
Value: 180
Hope this helps, -
Best Way to Create Array of Values From ArrayCollection
I often use Arrays of values from ArrayCollections in my
application in order to facilitate faster performance when
filtering other Collections.
For instance, let's say I have an ArrayCollection of Objects
with the properties "id", "name", "label", and "isActive". What
would be the best way to get an Array of all the "id" values?
Right now, I am looping over the ArrayCollection, and adding
the value of the property I need to an Array using Array.push().
For larger Collections, this seems like overkill. I was wondering
if anyone knew of some quicker way to get an Array of values of one
property from an Array Collection.
My current method for doing this is attached below...
Thanks.No, that won't work - toArray() simply returns the entire
Collection as an Array. -
What is the best way of returning group-by sql results in Toplink?
I have many-to-many relationship between Employee and Project; so,
a Employee can have many Projects, and a Project can be owned by many Employees.
I have three tables in the database:
Employee(id int, name varchar(32)),
Project(id int, name varchar(32)), and
Employee_Project(employee_id int, project_id int), which is the join-table between Employee and Project.
Now, I want to find out for each employee, how many projects does the employee has.
The sql query that achieves what I want would look like this:
select e.id, count(*) as numProjects
from employee e, employee_project ep
where e.id = ep.employee_id
group by e.id
Just for information, currently I am using a named ReadAllQuery and I write my own sql in
the Workbench rather than using the ExpressionBuilder.
Now, my two questions are :
1. Since there is a "group by e.id" on the query, only e.id can appear in the select clause.
This prevent me from returning the full Employee pojo using ReadAllQuery.
I can change the query to a nested query like this
select e.eid, e.name, emp.cnt as numProjects
from employee e,
(select e_inner.id, count(*) as cnt
from employee e_inner, employee_project ep_inner
where e_inner.id = ep_inner.employee_id
group by e_inner.id) emp
where e.id = emp.id
but, I don't like the complication of having extra join because of the nested query. Is there a
better way of doing something like this?
2. The second question is what is the best way of returning the count(*) or the numProjects.
What I did right now is that I have a ReadAllQuery that returns a List<Employee>; then for
each returned Employee pojo, I call a method getNumProjects() to get the count(*) information.
I had an extra column "numProjects" in the Employee table and in the Employee descriptor, and
I set this attribute to be "ReadOnly" on the Workbench; (the value for this dummy "numProjects"
column in the database is always 0). So far this works ok. However, since the numProjects is
transient, I need to set the query to refreshIdentityMapResult() or otherwise the Employee object
in the cache could contain stale numProjects information. What I worry is that refreshIdentityMapResult()
will cause the query to always hit the database and beat the purpose of having a cache. Also, if
there are multiple concurrent queries to the database, I worry that there will be a race condition
of updating this transient "numProjects" attribute. What are the better way of returning this kind
of transient information such as count(*)? Can I have the query to return something like a tuple
containing the Employee pojo and an int for the count(*), rather than just a Employee pojo with the
transient int inside the pojo? Please advise.
I greatly appreciate any help.
Thanks,
FransNo I don't want to modify the set of attributes after TopLink returns it to me. But I don't
quite understand why this matters?
I understand that I can use ReportQuery to return all the Employee's attributes plus the int count(*)
and then I can iterate through the list of ReportQueryResult to construct the Employee pojo myself.
I was hesitant of doing this because I think there will be a performance cost of not being able to
use lazy fetching. For example, in the case of large result sets and the client only needs a few of them,
if we use the above aproach, we need to iterate through all of them and wastefully create all the Employee
pojos. On the other hand, if we let Toplink directly return a list of Employee pojo, then we can tell
Toplink to use ScrollableCursor and to fetch only the first several rows. Please advise.
Thanks. -
AS3 Best way to clear an array
What is the best way to clear an array? (performance and resources)
array.length = 0;
array = [];
array.splice(0);that's creating a new array which is extra work, and it's adding an array to the objects that flash will gc, and that's extra work.
assigning the length to 0 is the most efficient way to clear an array. -
Best way to return structured data?procedure or function?
Hi everyone.
I cannot choose the best way to return, from java stored procedure, some data.
for example , which is best way to write this method:
public STRUCT function(input parameters) { STRUCT result ... return result; }
or
public void procedure(input parameters, STRUCT[] struct) { STRUCT result ... struct[0] = result; }
in the last way, the wrapper obviously will be: CREATE PROCEDURE name(... IN datatype, data OUT STRUCT)
Which is best way in efficiency ?
thank you ;)da`,
By "efficiency", I assume you mean, best performance/least time.
In my experience, the only way to discover this is to compare the two, since there are many factors that affect the situation.
Good Luck,
Avi. -
Accidentally I enlarge a window. Best way to return to its original size?
Sometimes I accidentally enlarge a window. What is the best way to return to its original sign?
Usually there is a place in the window where three colors of dots are, and the Green dot can make the screen larger and then the same one can make it smaller again.
Not sure if this helps...
Good luck & happy computing! -
What is the best way to transfer my music and pictures from my old PC to my new Macbook Pro?
What is the best way to transfer my music and pictures from my old PC to my new Macbook Pro?
This may help;
http://www.apple.com/support/macbasics/migration/
Ciao. -
What is the best way to copy a DVD I made from iMovie? It was HD720 and I don't want to lose any quality in the copies. I need to distribute it to about 20 people. It's 42 minutes long.
You will need to save it as a video to the camera roll.
Import it into windows as you would a photo.
Then purchase DVD authoring software, and create a DVD. -
Passing array of values from front panel to menu
I am working with a group programming a multichannel current stimulation device and we are currently having trouble setting up the run time menu. The burning question that we have is trying to figure out how to pass an array of values from the front panel to the menu that we are setting up in the block diagram, any advice?
Thank youTake a look at this. Its just a simple menu update using a for loop. Uses whatever your values are as the item tag and item names. Remember, item tags need to be unique. This specifically goes under the sub menu of channel, which has to be created before.
Attachments:
menu.vi 12 KB -
What is the best way to transfer apps, contacts, et cetera from one iPhone (3G) and computer (2006 MacBook) to a new iPhone (4S) and computer (early 2011 MacBook Pro)? I have already moved music, among other things using Migration Assistant but I cannot locate contacts or Apps.
transfer just SOME of the applications
it's all or none, i'm afraid. -
What's the best way to transfer (not forward) a call from one iPhone to another?
What's the best way to transfer (not forward) a call from one iPhone to another? Is there an app available that does this? I'm asking about receiving a call, then transferring that caller to another iPhone on a separate number and then disconnecting while those two users are joined up in a conversation.
Ask your carrier. This would be a feature provided by them.
-
I have a PPC iMac 10.4.11 and will shortly buy a new i Mac. What is the best way to transfer all my HD date from old to new? Thank you!
Migrating from PPC Macs to Intel Macs:
https://discussions.apple.com/docs/DOC-2295
How to use Migration Assistant:
http://support.apple.com/kb/HT4413?viewlocale=en_US
http://support.apple.com/kb/TS1963
Troubleshooting Firewire target disk mode:
http://support.apple.com/kb/HT1661 -
What is the best way to backup my computer before upgrading from OSX 10.5.8 to 10.6? I just purchased a WD portable external hard drive.
Use Carbon Copy Cloner http://www.bombich.com/index.html or SuperDuper. http://www.shirt-pocket.com/SuperDuper/SuperDuperDescription.html Both make a bootable copy of everything on your hard drive.
-
What is the best way to move my itunes music library from my old computer to my new one?
what is the best way to move my itunes music library from my old computer to my new one?
Home Sharing is option A.
http://support.apple.com/kb/HT3819
Option B is consolidating the library to an external HD.
http://support.apple.com/kb/ht1364
http://support.apple.com/kb/ht1751
Or you can use one of the other 8000 methods. I just prefer these two.
Maybe you are looking for
-
Unable to open the HFM web server link
I am gettng the following error while connecting to the url "http://<servername>/hfm/home/LaunchPage.asp" Error Number:1 Error Description:Please Relogin. Session has Timed out Error Source: Page On which Error Occurred: I have tried setting the Full
-
Drop down selection for a column in Table Control
Hello, I have a table control where data is displayed. The user can fill data in one of the column and save. We want to have a drop down in that column where user can select a value. This column is a qunatity field. Please guide how it can be done. R
-
When I print an image (File->Print Image...) I adjust the Printer Settings using using the dialog you get when pressing the "Printer Settings" button. I pick a preset (that is a printer driver settings preset, not an Aperture Printer Settings Preset)
-
Reel to Reel audio tapes to Mac
I have sme old recordings on reel-to-reel audio tapes which I want to convert via my G5 into digital sound then to put on DVD does anybody know of what way I can best do this and the hardware/software I might need to complete thi task. The computer u
-
How do i know if my Bw IGs version is integrated in the sap kernel or if it is a standalone installation.