tagged release 0.6.4
[parrot.git] / languages / lua / src / pmc / luaboolean.pmc
blobc559d385163d905a1b9b5c43807d92c27b366931
1 /*
2 Copyright (C) 2005-2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 pmc/luaboolean.pmc - Lua Boolean
9 =head1 DESCRIPTION
11 C<LuaBoolean> extends C<LuaAny> to provide a class with the behaviour of
12 the Lua C<Boolean> type.
14 The value is stored as an Integer.
16 =head2 Methods
18 =over 4
20 =cut
24 #include "lua_private.h"
27 pmclass LuaBoolean
28     extends  LuaAny
29     provides scalar
30     provides boolean
31     provides integer
32     dynpmc
33     group    lua_group
34     hll      Lua
35     maps     Boolean {
39 =item C<void init()>
41 Initializes the Boolean with a default value of C<false>.
43 =cut
46     void init() {
47         PMC_int_val(SELF) = 0;
48     }
52 =item C<PMC new_from_string(STRING *rep)>
54 Class method to construct an Boolean from the string representation C<rep>.
56 =cut
59     PMC* new_from_string(STRING *rep, INTVAL flags) {
60         PMC *res;
61         INTVAL type = PMC_type(SELF);
62         if (flags & PObj_constant_FLAG)
63             res = constant_pmc_new(INTERP, type);
64         else
65             res = pmc_new(INTERP, type);
66         PMC_int_val(res) = (string_to_int(INTERP, rep) != 0);
67         return res;
68     }
72 =item C<STRING* name()>
74 Return the string "boolean".
76 =cut
79     STRING* name() {
80         return const_string(INTERP, "boolean");
81     }
85 =item C<PMC *clone()>
87 Creates and returns a clone of the scalar.
89 =cut
92     PMC* clone() {
93         PMC *dest = pmc_new(INTERP, PMC_type(SELF));
94         STRUCT_COPY(&PMC_union(dest), &PMC_union(SELF));
95         return dest;
96     }
100 =item C<INTVAL get_integer()>
102 Returns the integer value of the Boolean.
104 =cut
107     INTVAL get_integer() {
108         return PMC_int_val(SELF);
109     }
113 =item C<STRING* get_string()>
115 Return the string "true" or "false".
117 =cut
120     STRING* get_string() {
121         if (PMC_int_val(SELF))
122             return const_string(INTERP, "true");
123         else
124             return const_string(INTERP, "false");
125     }
130 =item C<INTVAL get_bool()>
132 Returns the boolean value of the Boolean.
134 =cut
137     INTVAL get_bool() {
138         return PMC_int_val(SELF) ? 1 : 0;
139     }
143 =item C<void set_integer_native(INTVAL value)>
145 =item C<void set_bool(INTVAL value)>
147 =cut
150     void set_integer_native(INTVAL value) {
151         PMC_int_val(SELF) = (value != 0);
152     }
154     void set_bool(INTVAL value) {
155         PMC_int_val(SELF) = (value != 0);
156     }
160 =item C<void set_pmc(PMC *value)>
162 Sets the value of the boolean to the value in C<*value>.
164 =cut
167     void set_pmc(PMC *value) {
168         PMC_int_val(SELF) = VTABLE_get_bool(INTERP, value);
169     }
173 =item C<void freeze(visit_info *info)>
175 Used to archive the boolean.
177 =cut
180     void freeze(visit_info *info) {
181         IMAGE_IO *io = info->image_io;
182         SUPER(info);
183         VTABLE_push_integer(INTERP, io, PMC_int_val(SELF));
184     }
188 =item C<void thaw(visit_info *info)>
190 Used to unarchive the boolean.
192 =cut
195     void thaw(visit_info *info) {
196         IMAGE_IO *io = info->image_io;
197         SUPER(info);
198         if (info->extra_flags == EXTRA_IS_NULL)
199             PMC_int_val(SELF) = VTABLE_shift_integer(INTERP, io);
200     }
204 =back
206 =head2 non-Vtable Methods
208 =over 4
210 =item C<INTVAL is_equal(PMC *value)>
212 =cut
215     INTVAL is_equal(PMC *value) {
216 MMD_LuaBoolean: {
217             return (INTVAL)(PMC_int_val(SELF) == PMC_int_val(value));
218         }
219 MMD_DEFAULT: {
220             return (INTVAL)0;
221         }
222     }
226 =back
228 =head2 Specific Methods
230 =over 4
232 =item C<PMC* rawequal(PMC *value)>
234 =cut
237     METHOD PMC* rawequal(PMC *value) {
238         PMC *retval = pmc_new(INTERP, dynpmc_LuaBoolean);
240         if (PMC_type(SELF)    == PMC_type(value)
241         &&  PMC_int_val(SELF) == VTABLE_get_integer(INTERP, value))
242             VTABLE_set_integer_native(INTERP, retval, 1);
243         else
244             VTABLE_set_integer_native(INTERP, retval, 0);
246         RETURN(PMC *retval);
247     }
253 =back
255 =head1 AUTHORS
257 Francois Perrad.
259 Klaas-Jan Stol.
261 =cut
267  * Local variables:
268  *   c-file-style: "parrot"
269  * End:
270  * vim: expandtab shiftwidth=4:
271  */