/*
Copyright (C) 2001-2008, The Perl Foundation.
$Id: /mirror/trunk/src/pmc/capture.pmc 35686 2009-01-16T23:27:15.821289Z cotto  $

=head1 NAME

src/pmc/capture.pmc - Capture PMC

=head1 DESCRIPTION

These are the vtable functions for the Capture PMC.

=head2 Functions

=over 4

=cut

*/

#include "parrot/parrot.h"


#define CAPTURE_DATA_SIZE   2
#define CAPTURE_array_CREATE(i, obj) \
    if (!PARROT_CAPTURE(obj)->array) \
        PARROT_CAPTURE(obj)->array = pmc_new((i), enum_class_ResizablePMCArray);
#define CAPTURE_hash_CREATE(i, obj) \
    if (!PARROT_CAPTURE(obj)->hash) \
        PARROT_CAPTURE(obj)->hash = pmc_new((i), enum_class_Hash);

pmclass Capture need_ext {
    ATTR PMC *array;
    ATTR PMC *hash;

/*

=item C<void init()>

Initializes the Capture instance.

=item C<void destroy()>

Free structures.

=cut

*/

    VTABLE void init() {
        Parrot_Capture_attributes *capture = mem_allocate_typed(Parrot_Capture_attributes);
        PMC_data(SELF)          = capture;
        capture->array          = NULL;
        capture->hash           = NULL;
        PMC_int_val(SELF)       = CAPTURE_DATA_SIZE;
        PObj_active_destroy_SET(SELF);
        PObj_custom_mark_SET(SELF);
    }

    VTABLE void destroy() {
        if (PARROT_CAPTURE(SELF)) {
            mem_sys_free(PARROT_CAPTURE(SELF));
            PMC_data(SELF) = NULL;
        }

        PMC_int_val(SELF)  = 0;
    }

/*

=item C<PMC *clone()>

Creates an identical copy of the Capture.

=cut

*/

    VTABLE PMC *clone() {
        PMC *array, *hash;
        PMC *dest = pmc_new(INTERP, VTABLE_type(INTERP, SELF));
        GET_ATTR_array(INTERP, SELF, array);
        GET_ATTR_hash(INTERP, SELF, hash);
        if (array)
             array = VTABLE_clone(INTERP, array);
        if (hash)
             hash = VTABLE_clone(INTERP, hash);
        SET_ATTR_array(INTERP, dest, array);
        SET_ATTR_hash(INTERP, dest, hash);

        return dest;
    }

/*

=item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>

=item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>

=item C<void set_pmc_keyed_int(INTVAL key, PMC *value)>

=item C<void set_string_keyed_int(INTVAL key, STRING *value)>

Sets a value in the array component of the Capture.

=cut

*/

    VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_set_number_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                    key, value);
    }

    VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_set_integer_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                     key, value);
    }

    VTABLE void set_pmc_keyed_int(INTVAL key, PMC *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_set_pmc_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                 key, value);
    }

    VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_set_string_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                    key, value);
    }

/*

=item C<FLOATVAL get_number_keyed_int(INTVAL key)>

=item C<INTVAL get_integer_keyed_int(INTVAL key)>

=item C<PMC *get_pmc_keyed_int(INTVAL key)>

=item C<STRING *get_string_keyed_int(INTVAL key)>

Retrieves a value in the array component of the Capture.

=cut

*/

    VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
        if (!(PARROT_CAPTURE(SELF)->array))
            return 0.0;
        return VTABLE_get_number_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                           key);
    }

    VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
        if (!(PARROT_CAPTURE(SELF)->array))
            return 0;
        return VTABLE_get_integer_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                            key);
    }

    VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
        if (!(PARROT_CAPTURE(SELF)->array))
            return PMCNULL;
        return VTABLE_get_pmc_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                        key);
    }

    VTABLE STRING *get_string_keyed_int(INTVAL key) {
        if (!(PARROT_CAPTURE(SELF)->array))
            return CONST_STRING(INTERP, "");
        return VTABLE_get_string_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                           key);
    }

/*

=item C<void push_float(FLOATVAL value)>

=item C<void push_integer(INTVAL value)>

=item C<void push_pmc(PMC *value)>

=item C<void push_string(STRING *value)>

Push a value onto the array component of the Capture.

=item C<void unshift_float(FLOATVAL value)>

=item C<void unshift_integer(INTVAL value)>

=item C<void unshift_pmc(PMC *value)>

=item C<void unshift_string(STRING *value)>

Unshift a value onto the array component of the Capture.

=cut

*/

    VTABLE void push_float(FLOATVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_push_float(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    VTABLE void push_integer(INTVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_push_integer(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    void push_pmc(PMC *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_push_pmc(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    VTABLE void push_string(STRING *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_push_string(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    VTABLE void unshift_float(FLOATVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_unshift_float(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    VTABLE void unshift_integer(INTVAL value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_unshift_integer(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    void unshift_pmc(PMC *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_unshift_pmc(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

    VTABLE void unshift_string(STRING *value) {
        CAPTURE_array_CREATE(INTERP, SELF);
        VTABLE_unshift_string(INTERP, PARROT_CAPTURE(SELF)->array, value);
    }

/*

=item C<FLOATVAL pop_float()>

=item C<INTVAL pop_integer()>

=item C<PMC *pop_pmc()>

=item C<STRING *pop_string()>

Pop a value from the array component of the Capture.

=item C<FLOATVAL shift_float()>

=item C<INTVAL shift_integer()>

=item C<PMC *shift_pmc()>

=item C<STRING *shift_string()>

Shift a value from the array component of the Capture.

=cut

*/

    VTABLE FLOATVAL pop_float() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_pop_float(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE INTVAL pop_integer() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_pop_integer(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE PMC *pop_pmc() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_pop_pmc(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE STRING *pop_string() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_pop_string(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE FLOATVAL shift_float() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_shift_float(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE INTVAL shift_integer() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_shift_integer(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE PMC *shift_pmc() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_shift_pmc(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE STRING *shift_string() {
        CAPTURE_array_CREATE(INTERP, SELF);
        return VTABLE_shift_string(INTERP, PARROT_CAPTURE(SELF)->array);
    }

/*

=item C<INTVAL elements()>

Return the number of elements in the array component of the Capture.

=item C<INTVAL defined_keyed_int(INTVAL key)>

Return true if element C<key> of the array component is defined.

=item C<INTVAL exists_keyed_int(INTVAL key)>

Return true if element C<key> of the array component exists.

=item C<void delete_keyed_int(INTVAL key)>

Delete the element corresponding to C<key> in the array component.

=cut

*/

    VTABLE INTVAL elements() {
        if (!PARROT_CAPTURE(SELF)->array) return 0;
        return VTABLE_elements(INTERP, PARROT_CAPTURE(SELF)->array);
    }

    VTABLE INTVAL defined_keyed_int(INTVAL key) {
        if (!PARROT_CAPTURE(SELF)->array)
            return 0;

        return VTABLE_defined_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                        key);
    }

    VTABLE INTVAL exists_keyed_int(INTVAL key) {
        if (!PARROT_CAPTURE(SELF)->array)
            return 0;

        return VTABLE_exists_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
                                               key);
    }

    VTABLE void delete_keyed_int(INTVAL key) {
        if (PARROT_CAPTURE(SELF)->array)
            VTABLE_delete_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array, key);
    }

/*

=item C<void set_number_keyed(PMC *key, FLOATVAL value)>

=item C<void set_integer_keyed(PMC *key, INTVAL value)>

=item C<void set_pmc_keyed(PMC *key, PMC *value)>

=item C<void set_string_keyed(PMC *key, STRING *value)>

Sets a value in the hash component of the Capture.

=cut

*/

    VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
        CAPTURE_hash_CREATE(INTERP, SELF);
        VTABLE_set_number_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
    }

    VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
        CAPTURE_hash_CREATE(INTERP, SELF);
        VTABLE_set_integer_keyed(INTERP, PARROT_CAPTURE(SELF)->hash,
                                 key, value);
    }

    VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
        CAPTURE_hash_CREATE(INTERP, SELF);
        VTABLE_set_pmc_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
    }

    VTABLE void set_string_keyed(PMC *key, STRING *value) {
        CAPTURE_hash_CREATE(INTERP, SELF);
        VTABLE_set_string_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
    }

/*

=item C<FLOATVAL get_number_keyed(PMC *key)>

=item C<INTVAL get_integer_keyed(PMC *key)>

=item C<PMC *get_pmc_keyed(PMC *key)>

=item C<STRING *get_string_keyed(PMC *key)>

Retrieves a value from the hash component of the Capture.

=cut

*/

    VTABLE FLOATVAL get_number_keyed(PMC *key) {
        if (!(PARROT_CAPTURE(SELF)->hash))
            return 0.0;
        return VTABLE_get_number_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

    VTABLE INTVAL get_integer_keyed(PMC *key) {
        if (!(PARROT_CAPTURE(SELF)->hash))
            return 0;
        return VTABLE_get_integer_keyed(INTERP, PARROT_CAPTURE(SELF)->hash,
                                        key);
    }

    VTABLE PMC *get_pmc_keyed(PMC *key) {
        if (!(PARROT_CAPTURE(SELF)->hash))
            return PMCNULL;
        return VTABLE_get_pmc_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

    VTABLE STRING *get_string_keyed(PMC *key) {
        if (!(PARROT_CAPTURE(SELF)->hash))
            return CONST_STRING(INTERP, "");
        return VTABLE_get_string_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

/*

=item C<INTVAL defined_keyed(PMC *key)>

Return true if element C<key> of the hash component is defined.

=item C<INTVAL exists_keyed(PMC *key)>

Return true if element C<key> of the hash component exists.

=item C<void delete_keyed(PMC *key)>

Delete the element corresponding to C<key> in the hash component.

=cut

*/

    VTABLE INTVAL defined_keyed(PMC *key) {
        if (!PARROT_CAPTURE(SELF)->hash) return 0;
        return VTABLE_defined_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

    VTABLE INTVAL exists_keyed(PMC *key) {
        if (!PARROT_CAPTURE(SELF)->hash) return 0;
        return VTABLE_exists_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

    VTABLE void delete_keyed(PMC *key) {
        if (PARROT_CAPTURE(SELF)->hash)
            VTABLE_delete_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
    }

/*

=item C<void set_pmc(PMC *capture)>

Set this capture to hold the value of another.  If set to PMCNULL,
erase the contents of the array and hash components.

=cut

*/

    VTABLE void set_pmc(PMC *capture) {
        if (PMC_IS_NULL(capture)) {
            PARROT_CAPTURE(SELF)->array = NULL;
            PARROT_CAPTURE(SELF)->hash = NULL;
        }
        else if (VTABLE_isa(INTERP, capture, CONST_STRING(INTERP, "Capture"))) {
            PARROT_CAPTURE(SELF)->array = PARROT_CAPTURE(capture)->array;
            PARROT_CAPTURE(SELF)->hash  = PARROT_CAPTURE(capture)->hash;
        }
        else
            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
                "Can only set a capture to another capture.");
    }

/*

=item C<STRING get_string()>

Return a string representation of the hash, showing class
and memory address.

=cut

*/

    VTABLE STRING *get_string() {
        STRING *classname = VTABLE_name(INTERP, SELF);
        return Parrot_sprintf_c(INTERP, "%S[0x%x]", classname, SELF);
    }

/*

=item C<void mark(void)>

Mark the array.

=cut

*/

    VTABLE void mark() {
        PMC ** const data = PMC_data_typed(SELF, PMC **);
        INTVAL i;

        if (!data)
            return;

        for (i = PMC_int_val(SELF) - 1; i >= 0; --i)
            if (data[i])
                pobject_lives(interp, (PObj *)data[i]);
    }

/*

=back

=head2 Methods

=over 4

=cut

*/

    METHOD list() {
        PMC *capt_array;
        PMC *capt = SELF;
        /* XXX:  This workaround is for when we get here as
                 part of a subclass of Capture */
        if (PObj_is_object_TEST(SELF)) {
            STRING *classname = CONST_STRING(INTERP, "Capture");
            PMC    *classobj  = Parrot_oo_get_class_str(INTERP, classname);
            STRING *attribute = CONST_STRING(interp, "proxy");
            capt              = VTABLE_get_attr_keyed(interp, SELF, classobj, attribute);
        }

        CAPTURE_array_CREATE(INTERP, capt);
        capt_array = PARROT_CAPTURE(capt)->array;

        RETURN(PMC *capt_array);
    }

    METHOD hash() {
        PMC *capt_hash;
        PMC *capt = SELF;
        /* XXX:  This workaround is for when we get here as
                 part of a subclass of Capture */
        if (PObj_is_object_TEST(SELF)) {
            STRING *classname = CONST_STRING(INTERP, "Capture");
            PMC    *classobj  = Parrot_oo_get_class_str(INTERP, classname);
            STRING *attribute = CONST_STRING(interp, "proxy");
            capt              = VTABLE_get_attr_keyed(interp, SELF, classobj, attribute);
        }
        CAPTURE_hash_CREATE(INTERP, capt);
        capt_hash = PARROT_CAPTURE(capt)->hash;
        RETURN(PMC *capt_hash);
    }

}

/*

=back

=head1 HISTORY

Initial version                  - Patrick Michaud 2006-11-03

=cut

*/

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