[t] Refactor some namespace pmc tests to use throws_like
[parrot.git] / src / pmc / pointer.pmc
blobb6eacdf828b1a5ea95142933165a9fa073ea78be
1 /*
2 Copyright (C) 2001-2007, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/pmc/pointer.pmc - Pointer
9 =head1 DESCRIPTION
11 These are the vtable functions for the Pointer base class.
13 The actual pointer is in C<PMC_data>.
15 =head2 Methods
17 =over 4
19 =cut
23 pmclass Pointer auto_attrs {
24     ATTR void * mark_function;
25     ATTR void * pointer;
29 =item C<void init()>
31 Initializes the pointer.
33 =cut
37     VTABLE void init() {
38         PObj_custom_mark_SET(SELF);
39     }
43 =item C<void mark()>
45 Marks the pointer as live.
47 =cut
51     VTABLE void mark() {
52         void (*mark_function)(Interp *, void *) =
53             (void (*)(Interp *, void *))D2FPTR(PARROT_POINTER(SELF)->mark_function);
54         void * data = PARROT_POINTER(SELF)->pointer;
55         if (data && mark_function)
56             (*mark_function)(INTERP, data);
57     }
61 =item C<PMC *clone()>
63 Creates and returns a clone of the pointer.
65 =cut
69     VTABLE PMC *clone() {
70         PMC * const dest = pmc_new_noinit(INTERP, SELF->vtable->base_type);
71         PObj_custom_mark_SET(dest);
72         PMC_data(dest) = PMC_data(SELF);
73         return dest;
74     }
78 =item C<void set_pointer(void *)>
80 Sets the pointer value.
82 =cut
86     VTABLE void set_pointer(void *ptr) {
87         PARROT_POINTER(SELF)->pointer = ptr;
88     }
92 =item C<void *get_pointer()>
94 Returns the pointer value.
96 =cut
100     VTABLE void *get_pointer() {
101         return PARROT_POINTER(SELF)->pointer;
102     }
106 =item C<INTVAL get_integer()>
108 Returns the pointer value as an integer.
110 =cut
114     VTABLE INTVAL get_integer() {
115         return (INTVAL)(PARROT_POINTER(SELF)->pointer);
116     }
120 =item C<FLOATVAL get_number()>
122 Returns the pointer value as a floating-point number.
124 =cut
128     VTABLE FLOATVAL get_number() {
129         return (FLOATVAL)(INTVAL)(PARROT_POINTER(SELF)->pointer);
130     }
134 =item C<STRING *get_repr()>
136 Returns the pointer value as a Parrot string.
138 =cut
142     VTABLE STRING *get_repr() {
143         return Parrot_sprintf_c(INTERP, "Pointer = 0x%p", PARROT_POINTER(SELF)->pointer);
144     }
149 =item C<STRING *get_string()>
151 Returns the pointer value as a Parrot string.
153 =cut
157     VTABLE STRING *get_string() {
158         return Parrot_sprintf_c(INTERP, "%s", PARROT_POINTER(SELF)->pointer);
159     }
163 =item C<INTVAL get_bool()>
165 Returns whether the pointer is not C<NULL>.
167 =cut
171     VTABLE INTVAL get_bool() {
172         return (INTVAL)(PMC_data(SELF) != NULL);
173     }
177 =item C<INTVAL is_same(PMC *pmc2)>
179 Returns whether the pointer has the same value as C<*pmc2>.
181 =cut
185     VTABLE INTVAL is_same(PMC *pmc2) {
186         return (INTVAL)(SELF->vtable   == pmc2->vtable &&
187                         PARROT_POINTER(SELF)->pointer == PARROT_POINTER(pmc2)->pointer);
188     }
193 =back
195 =cut
200  * Local variables:
201  *   c-file-style: "parrot"
202  * End:
203  * vim: expandtab shiftwidth=4:
204  */