Appendix B. Variable Arguments

It should be noted that we have implemented variable arguments using a trick that will limit the maximum number of (variable) arguments to ten.

When we were bussy implementing the code changes that was needed to add GNOME Classes we came accross the following variable argument function:

GtkWidget* gnome_message_box_new(const gchar *message, const gchar *messagebox_type, ...);

For those who does not know what it means - it basically means that the function can have variable arguments. Anything from 0 to N parameters instead of the ..., Luckily for us the way that the GTK/GNOME developers implemented it is to evaluate all parameters until it sees a NULL.

Well, since we were unaware of another solution we implemented all of these as having at most 10 arguments (development limitation) with different methods that has from one to 10 arguments. That is the definition in the defs file we used is:
(define-func gnome_message_box_new
  GtkWidget
  ((string message)
   (string messagebox_type)
   (string button0)
   (string button1 (null-ok) (= "NULL"))
   (string button2 (null-ok) (= "NULL"))
   (string button3 (null-ok) (= "NULL"))
   (string button4 (null-ok) (= "NULL"))
   (string button5 (null-ok) (= "NULL"))
   (string button6 (null-ok) (= "NULL"))
   (string button7 (null-ok) (= "NULL"))
   (string button8 (null-ok) (= "NULL"))
   (string button9 (null-ok) (= "NULL"))))

The resulting Java code that gets generated wil look something like this:
	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5,
		String button6, String button7,
		String button8, String button9) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, button5,
			button6, button7,
			button8, button9);
	}
	private native long nativenew(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5,
		String button6, String button7,
		String button8, String button9);


	/* GnomeMessageBox has 9 default argument value(s) */
	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5,
		String button6, String button7,
		String button8) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, button5,
			button6, button7,
			button8, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5,
		String button6, String button7) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, button5,
			button6, button7,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5,
		String button6) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, button5,
			button6, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4, String button5) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, button5,
			null, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3,
		String button4) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			button4, null,
			null, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2, String button3) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, button3,
			null, null,
			null, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1,
		String button2) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			button2, null,
			null, null,
			null, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0, String button1) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, button1,
			null, null,
			null, null,
			null, null,
			null, null);
	}

	public GnomeMessageBox(String message,
		String messagebox_type,
		String button0) {
		nativepeer = nativenew(message,
			messagebox_type,
			button0, null,
			null, null,
			null, null,
			null, null,
			null, null);
	}

If you are interested in the implementation of variable arguments refer to a good ANSI C book and/or the code that gets generated from the defs file.

If anyone know of another way of implementing it please let us know. The other solution is to use an array to hold the arguments but I think this is slighlty more elegant.