[TT #871] Add rand as a dynop, with tests
[parrot.git] / src / pmc / boolean.pmc
blob406c5796f9ed9b9fd26ca9413597ab11a9f4c405
1 /*
2 Copyright (C) 2001-2007, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/boolean.pmc - Boolean PMC
9 =head1 DESCRIPTION
11 This class implements a boolean value variable.
13 Albeit the C<Boolean PMC> is derived from the C<Integer PMC>,
14 it doesn't morph to other types. Only its value is changed.
16 =head2 Methods
18 =over 4
20 =cut
24 pmclass Boolean extends Integer provides boolean provides scalar {
28 =item C<PMC *instantiate(PMC *sig)>
30 Object constructor. SELF is a Boolean Class object. Return a new
31 C<bool> object according to the passed PMC value.
33 =cut
36     VTABLE PMC *instantiate(PMC *sig) {
37         /* XXX other types */
38         const int argcP = REG_INT(interp, 3);
39         PMC * const res = pmc_new(interp, enum_class_Boolean);
41         if (argcP)
42             SELF.set_bool(VTABLE_get_bool(interp, REG_PMC(interp, 5)));
44         return PMCNULL; /* TODO */
45     }
48 =item C<STRING *get_string()>
50 Return "1" or "0".
52 =cut
55     VTABLE STRING *get_string() {
56         return SUPER();
57     }
62 =item C<void set_integer_native(INTVAL value)>
64 =item C<void set_bool(INTVAL value)>
66 =item C<void set_pmc(PMC *value)>
68 =cut
71     VTABLE void set_integer_native(INTVAL value) {
72         SUPER((value != 0));
73     }
76     VTABLE void set_bool(INTVAL value) {
77         SELF.set_integer_native(value);
78     }
82 =item C<void set_number_native(FLOATVAL value)>
84 Sets the value to C<value> evaluated in a boolean context.
86 =cut
90     VTABLE void set_number_native(FLOATVAL value) {
91         SELF.set_bool(!FLOAT_IS_ZERO(value));
92     }
96 =item C<void set_string_native(STRING *value)>
98 Sets the value to C<*value> evaluated in a boolean context.
100 =cut
104     VTABLE void set_string_native(STRING *value) {
105         SELF.set_bool(Parrot_str_boolean(INTERP, value));
106     }
111 =back
113 =cut
118  * Local variables:
119  *   c-file-style: "parrot"
120  * End:
121  * vim: expandtab shiftwidth=4:
122  */