#ifndef CODE_CALLBACK_CXX
#define CODE_CALLBACK_CXX

/*-----------------------------------------------------------------------
 * Xclasses code file
 * ($Id: callback.cxx,v 1.4 2003/01/22 12:31:51 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 "types.h"
#include "../include/callback.h"
#include <Xclasses/callable.h>

static int old_callway=0;

callback::callback(void)
{
	oldway=0;
	fclass=NULL;
	func=NULL;
	cfunc=0;
}
callback::~callback()
{
}
void callback::set(void* (*f)(void*,void*,void*,void*))
{
	func=f;
	fclass=NULL;
}
void callback::set(void *fc,void* (*f)(void*,void*,void*,void*))
{
	func=f;
	fclass=fc;
	oldway=1;

	if (old_callway<10)
	{
		old_callway++;
		fprintf(stderr,"Warning:\nLooks like this program uses the old way to work with\ncallback/callable object. Try to update this program or contact the author.\n(contact only if he/she is not part of the XC development team!)\n\n");
	}
}
void callback::set(callable *fc,void* (callable::*f)(void*,void*,void*,void*))
{
	cfclass=fc;
	cfunc=f;
}

int callback::compare(void* (*f)(void*,void*,void*,void*))
{
	return (func==f && fclass==NULL);
}
int callback::compare(callable *fc,void* (callable::*f)(void*,void*,void*,void*))
{
	return (cfclass==fc && cfunc==f);
}

void* callback::call(void* a1,void* a2,void* a3,void* a4)
{
	if (func)
	{
		if (cfclass)
		{
			if (oldway)
				return ((void* (*)(void *,void*,void*,void*,void*))func)(fclass,a1,a2,a3,a4);
			else
				return (cfclass->*cfunc)(a1,a2,a3,a4);
		}
		else
		{
			return func(a1,a2,a3,a4);
		}
	}
	return NULL;
}

#endif
