[mercury-users] using univ with C
Mark Brown
mark at csse.unimelb.edu.au
Thu Jun 5 15:21:23 AEST 2008
On 05-Jun-2008, QRL_Charles QRL_Gretton <g12.charles.gretton at gmail.com> wrote:
> I'm not trying to store a list (or array) of words in C. The C macro
> that gives me different "pushes" for different types looks as follows
> -- i.e., I want the C code to be able to reason about the type of the
> contents.
>
>
> #define __IMPLEMENT__POLYMORPHIC_LIST_IN_C__PUSH(TYPE_NAME,
> FUNCTION_NAME) struct polymorphic_list_in_c* \
> FUNCTION_NAME(struct polymorphic_list_in_c* list, \
> void* _data_element){ \
> TYPE_NAME* data_element = (TYPE_NAME*)_data_element; \
> TYPE_NAME* list_data; \
> ...
>
>
> In the end, I expect the code I'm looking for will occure something like:
>
>
> :- pragma foreign_proc("C",
> push_back_c_vector(Univ::in, Vector_In::in,S0::mdi, S::muo),
> [promise_pure],
> "
> Vector_In->push_back(Vector_In, Univ->the_actual_data);
> S = S0;
> ").
Simply make push_back_c_vector polymorphic:
:- pred push_back_c_vector(T::in, c_vector::in, int_store::mdi,
int_store::muo) is det.
When calling it, access the value of the univ with univ_value.
To interpret the *type* stored in the univ, use univ_type and the predicates
and functions in the type_desc library module. Alternatively, you can use
univ_to_type to pick out the types you know about.
Cheers,
Mark.
--------------------------------------------------------------------------
mercury-users mailing list
Post messages to: mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions: mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the users
mailing list