Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!math.fu-berlin.de!opal!mailgzrz!duns1222 From: duns1222@w203zrz.zrz.tu-berlin.de (Martin Dunschen) Newsgroups: comp.lang.eiffel Subject: Re: passing address of an eiffel feature Message-ID: <281@mailgzrz.tu-berlin.de> Date: 6 Mar 91 17:14:22 GMT Sender: news@mailgzrz.tu-berlin.de Organization: TUBerlin Lines: 133 Nntp-Posting-Host: w203zrz.zrz.tu-berlin.de This posting is especially to Greg Economides of Biosystems Modelling Group at Texas A&M University, e-mail greg@carnivore.tamu.edu BUT everybody interested, feel free to read on, perhaps read first my posting from the day before, same subject! Hi Greg, I tried to send you a mail, but it turned out that your configuration is not ready for receiving mail, so I try this way. thank you for your help regarding our problems passing eiffel features to foreign routines. I understand the main principle now. You call the "methods", e. g. the features `item' and `put', from within the C routine. For this you have to find them, depending on the object, to which these features belong. In our example we refer to the features `item' and `put' via the ROUT_PTR eif_rout. We store the pointers to these features on the variables `put_ptr' and `item_ptr' for further use. Ok, so far so good, but I tried your code-examples, with a few minor changes, and they didn't work. The following questions arose: 1. Do you pass an_array as it is or as @an_array ? 2. How to declare an_array in c_func, as Object or OBJPTR ? 3. How do I know the number/types of parameters to item_ptr and put_ptr? Are they according to the syntax in an eiffel-program ? 4. How to declare myvar, as float, or as a pointer to something else ? Again, I include eiffel and C code, what I got from you with some changes. (Our C compiler seems not ANSI standardized !) Excuse me, when I bother you again, but what can I do? Bye, Martin CUT HERE ------ CUT HERE ------ CUT HERE ------ CUT HERE ------ CUT HERE ------ class WHATEVER feature an_array: ARRAY[REAL]; Create is external c_func language "C" do an_array.Create ( 0, 2); an_array.put (10.,0); an_array.put (20.,1); an_array.put (30.,2); output ("before calling c_func:", an_array.item(0), an_array.item(1), an_array.item(2)); c_func(@an_array); -- c_func(an_array); output ("after calling c_func:", an_array.item(0), an_array.item(1), an_array.item(2)); end; -- a_feature output (str: STRING; r1, r2, r3: REAL) is do io.putstring (str); io.putreal (r1); io.putstring (" "); io.putreal (r2); io.putstring (" "); io.putreal (r3); io.putstring ("\n") end -- output end; -- class WHATEVER CUT HERE ------ CUT HERE ------ CUT HERE ------ CUT HERE ------ CUT HERE ------ #include "/path/to/Eiffel/files/_eiffel.h" void c_func (array_ptr) OBJPTR array_ptr; /* Object array_ptr */ { extern ROUT_PTR eif_rout; /* in the example /path/to/eiffel/examples/eiffel-c/callbacks the previous declaration looks like extern ROUT_PTR (*eif_rout)(); */ ROUT_PTR put_ptr, item_ptr; float myvar; /* is this correct? I'm not shure */ /* assigning the eiffel routines to variables `put_ptr' and `item_ptr' is useful if these eiffel functions will be used more than once. otherwise a one-time call can be made like this: (*(eif_rout(array_ptr,"item")))(array_ptr, 3) arguments to `item' -----^ */ printf ("in c_func, before assigning pointers to eif_rout's\n"); put_ptr = eif_rout(array_ptr, "put"); item_ptr = eif_rout(array_ptr, "item"); printf ("in c_func, after assigning pointers to eif_rout's\n"); /* Now, to use these pointers to eiffel functions */ printf ("in c_func, before calling item \n"); /* myvar = *(eif_rout(array_ptr,"item"))(array_ptr, 3); */ myvar = (*item_ptr)(array_ptr,3); printf ("in c_func, after calling item, myvar=%f\n", myvar); /* maybe you'd make a fortran call here which would change the value of myvar.... and then you might want to put it back into the array */ myvar = myvar * 2.; /* just an example to change something */ printf ("in c_func, before calling put, myvar=%f\n", myvar); /* (*(eif_rout(array_ptr,"item")))(array_ptr,myvar, 3); */ (*put_ptr)(array_ptr,myvar,3); printf ("in c_func, after calling put, myvar=%f\n", myvar); }