tagged release 0.6.4
[parrot.git] / src / pmc / capture.pmc
blob0979d8beddded01048d47080f858ffa472108899
1 /*
2 Copyright (C) 2001-2008, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/capture.pmc - Capture PMC
9 =head1 DESCRIPTION
11 These are the vtable functions for the Capture PMC.
13 =head2 Functions
15 =over 4
17 =cut
21 #include "parrot/parrot.h"
24 #define CAPTURE_DATA_SIZE   2
25 #define CAPTURE_array_CREATE(i, obj) \
26     if (!PARROT_CAPTURE(obj)->array) \
27         PARROT_CAPTURE(obj)->array = pmc_new((i), enum_class_ResizablePMCArray);
28 #define CAPTURE_hash_CREATE(i, obj) \
29     if (!PARROT_CAPTURE(obj)->hash) \
30         PARROT_CAPTURE(obj)->hash = pmc_new((i), enum_class_Hash);
32 pmclass Capture need_ext provides array provides hash {
33     ATTR PMC *array;
34     ATTR PMC *hash;
38 =item C<void init()>
40 Initializes the Capture instance.
42 =item C<void destroy()>
44 Free structures.
46 =cut
50     VTABLE void init() {
51         Parrot_Capture *capture = mem_allocate_typed(Parrot_Capture);
52         PMC_data(SELF)          = capture;
53         capture->array          = NULL;
54         capture->hash           = NULL;
55         PMC_int_val(SELF)       = CAPTURE_DATA_SIZE;
56         PObj_active_destroy_SET(SELF);
57         PObj_data_is_PMC_array_SET(SELF);
58     }
60     VTABLE void destroy() {
61         if (PARROT_CAPTURE(SELF)) {
62             mem_sys_free(PARROT_CAPTURE(SELF));
63             PMC_data(SELF) = NULL;
64         }
66         PMC_int_val(SELF)  = 0;
67     }
71 =item C<void set_number_keyed_int(INTVAL key, FLOATVAL value)>
73 =item C<void set_integer_keyed_int(INTVAL key, INTVAL value)>
75 =item C<void set_pmc_keyed_int(INTVAL key, PMC *value)>
77 =item C<void set_string_keyed_int(INTVAL key, STRING *value)>
79 Sets a value in the array component of the Capture.
81 =cut
85     VTABLE void set_number_keyed_int(INTVAL key, FLOATVAL value) {
86         CAPTURE_array_CREATE(INTERP, SELF);
87         VTABLE_set_number_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
88                                     key, value);
89     }
91     VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
92         CAPTURE_array_CREATE(INTERP, SELF);
93         VTABLE_set_integer_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
94                                      key, value);
95     }
97     VTABLE void set_pmc_keyed_int(INTVAL key, PMC *value) {
98         CAPTURE_array_CREATE(INTERP, SELF);
99         VTABLE_set_pmc_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
100                                  key, value);
101     }
103     VTABLE void set_string_keyed_int(INTVAL key, STRING *value) {
104         CAPTURE_array_CREATE(INTERP, SELF);
105         VTABLE_set_string_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
106                                     key, value);
107     }
111 =item C<FLOATVAL get_number_keyed_int(INTVAL key)>
113 =item C<INTVAL get_integer_keyed_int(INTVAL key)>
115 =item C<PMC *get_pmc_keyed_int(INTVAL key)>
117 =item C<STRING *get_string_keyed_int(INTVAL key)>
119 Retrieves a value in the array component of the Capture.
121 =cut
125     VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
126         CAPTURE_array_CREATE(INTERP, SELF);
127         return VTABLE_get_number_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
128                                            key);
129     }
131     VTABLE INTVAL get_integer_keyed_int(INTVAL key) {
132         CAPTURE_array_CREATE(INTERP, SELF);
133         return VTABLE_get_integer_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
134                                             key);
135     }
137     VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
138         CAPTURE_array_CREATE(INTERP, SELF);
139         return VTABLE_get_pmc_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
140                                         key);
141     }
143     VTABLE STRING *get_string_keyed_int(INTVAL key) {
144         CAPTURE_array_CREATE(INTERP, SELF);
145         return VTABLE_get_string_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
146                                            key);
147     }
151 =item C<void push_float(FLOATVAL value)>
153 =item C<void push_integer(INTVAL value)>
155 =item C<void push_pmc(PMC *value)>
157 =item C<void push_string(STRING *value)>
159 Push a value onto the array component of the Capture.
161 =item C<void unshift_float(FLOATVAL value)>
163 =item C<void unshift_integer(INTVAL value)>
165 =item C<void unshift_pmc(PMC *value)>
167 =item C<void unshift_string(STRING *value)>
169 Unshift a value onto the array component of the Capture.
171 =cut
175     VTABLE void push_float(FLOATVAL value) {
176         CAPTURE_array_CREATE(INTERP, SELF);
177         VTABLE_push_float(INTERP, PARROT_CAPTURE(SELF)->array, value);
178     }
180     VTABLE void push_integer(INTVAL value) {
181         CAPTURE_array_CREATE(INTERP, SELF);
182         VTABLE_push_integer(INTERP, PARROT_CAPTURE(SELF)->array, value);
183     }
185     void push_pmc(PMC *value) {
186         CAPTURE_array_CREATE(INTERP, SELF);
187         VTABLE_push_pmc(INTERP, PARROT_CAPTURE(SELF)->array, value);
188     }
190     VTABLE void push_string(STRING *value) {
191         CAPTURE_array_CREATE(INTERP, SELF);
192         VTABLE_push_string(INTERP, PARROT_CAPTURE(SELF)->array, value);
193     }
195     VTABLE void unshift_float(FLOATVAL value) {
196         CAPTURE_array_CREATE(INTERP, SELF);
197         VTABLE_unshift_float(INTERP, PARROT_CAPTURE(SELF)->array, value);
198     }
200     VTABLE void unshift_integer(INTVAL value) {
201         CAPTURE_array_CREATE(INTERP, SELF);
202         VTABLE_unshift_integer(INTERP, PARROT_CAPTURE(SELF)->array, value);
203     }
205     void unshift_pmc(PMC *value) {
206         CAPTURE_array_CREATE(INTERP, SELF);
207         VTABLE_unshift_pmc(INTERP, PARROT_CAPTURE(SELF)->array, value);
208     }
210     VTABLE void unshift_string(STRING *value) {
211         CAPTURE_array_CREATE(INTERP, SELF);
212         VTABLE_unshift_string(INTERP, PARROT_CAPTURE(SELF)->array, value);
213     }
217 =item C<FLOATVAL pop_float()>
219 =item C<INTVAL pop_integer()>
221 =item C<PMC *pop_pmc()>
223 =item C<STRING *pop_string()>
225 Pop a value from the array component of the Capture.
227 =item C<FLOATVAL shift_float()>
229 =item C<INTVAL shift_integer()>
231 =item C<PMC *shift_pmc()>
233 =item C<STRING *shift_string()>
235 Shift a value from the array component of the Capture.
237 =cut
241     VTABLE FLOATVAL pop_float() {
242         CAPTURE_array_CREATE(INTERP, SELF);
243         return VTABLE_pop_float(INTERP, PARROT_CAPTURE(SELF)->array);
244     }
246     VTABLE INTVAL pop_integer() {
247         CAPTURE_array_CREATE(INTERP, SELF);
248         return VTABLE_pop_integer(INTERP, PARROT_CAPTURE(SELF)->array);
249     }
251     VTABLE PMC *pop_pmc() {
252         CAPTURE_array_CREATE(INTERP, SELF);
253         return VTABLE_pop_pmc(INTERP, PARROT_CAPTURE(SELF)->array);
254     }
256     VTABLE STRING *pop_string() {
257         CAPTURE_array_CREATE(INTERP, SELF);
258         return VTABLE_pop_string(INTERP, PARROT_CAPTURE(SELF)->array);
259     }
261     VTABLE FLOATVAL shift_float() {
262         CAPTURE_array_CREATE(INTERP, SELF);
263         return VTABLE_shift_float(INTERP, PARROT_CAPTURE(SELF)->array);
264     }
266     VTABLE INTVAL shift_integer() {
267         CAPTURE_array_CREATE(INTERP, SELF);
268         return VTABLE_shift_integer(INTERP, PARROT_CAPTURE(SELF)->array);
269     }
271     VTABLE PMC *shift_pmc() {
272         CAPTURE_array_CREATE(INTERP, SELF);
273         return VTABLE_shift_pmc(INTERP, PARROT_CAPTURE(SELF)->array);
274     }
276     VTABLE STRING *shift_string() {
277         CAPTURE_array_CREATE(INTERP, SELF);
278         return VTABLE_shift_string(INTERP, PARROT_CAPTURE(SELF)->array);
279     }
283 =item C<INTVAL elements()>
285 Return the number of elements in the array component of the Capture.
287 =item C<INTVAL defined_keyed_int(INTVAL key)>
289 Return true if element C<key> of the array component is defined.
291 =item C<INTVAL exists_keyed_int(INTVAL key)>
293 Return true if element C<key> of the array component exists.
295 =item C<void delete_keyed_int(INTVAL key)>
297 Delete the element corresponding to C<key> in the array component.
299 =cut
303     VTABLE INTVAL elements() {
304         if (!PARROT_CAPTURE(SELF)->array) return 0;
305         return VTABLE_elements(INTERP, PARROT_CAPTURE(SELF)->array);
306     }
308     VTABLE INTVAL defined_keyed_int(INTVAL key) {
309         if (!PARROT_CAPTURE(SELF)->array)
310             return 0;
312         return VTABLE_defined_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
313                                         key);
314     }
316     VTABLE INTVAL exists_keyed_int(INTVAL key) {
317         if (!PARROT_CAPTURE(SELF)->array)
318             return 0;
320         return VTABLE_exists_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array,
321                                                key);
322     }
324     VTABLE void delete_keyed_int(INTVAL key) {
325         if (PARROT_CAPTURE(SELF)->array)
326             VTABLE_delete_keyed_int(INTERP, PARROT_CAPTURE(SELF)->array, key);
327     }
331 =item C<void set_number_keyed(PMC *key, FLOATVAL value)>
333 =item C<void set_integer_keyed(PMC *key, INTVAL value)>
335 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
337 =item C<void set_string_keyed(PMC *key, STRING *value)>
339 Sets a value in the hash component of the Capture.
341 =cut
345     VTABLE void set_number_keyed(PMC *key, FLOATVAL value) {
346         CAPTURE_hash_CREATE(INTERP, SELF);
347         VTABLE_set_number_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
348     }
350     VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
351         CAPTURE_hash_CREATE(INTERP, SELF);
352         VTABLE_set_integer_keyed(INTERP, PARROT_CAPTURE(SELF)->hash,
353                                  key, value);
354     }
356     VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
357         CAPTURE_hash_CREATE(INTERP, SELF);
358         VTABLE_set_pmc_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
359     }
361     VTABLE void set_string_keyed(PMC *key, STRING *value) {
362         CAPTURE_hash_CREATE(INTERP, SELF);
363         VTABLE_set_string_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key, value);
364     }
368 =item C<FLOATVAL get_number_keyed(PMC *key)>
370 =item C<INTVAL get_integer_keyed(PMC *key)>
372 =item C<PMC *get_pmc_keyed(PMC *key)>
374 =item C<STRING *get_string_keyed(PMC *key)>
376 Retrieves a value from the hash component of the Capture.
378 =cut
382     VTABLE FLOATVAL get_number_keyed(PMC *key) {
383         CAPTURE_hash_CREATE(INTERP, SELF);
384         return VTABLE_get_number_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
385     }
387     VTABLE INTVAL get_integer_keyed(PMC *key) {
388         CAPTURE_hash_CREATE(INTERP, SELF);
389         return VTABLE_get_integer_keyed(INTERP, PARROT_CAPTURE(SELF)->hash,
390                                         key);
391     }
393     VTABLE PMC *get_pmc_keyed(PMC *key) {
394         CAPTURE_hash_CREATE(INTERP, SELF);
395         return VTABLE_get_pmc_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
396     }
398     VTABLE STRING *get_string_keyed(PMC *key) {
399         CAPTURE_hash_CREATE(INTERP, SELF);
400         return VTABLE_get_string_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
401     }
405 =item C<INTVAL defined_keyed(PMC *key)>
407 Return true if element C<key> of the hash component is defined.
409 =item C<INTVAL exists_keyed(PMC *key)>
411 Return true if element C<key> of the hash component exists.
413 =item C<void delete_keyed(PMC *key)>
415 Delete the element corresponding to C<key> in the hash component.
417 =cut
421     VTABLE INTVAL defined_keyed(PMC *key) {
422         if (!PARROT_CAPTURE(SELF)->hash) return 0;
423         return VTABLE_defined_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
424     }
426     VTABLE INTVAL exists_keyed(PMC *key) {
427         if (!PARROT_CAPTURE(SELF)->hash) return 0;
428         return VTABLE_exists_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
429     }
431     VTABLE void delete_keyed(PMC *key) {
432         if (PARROT_CAPTURE(SELF)->hash)
433             VTABLE_delete_keyed(INTERP, PARROT_CAPTURE(SELF)->hash, key);
434     }
438 =item C<void set_pmc(PMC *capture)>
440 Set this capture to hold the value of another.
442 =cut
446     void set_pmc(PMC *capture) {
447         if (VTABLE_isa(INTERP, capture, CONST_STRING(INTERP, "Capture"))) {
448             PARROT_CAPTURE(SELF)->array = PARROT_CAPTURE(capture)->array;
449             PARROT_CAPTURE(SELF)->hash = PARROT_CAPTURE(capture)->hash;
450         }
451         else {
452             real_exception(INTERP, NULL, INVALID_OPERATION,
453                            "Can only set a capture to another capture.");
454         }
455     }
459 =back
461 =head2 Methods
463 =over 4
467     METHOD get_array() {
468         PMC *capt_array;
469         PMC *capt = SELF;
470         /* XXX:  This workaround is for when we get here as
471                  part of a subclass of Capture */
472         if (PObj_is_object_TEST(SELF)) {
473             STRING *classname = CONST_STRING(INTERP, "Capture");
474             PMC    *classobj  = Parrot_oo_get_class_str(INTERP, classname);
475             STRING *attribute = CONST_STRING(interp, "proxy");
476             capt              = VTABLE_get_attr_keyed(interp, SELF, classobj, attribute);
477         }
479         CAPTURE_array_CREATE(INTERP, capt);
480         capt_array = PARROT_CAPTURE(capt)->array;
482         RETURN(PMC *capt_array);
483     }
485     METHOD get_hash() {
486         PMC *capt_hash;
487         PMC *capt = SELF;
488         /* XXX:  This workaround is for when we get here as
489                  part of a subclass of Capture */
490         if (PObj_is_object_TEST(SELF)) {
491             STRING *classname = CONST_STRING(INTERP, "Capture");
492             PMC    *classobj  = Parrot_oo_get_class_str(INTERP, classname);
493             STRING *attribute = CONST_STRING(interp, "proxy");
494             capt              = VTABLE_get_attr_keyed(interp, SELF, classobj, attribute);
495         }
496         CAPTURE_hash_CREATE(INTERP, capt);
497         capt_hash = PARROT_CAPTURE(capt)->hash;
498         RETURN(PMC *capt_hash);
499     }
505 =back
507 =head1 HISTORY
509 Initial version                  - Patrick Michaud 2006-11-03
511 =cut
516  * Local variables:
517  *   c-file-style: "parrot"
518  * End:
519  * vim: expandtab shiftwidth=4:
520  */