/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
#include <stdio.h>
#include <ch.h>
#include <string.h>
#include "sampclass.h"

EXPORTCH void Class1_Class1_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class1 *c = new Class1();

  Ch_VaStart(interp, ap, varg);
  Ch_CppChangeThisPointer(interp, c, sizeof(Class1));
  Ch_VaEnd(interp, ap);
}

EXPORTCH void Class1_dClass1_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class1 *c;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class Class1 *);
  if(Ch_CppIsArrayElement(interp))
    c->~Class1();
  else
    delete c;
  Ch_VaEnd(interp, ap);
  return;
}

EXPORTCH int Class1_memfun1_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class1 *c;
  int i;
  int retval;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class Class1*); 
  i = Ch_VaArg(interp, ap, int);         /* get 1st arg  */
  retval = c->memfun1(i);
  Ch_VaEnd(interp, ap);
  return retval;
}

EXPORTCH void Class2_Class2_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class2 *c = new Class2();

  Ch_VaStart(interp, ap, varg);
  Ch_CppChangeThisPointer(interp, c, sizeof(Class2));
  Ch_VaEnd(interp, ap);
}

EXPORTCH void Class2_dClass2_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class2 *c;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class Class2 *);
  if(Ch_CppIsArrayElement(interp))
    c->~Class2();
  else
    delete c;
  Ch_VaEnd(interp, ap);
  return;
}

EXPORTCH void Class2_memfun1_chdl(void *varg) { /* returning Class1 */
  ChInterp_t interp;
  ChVaList_t ap;
  class Class2 *c;
  Class1 *retval;

  Ch_VaStart(interp, ap, varg);
  retval = Ch_VaArg(interp, ap, class Class1*);
  c = Ch_VaArg(interp, ap, class Class2*);
  *retval = c->memfun1(); /* copy over */
  Ch_VaEnd(interp, ap);
}

EXPORTCH int Class2_memfun2_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class Class2 *c;
  class Class1 *pc;
  int retval;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class Class2*);
  pc = Ch_VaArg(interp, ap, class Class1*);        
  retval = c->memfun2(*pc);
  Ch_VaEnd(interp, ap);
  return retval;
}
