/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.45 from the
 * contents of XMMSClientResult.xs. Do not edit this file, edit XMMSClientResult.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "../src/clients/lib/perl/XMMSClientResult.xs"
#include "perl_xmmsclient.h"

STATIC SV *value_to_sv (xmmsv_t *val);

STATIC int
notifyer_cb (xmmsv_t *val, void *user_data)
{
	int ret;
	PerlXMMSClientCallback *cb = (PerlXMMSClientCallback *)user_data;

	perl_xmmsclient_callback_invoke (cb, &ret, value_to_sv (val));

	return ret;
}

STATIC SV *
sv_from_value_int (xmmsv_t *val)
{
	int64_t num;
	int ret;

	ret = xmmsv_get_int64 (val, &num);

	if (ret == 0) {
		croak("could not fetch int value");
	}

	return newSViv (num);
}

STATIC SV *
sv_from_value_float (xmmsv_t *val)
{
	float num;
	int ret;

	ret = xmmsv_get_float (val, &num);

	if (ret == 0) {
		croak("could not fetch int value");
	}

	return newSVnv ((double) num);
}

STATIC SV *
sv_from_value_string (xmmsv_t *val)
{
	int ret;
	const char *str = NULL;

	ret = xmmsv_get_string (val, &str);

	if (ret == 0) {
		croak("could not fetch string value");
	}

	return newSVpv (str, 0);
}

STATIC SV *
sv_from_value_coll (xmmsv_t *val)
{
	return perl_xmmsclient_new_sv_from_ptr ((void *) xmmsv_ref (val), "Audio::XMMSClient::Collection");
}

STATIC SV *
sv_from_value_bin (xmmsv_t *val)
{
	int ret;
	const unsigned char *bin;
	unsigned int bin_len = 0;

	ret = xmmsv_get_bin (val, &bin, &bin_len);

	if (ret == 0) {
		croak("could not fetch bin value");
	}

	return newSVpv ((char *)bin, bin_len);
}

STATIC void
list_foreach_cb (xmmsv_t *value, void *user_data)
{
	AV *list = (AV *)user_data;
	av_push (list, value_to_sv (value));
}

STATIC SV *
sv_from_value_list (xmmsv_t *val)
{
	int ret;
	AV *list = newAV ();

	ret = xmmsv_list_foreach (val, list_foreach_cb, list);

	if (ret == 0) {
		croak ("could not fetch list value");
	}

	return newRV_inc ((SV *)list);
}

STATIC void
dict_foreach_cb (const char *key, xmmsv_t *value, void *user_data)
{
	HV *hash = (HV *)user_data;

	if (!hv_store (hash, key, strlen (key), value_to_sv (value), 0)) {
		croak ("failed to convert result to hash");
	}
}

STATIC SV *
sv_from_value_dict (xmmsv_t *val)
{
	int ret;
	HV *dict = newHV ();

	ret = xmmsv_dict_foreach (val, dict_foreach_cb, dict);

	if (ret == 0) {
		croak ("could not fetch dict value");
	}

	return newRV_inc ((SV *)dict);
}

STATIC void
croak_value_error (xmmsv_t *val) {
	int ret;
	const char *msg;

	ret = xmmsv_get_error (val, &msg);

	if (ret == 0) {
		croak ("could not fetch error message");
	}

	croak ("%s", msg);
}

STATIC SV *
value_to_sv (xmmsv_t *value) {
	xmmsv_type_t type;
	SV *ret;

	type = xmmsv_get_type (value);

	switch (type) {
		case XMMSV_TYPE_NONE:
			ret = &PL_sv_undef;
			break;
		case XMMSV_TYPE_ERROR:
			croak_value_error (value);
			break;
		case XMMSV_TYPE_INT64:
			ret = sv_from_value_int (value);
			break;
		case XMMSV_TYPE_FLOAT:
			ret = sv_from_value_float (value);
			break;
		case XMMSV_TYPE_STRING:
			ret = sv_from_value_string (value);
			break;
		case XMMSV_TYPE_COLL:
			ret = sv_from_value_coll (value);
			break;
		case XMMSV_TYPE_BIN:
			ret = sv_from_value_bin (value);
			break;
		case XMMSV_TYPE_LIST:
			ret = sv_from_value_list (value);
			break;
		case XMMSV_TYPE_DICT:
			ret = sv_from_value_dict (value);
			break;
		default:
			croak ("unhandled value type");
	}

	return ret;
}

#line 196 "../src/clients/lib/perl/XMMSClientResult.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 340 "../src/clients/lib/perl/XMMSClientResult.c"

XS_EUPXS(XS_Audio__XMMSClient__Result_get_class); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_get_class)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
	xmmsc_result_type_t	RETVAL;

	RETVAL = xmmsc_result_get_class(res);
	{
	    SV * RETVALSV;
	    RETVALSV = sv_newmortal();
	    {
		    RETVALSV = newSVpv ("unknown", 0);
		    if (RETVAL == XMMSC_RESULT_CLASS_DEFAULT) {
			    sv_setpv (RETVALSV, "default");
		    } else if (RETVAL == XMMSC_RESULT_CLASS_SIGNAL) {
			    sv_setpv (RETVALSV, "signal");
		    } else if (RETVAL == XMMSC_RESULT_CLASS_BROADCAST) {
			    sv_setpv (RETVALSV, "broadcast");
		    }
	    }
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_notifier_set); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_notifier_set)
{
    dVAR; dXSARGS;
    if (items < 2 || items > 3)
       croak_xs_usage(cv,  "res, func, data=NULL");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
	SV *	func = ST(1)
;
	SV *	data;
#line 254 "../src/clients/lib/perl/XMMSClientResult.xs"
		PerlXMMSClientCallback *cb = NULL;
		PerlXMMSClientCallbackParamType param_types[1];
#line 389 "../src/clients/lib/perl/XMMSClientResult.c"

	if (items < 3)
	    data = NULL;
	else {
	    data = ST(2)
;
	}
#line 257 "../src/clients/lib/perl/XMMSClientResult.xs"
		param_types[0] = PERL_XMMSCLIENT_CALLBACK_PARAM_TYPE_VALUE;

		cb = perl_xmmsclient_callback_new (func, data, ST(0), 1, param_types,
		                                   PERL_XMMSCLIENT_CALLBACK_RETURN_TYPE_INT);

		xmmsc_result_notifier_set_full (res, notifyer_cb, cb,
		                                (xmmsc_user_data_free_func_t)perl_xmmsclient_callback_destroy);
#line 405 "../src/clients/lib/perl/XMMSClientResult.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Audio__XMMSClient__Result_wait); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_wait)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	SV *	res = ST(0)
;
#line 287 "../src/clients/lib/perl/XMMSClientResult.xs"
		xmmsc_result_t *c_res;
#line 422 "../src/clients/lib/perl/XMMSClientResult.c"
	SV *	RETVAL;
#line 289 "../src/clients/lib/perl/XMMSClientResult.xs"
		c_res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (res, "Audio::XMMSClient::Result");

		xmmsc_result_wait (c_res);

		SvREFCNT_inc (res);
		RETVAL = res;
#line 431 "../src/clients/lib/perl/XMMSClientResult.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_get_type); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_get_type)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
	xmmsv_type_t	RETVAL;
#line 319 "../src/clients/lib/perl/XMMSClientResult.xs"
		RETVAL = xmmsv_get_type (xmmsc_result_get_value (res));
#line 451 "../src/clients/lib/perl/XMMSClientResult.c"
	{
	    SV * RETVALSV;
	    RETVALSV = sv_newmortal();
	    {
		    RETVALSV = newSVpv ("unknown", 0);
		    if (RETVAL == XMMSV_TYPE_NONE) {
			    sv_setpv (RETVALSV, "none");
		    } else if (RETVAL == XMMSV_TYPE_ERROR) {
			    sv_setpv (RETVALSV, "error");
		    } else if (RETVAL == XMMSV_TYPE_INT64) {
			    sv_setpv (RETVALSV, "int");
	    } else if (RETVAL == XMMSV_TYPE_FLOAT) {
			    sv_setpv (RETVALSV, "float");
		    } else if (RETVAL == XMMSV_TYPE_STRING) {
			    sv_setpv (RETVALSV, "string");
		    } else if (RETVAL == XMMSV_TYPE_DICT) {
			    sv_setpv (RETVALSV, "dict");
		    } else if (RETVAL == XMMSV_TYPE_BIN) {
			    sv_setpv (RETVALSV, "bin");
		    } else if (RETVAL == XMMSV_TYPE_COLL) {
			    sv_setpv (RETVALSV, "coll");
		    } else if (RETVAL == XMMSV_TYPE_LIST) {
			    sv_setpv (RETVALSV, "list");
		    }
	    }
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_iserror); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_iserror)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
	int	RETVAL;
	dXSTARG;

	RETVAL = xmmsc_result_iserror(res);
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_get_error); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_get_error)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
#line 364 "../src/clients/lib/perl/XMMSClientResult.xs"
		xmmsv_t *val;
#line 515 "../src/clients/lib/perl/XMMSClientResult.c"
	const char *	RETVAL;
	dXSTARG;
#line 366 "../src/clients/lib/perl/XMMSClientResult.xs"
		RETVAL = NULL;
		val = xmmsc_result_get_value (res);
		if (val) {
			xmmsv_get_error (val, &RETVAL);
		}
#line 524 "../src/clients/lib/perl/XMMSClientResult.c"
	sv_setpv(TARG, RETVAL);
	XSprePUSH;
	PUSHTARG;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_value); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_value)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
	SV *	RETVAL;
#line 395 "../src/clients/lib/perl/XMMSClientResult.xs"
		RETVAL = value_to_sv (xmmsc_result_get_value (res));
#line 545 "../src/clients/lib/perl/XMMSClientResult.c"
	RETVAL = sv_2mortal(RETVAL);
	ST(0) = RETVAL;
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Audio__XMMSClient__Result_DESTROY); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Audio__XMMSClient__Result_DESTROY)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "res");
    {
	xmmsc_result_t *	res = (xmmsc_result_t *)perl_xmmsclient_get_ptr_from_sv (ST(0), "Audio::XMMSClient::Result")
;
#line 467 "../src/clients/lib/perl/XMMSClientResult.xs"
		xmmsc_result_unref (res);
#line 564 "../src/clients/lib/perl/XMMSClientResult.c"
    }
    XSRETURN_EMPTY;
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Audio__XMMSClient__Result); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Audio__XMMSClient__Result)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("Audio::XMMSClient::Result::get_class", XS_Audio__XMMSClient__Result_get_class);
        newXS_deffile("Audio::XMMSClient::Result::notifier_set", XS_Audio__XMMSClient__Result_notifier_set);
        newXS_deffile("Audio::XMMSClient::Result::wait", XS_Audio__XMMSClient__Result_wait);
        newXS_deffile("Audio::XMMSClient::Result::get_type", XS_Audio__XMMSClient__Result_get_type);
        newXS_deffile("Audio::XMMSClient::Result::iserror", XS_Audio__XMMSClient__Result_iserror);
        newXS_deffile("Audio::XMMSClient::Result::get_error", XS_Audio__XMMSClient__Result_get_error);
        newXS_deffile("Audio::XMMSClient::Result::value", XS_Audio__XMMSClient__Result_value);
        newXS_deffile("Audio::XMMSClient::Result::DESTROY", XS_Audio__XMMSClient__Result_DESTROY);
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

