Re: Using handles with C++

From: Andrew Cheadle <amc4_at_icparc.ic.ac.uk>
Date: Thu 27 Feb 2003 03:05:45 PM GMT
Message-ID: <20030227150545.GA25906@elstree.icparc.ic.ac.uk>
Hi Christian,

With respect to your first problem. You can try:

struct _mystruct {
  int x;
  double db;
};
 
struct _mystruct mydata;
 
EC_word myget(t_ext_ptr obj, int idx) {
  return mydata.x;
}
 
t_ext_type my_method_table = { 0, 0, 0, 0, 0, 0, 0, (pword (*)(void *, int))myget, 0};

If it works, great... if not can you manage this part of your program in C
with the appropriate 'extern "C"' declarations?

I will discuss a C++ solution with Joachim.

With respect to your second problem:

For either assert or setval to work, you *must* define a copy function as
Joachim indicated. A short answer to your question:

> We were using the predefined 'ec_xt_double_arr'. Doesn't that have a
> copy-function defined?

Is no :-$ and if you define it I *think* either will work...

Here's an example of my own 'my_xt_long_arr' (the example code you sent me
used 'ec_xt_long_arr', not 'ec_xt_double_arr'. You should be able to achieve
what you want doing something similar:

extern "C"  {
 
static void my_long_arr_free( t_ext_ptr h )
{
}
 
static t_ext_ptr my_long_arr_copy( t_ext_ptr h )
{
    return h;
}
 
static pword my_long_arr_get(t_ext_ptr h, int i)
{
    return ec_long(((long*)h)[i]);
}
 
static int my_long_arr_set( t_ext_ptr h, int i, pword pw)
{
    return ec_get_long(pw, &((long*)h)[i]);
}
 
t_ext_type my_xt_long_arr = {
    my_long_arr_free, my_long_arr_copy, 0, 0, 0, 0, 0,
    my_long_arr_get,
    my_long_arr_set
};
 
long data[5] = {12, 23, 34, 45, 56};
 
int get_handle()
{
  EC_word hnd = handle(&my_xt_long_arr, &data);
  int rtv;
  rtv = unify(EC_arg(1), hnd);
  return rtv;
}
 
}

Notice that because 'data' is declared global I just return a reference to it
for the copy function, similarly, the free function need do nothing. However
I'd probably expect something a little more advanced than this. You might
malloc 'data' then bump the reference count to it then the copy function
bumps the reference count and returns it's address and the free function
just decrements this reference count and if it becomes zero calls the libc 
free() on it.
Similarly where you would free it in your external code, you'd decrement
the reference count and if it's zero libc free() it, otherwise assume that 
ECLiPSe is holding it live and will deal with the release of it by calling
my_long_arr_free() the appropriate number of times.

We'll look at addressing these issues in an upcoming release, however you
should be able to work around it as I've demonstrated.

Hope that helps!

Regards

Andy

On Wed, Feb 26, 2003 at 10:28:21PM +0100, Christian Fritz wrote:
> first of all, thanks for the quick answer.
> 
> Joachim Schimpf wrote:
> 
> >>As described in the embedding manual we are trying to pass generic C++
> >>data to ECLiPSe. We are facing two problems:
> >>
> >>1. The struct type (t_ext_type) for setting up a proper method table is
> >>differently defined (in types.h and eclipse_cc.h) from what is said in
> >>the manual. As far as we can see, it is for pure C programming (using
> >>pword instead of EC_word..).
> >>   
> >>
> >
> >It will probably work anyway (but I haven't verified this).
> >
> > 
> >
> Unfortunately the compiler already complains about this mismatch.
> 
> >>2. After getting a handle we'd like to store it somewhere in eclipse for
> >>further accessing the data. We are able to obtain a handle and xget some
> >>data. But when trying to save this handle (e.g. assert(
> >>myHandle(Hbound)) ) and recalling it (myHandle(Hyet_unbound)), we
> >>immediately receive a segmentation violation.
> >>   
> >>
> >
> >You need to provide a copy-method for this to work. It seems that
> >assert/1 erroneously does not detect this problem.
> >Try setval/getval instead of assert, and make sure you have a copy-
> >function defined.
> > 
> >
> Tried that already: setval(myhandle, H) with H bound to the handle gives:
> "type error in setval_body(myhandle, 'HANDLE'(16'40192468), eclipse)."
> We were using the predefined 'ec_xt_double_arr'. Doesn't that have a 
> copy-function defined?
> 
> 
> 
> 
> 
> 
> 
Received on Thu Feb 27 15:08:49 2003

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:22 PM GMT GMT