/*
Copyright (C) 2005-2008, The Perl Foundation.
$Id: /mirror/trunk/languages/lua/src/pmc/luaboolean.pmc 35819 2009-01-20T05:19:27.074079Z chromatic  $

=head1 NAME

pmc/luaboolean.pmc - Lua Boolean

=head1 DESCRIPTION

C<LuaBoolean> extends C<LuaAny> to provide a class with the behaviour of
the Lua C<Boolean> type.

The value is stored as an Integer.

=head2 Methods

=over 4

=cut

*/

#include "lua_private.h"


pmclass LuaBoolean
    extends  LuaAny
    provides scalar
    provides boolean
    provides integer
    dynpmc
    group    lua_group
    hll      lua
    maps     Boolean {

/*

=item C<void init()>

Initializes the Boolean with a default value of C<false>.

=cut

*/
    VTABLE void init() {
        PMC_int_val(SELF) = 0;
    }

/*

=item C<PMC instantiate_str(STRING *rep)>

Class method to construct an Boolean from the string representation C<rep>.

=cut

*/
    VTABLE PMC* instantiate_str(STRING *rep, INTVAL flags) {
        PMC *res;
        INTVAL type = PMC_type(SELF);
        if (flags & PObj_constant_FLAG)
            res = constant_pmc_new(INTERP, type);
        else
            res = pmc_new(INTERP, type);
        PMC_int_val(res) = (string_to_int(INTERP, rep) != 0);
        return res;
    }

/*

=item C<STRING* name()>

Return the string "boolean".

=cut

*/
    VTABLE STRING* name() {
        return const_string(INTERP, "boolean");
    }

/*

=item C<PMC *clone()>

Creates and returns a clone of the scalar.

=cut

*/
    VTABLE PMC* clone() {
        PMC *dest = pmc_new(INTERP, PMC_type(SELF));
        STRUCT_COPY(&PMC_union(dest), &PMC_union(SELF));
        return dest;
    }

/*

=item C<INTVAL get_integer()>

Returns the integer value of the Boolean.

=cut

*/
    VTABLE INTVAL get_integer() {
        return PMC_int_val(SELF);
    }

/*

=item C<STRING* get_string()>

Return the string "true" or "false".

=cut

*/
    VTABLE STRING* get_string() {
        if (PMC_int_val(SELF))
            return const_string(INTERP, "true");
        else
            return const_string(INTERP, "false");
    }


/*

=item C<INTVAL get_bool()>

Returns the boolean value of the Boolean.

=cut

*/
    VTABLE INTVAL get_bool() {
        return PMC_int_val(SELF) ? 1 : 0;
    }

/*

=item C<void set_integer_native(INTVAL value)>

=item C<void set_bool(INTVAL value)>

=cut

*/
    VTABLE void set_integer_native(INTVAL value) {
        PMC_int_val(SELF) = (value != 0);
    }

    VTABLE void set_bool(INTVAL value) {
        PMC_int_val(SELF) = (value != 0);
    }

/*

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

Sets the value of the boolean to the value in C<*value>.

=cut

*/
    VTABLE void set_pmc(PMC *value) {
        PMC_int_val(SELF) = VTABLE_get_bool(INTERP, value);
    }

/*

=item C<void freeze(visit_info *info)>

Used to archive the boolean.

=cut

*/
    VTABLE void freeze(visit_info *info) {
        IMAGE_IO *io = info->image_io;
        SUPER(info);
        VTABLE_push_integer(INTERP, io, PMC_int_val(SELF));
    }

/*

=item C<void thaw(visit_info *info)>

Used to unarchive the boolean.

=cut

*/
    VTABLE void thaw(visit_info *info) {
        IMAGE_IO *io = info->image_io;
        SUPER(info);
        if (info->extra_flags == EXTRA_IS_NULL)
            PMC_int_val(SELF) = VTABLE_shift_integer(INTERP, io);
    }

/*

=back

=head2 non-Vtable Methods

=over 4

=item C<INTVAL is_equal(PMC *value)>

=cut

*/
    MULTI INTVAL is_equal(LuaBoolean value) {
        return (INTVAL)(PMC_int_val(SELF) == PMC_int_val(value));
    }

    MULTI INTVAL is_equal(DEFAULT value) {
        return (INTVAL)0;
    }

/*

=back

=head2 Specific Methods

=over 4

=item C<PMC* rawequal(PMC *value)>

=cut

*/
    METHOD PMC* rawequal(PMC *value) {
        PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);

        if (PMC_type(SELF)    == PMC_type(value)
        &&  PMC_int_val(SELF) == VTABLE_get_integer(INTERP, value))
            VTABLE_set_integer_native(INTERP, retval, 1);
        else
            VTABLE_set_integer_native(INTERP, retval, 0);

        RETURN(PMC *retval);
    }

}

/*

=back

=cut

*/


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