#ifndef INCL_CALLBACK_H
#define INCL_CALLBACK_H

/*-----------------------------------------------------------------------
 * Xclasses header file
 * ($Id: callback.h,v 1.4 2003/01/22 12:49:05 schmitzj Exp $)
 * 
 * Copyright (C) 1998 Jürgen Schmitz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *-----------------------------------------------------------------------
 */

#include <stdarg.h>
#include <Xclasses/callable.h>

class callback
{
	union
	{
		void* (callable::*cfunc)(void*,void*,void*,void*);
		void* (*func)(void*,void*,void*,void*);
	};
	union
	{
		callable *cfclass;
		void *fclass;
	};

	unsigned int oldway:1;

public:
	callback(void);
	~callback();
	
	void set(void* (*func)(void*,void*,void*,void*));
	void set(void *fclass,void* (*func)(void*,void*,void*,void*));
	void set(callable *fc,void* (callable::*f)(void*,void*,void*,void*));

	int compare(void* (*func)(void*,void*,void*,void*));
	int compare(callable *fc,void* (callable::*f)(void*,void*,void*,void*));

	int iscallback(void) { return func!=NULL; };
	void *call(void* arg1=NULL,void* arg2=NULL,void* arg3=NULL,void* arg4=NULL);	
};

// typedef void *(callable::*)(void*,void*,void*,void*) CALLFUNCTION;	// this doesn't work, however!

#define CALLFUNCTION void *(callable::*)(void*,void*,void*,void*)
#define CBCAST	void* (*)(void*,void*,void*,void*)


#define cbset(cb,fc)		\
	{						\
 		va_list ap;			\
		va_start(ap, fc);	\
		(cb).set(fc,(void* (*)(void*,void*,void*,void*))va_arg(ap,void *));	\
		va_end(ap);			\
	}

// this define doesn't work - bug in va_arg: *(TYPE*) don't work with function pointer!
#define ca_cbset(cb,fc)		\
	{						\
 		va_list ap;			\
		va_start(ap, fc);	\
		(cb).set(fc,(void* (callable::*)(void*,void*,void*,void*))va_arg(ap,(void* (callable::*)(void*,void*,void*))));	\
		va_end(ap);			\
	}

#endif
