Agent wrapper generation

Revision as of 04:39, 26 April 2007 by Colin-adams (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Construction.png Not Ready for Review: This Page is Under Development!

Sometimes the situation arises when you have a routine that you want to use as an agent, but the number of parameters required by the routine is fewer than those that will be passed by the agent call. An example from Eiffel Software's Vision2 library:

class
	EV_POINTER_BUTTON_ACTION_SEQUENCE
 
inherit
	EV_ACTION_SEQUENCE [TUPLE [INTEGER, INTEGER, INTEGER, DOUBLE, DOUBLE, DOUBLE, INTEGER, INTEGER]]
	-- EV_ACTION_SEQUENCE [TUPLE [x, y, button: INTEGER; x_tilt, y_tilt, pressure: DOUBLE; screen_x, screen_y: INTEGER]]

If have have a routine that wants to know when a button is pressed, and which button it was, then it needs the `button' argument, but none of the others. Best practice for this situation at the moment (according to Eric Bezault) is to write a wrapper routine that just passes on the `button' argument.

A similar situation is when you have the correct number of arguments, but they are the wrong way round. This happened to me yesterday, so I had to write a wrapper routine.

It would be much nicer if the compiler were able to generate the necessary wrapper code for you. This artcile explores possible syntaxes for these problems.

Argument rotation

A simple solution for specifying a rotation of 2 arguments (or any permutation that does not drop arguments), is to extend the use of the question mark by suffixing it with a decimal specifying the number of the argument to be passed in that location. For example, to index a list of floating point numbers:

floats_list: DS_ARRAYED_LIST [REAL_64]
  -- Floating point numbers
 
floats_map: DS_HASH_TABLE [INTEGER, REAL_64]
  -- Map of floating-point indices within `floats_array'
 
build_map
  -- Build `floats_map' from `floats_list'.
 require
  floats_list_not_void: floats_list /= Void
 do
  create floats_map.make (floats_list.count)
  floats_list.do_all_with_index (agent {DS_HASH_TABLE} floats_map.put_new (?2, ?1))
 end