Re: call_func and routine_id and...

new topic     » goto parent     » topic index » view thread      » older message » newer message

Since, you used my name, I'll show you some example code.
It seems, you want to use quoted strings as identifers. Or at least, an
identifer of yourself (otherwise, you dont need any mechanism other than
routine pointers themselves)

Below is a little small library that does this for you. It does two things
mainly.
You first register your routine and assign an handle. (handle can be
something like "my_var" or a integer that has been assigned to a constant,
or just any type of arbiturary object)

You call the routine with call_routine (handle, arguments).
It will always return at least a one element sequence.
The first element will be either TRUE or FALSE, as in SUCCES or FAILURE.
The second element contains whatever has been returned, *if* it was a
function. Otherwise there is no second element. Example:

--------

function my_func (integer a, integer b)
    return "HellO!"
end function

add_function ("my_func", routine_id ("my_func"))

procedure my_proc (integer a, integer b)
    puts (1, {a,b})
end procedure

add_procedure ("my_proc", routine_id ("my_proc"))

-- Now both the function and procedure have been registered, you can do
this:

sequence result

result = call_routine ("my_proc", {'a','b'})
-- result is now {TRUE}

result = call_routine ("my_func", {'a','b'})
-- result is now {TRUE, "HellO!"}

result = call_routine ("my_house", {'a','b'})
-- result is now {FALSE}

I dont think this is exactly what you want, however, the code can be a
guideline how to set such a thing up. If you explain where you want to use
the routine mechanism for, rather than how (give a little more detailed
information, what you exactly want), then I can write something more towards
your needs. On the other hand, maybe this was just what you  wanted.

Anyways, good luck, the library follows. (really small anyway)

-- rpman.e

constant
    TRUE = 1,
    FALSE = 0

sequence
    types,
    handles,
    rps

global procedure add_function (object handle, integer rp)
integer pos
    pos = find (handle, handles)
    if pos then
        types[pos] = TRUE         -- It does return something
        rps[pos] = rp
    else
        handles = append(handles, handle)
        types = append(types, TRUE)
        rps = append(rps, TRUE)
    end if
end procedure

global procedure add_procedure (object handle, integer rp)
integer pos
    pos = find (handle, handles)
    if pos then
        types[pos] = FALSE         -- It does return something
        rps[pos] = rp
    else
        handles = append(handles, handle)
        types = append(types, FALSE)
        rps = append(rps, FALSE)
    end if
end procedure

global function call_routine (object handle, sequence arg)
integer pos
    pos = find (handle, handles)
    if pos then
        if types[pos] then
            return {TRUE, call_func (rps[pos], arg)
        else
            call_proc (rps[pos], arg)
            return {TRUE}
    else
        return {FALSE}
    end if
end function

--- End of it

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu