/* This file is compiled to a shared library that QP can load with load_foreign_executable/1 */
#include <stdio.h>


extern long real_callback_fun(long); /* in testapp.c */

#ifdef __cplusplus              /* C++ */
#define LANG_TAG "C++: "
#else  /* !C++ */
#define LANG_TAG "C: "
#endif /* !C++ */


#ifdef __cplusplus
static int static_foreign_initializer(void)
{
  /* If this is called then static initializer works in a foreign resource, implying the C++ is properly set up. */
  fprintf(stderr, LANG_TAG "Static initializer in foreign code called, must be C++\n");fflush(stderr);
  return 42;
}

int foreign_statically_initialized = static_foreign_initializer(); /* only valid in C++ */
#endif /* __cplusplus */


#if DUMMY_VERSION
long real_callback_fun(long x)
{
  fprintf(stderr, LANG_TAG "In DUMMY real_callback_fun(%ld)\n", x);fflush(stderr);  
  return 4711;
}
#endif /* DUMMY_VERSION */

#ifdef __cplusplus
extern "C" long callback_fun(long x); /* ensure C linkage (e.g., no name mangling) */
#endif

/* Declared as foreign(callback_fun, c, callback_pred(+integer, -integer)). */
long callback_fun(long x)
{
  long res;
  fprintf(stderr, LANG_TAG "In callback_fun(%ld)\n", x);fflush(stderr);
  res = real_callback_fun(x);
  fprintf(stderr, LANG_TAG "In callback_fun(%ld), returning %ld\n", x, res);fflush(stderr);
  return res;
}
