2 Copyright (C) 2005-2008, The Perl Foundation.
7 src/pmc/pair.pmc - Pair PMC
11 A Pair PMC represents one key => value mapping like a one element hash.
21 #include "parrot/parrot.h"
24 pmclass Pair need_ext {
32 Initializes the instance.
34 =item C<PMC *instantiate(PMC *sig)>
36 Class method to construct an Integer according to passed arguments.
43 PMC_data(SELF) = mem_allocate_zeroed_typed(Parrot_Pair_attributes);
44 PObj_custom_mark_SET(SELF);
47 VTABLE PMC *instantiate(PMC *sig) {
50 /* TODO -- really create this thing */
52 PMC * const _class = REG_PMC(interp, 2);
53 Parrot_Pair_attributes *pair = PARROT_PAIR(SELF);
54 const int argcP = REG_INT(interp, 3);
55 const int argcS = REG_INT(interp, 2);
57 SELF = pmc_new(INTERP, _class->vtable->base_type);
58 if (argcS == 1 && argcP == 1) {
59 PObj_key_is_string_SET(SELF);
60 pair->key = REG_STR(interp, 5);
61 pair->value = REG_PMC(interp, 5);
63 else if (argcP == 2) {
64 pair->key = REG_PMC(interp, 5);
65 pair->value = REG_PMC(interp, 6);
68 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
69 "wrong argument count for Pair creation");
78 Marks the hash as live.
85 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
88 pobject_lives(INTERP, (PObj *)pair->key);
91 pobject_lives(INTERP, (PObj *)pair->value);
96 =item C<PMC *get_pmc_keyed_str(STRING *key)>
98 =item C<PMC *get_pmc_keyed(PMC *key)>
104 VTABLE PMC *get_pmc_keyed_str(STRING *key) {
105 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
110 VTABLE PMC *get_pmc_keyed(PMC *key) {
111 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
118 =item C<void set_pmc_keyed(PMC *key, PMC *value)>
120 =item C<void set_pmc_keyed_str(STRING *key, PMC *value)>
122 Set key and value. The key can only set once.
124 =item C<void assign_pmc(PMC *value)>
126 Set the value of the Pair.
132 VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
133 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
136 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
137 "attempt to set existing Pair key");
145 VTABLE void set_pmc_keyed_str(STRING *key, PMC *value) {
146 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
150 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
151 "attempt to set existing Pair key");
154 key_pmc = pmc_new(interp, enum_class_String);
155 VTABLE_set_string_native(interp, key_pmc, key);
161 VTABLE void assign_pmc(PMC *value) {
162 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
168 =item C<void set_pmc(PMC *pair)>
170 Sets this pair to hold the value of another.
176 void set_pmc(PMC *pair) {
177 if (pair->vtable->base_type == enum_class_Pair) {
178 Parrot_Pair_attributes * const from = PARROT_PAIR(SELF);
179 Parrot_Pair_attributes * const to = PARROT_PAIR(SELF);
182 to->value = from->value;
185 Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
186 "Can only set a pair to another pair.");
191 =item C<INTVAL is_equal(PMC *value)>
195 Check if two Pairs hold the same keys and values.
201 VTABLE INTVAL is_equal(PMC *value) {
202 Parrot_Pair_attributes * const from = PARROT_PAIR(SELF);
203 Parrot_Pair_attributes * const to = PARROT_PAIR(SELF);
207 if (value->vtable->base_type != SELF->vtable->base_type)
213 if (!mmd_dispatch_i_pp(INTERP, k1, k2, MMD_EQ))
227 =item C<void visit(visit_info *info)>
229 Used during archiving to visit the elements in the pair.
231 =item C<void freeze(visit_info *info)>
233 Used to archive the Pair.
235 =item C<void thaw(visit_info *info)>
237 Used to unarchive the Pair.
243 VTABLE void visit(visit_info *info) {
245 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
246 IMAGE_IO * const io = info->image_io;
247 DPOINTER ** const temp_pos = (DPOINTER **)pair->key;
248 info->thaw_ptr = (PMC **)temp_pos;
249 (info->visit_pmc_now)(INTERP, (PMC *)temp_pos, info);
252 info->thaw_ptr = pos;
254 (info->visit_pmc_now)(INTERP, *pos, info);
259 VTABLE void freeze(visit_info *info) {
260 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
261 IMAGE_IO * const io = info->image_io;
263 VTABLE_push_pmc(INTERP, io, pair->key);
264 VTABLE_push_pmc(INTERP, io, pair->value);
267 VTABLE void thaw(visit_info *info) {
268 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
269 IMAGE_IO * const io = info->image_io;
273 pair->key = VTABLE_shift_pmc(interp, io);
274 pair->value = VTABLE_shift_pmc(interp, io);
284 =item C<METHOD key()>
286 Return the key of the pair.
293 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
294 PMC *key = pair->key;
301 =item C<METHOD value()>
303 Return the value of the pair.
310 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
311 PMC * const value = pair->value;
319 Return a tuple of (key, value) for the pair.
326 Parrot_Pair_attributes * const pair = PARROT_PAIR(SELF);
327 PMC * const t = pmc_new(INTERP,
328 Parrot_get_ctx_HLL_type(INTERP, enum_class_FixedPMCArray));
330 VTABLE_set_integer_native(INTERP, t, 2);
331 VTABLE_set_pmc_keyed_int(INTERP, t, 0, pair->key);
333 VTABLE_set_pmc_keyed_int(INTERP, t, 1, pair->value);
348 * c-file-style: "parrot"
350 * vim: expandtab shiftwidth=4: