fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / pmc / undef.pmc
blobf72ffc92981d8ad34182707c45a869700ac0cc14
1 /*
2 Copyright (C) 2004-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/undef.pmc - Generic undefined value
9 =head1 DESCRIPTION
11 This is Parrot's generic undef type. This PMC has no defined value.
12 It returns a numeric value of 0, a boolean of false, and an empty string.
13 When assigned a number, integer, or string it morphs to a Number,
14 Integer, or String PMC respectively, and when assigned a generic PMC
15 it morphs into a PMC of the passed-in type and does a same-type assignment
16 from there.
18 =head2 Methods
20 =over 4
22 =cut
26 #include "pmc/pmc_object.h"
27 #define UNDEF_STRING_CLASS enum_class_String
29 /* HEADERIZER HFILE: none */
30 /* HEADERIZER BEGIN: static */
31 /* HEADERIZER END: static */
33 pmclass Undef no_ro {
37 =item C<void set_pmc(PMC *other)>
39 Sets the current PMC to C<*other> by first changing the current PMC to the
40 appropriate type.
42 =cut
46     VTABLE void set_pmc(PMC *other) {
47         if (!PObj_is_object_TEST(other)) {
48             Parrot_pmc_reuse(INTERP, SELF, other->vtable->base_type, 0);
49             VTABLE_set_pmc(INTERP, SELF, other);
50         }
51         else {
52             PMC * const class_  = PARROT_OBJECT(other)->_class;
53             PMC        *clone   = VTABLE_clone(INTERP, other);
54             void       *attrs   = PMC_data(clone);
55             PMC        *meta    = PMC_metadata(clone);
57             /* now swap memory without leaking it */
58             PMC_data(clone)     = PMC_data(SELF);
59             PMC_data(SELF)      = attrs;
60             SELF->vtable        = clone->vtable;
62             /* Restore metadata. */
63             if (!PMC_IS_NULL(meta)) {
64                 PMC * const iter = VTABLE_get_iter(INTERP, meta);
65                 while (VTABLE_get_bool(INTERP, iter)) {
66                     STRING * const key = VTABLE_shift_string(INTERP, iter);
67                     PMC * const value  = VTABLE_get_pmc_keyed_str(INTERP, meta, key);
68                     VTABLE_setprop(INTERP, SELF, key, value);
69                 }
70             }
71             PMC_data(clone) = NULL;
73             PObj_is_object_SET(SELF);
74         }
75     }
79 =item C<void assign_pmc(PMC *other)>
81 Assigns the PMC to the value of C<*other> by first changing the PMC to the
82 appropriate type.
84 =cut
88     VTABLE void assign_pmc(PMC *other) {
89         /* don't want to call set_pmc if we're assigning an Undef to an Undef */
90         if (other->vtable->base_type != enum_class_Undef)
91             VTABLE_set_pmc(INTERP, SELF, other);
92     }
97 =item C<INTVAL get_integer()>
99 Returns 0.
101 =cut
105     VTABLE INTVAL get_integer() {
106         return 0;
107     }
111 =item C<INTVAL defined()>
113 Returns 0.
115 =cut
119     VTABLE INTVAL defined() {
120         return 0;
121     }
125 =item C<void set_integer_native(INTVAL value)>
127 Morphs the current PMC to an C<Integer> and sets the value from C<value>.
129 =cut
133     VTABLE void set_integer_native(INTVAL value) {
134         Parrot_pmc_reuse(INTERP, SELF, enum_class_Integer, 0);
135         VTABLE_set_integer_native(INTERP, SELF, value);
136     }
140 =item C<FLOATVAL get_number()>
142 Returns 0.0.
144 =cut
148     VTABLE FLOATVAL get_number() {
149         return 0.0;
150     }
154 =item C<void set_number_native(FLOATVAL value)>
156 Morphs the current PMC to a C<Float> and sets the value from C<value>.
158 =cut
162     VTABLE void set_number_native(FLOATVAL value) {
163         Parrot_pmc_reuse(INTERP, SELF, enum_class_Float, 0);
164         VTABLE_set_number_native(INTERP, SELF, value);
165     }
169 =item C<STRING *get_string()>
171 Returns an empty string.
173 =cut
177     VTABLE STRING *get_string() {
178         Parrot_warn(INTERP, PARROT_WARNINGS_UNDEF_FLAG,
179             "Stringifying an Undef PMC");
181         return CONST_STRING(INTERP, "");
182     }
186 =item C<void set_string_native(STRING *value)>
188 Morphs the current PMC to a C<String> and sets the value from C<value>.
190 =cut
194     VTABLE void set_string_native(STRING *value) {
195         Parrot_pmc_reuse(INTERP, SELF, enum_class_String, 0);
196         VTABLE_set_string_native(INTERP, SELF, value);
197     }
201 =item C<INTVAL get_bool()>
203 Returns 0.
205 =cut
209     VTABLE INTVAL get_bool() {
210         return 0;
211     }
215 =item C<void share()>
217 Unknown. (TODO)
219 =cut
223     VTABLE void share() {
224         /* see src/pmc/integer.pmc */
225     }
229 =item C<PMC *share_ro()>
231 Unknown. (TODO)
233 =cut
237     VTABLE PMC *share_ro() {
238         if (PObj_is_PMC_shared_TEST(SELF))
239             return SELF;
240         else
241             return pt_shared_fixup(INTERP, SELF);
242     }
246 =item C<PMC *clone()>
248 Clones the current Undef PMC.
250 =cut
254     VTABLE PMC *clone() {
255         return Parrot_pmc_new(INTERP, SELF->vtable->base_type);
256     }
260 =item C<INTVAL is_equal(PMC *value)>
262 Returns 1 if the C<*value> is an Undef PMC, 0 otherwise.
264 =cut
268     MULTI INTVAL is_equal(Undef value) {
269         return 1;
270     }
272     MULTI INTVAL is_equal(DEFAULT value) {
273         return 0;
274     }
279 =back
281 =cut
287  * Local variables:
288  *   c-file-style: "parrot"
289  * End:
290  * vim: expandtab shiftwidth=4:
291  */