/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/* list_chdl.cpp */
#include <ch.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "list.h"


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

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

EXPORTCH void List_dList_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;

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

EXPORTCH void List_append_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;
  struct Data data;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*);
  data = Ch_VaArg(interp, ap, struct Data);
  c->append(data);

  Ch_VaEnd(interp, ap);
}

EXPORTCH void List_prepend_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;
  struct Data data;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*);
  data = Ch_VaArg(interp, ap, struct Data);
  c->prepend(data);

  Ch_VaEnd(interp, ap);
}

EXPORTCH void List_insert_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;
  struct Data data;
  int pos;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*);
  data = Ch_VaArg(interp, ap, struct Data);
  pos = Ch_VaArg(interp, ap, int);
  c->insert(data, pos);

  Ch_VaEnd(interp, ap);
}

EXPORTCH void List_printList_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*); 
  c->printList();
  Ch_VaEnd(interp, ap);
}

EXPORTCH int List_printName_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;
  char *name;
  int retval;

  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*);
  name  = Ch_VaArg(interp, ap, char *);
  retval = c->printName(name);
  Ch_VaEnd(interp, ap);

  return retval;
}

EXPORTCH void List_removeElement_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  int id ;
  class List *c;
  
  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*); 
  id = Ch_VaArg(interp, ap, int);
  c->removeElement(id);
  Ch_VaEnd(interp, ap);
}

EXPORTCH void List_clear_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  class List *c;
 
  Ch_VaStart(interp, ap, varg);
  c = Ch_VaArg(interp, ap, class List*);
  c->clear();
  Ch_VaEnd(interp, ap);
}

EXPORTCH void createData_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  struct Data *newData;
  int id;
  char *name;

  Ch_VaStart(interp, ap, varg);
  newData = Ch_VaArg(interp, ap, struct Data *);
  id = Ch_VaArg(interp, ap, int);
  name = Ch_VaArg(interp, ap, char *);
  createData(newData, id, name);
  Ch_VaEnd(interp, ap);
}

EXPORTCH void freeMem_chdl(void *varg) {
  ChInterp_t interp;
  ChVaList_t ap;
  struct Data *elem;

  Ch_VaStart(interp, ap, varg);
  elem = Ch_VaArg(interp, ap, struct Data *);
  freeMem(elem);
  Ch_VaEnd(interp, ap);
}

