#include <iostream>

int pass(int i) {
  return i * i;
}

#ifndef __CINT__
#include "Api.h"
#endif /* __CINT__ */

int call(int (*F) (int), int I)
{
  int result = 0;
  
#ifndef __CINT__
  
  // # First define some local variables and objects. 
  G__CallFunc func; // INTERPRETEDFUNC, COMPILEDINTERFACEMETHOD, BYTECODEFUNC
  
  // # Then call the function
  switch(G__isinterpretedp2f(((void*)F))) {
    // # using function call as string 
  case G__INTERPRETEDFUNC:
    {
      char *fname;
      fname = G__p2f2funcname(((void*)F));
      if(fname) {
	G__ClassInfo globalscope;
	G__MethodInfo method;
	long dummy = 0;
	// resolve function overloading 
	method = globalscope.GetMethod(fname,
				       "int",
				       &dummy);
	if(method.IsValid()) {
	  char temp[128];
	  sprintf(temp,"(int)%i",
		  I);
	  func.SetFunc(&globalscope,fname,temp,&dummy);
	  result = func.ExecInt((void*)NULL);
	  // get pointer to function again 
	  F = (int (*)(int))method.PointerToFunc();
	} else cerr <<  "Error : Invalid method : " << fname << endl;
      } else {
	cerr <<  "Error : Unknown pointer to function" << endl;
      }
    }
    break;
    // # using interface method 
  case G__COMPILEDINTERFACEMETHOD:
    func.SetFunc((G__InterfaceMethod)F);
    func.SetArg((long)I);
    result = func.ExecInt((void*)NULL);
    break;
    // # bytecode version of interpreted func 
  case G__BYTECODEFUNC:
    func.SetBytecode((struct G__bytecodefunc*)F);
    func.SetArg((long)I);
    result = func.ExecInt((void*)NULL);
    break;
    // # using true pointer to function 
  case G__COMPILEDTRUEFUNC:
    // # pointer not in CINT global function table 
  case G__UNKNOWNFUNC:
    result = (*F)(I);
    break;
    // # this should never happen ( unknown kind of pointer ) 
  default:
    cerr << "Error : Unknown kind of pointer to function" << endl;
    break;
  }
  
#else /* __CINT__ */
  
  result = (*F)(I);
  
#endif /* __CINT__ */
  
  return result;
}

// end of file call.cxx by Jacek M. Holeczek

