# $Header$ 
# basic C types
WORD			T_IV
LONG                    T_IV
int                     T_IV
DWORD                   T_IV
unsigned                T_IV
unsigned int            T_IV
long                    T_IV
unsigned long           T_IV
short                   T_IV
unsigned short          T_IV
char                    T_CHAR
unsigned char           T_U_CHAR
char *                  T_PV
unsigned char *         T_PV
caddr_t                 T_PV
long *                  T_PV
DWORD *                 T_PV
wchar_t *               T_PV
wchar_t                 T_IV
bool_t                  T_IV
size_t                  T_IV
ssize_t                 T_IV
time_t                  T_NV
unsigned long *         T_OPAQUEPTR
char **                 T_PTR
void **                 T_PTR
void *                  T_PTR
Time_t *                T_PV
SV *                    T_SV
SVREF                   T_SVREF
AV *                    T_AVREF
HV *                    T_HVREF
CV *                    T_CVREF

INT                     T_IV
VOID                    T_IV
LPWSTR                  T_PV
LPBYTE                  T_PV
LPSTR			T_PV
LPDWORD                 T_PV
LPBYTE *                T_PTRREF
HANDLE                  T_PTR
LPCSTR                  T_PV
LPCTSTR                 T_PV
LPCTSTR *               T_PTR
PTSHARE_INFO			T_OPAQUEPTR
LPSECURITY_ATTRIBUTES   T_PTRREF
SECURITY_DESCRIPTOR     T_PV
PSECURITY_DESCRIPTOR    T_PTRREF
SECURITY_INFORMATION    T_PTRREF
WORD                    T_IV
LPSTARTUPINFO           T_PTRREF
LPNETRESOURCE           T_OPAQUEPTR
LPNETRESOURCEA          T_OPAQUEPTR
LPSERVICE_STATUS        T_PTRREF
LPPROCESS_INFORMATION   T_PTRREF
LPQUERY_SERVICE_CONFIG  T_PTRREF
LPENUM_SERVICE_STATUS   T_PTRREF
BOOL                    T_IV
LPVOID                  T_PTR
LPFILETIME              T_PTRREF
UINT                    T_IV
LPTSTR                  T_PV
LPHANDLE                T_PACKED
SC_HANDLE               T_PV
cProcess *               T_PTROBJ
Cipc *                  T_PTROBJ
Semaphore *            T_PTROBJ
Mutex *                 T_PTROBJ
ChangeNotification *    T_PTROBJ

IV                      T_IV
I32                     T_IV
I16                     T_IV
I8                      T_IV
U32                     T_U_LONG
U16                     T_U_SHORT
U8                      T_IV
Result                  T_U_CHAR
boolean                 T_IV
Boolean                 T_IV
double                  T_DOUBLE
SysRet                  T_SYSRET
SysRetLong              T_SYSRET
FILE *                  T_IN
FHANDLE					T_INOUT
FileHandle              T_PTROBJ
InputStream             T_IN
InOutStream             T_INOUT
OutputStream            T_OUT

#############################################################################
INPUT
T_SV
	$var = $arg
T_SVREF
	if (sv_isa($arg, \"${ntype}\"))
	    $var = (SV*)SvRV($arg);
	else
	    croak(\"$var is not of type ${ntype}\")
T_AVREF
	if (sv_isa($arg, \"${ntype}\"))
	    $var = (AV*)SvRV($arg);
	else
	    croak(\"$var is not of type ${ntype}\")
T_HVREF
	if (sv_isa($arg, \"${ntype}\"))
	    $var = (HV*)SvRV($arg);
	else
	    croak(\"$var is not of type ${ntype}\")
T_CVREF
	if (sv_isa($arg, \"${ntype}\"))
	    $var = (CV*)SvRV($arg);
	else
	    croak(\"$var is not of type ${ntype}\")
T_SYSRET
	$var NOT IMPLEMENTED
T_IV
	$var = ($type)SvIV($arg)
T_INT
	$var = (int)SvIV($arg)
T_ENUM
	$var = ($type)SvIV($arg)
T_U_INT
	$var = (unsigned int)SvIV($arg)
T_SHORT
	$var = (short)SvIV($arg)
T_U_SHORT
	$var = (unsigned short)SvIV($arg)
T_LONG
	$var = (long)SvIV($arg)
T_U_LONG
	$var = (unsigned long)SvIV($arg)
T_CHAR
	{ STRLEN n_a; $var = (char)*SvPV($arg,n_a); }
T_U_CHAR
	$var = (unsigned char)SvIV($arg)
T_FLOAT
	$var = (float)SvNV($arg)
T_NV
	$var = ($type)SvNV($arg)
T_DOUBLE
	$var = (double)SvNV($arg)
T_PV
	{ STRLEN n_a; $var = ($type)SvPV($arg,n_a); }
T_PTR
	$var = ($type)SvIV($arg)
T_PTRREF
	if (SvROK($arg)) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = ($type) tmp;
	}
	else
	    croak(\"$var is not a reference\")
T_REF_IV_REF
	if (sv_isa($arg, \"${type}\")) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = *($type *) tmp;
	}
	else
	    croak(\"$var is not of type ${ntype}\")
T_REF_IV_PTR
	if (sv_isa($arg, \"${type}\")) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = ($type) tmp;
	}
	else
	    croak(\"$var is not of type ${ntype}\")
T_PTROBJ
	if (sv_isa($arg, \"${ntype}\")) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = ($type) tmp;
	}
	else
	    croak(\"$var is not of type ${ntype}\")
T_PTRDESC
	if (sv_isa($arg, \"${ntype}\")) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    ${type}_desc = (\U${type}_DESC\E*) tmp; 
	    $var = ${type}_desc->ptr;
	}
	else
	    croak(\"$var is not of type ${ntype}\")
T_REFREF
	if (SvROK($arg)) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = *($type) tmp;
	}
	else
	    croak(\"$var is not a reference\")
T_REFOBJ
	if (sv_isa($arg, \"${ntype}\")) {
	    IV tmp = SvIV((SV*)SvRV($arg));
	    $var = *($type) tmp;
	}
	else
	    croak(\"$var is not of type ${ntype}\")
T_OPAQUE
	$var NOT IMPLEMENTED
T_OPAQUEPTR
	{STRLEN n_a; $var = ($type)SvPV($arg,n_a); }
T_PACKED
	$var = XS_unpack_$ntype($arg)
T_PACKEDARRAY
	$var = XS_unpack_$ntype($arg)
T_CALLBACK
	$var = make_perl_cb_$type($arg)
T_ARRAY
	$var = $ntype(items -= $argoff);
	U32 ix_$var = $argoff;
	while (items--) {
	    DO_ARRAY_ELEM;
	}
T_IN
	$var = IoIFP(sv_2io($arg))
T_INOUT
	$var = IoIFP(sv_2io($arg))
T_OUT
	$var = IoOFP(sv_2io($arg))
T_FHANDLE
	GV* gv = (GV *)($arg);
	IO* io;
	FILE* fp;
	if( !gv ||!(io= GvIO(gv))||!(fp=IoIFP(io)))
		croak(\"$var is not of type ${ntype}\");
	else
		$var = ($ntype)fileno(fp);

		
		
	
#############################################################################
OUTPUT
T_SV
	$arg = $var;
T_SVREF
	$arg = newRV((SV*)$var);
T_AVREF
	$arg = newRV((SV*)$var);
T_HVREF
	$arg = newRV((SV*)$var);
T_CVREF
	$arg = newRV((SV*)$var);
T_IV
	sv_setiv($arg, (IV)$var);
T_INT
	sv_setiv($arg, (IV)$var);
T_SYSRET
	if ($var != -1) {
	    if ($var == 0)
		sv_setpvn($arg, "0 but true", 10);
	    else
		sv_setiv($arg, (IV)$var);
	}
T_ENUM
	sv_setiv($arg, (IV)$var);
T_U_INT
	sv_setiv($arg, (IV)$var);
T_SHORT
	sv_setiv($arg, (IV)$var);
T_U_SHORT
	sv_setiv($arg, (IV)$var);
T_LONG
	sv_setiv($arg, (IV)$var);
T_U_LONG
	sv_setiv($arg, (IV)$var);
T_CHAR
	sv_setpvn($arg, (char *)&$var, 1);
T_U_CHAR
	sv_setiv($arg, (IV)$var);
T_FLOAT
	sv_setnv($arg, (double)$var);
T_NV
	sv_setnv($arg, (double)$var);
T_DOUBLE
	sv_setnv($arg, (double)$var);
T_PV
	sv_setpv((SV*)$arg, (char *)$var);
T_PTR
	sv_setiv($arg, (IV)$var);
T_PTRREF
	sv_setref_pv($arg, Nullch, (void*)$var);
T_REF_IV_REF
	sv_setref_pv($arg, \"${ntype}\", (void*)new $ntype($var));
T_REF_IV_PTR
	sv_setref_pv($arg, \"${ntype}\", (void*)$var);
T_PTROBJ
	sv_setref_pv($arg, \"${ntype}\", (void*)$var);
T_PTRDESC
	sv_setref_pv($arg, \"${ntype}\", (void*)new\U${type}_DESC\E($var));
T_REFREF
	sv_setrefref($arg, \"${ntype}\", XS_service_$ntype,
		    ($var ? (void*)new $ntype($var) : 0));
T_REFOBJ
	NOT IMPLEMENTED
T_OPAQUE
	sv_setpvn($arg, (char *)&$var, sizeof($var));
T_OPAQUEPTR
	sv_setpvn($arg, (char *)$var, sizeof(*$var) ) ;
T_PACKED
	XS_pack_$ntype($arg, $var);
T_PACKEDARRAY
	XS_pack_$ntype($arg, $var, count_$ntype);
T_DATAUNIT      
	sv_setpvn($arg, $var.chp(), $var.size());
T_CALLBACK
	sv_setpvn($arg, $var.context.value().chp(),
		$var.context.value().size());
T_ARRAY
	ST_EXTEND($var.size);
	for (U32 ix_$var = 0; ix_$var < $var.size; ix_$var++) {
		ST(ix_$var) = sv_newmortal();
	DO_ARRAY_ELEM
	}
	sp += $var.size - 1;
T_IN
	{
	    GV *gv = newGVgen("$Package");
	    if ( do_open(gv, "<&", 2, $var) )
		sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1)));
	    else
		$arg = &sv_undef;
	}
T_INOUT
	{
	    GV *gv = newGVgen("$Package");
	    if ( do_open(gv, "+<&", 3, $var) )
		sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1)));
	    else
		$arg = &sv_undef;
	}
T_OUT
	{
	    GV *gv = newGVgen("$Package");
	    if ( do_open(gv, "+>&", 3, $var) )
		sv_setsv($arg, sv_bless(newRV((SV*)gv), gv_stashpv("$Package",1)));
	    else
		$arg = &sv_undef;
	}
T_FHANDLE
	IoIFP(sv_2io( GvIO( (GV*) $arg))) = $var
