2 Copyright (C) 2007-2008, The Perl Foundation.
7 src/pmc/eventhandler.pmc - a handler for events
11 A PMC that captures the state of the interpreter to invoke when handling an
14 =head2 Vtable Functions
22 #include "parrot/parrot.h"
24 pmclass EventHandler extends Sub need_ext {
26 ATTR STRING *type; /* the type of the event to handle */
27 ATTR PMC *code; /* the code object to execute */
28 ATTR PMC *interp; /* the registered interpreter */
29 ATTR INTVAL priority; /* the minimum priority threshhold of events */
35 Initializes an empty C<EventHandler>. Add attributes to it if you want it to
43 Parrot_EventHandler *e = mem_allocate_zeroed_typed(Parrot_EventHandler);
45 PObj_custom_mark_SET(SELF);
46 PObj_active_destroy_SET(SELF);
53 =item C<void init_pmc(PMC *data)>
55 Initializes a new EventHandler with either a C<Sub> PMC (or descendant) or a
56 C<Hash> PMC. With the latter, the keys should be any or all of:
62 a C<Sub> (or descendant) PMC containing code to invoke when handling the event
66 a C<ParrotInterpreter> PMC in which to invoke the code PMC
70 a STRING recording the type of event to handle
74 the minimum threshhold of priority which the event must meet or exceed for the
83 VTABLE void init_pmc(PMC *data) {
85 PMC *interpreter = PMCNULL;
88 Parrot_EventHandler *e = NULL;
90 if (VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Sub"))) {
93 else if (VTABLE_isa(INTERP, data, CONST_STRING(INTERP, "Hash"))) {
95 VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "code"));
97 VTABLE_get_pmc_keyed_str(INTERP, data, CONST_STRING(INTERP, "interp"));
99 VTABLE_get_string_keyed_str(INTERP, data, CONST_STRING(INTERP, "type"));
101 VTABLE_get_integer_keyed_str(INTERP, data, CONST_STRING(INTERP, "priority"));
104 real_exception(INTERP, NULL, INVALID_OPERATION,
105 "EventHandler initializer must be Sub or Hash");
108 if (PMC_IS_NULL(interpreter))
109 interpreter = VTABLE_get_pmc_keyed_int(INTERP,
110 INTERP->iglobals, IGLOBALS_INTERPRETER);
112 e = mem_allocate_zeroed_typed(Parrot_EventHandler);
114 PObj_custom_mark_SET(SELF);
115 PObj_active_destroy_SET(SELF);
120 e->interp = interpreter;
121 e->priority = priority;
128 Marks this PMC and any of its contents as live.
135 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
139 pobject_lives(INTERP, (PObj *)e->type);
141 if (! PMC_IS_NULL(e->interp))
142 pobject_lives(INTERP, (PObj *)e->interp);
144 if (! PMC_IS_NULL(e->code))
145 pobject_lives(INTERP, (PObj *)e->code);
151 =item C<void destroy()>
153 Frees any memory held by this PMC.
157 VTABLE void destroy() {
158 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
162 PMC_data(SELF) = NULL;
168 =item C<void set_string(STRING *type)>
170 Sets the C<type> attribute of this event handler to the passed-in string.
176 void set_string(STRING *type) {
177 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
185 =item C<STRING *get_string()>
187 Retrieves the C<type> attribute of this event handler.
193 VTABLE STRING *get_string() {
194 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
197 return string_copy(INTERP, e->type);
199 return CONST_STRING(INTERP, "");
204 =item C<void set_integer_native(INTVAL priority)>
206 Sets the minimum interesting priority for this event handler.
211 VTABLE void set_integer_native(INTVAL priority) {
212 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
215 e->priority = priority;
220 =item C<void set_pmc(PMC *interpreter)>
222 Sets the passed-in C<ParrotInterpreter> as the active interpreter in which to
223 handle the registered events.
228 VTABLE void set_pmc(PMC *interpreter) {
229 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
232 e->interp = interpreter;
237 =item C<PMC *get_attr_str(STRING *name)>
243 VTABLE PMC *get_attr_str(STRING *name) {
244 Parrot_EventHandler *core_struct = PMC_data_typed(SELF, Parrot_EventHandler *);
245 PMC *value = PMCNULL;
247 if (string_equal(interp, name, CONST_STRING(interp, "code")) == 0) {
248 value = core_struct->code;
256 =item C<opcode_t *invoke(void *next)>
258 Runs the contained code, if any; this is what handles the event.
263 VTABLE opcode_t *invoke(void *next) {
264 Parrot_EventHandler *e = PMC_data_typed(SELF, Parrot_EventHandler *);
267 /* can't invoke on INTERP and can't return its result; this may not be
268 * the right interpreter */
270 unused = VTABLE_invoke(PMC_data_typed(e->interp, Parrot_Interp),
275 return (opcode_t *)next;
292 =item C<METHOD can_handle(PMC *event)>
294 Report whether the event handler can handle a particular type of event.
300 METHOD can_handle(PMC *event) {
301 Parrot_EventHandler *handler_struct = PMC_data_typed(SELF, Parrot_EventHandler *);
302 if (event->vtable->base_type == enum_class_Task) {
303 PMC *type = VTABLE_get_attr_str(interp, event, CONST_STRING(interp, "type"));
304 STRING *type_str = VTABLE_get_string(interp, type);
306 if (string_equal(interp, type_str, CONST_STRING(interp, "event")) == 0) {
307 PMC *subtype = VTABLE_get_attr_str(interp, event, CONST_STRING(interp, "subtype"));
308 STRING *subtype_str = VTABLE_get_string(interp, subtype);
310 if (string_equal(interp, subtype_str, handler_struct->type) == 0) {
331 * c-file-style: "parrot"
333 * vim: expandtab shiftwidth=4: