#ifndef XM_PRIMITIVE_H_

/*
// This file is based on XM44 primitive.h so that
// NeXtMidas's XBC mode can support C++ and C primitives.
//
// Include file for C/C++ HOST primitives.
//
// This include file is typically being included as part of the main
// body of a HOST primitive.  The body of code that it would normally
// refer to as main() is provided in a routine called mainroutine().
// Below we pre-declare this mainroutine() for the primitive writer to
// ensure that they provide the proper signature.
//
// void mainroutine (void);
//
// Author:  Based on X-Midas 4.4.5 primitive.h,v 1.4 2004/09/29 16:54:13 rawson
// Author:  Neon Ngo
// Version: $Id: primitive.h,v 1.1 2009/08/11 00:25:53 ntn Exp $
*/

#include <math.h>

#if __cplusplus

/* C++ Primitives */

/* ///// System include files (C++) */

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

/* ///// X-Midas include files (C++) */

// Because the midaslib.h jackets require them, it includes: mtypes.h,
// fcinterface.h, queue.h, complex_n.h and headers.h.

#include "midaslib.h"
#include "midas.h"
#include "constants.h"
#include "fintrinsics.h"


/* ///// Mangling mainroutine() */

// As of NeXtMidas 2.7.2, XBC loads the "C" callable mainroutine_() when starting
// up any host primitive. For C++ primitives, the mainroutine_() method is
// defined in hostmainroutine.o; for C primitive, see below; for Fortran primitives
// it is automatically generated during the pre-processing with xfpp.

// In C++, the mainroutine_() adds a try-catch block around the call to the C++
// name mangled mainroutine() method provided by the primitive writer.
// This wrapper is necessary to gracefully recover from uncaught exceptions
// thrown by the C++ primitive

// On UNIX platforms, hostmainroutine.o and the NeXtMidas XBC library routines
// that load DLLs look for a symbol named "mainroutine_". This is due
// to the way they expect UNIX FORTRAN compilers to generate symbols
// for FORTRAN routines (formerly the only way to write primitives).
// This allows hostmainroutine.o to provide mainroutine_() as a
// try-catch wrapper around the call to the C++ primitive's mainroutine().


#else /* !__cplusplus */

/* C Primitives */


/* ///// X-Midas include files (C) */

#include "mjackets.h"
#include "headers.h"
#include "queue.h"
#include "midas.h"
#include "constants.h"

/* ///// Mangling mainroutine() */

/*
 * On UNIX platforms the symbol looked for by the X-Midas library
 * routines that load DLLs is "mainroutine_".  Therefore, on UNIX we
 * need to change the name of the mainroutine() method in a C-only
 * primitive to mainroutine_().
 */
#define mainroutine mainroutine_

#endif /* !__cplusplus */

void mainroutine (void);

#define XM_PRIMITIVE_H_
#endif /* XM_PRIMITIVE_H_ */
