2 Copyright (C) 2001-2010, Parrot Foundation.
7 src/pmc/boolean.pmc - Boolean PMC
11 This PMC implements a Boolean type with a single true/false value.
12 A C<Boolean> does not morph to other types when its value is set; it simply
15 This implementation of C<Boolean> inherits from the C<Scalar> PMC.
16 Unlike the previous implementation, it does I<not> inherit
27 /* HEADERIZER HFILE: none */
28 /* HEADERIZER BEGIN: static */
29 /* HEADERIZER END: static */
31 /* This new Boolean PMC stores its boolean value in a private PObj flag. */
33 #define boolean_FLAG PObj_private0_FLAG
35 #define get_boolean_FLAG(pmc) \
36 ((PObj_get_FLAGS(pmc) & boolean_FLAG) != 0)
38 #define set_boolean_FLAG(pmc, val) \
40 PObj_get_FLAGS(pmc) |= boolean_FLAG; \
42 PObj_get_FLAGS(pmc) &= ~boolean_FLAG;
44 #define flip_boolean_FLAG(pmc) \
45 PObj_get_FLAGS(pmc) ^= boolean_FLAG
48 pmclass Boolean extends scalar provides boolean provides scalar manual_attrs {
54 Create a new C<Boolean> with initial value C<FALSE>.
56 =item C<void init_pmc(PMC *value)>
58 Create a new C<Boolean> with the given initial value interpreted
61 =item C<void init_int(INTVAL value)>
63 Create a new C<Boolean> with the given initial value interpreted
70 /* These init functions set the boolean flag directly. */
73 set_boolean_FLAG(SELF, 0);
76 VTABLE void init_pmc(PMC *value) {
77 INTVAL v = PMC_IS_NULL(value) ? 0 : VTABLE_get_bool(INTERP, value);
78 set_boolean_FLAG(SELF, v);
81 VTABLE void init_int(INTVAL value) {
82 set_boolean_FLAG(SELF, value);
87 =item C<INTVAL get_bool()>
89 Obtain the value of the C<Boolean> as an integer: 1 = C<TRUE>, 0 = C<FALSE>.
91 =item C<INTVAL get_integer()>
93 Same as C<get_bool()>.
95 =item C<FLOATVAL get_number()>
97 Obtain the value of the C<Boolean> as a float: 1.0 = C<TRUE>, 0.0 = C<FALSE>.
99 =item C<STRING *get_string()>
101 Obtain the value of the C<Boolean> as a string: "1" = C<TRUE>, "0" = C<FALSE>.
107 VTABLE INTVAL get_bool() {
108 return get_boolean_FLAG(SELF);
111 VTABLE INTVAL get_integer() {
112 return SELF.get_bool();
115 VTABLE FLOATVAL get_number() {
116 INTVAL value = SELF.get_bool();
117 return (FLOATVAL)value;
120 VTABLE STRING *get_string() {
121 return Parrot_str_from_int(INTERP, SELF.get_integer());
126 =item C<void set_bool(INTVAL value)>
128 Sets the value of the Boolean to the specified integer value: 0 = C<FALSE>, non-0 =
131 =item C<void set_integer_native(INTVAL value)>
133 Same as C<set_bool()>.
135 =item C<void set_number_native(FLOATVAL value)>
137 Sets the value of the Boolean to the specified float value: 0.0 = C<FALSE>, non-0.0 =
140 =item C<void set_string_native(STRING *value)>
142 Sets the Boolean to the value represented by the specified string. All values are
143 considered C<TRUE> except for C<""> and C<"0>", which are considered
150 VTABLE void set_bool(INTVAL value) {
151 set_boolean_FLAG(SELF, value);
154 VTABLE void set_integer_native(INTVAL value) {
155 SELF.set_bool(value);
158 VTABLE void set_number_native(FLOATVAL value) {
159 SELF.set_bool(!FLOAT_IS_ZERO(value));
162 VTABLE void set_string_native(STRING *value) {
163 SELF.set_bool(Parrot_str_boolean(INTERP, value));
168 =item C<void freeze(PMC *info)>
170 Used to archive the C<Boolean>.
172 =item C<void thaw(PMC *info)>
174 Used to unarchive the C<Boolean>.
180 VTABLE void freeze(PMC *info) {
182 VTABLE_push_integer(INTERP, info, SELF.get_bool());
185 VTABLE void thaw(PMC *info) {
187 SELF.set_bool(VTABLE_shift_integer(INTERP, info));
196 See also the C<Scalar> PMC.
204 * c-file-style: "parrot"
206 * vim: expandtab shiftwidth=4: