/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
/****** member functions of Class1 ******/
Class1::Class1(...){
  void *fptr;
  va_list ap;
  int vacount;
  int i;
  double d;

  if(g_sample_dlhandle == NULL || g_sample_dlcount == 0) {
    g_sample_dlhandle = dlopen("libsampclass.dl", RTLD_LAZY);
    if(g_sample_dlhandle == NULL) {
       printf("Error: %s(): dlopen(): %s\n", __class_func__, dlerror());
       return;
    }
  }
  fptr = dlsym(g_sample_dlhandle, "Class1_Class1_chdl");
  if(fptr == NULL)  {
    printf("Error: %s(): dlsym(): %s\n", __class_func__, dlerror());
    return;
  }

  va_start(ap, VA_NOARG);
  vacount = va_count(ap);
  if(vacount == 0) {
    dlrunfun(fptr, NULL, NULL);
  }
  else if(vacount == 1) { /* integral value for 1st arg */
    if(va_elementtype(ap)  <= elementtype(int)) {
      i = va_arg(ap, int);
      dlrunfun(fptr, NULL, NULL, i);
    }
    else {
      printf("Error: wrong data type for arg %d in %s()\n", vacount,__class_func__);
    }
  }
  else if(vacount == 2) { /* floating-point number for 2nd arg */
    if(va_elementtype(ap)  <= elementtype(int)) {
      i = va_arg(ap, int);
    }
    else {
      i = va_arg(ap, int); // remove the wrong argument
      printf("Error: wrong data type for arg %d in %s()\n", vacount-1,__class_func__);
    }
    if(va_elementtype(ap)  == elementtype(float)) {
      d = va_arg(ap, float);
      dlrunfun(fptr, NULL, NULL, i, d);
    }
    else if(va_elementtype(ap)  == elementtype(double)) {
      d = va_arg(ap, double);
      dlrunfun(fptr, NULL, NULL, i, d);
    }
    else {
      printf("Error: wrong data type for arg %d in %s()\n", vacount,__class_func__);
    }
  }
  else {
      printf("Error: wrong number of arguments for %s()\n", __class_func__);
  }
  g_sample_dlcount++; // to increase count of instance
  return;
}

Class1::~Class1() {
  void *fptr;
  fptr = dlsym(g_sample_dlhandle, "Class1_dClass1_chdl");
  if(fptr == NULL)  {
     printf("Error: %s(): dlsym(): %s\n", __class_func__, dlerror());
     return;
  }

  dlrunfun(fptr, NULL, NULL, this);
  g_sample_dlcount--; // to decrease count of instance

  if(g_sample_dlcount <= 0 && g_sample_dlhandle != NULL) {
    if(dlclose(g_sample_dlhandle)!=0)
       printf("Error: %s(): dlclose(): %s\n", __class_func__, dlerror());
  }
  return;
}
