Ripped out deprecated logical VTABLES
[parrot.git] / src / pmc / boolean.pmc
blob6571ae67f98134e9b122e409550b0040c4e81948
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/boolean.pmc - Boolean PMC
9 =head1 DESCRIPTION
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
13 changes its value.
15 This implementation of C<Boolean> inherits from the C<Scalar> PMC.
16 Unlike the previous implementation, it does I<not> inherit
17 from C<Integer>.
19 =head2 Functions
21 =over
23 =cut
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) \
39      if (val) \
40          PObj_get_FLAGS(pmc) |= boolean_FLAG; \
41      else \
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 {
52 =item C<void init()>
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
59 as a Boolean.
61 =item C<void init_int(INTVAL value)>
63 Create a new C<Boolean> with the given initial value interpreted
64 as a Boolean.
66 =cut
70     /* These init functions set the boolean flag directly. */
72     VTABLE void init() {
73         set_boolean_FLAG(SELF, 0);
74     }
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);
79     }
81     VTABLE void init_int(INTVAL value) {
82         set_boolean_FLAG(SELF, value);
83     }
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>.
103 =cut
107     VTABLE INTVAL get_bool() {
108         return get_boolean_FLAG(SELF);
109     }
111     VTABLE INTVAL get_integer() {
112         return SELF.get_bool();
113     }
115     VTABLE FLOATVAL get_number() {
116         INTVAL value = SELF.get_bool();
117         return (FLOATVAL)value;
118     }
120     VTABLE STRING *get_string() {
121         return Parrot_str_from_int(INTERP, SELF.get_integer());
122     }
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 =
129 C<TRUE>.
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 =
138 C<TRUE>.
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
144 C<FALSE>.
146 =cut
150     VTABLE void set_bool(INTVAL value) {
151         set_boolean_FLAG(SELF, value);
152     }
154     VTABLE void set_integer_native(INTVAL value) {
155         SELF.set_bool(value);
156     }
158     VTABLE void set_number_native(FLOATVAL value) {
159         SELF.set_bool(!FLOAT_IS_ZERO(value));
160     }
162     VTABLE void set_string_native(STRING *value) {
163         SELF.set_bool(Parrot_str_boolean(INTERP, value));
164     }
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>.
176 =cut
180     VTABLE void freeze(PMC *info) {
181         SUPER(info);
182         VTABLE_push_integer(INTERP, info, SELF.get_bool());
183     }
185     VTABLE void thaw(PMC *info) {
186         SUPER(info);
187         SELF.set_bool(VTABLE_shift_integer(INTERP, info));
188     }
194 =back
196 See also the C<Scalar> PMC.
198 =cut
203  * Local variables:
204  *   c-file-style: "parrot"
205  * End:
206  * vim: expandtab shiftwidth=4:
207  */