/*
Copyright (C) 2001-2009, Parrot Foundation.
$Id: multisub.pmc 46700 2010-05-16 12:30:35Z bacek $

=head1 NAME

src/pmc/multisub.pmc - A container for multi-dispatched subs

=head1 DESCRIPTION

This class inherits from ResizablePMCArray and provides an Array of
Sub PMCs with the same short name, but different long names.

=head2 Functions

=over 4

=cut

*/

#include "pmc/pmc_callcontext.h"

/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */

pmclass MultiSub extends ResizablePMCArray auto_attrs provides array provides invokable {

    VTABLE STRING * get_string() {
        PMC * const sub0    = VTABLE_get_pmc_keyed_int(INTERP, SELF, 0);
        STRING * const name = VTABLE_get_string(INTERP, sub0);
        return name;
    }

    VTABLE void push_pmc(PMC *value) {
        STRING * const _sub = CONST_STRING(INTERP, "Sub");
        STRING * const _nci = CONST_STRING(INTERP, "NCI");

        if (!VTABLE_isa(INTERP, value, _sub)
        &&  !VTABLE_isa(INTERP, value, _nci))
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                    "attempt to push non Sub PMC");

        SUPER(value);
    }

    VTABLE void set_pmc_keyed_int(INTVAL key, PMC *value) {
        STRING * const _sub = CONST_STRING(INTERP, "Sub");
        if (!VTABLE_isa(INTERP, value, _sub))
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                    "attempt to set non Sub PMC");
        SUPER(key, value);
    }

    VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
        Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                "attempt to set non Sub PMC");
    }

    VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
        Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                "attempt to set non Sub PMC");
    }

    VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
        Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                "attempt to set non Sub PMC");
    }

    VTABLE opcode_t *invoke(void *next) {
        PMC * const sig_obj = CONTEXT(INTERP)->current_sig;
        PMC * const func    = Parrot_mmd_sort_manhattan_by_sig_pmc(INTERP,
                SELF, sig_obj);

        if (PMC_IS_NULL(func))
            Parrot_ex_throw_from_c_args(INTERP, NULL, 1,
                    "No applicable candidates found to dispatch to for '%Ss'",
                    VTABLE_get_string(INTERP, SELF));
        return VTABLE_invoke(INTERP, func, next);
    }


    /* I don't really know how to implement these if they need something
       special, so I'll sort the sub list and defer processing to the
       ResizablePMCArray's VTABLEs of the same names. Hopefully we
       don't need anything beyond that. */
    VTABLE PMC *get_pmc_keyed(PMC *key) {
        PMC * const sig_obj = CONTEXT(INTERP)->current_sig;
        PMC * const sub     = Parrot_mmd_sort_manhattan_by_sig_pmc(INTERP,
                SELF, sig_obj);

        if (PMC_IS_NULL(sub))
            Parrot_ex_throw_from_c_args(INTERP, NULL, 1,
                    "No applicable candidates found to dispatch to for '%Ss'",
                    VTABLE_get_string(INTERP, SELF));
        return SUPER(key);
    }

    VTABLE PMC *get_pmc_keyed_str(STRING *s) {
        PMC * const sig_obj = CONTEXT(INTERP)->current_sig;
        PMC * const sub     = Parrot_mmd_sort_manhattan_by_sig_pmc(INTERP,
                SELF, sig_obj);

        if (PMC_IS_NULL(sub))
            Parrot_ex_throw_from_c_args(INTERP, NULL, 1,
                    "No applicable candidates found to dispatch to for '%Ss'",
                    VTABLE_get_string(INTERP, SELF));
        return SUPER(s);
    }

    /* get_iter method should take one of two inputs: either an array of
       arguments, or a string with a whitespace delimited function signature
       (N, I, S, P). It should sort the argument list according to closeness
       to this input argument list and return an iterator over that list. */

    METHOD get_iter(PMC *args) {
        STRING * const _array  = CONST_STRING(INTERP, "Array");
        STRING * const _string = CONST_STRING(INTERP, "String");

        if (VTABLE_isa(INTERP, args, _array)){
            /* TODO: What goes here? */
        }
        else if (VTABLE_isa(INTERP, args, _string)) {
            STRING * const s = VTABLE_get_string(INTERP, args);
            /* TODO: What goes here? */
        }
        else
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                    "attempt to call get_iter method with invalid arg type.\n");
    }
}

/*

=back

=head1 SEE ALSO

F<src/multidispatch.c>,
F<$perl6/doc/trunk/design/apo/A12.pod>,
F<$perl6/doc/trunk/design/syn/S12.pod>

=cut

*/

/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
