/*
 *   SFC_MEX.H  Stateflow mex header file.
 *
 *   Copyright (c) 1995-2000 by The MathWorks, Inc.
 *
 *   $Revision: 1.15 $  $Date: 2000/08/29 21:51:32 $
 */

#ifndef SFC_MEX_H

#define SFC_MEX_H

#include "mex.h"
#include <setjmp.h>



typedef enum {
	ML_VOID,
	ML_DOUBLE,
    ML_INTEGER,
	ML_STRING,
	ML_MX_ARRAY} MatlabDataType; /* useful for passing arguments to ml_call_function*/

typedef enum {
	BASE_WORKSPACE,
	CALLER_WORKSPACE,
	GLOBAL_WORKSPACE,
	ALL_WORKSPACES} MatlabWorkspaceType; 


extern int strcmp_ignore_ws( const char *string1, const char *string2 );
extern bool sf_mex_get_halt_simulation(void);
extern void sf_mex_clear_error_message(void);
extern const char* sf_mex_get_error_message_for_read(void);
extern unsigned int sf_mex_get_error_message_buffer_length(void);
void sf_mex_listen_for_ctrl_c(void);
void sf_mex_printf(const char *msg, ...);


/* 
 * Calls to the following functions and macros should always be followed immediately by either RETURN_ON_ERROR
 * or RETURN_VALUE_ON_ERROR to propagate errors up the call stack.
 * Technically, these are not necessary if the call is followed immediately by an
 * unconditional return.  However, any failure to follow this regime is likely to produce
 * very subtle and difficult bugs so I urge rigid adherence to the discipline of adding
 * the macros.  It's also good documentation that you know what you're doing.
 *
 * Sometimes these macros don't work well with sf_mex_call_matlab() because you need to 
 * clean up mxArrays etc before returning (to avoid leaks).  So sf_mex_call_matlab traps the error 
 * and also returns it.  Test the result for non-zero and do the right thing.
 */
extern int sf_mex_call_matlab(int nlhs, mxArray* plhs[], int nrhs, mxArray* prhs[], const char* fcnName);
extern double sf_matlab( const char *eval, ... );
extern double ml_set_scalar(const char *variableName, MatlabWorkspaceType workspaceType, unsigned int numDimensions,...);
extern double ml_get_scalar(const char *variableName, MatlabWorkspaceType workspaceType, unsigned int numDimensions,...);
extern double ml_call_function(const char *functionName, MatlabDataType functionReturnType,unsigned int numArguments,...);
extern const mxArray *ml_get_vector(const char *variableName, 
									MatlabWorkspaceType workspaceType, 
									unsigned int numDimensions,
									int indexArray[],
									unsigned int ignoreErrors);
extern const mxArray *ml_get_typed_vector(const char *variableName, 
										  MatlabWorkspaceType workspaceType, 
										  unsigned int numDimensions, 
										  int indexArray[], 
										  mxClassID classId, 
										  unsigned int implicitCastingEnabled,
										  unsigned int ignoreErrors);
extern double ml_get_typed_scalar(const char *variableName, 
						   MatlabWorkspaceType workspaceType, 
						   mxClassID requiredType,
						   unsigned int ignoreErrors);
extern double ml_get_element_value_from_array(const mxArray *mxArrayPtr, unsigned int index);
extern void ml_set_element_value_in_array(const mxArray *mxArrayPtr, unsigned int index, double value);
extern double ml_eval_scalar(const char *expression, 
							 MatlabWorkspaceType workspaceType, 
							 unsigned int ignoreErrors);
extern const mxArray *ml_eval_vector_with_scalar_expansion(const char *expression,
						   MatlabWorkspaceType workspaceType,
						   unsigned int numDimensions,
						   int indexArray[],
						   unsigned int ignoreErrors);
const mxArray *ml_get_or_create_vector(const char *variableName,
						   MatlabWorkspaceType workspaceType,
						   unsigned int numDimensions,
						   int indexArray[],
						   unsigned int ignoreErrors);
extern void sf_mex_set_halt_simulation(bool halt);
extern void sf_mex_error_message(const char *msg, ...);
extern void sf_mex_warning_message(const char *msg, ...);
extern void sf_mex_long_jump_on_error(unsigned int longJumpType);
extern void sf_assertion_failed(const char *expr, const char *in_file, int at_line, const char *msg);
extern void sf_clear_error_manager_info(void);
extern void sf_set_error_prefix_string(const char *errorPrefixString);
extern char * sf_get_error_prefix_string(void);
extern void sf_echo_expression(const char *expr, real_T value);


extern jmp_buf *sf_get_current_jmp_buf_ptr(void);
extern void sf_set_current_jmp_buf_ptr(jmp_buf *jumpbufPtr);


#define sf_assertion(expr) ((expr)?(void)0:sf_assertion_failed(#expr,__FILE__,__LINE__,NULL))
#define sf_assertion_msg(expr,msg) ((expr)?(void)0:sf_assertion_failed(#expr,__FILE__,__LINE__,msg))

#ifdef TRUE
#undef TRUE
#endif

#ifdef FALSE
#undef FALSE
#endif


#endif /*SF_MEX_H */

