Subject: Foreign language interface : PL_new_term_ref()

From: Lionel Ains <[email-masked]>
Date: Thu, 10 Aug 2000 11:27:06 +0100 (WET DST)

Hello,

Just a quick question.
I believe that any term created by PL_new_term_ref() is automatically destructed when it goes out of 
scope. Could someone confirm?

Lionel

PS: So far, I'm creating handle terms from an integer (which is a table index):

static int unify_dictionary_handle_with_index(term_t handle_out, int index_in) {
 term_t index_in_term;
 
 if (PL_unify_functor(handle_out, FUNCTOR_dictionary1)) {
 index_in_term = PL_new_term_ref();
 PL_get_arg(1, handle_out, index_in_term);
 return (PL_unify_integer(index_in_term, index_in));
 }
 else {
 PL_fail;
 }
}

static int get_index_from_dictionary_handle(term_t handle_in, int *index_out) {
 term_t index_out_term;
 
 if (PL_is_functor(handle_in, FUNCTOR_dictionary1)) { /* Check if handle follows the '$dictionary'(...) 
template */
 index_out_term = PL_new_term_ref(); /* Create a term to contain the integer */
 if (!PL_get_arg(1, handle_in, index_out_term))
 PL_fail; /* Error, the handle doesn't follow the right template */
 if (!PL_get_integer(index_out_term, index_out))
 PL_fail; /* Error, the parameter for '$dictionary'/1 is not an integer! */
 if (*index_out>=NB_DICTIONARIES)
 PL_fail; /* Error, the index is greater than the dict_table size */
 }
 else
 PL_fail; /* Error, the handle is invalid because it is not a '$dictionary'/1 functor-based compound */
}

Thi relies on the fact that FUNCTOR_dictionary1 has been declared this way:
static functor_t FUNCTOR_dictionary1;
And has been create inside Prolog like that:
FUNCTOR_dictionary1 = PL_new_functor(PL_new_atom("$dictionary"), 1);

The two functions included just allow to create a handle from an index 
(unify_dictionary_handle_with_index) or an index from the handle 
(get_index_from_dictionary_handle).
This way, a '$dictionary'(1) term as the input of get_index_from_dictionary_handle will result by 
outputting the integer 1 and the index 3 as the input of unify_dictionary_handle_with_index will result 
by creating a handle term '$dictionary'(3).
______________________________________________________
Boîte aux lettres - Caramail - http://www.caramail.com