/* Copyright (c) 2010, Parrot Foundation. $Id$ =head1 NAME src/pmc/boolean.pmc - Boolean PMC =head1 DESCRIPTION This PMC implements a Boolean type with a single true/false value. A C does not morph to other types when its value is set; it simply changes its value. This implementation of C inherits from the C PMC. Unlike the previous implementation, it does I inherit from C. =head2 Functions =over =cut */ /* This new Boolean PMC stores its boolean value in a private PObj flag. */ #define boolean_FLAG PObj_private0_FLAG #define get_boolean_FLAG(pmc) \ ((PObj_get_FLAGS(pmc) & boolean_FLAG) != 0) #define set_boolean_FLAG(pmc, val) \ if (val) \ PObj_get_FLAGS(pmc) |= boolean_FLAG; \ else \ PObj_get_FLAGS(pmc) &= ~boolean_FLAG; #define flip_boolean_FLAG(pmc) \ PObj_get_FLAGS(pmc) ^= boolean_FLAG pmclass Boolean extends scalar provides boolean provides scalar manual_attrs { /* =item C Create a new C with initial value C. =item C Create a new C with the given initial value interpreted as a Boolean. =cut */ /* These two init functions set the boolean flag directly. */ VTABLE void init () { set_boolean_FLAG(SELF, FALSE); } VTABLE void init_pmc (PMC *value) { INTVAL v = PMC_IS_NULL(value) ? FALSE : VTABLE_get_bool(INTERP, value); set_boolean_FLAG(SELF, v); } /* =item C Obtain the value of the C as an integer: 1 = C, 0 = C. =item C Same as C. =item C Obtain the value of the C as a float: 1.0 = C, 0.0 = C. =item C Obtain the value of the C as a string: "1" = C, "0" = C. =cut */ VTABLE INTVAL get_bool () { return get_boolean_FLAG(SELF); } VTABLE INTVAL get_integer () { return SELF.get_bool(); } VTABLE FLOATVAL get_number () { INTVAL value = SELF.get_bool(); return (FLOATVAL)value; } VTABLE STRING *get_string () { return Parrot_str_from_int(INTERP, SELF.get_integer()); } /* =item C Sets the value of the Boolean to the specified integer value: 0 = C, non-0 = C. =item C Same as C. =item C Sets the value of the Boolean to the specified float value: 0.0 = C, non-0.0 = C. =item C Sets the Boolean to the value represented by the specified string. All values are considered C except for C<""> and C<"0>", which are considered C. =cut */ VTABLE void set_bool (INTVAL value) { set_boolean_FLAG(SELF, value); } VTABLE void set_integer_native (INTVAL value) { SELF.set_bool(value); } VTABLE void set_number_native (FLOATVAL value) { SELF.set_bool(!FLOAT_IS_ZERO(value)); } VTABLE void set_string_native (STRING *value) { SELF.set_bool(Parrot_str_boolean(INTERP, value)); } /** VTABLE PMC *neg (PMC *dest) { dest = Parrot_pmc_new_init_int(INTERP, VTABLE_type(INTERP, SELF), SELF.get_bool()); return dest; } VTABLE void i_neg () { /* Nothing to do for an in-place negate. }**/ /* No POD documentation, since the reader should see Scalar. */ VTABLE void i_logical_not () { flip_boolean_FLAG(SELF); } /* =item C Used to archive the C. =item C Used to unarchive the C. =cut */ VTABLE void freeze (PMC *info) { SUPER(info); VTABLE_push_integer(INTERP, info, SELF.get_bool()); } VTABLE void thaw (PMC *info) { SUPER(info); SELF.set_bool(VTABLE_shift_integer(INTERP, info)); } } /* =back See also the C PMC. =cut */ /* Local variables: * c-file-style: "parrot" * End: * vim: expandtab shiftwidth=4: */